Skip to content

Commit

Permalink
Merge pull request #38 from cern1710/37-use-pytest-for-tests
Browse files Browse the repository at this point in the history
Use pytest for tests
  • Loading branch information
cern1710 authored Aug 28, 2024
2 parents db650d7 + a20b882 commit c137217
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 34 deletions.
6 changes: 0 additions & 6 deletions backend/tests/test_api.py

This file was deleted.

19 changes: 15 additions & 4 deletions backend/tests/test_data_integration.py
Original file line number Diff line number Diff line change
@@ -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)
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
31 changes: 15 additions & 16 deletions backend/tests/test_database.py
Original file line number Diff line number Diff line change
@@ -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!")
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)
17 changes: 12 additions & 5 deletions backend/tests/test_lbxd_scraper.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import pytest
import sys
import os
import asyncio

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)
@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)
58 changes: 55 additions & 3 deletions backend/tests/test_tmdb_api.py
Original file line number Diff line number Diff line change
@@ -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)
info_filtered = remove_key_from_list_of_dicts(info, 'popularity')
assert info_filtered == expected_info
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c137217

Please sign in to comment.