From 05d387a6b5589c9dedc2119ecba1b5c0d4d0cd3e Mon Sep 17 00:00:00 2001 From: ducklet Date: Sat, 11 May 2024 17:15:21 +0200 Subject: [PATCH] fix: tests sometimes failed because of missing sorting --- pyproject.toml | 2 ++ tests/test_db.py | 26 +++++++++++++------------- unwind/db.py | 4 ++++ unwind/imdb.py | 2 +- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4058296..e36058b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,11 +48,13 @@ select = [ # See https://docs.astral.sh/ruff/rules/ for a list of all rules. "B", # flake8-bugbear "C4", # flake8-comprehensions + "DTZ", # flake8-datetimez "E", # pycodestyle Error "F", # Pyflakes unused-import "I", # isort "RUF", # Ruff-specific rules "S", # flake8-bandit + "T20", # flake8-print "W", # pycodestyle Warning ] ignore = [ diff --git a/tests/test_db.py b/tests/test_db.py index 3619497..73c526f 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import UTC, datetime import pytest @@ -141,21 +141,21 @@ async def test_remove(conn: db.Connection): @pytest.mark.asyncio async def test_find_ratings(conn: db.Connection): m1 = a_movie( - title="test movie", + title="a test movie", release_year=2013, genres={"genre-1"}, ) await db.add(conn, m1) m2 = a_movie( - title="it's anöther Movie, Part 2", + title="b it's anöther Movie, Part 2", release_year=2015, genres={"genre-2"}, ) await db.add(conn, m2) m3 = a_movie( - title="movie it's, Part 3", + title="c movie it's, Part 3", release_year=m2.release_year, genres=m2.genres, ) @@ -181,7 +181,7 @@ async def test_find_ratings(conn: db.Connection): user_id=u1.id, user=u1, score=66, - rating_date=datetime.now(), + rating_date=datetime.now(tz=UTC), ) await db.add(conn, r1) @@ -191,7 +191,7 @@ async def test_find_ratings(conn: db.Connection): user_id=u2.id, user=u2, score=77, - rating_date=datetime.now(), + rating_date=datetime.now(tz=UTC), ) await db.add(conn, r2) @@ -224,35 +224,35 @@ async def test_find_ratings(conn: db.Connection): ratings = tuple(web_models.Rating(**r) for r in rows) assert ( web_models.Rating.from_movie(m1), + web_models.Rating.from_movie(m3), web_models.Rating.from_movie(m2, rating=r1), web_models.Rating.from_movie(m2, rating=r2), - web_models.Rating.from_movie(m3), ) == ratings aggr = web_models.aggregate_ratings(ratings, user_ids=[]) assert tuple( - web_models.RatingAggregate.from_movie(m) for m in [m1, m2, m3] + web_models.RatingAggregate.from_movie(m) for m in [m1, m3, m2] ) == tuple(aggr) aggr = web_models.aggregate_ratings(ratings, user_ids=[str(u1.id)]) assert ( web_models.RatingAggregate.from_movie(m1), - web_models.RatingAggregate.from_movie(m2, ratings=[r1]), web_models.RatingAggregate.from_movie(m3), + web_models.RatingAggregate.from_movie(m2, ratings=[r1]), ) == tuple(aggr) aggr = web_models.aggregate_ratings(ratings, user_ids=[str(u1.id), str(u2.id)]) assert ( web_models.RatingAggregate.from_movie(m1), - web_models.RatingAggregate.from_movie(m2, ratings=[r1, r2]), web_models.RatingAggregate.from_movie(m3), + web_models.RatingAggregate.from_movie(m2, ratings=[r1, r2]), ) == tuple(aggr) rows = await db.find_ratings(conn, title="movie", include_unrated=True) ratings = (web_models.Rating(**r) for r in rows) aggr = web_models.aggregate_ratings(ratings, user_ids=[]) assert tuple( - web_models.RatingAggregate.from_movie(m) for m in [m1, m2, m3] + web_models.RatingAggregate.from_movie(m) for m in [m1, m3, m2] ) == tuple(aggr) rows = await db.find_ratings(conn, title="test", include_unrated=True) @@ -288,7 +288,7 @@ async def test_ratings_for_movies(conn: db.Connection): user_id=u1.id, user=u1, score=66, - rating_date=datetime.now(), + rating_date=datetime.now(tz=UTC), ) await db.add(conn, r1) @@ -353,7 +353,7 @@ async def test_find_movies(conn: db.Connection): user_id=u1.id, user=u1, score=66, - rating_date=datetime.now(), + rating_date=datetime.now(tz=UTC), ) await db.add(conn, r1) diff --git a/unwind/db.py b/unwind/db.py index 3ebca66..c12a450 100644 --- a/unwind/db.py +++ b/unwind/db.py @@ -582,6 +582,10 @@ async def ratings_for_movie_ids( ) .outerjoin_from(movies, ratings, movies.c.id == ratings.c.movie_id) .where(sa.or_(*conds)) + .order_by( + ratings.c.rating_date.asc(), + movies.c.title.asc(), + ) ) rows = await fetch_all(conn, query) return tuple(dict(r._mapping) for r in rows) diff --git a/unwind/imdb.py b/unwind/imdb.py index 2a54e4f..4e07787 100644 --- a/unwind/imdb.py +++ b/unwind/imdb.py @@ -356,7 +356,7 @@ async def _load_ratings_page_legacy(url: str, soup: bs4.BeautifulSoup) -> _Ratin async def load_and_store_ratings( - user_id: MovieId, + user_id: UserId, ) -> AsyncIterable[tuple[Rating, bool]]: async with db.new_connection() as conn: user = await db.get(conn, User, imdb_id=user_id) or User(