From c497bb2385b5c50b1923ae2d7b0ed7aa6bf24363 Mon Sep 17 00:00:00 2001 From: Samuel Wu Date: Wed, 28 Aug 2024 18:05:48 +0100 Subject: [PATCH 1/5] Rewrote database test script using pytest. --- backend/tests/test_database.py | 31 +++++++++++++++---------------- requirements.txt | 2 ++ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/backend/tests/test_database.py b/backend/tests/test_database.py index 32bf39e..c3a2b15 100644 --- a/backend/tests/test_database.py +++ b/backend/tests/test_database.py @@ -1,27 +1,26 @@ +import pytest import sys import os sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from database import * -from utils import * -def test_database_connection(): +@pytest.fixture(scope="module") +def db_connection(): client, db = connect_to_mongodb(config_path="../config.json") - assert db is not None - return client, db + yield db + client.close() -def test_insert_and_retrieve_movie(db): - test_movie = {"tmdb_id": 12345, "title": "Test Movie"} - insert_movie(db, test_movie) - movies = get_all_movies(db) - assert any(movie['tmdb_id'] == 12345 for movie in movies) - delete_movie_by_id(db, 12345) +def test_database_connection(db_connection): + assert db_connection is not None -if __name__ == "__main__": - client, db = test_database_connection() - print("Connected to database!") +def test_insert_and_retrieve_movie(db_connection): + test_movie = {"tmdb_id": 12345, "title": "Test Movie"} + insert_movie(db_connection, test_movie) - test_insert_and_retrieve_movie(db) - print("Inserted and retrieved movie!") + movies = get_all_movies(db_connection) + assert any(movie['tmdb_id'] == 12345 for movie in movies) - print("All tests passed!") \ No newline at end of file + delete_movie_by_id(db_connection, 12345) + movies_after_deletion = get_all_movies(db_connection) + assert not any(movie['tmdb_id'] == 12345 for movie in movies_after_deletion) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 3bc82cf..b04a590 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,8 @@ aiohttp==3.10.5 flask==3.0.3 lxml==4.9.4 pymongo==4.8.0 +pytest==8.3.2 +pytest-asyncio==0.24.0 redis==5.0.8 tmdbv3api==1.9.0 urllib3==2.2.1 \ No newline at end of file From 78cfb37b6c53d1b1e20d1385cca1ddabacf50cb1 Mon Sep 17 00:00:00 2001 From: Samuel Wu Date: Wed, 28 Aug 2024 18:17:03 +0100 Subject: [PATCH 2/5] Rewrote TMDB API testing using pytest; proper asserts on all dictionaries performed --- backend/tests/test_tmdb_api.py | 58 ++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/backend/tests/test_tmdb_api.py b/backend/tests/test_tmdb_api.py index 485af51..dd675fd 100644 --- a/backend/tests/test_tmdb_api.py +++ b/backend/tests/test_tmdb_api.py @@ -1,11 +1,63 @@ +import pytest import sys import os sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from utils import * -if __name__ == "__main__": - init_tmdb() +@pytest.fixture(scope="module") +def tmdb_setup(): + init_tmdb("../config.json") + +def remove_key_from_list_of_dicts(lst, key): + """Remove specified key from all dictionaries in a list.""" + for item in lst: + if key in item: + del item[key] + return lst + +def test_get_tmdb_data(tmdb_setup): movies = [1730, 104871, 31414, 76819] + expected_info = [ + { + 'tmdb_id': 1730, + 'title': 'Inland Empire', + 'directors': [{'id': 5602, 'name': 'David Lynch'}], + 'genres': ['Thriller', 'Mystery', 'Fantasy', 'Horror'], + 'release_year': 2006, + 'runtime': 180, + 'user_rating': [] + }, + { + 'tmdb_id': 104871, + 'title': 'Sicily!', + 'directors': [{'id': 935136, 'name': 'Jean-Marie Straub'}, + {'id': 935137, 'name': 'Danièle Huillet'}], + 'genres': ['Drama'], + 'release_year': 1999, + 'runtime': 66, + 'user_rating': [] + }, + { + 'tmdb_id': 31414, + 'title': 'Satantango', + 'directors': [{'id': 85637, 'name': 'Béla Tarr'}], + 'genres': ['Drama'], + 'release_year': 1994, + 'runtime': 432, + 'user_rating': [] + }, + { + 'tmdb_id': 76819, + 'title': 'Teenage Hooker Became A Killing Machine In Daehakro', + 'directors': [{'id': 1371835, 'name': 'Nam Gee-woong'}], + 'genres': ['Science Fiction', 'Horror'], + 'release_year': 2000, + 'runtime': 60, + 'user_rating': [] + } + ] + info = [get_tmdb_data(movie) for movie in movies] - print(info) \ No newline at end of file + info_filtered = remove_key_from_list_of_dicts(info, 'popularity') + assert info_filtered == expected_info \ No newline at end of file From b644f3160ef2ebd7b17ed53803b475fd7c88f174 Mon Sep 17 00:00:00 2001 From: Samuel Wu Date: Wed, 28 Aug 2024 18:17:54 +0100 Subject: [PATCH 3/5] Test API is redundant --- backend/tests/test_api.py | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 backend/tests/test_api.py diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py deleted file mode 100644 index 6e7b105..0000000 --- a/backend/tests/test_api.py +++ /dev/null @@ -1,6 +0,0 @@ -import requests - -if __name__ == "__main__": - r = requests.get("http://localhost:5000/movies") - data = r.json() - print(data) \ No newline at end of file From f1aefbce19ce7cdd5ab23238c477052376928f6f Mon Sep 17 00:00:00 2001 From: Samuel Wu Date: Wed, 28 Aug 2024 18:28:52 +0100 Subject: [PATCH 4/5] Update test data integration to fetch mscorsese's fields --- backend/tests/test_data_integration.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/backend/tests/test_data_integration.py b/backend/tests/test_data_integration.py index cf54ebf..2265968 100644 --- a/backend/tests/test_data_integration.py +++ b/backend/tests/test_data_integration.py @@ -1,13 +1,24 @@ +import pytest import sys import os -import asyncio PARENT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(PARENT_DIR) from utils import write_to_csv, save_user_data_to_db, get_user_movie_data -if __name__ == "__main__": +@pytest.mark.asyncio +async def test_get_user_movie_data(): username = "mscorsese" - user_movie_data = asyncio.run(get_user_movie_data(username)) - write_to_csv(username, user_movie_data) \ No newline at end of file + user_movie_data = await get_user_movie_data(username) + + assert isinstance(user_movie_data, list), "Expected a list of movie data" + assert len(user_movie_data) > 0, "Expected at least one movie in the list" + + fields = ['tmdb_id', 'title', 'directors', 'genres', + 'release_year', 'num_ratings', 'avg_rating', + 'runtime', 'user_ratings'] + + for movie in user_movie_data: + for field in fields: + assert field in movie \ No newline at end of file From a20b88238576f83846bb5a7a5c60599850957a54 Mon Sep 17 00:00:00 2001 From: Samuel Wu Date: Wed, 28 Aug 2024 18:33:29 +0100 Subject: [PATCH 5/5] Lbxd scraper converted into a pytest --- backend/tests/test_lbxd_scraper.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/backend/tests/test_lbxd_scraper.py b/backend/tests/test_lbxd_scraper.py index 8f94f82..1c23c41 100644 --- a/backend/tests/test_lbxd_scraper.py +++ b/backend/tests/test_lbxd_scraper.py @@ -1,3 +1,4 @@ +import pytest import sys import os import asyncio @@ -5,8 +6,14 @@ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from utils import * -if __name__ == "__main__": - movies = [{'film_slug':'inland-empire'}, {'film_slug':'satantango'}, - {'film_slug':'sicily'}] - info = asyncio.run(scrape_movies(movies)) - print(info) \ No newline at end of file +@pytest.mark.asyncio +async def test_scrape_movies(): + username = "mscorsese" + expected_info = [ + { + 'film_slug': 'yeelen', + 'title': 'yeelen', + }, + ] + user_ratings = await scrape_user_ratings(username) + info = await scrape_movies(user_ratings, username=username) \ No newline at end of file