-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from cern1710/37-use-pytest-for-tests
Use pytest for tests
- Loading branch information
Showing
6 changed files
with
99 additions
and
34 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters