def test_repository_cannot_add_invalid_movie(in_memory_repo): with pytest.raises(RepositoryException): in_memory_repo.add_movie(Movie('', 2009)) with pytest.raises(RepositoryException): in_memory_repo.add_movie(Movie(0, 2010)) with pytest.raises(RepositoryException): in_memory_repo.add_movie(Movie('Bee Movie', 1899))
def test_watchlist_remove_movie(watchlist, movie, movie_invalid): movies = [ movie, movie_invalid, (" ", 1899), Movie("Jaws", 2020), Movie("Moana", 2016), Movie("Moana", 1900) ] for movie_x in movies: watchlist.add_movie(movie_x) assert watchlist.first_movie_in_watchlist() == movie assert watchlist.size() == 3 watchlist.remove_movie(movie) assert watchlist.first_movie_in_watchlist() == movies[3] assert watchlist.size() == 2 watchlist.remove_movie(movie_invalid) assert watchlist.size() == 2 watchlist.remove_movie(movies[3]) assert watchlist.first_movie_in_watchlist() == movies[5] assert watchlist.size() == 1 watchlist.remove_movie(movies[5]) assert watchlist.first_movie_in_watchlist() is None assert watchlist.size() == 0 watchlist.remove_movie(movies[5]) assert watchlist.size() == 0
def test_repository_cannot_add_invalid_review(in_memory_repo): with pytest.raises(RepositoryException): in_memory_repo.add_review(Director('Joe')) with pytest.raises(RepositoryException): in_memory_repo.add_review(Review('', '', 0)) user = User("Martin", "pw12345") movie = Movie('Sing', 2016) review = Review(movie, "This movie was very enjoyable.", 8) review1 = Review(review.movie, review.review_text, review.rating) assert review is not review1 and review.id is not review1.id with pytest.raises(RepositoryException): in_memory_repo.add_review(review) movie.add_review(review) with pytest.raises(RepositoryException): in_memory_repo.add_review(review) review.user = user with pytest.raises(RepositoryException): in_memory_repo.add_review(review) user.add_review(review) with pytest.raises(RepositoryException): in_memory_repo.add_review(review) user.add_review(review1) in_memory_repo.add_review(review1) assert in_memory_repo.get_review(review1.id) is review1
def test_watchlist_add_movie(watchlist, movie, movie_invalid): movies = [ movie, movie_invalid, (" ", 1899), Movie("Jaws", 2020), Movie("Moana", 2016), Movie("Moana", 1900) ] for movie_x in movies: watchlist.add_movie(movie_x) assert watchlist.first_movie_in_watchlist() == movie assert watchlist.size() == 3 assert movie in watchlist assert movie_invalid not in watchlist assert movies[2] not in watchlist assert movies[3] in watchlist assert movies[4] in watchlist assert movies[5] in watchlist assert next(watchlist) == movie assert next(watchlist) == movies[3] assert next(watchlist) == movies[5] with pytest.raises(StopIteration): next(watchlist)
def test_watching_sim_file_reader(watching_sim_file_reader): watching_sim_file_reader.read_csv_file() watching_sims = [ watching_sim for watching_sim in watching_sim_file_reader.dataset_of_watching_sims ] assert len(watching_sims) == 5 assert watching_sims[0].movie == Movie('The Great Wall', 2016) assert watching_sims[1].movie == Movie('Split', 2016) assert watching_sims[2].movie == Movie('Sing', 2016) assert watching_sims[3].movie == Movie('The Lost City of Z', 2016) assert User('Martin', 'pw12345') in watching_sims[0].users assert sum(1 for _ in watching_sims[1].users) == 2 assert User('Daniel', 'pw87645') in watching_sims[2].users assert sum(1 for _ in watching_sims[3].users) == 3 review = next( (review for review in watching_sims[0].reviews if review.id == 5), None) assert review.rating == 7 and review.user in watching_sims[ 0].users and review.movie is watching_sims[0].movie assert len([review for review in watching_sims[2].reviews]) == 0 reviews = [review for review in watching_sims[3].reviews] assert len(reviews) == 2 assert reviews[0].movie == reviews[1].movie and reviews[ 0].movie == watching_sims[3].movie assert reviews[0].user in watching_sims[3].users and reviews[ 1].user in watching_sims[3].users
def test_repository_can_get_movies_by_rank(in_memory_repo): movie = in_memory_repo.get_movie_by_rank(3) assert movie == Movie('Split', 2016) movies = in_memory_repo.get_movies_by_rank([2, 8, 6]) assert len(movies) == 3 assert movies[0] == Movie('Prometheus', 2012) assert movies[1] == Movie('Mindhorn', 2016) assert movies[2] == Movie('The Great Wall', 2016)
def test_repository_can_get_movies_by_release_year(in_memory_repo): movie_ranks = in_memory_repo.get_movie_ranks_by_release_year(2016) movies = in_memory_repo.get_movies_by_rank(movie_ranks) assert Movie('Passengers', 2016) in movies assert Movie('La La Land', 2016) in movies assert len(movies) == 8 movie_ranks = in_memory_repo.get_movie_ranks_by_release_year(2010) movies = in_memory_repo.get_movies_by_rank(movie_ranks) assert len(movies) == 0
def test_movie_hash(movie, movie_invalid): movies = [ Movie(" ", 1899), Movie("Jaws", 2020), Movie("Moana", 2016), Movie("Moana", 1900) ] assert hash(movie) == hash(movies[2]) assert hash(movie) != hash(movies[1]) assert hash(movie_invalid) == hash(movies[0]) assert hash(movie) != hash(movies[3])
def test_movie_eq(movie, movie_invalid): movies = [ Movie(" ", 1899), Movie("Jaws", 2020), Movie("Moana", 2016), Movie("Moana", 1900) ] assert movie != movies[1] assert movie == movies[2] assert movie != movies[3] assert movie_invalid == movies[0]
def test_repository_can_get_movies_by_director(in_memory_repo): movie_ranks = in_memory_repo.get_movie_ranks_by_director( Director('James Gunn')) movies = in_memory_repo.get_movies_by_rank(movie_ranks) assert Movie('Guardians of the Galaxy', 2014) in movies assert Movie('Slither', 2006) in movies assert len(movies) == 2 movie_ranks = in_memory_repo.get_movie_ranks_by_director( Director('John Doe')) movies = in_memory_repo.get_movies_by_rank(movie_ranks) assert len(movies) == 0
def test_movie_lt(movie, movie_invalid): movies = [ Movie(" ", 1899), Movie("Jaws", 2020), Movie("Moana", 2016), Movie("Moana", 1900) ] assert movies[1] < movies[2] assert not movie < movies[2] assert movie_invalid < movie assert not movie_invalid < movies[0] assert movies[3] < movies[2]
def test_review_eq(): movies = [Movie("Jaws", 1975), Movie("Moana", 2016), Movie("Moana", 2016)] review_texts = [ "This movie was very exciting.", "This movie was very entertaining.", " " ] ratings = [10, 8, 5] reviews = [ Review(movies[i], review_texts[i], ratings[i]) for i in range(len(movies)) ] assert reviews[0] != reviews[1] assert reviews[1] != reviews[2]
def test_repository_can_get_movies_with_actors(in_memory_repo): actor_names = 'Vin Diesel, Bradley Cooper, Zoe Saldana' actors = [Actor(name) for name in actor_names.split(',')] movie_ranks = in_memory_repo.get_movie_ranks_by_actors(actors) movies_with_actors = in_memory_repo.get_movies_by_rank(movie_ranks) assert Movie('Guardians of the Galaxy', 2014) in movies_with_actors assert len(movies_with_actors) == 1 actors = [Actor('Chris Pratt')] movie_ranks = in_memory_repo.get_movie_ranks_by_actors(actors) movies_with_actors = in_memory_repo.get_movies_by_rank(movie_ranks) assert Movie('Guardians of the Galaxy', 2014) in movies_with_actors assert Movie('Passengers', 2016) in movies_with_actors assert len(movies_with_actors) == 2
def test_review_file_reader(review_file_reader): review_file_reader.read_csv_file() reviews = [review for review in review_file_reader.dataset_of_reviews] assert len(reviews) == 8 assert reviews[0].user == User('Ian', 'pw67890') assert reviews[1].user == User('Martin', 'pw12345') assert reviews[2].user == User('Daniel', 'pw87465') assert reviews[0].movie == Movie('Suicide Squad', 2016) assert reviews[1].movie == Movie('Mindhorn', 2016) assert reviews[2].movie == Movie('Guardians of the Galaxy', 2014) assert "loved" in reviews[0].review_text and reviews[0].rating == 10 assert "enjoyable" in reviews[1].review_text and reviews[1].rating == 7 assert "entertaining" in reviews[2].review_text and reviews[2].rating == 8
def test_repository_can_add_watchlist(in_memory_repo): user = User('Matt', 'pw4567') user.watchlist.add_movie(Movie('Sing', 2016)) in_memory_repo.add_watchlist(user.watchlist) assert in_memory_repo.get_watchlist_by_user_id(user.id) == user.watchlist assert in_memory_repo.get_watchlist_by_user_id(user.id).size() == 1
def test_watching_simulation_reviews(watching_simulation): users = [User('Martin', 'pw12345'), User('Ian', 'pw67890'), User('Daniel', 'pw87465')] for user in users: watching_simulation.add_user(user) watching_simulation.remove_user(users[1]) watching_simulation.remove_user(users[1]) user4 = User('Bob', 'pw12347') watching_simulation.add_user(user4) watching_simulation.add_user(user4) assert sum(1 for _ in watching_simulation.users) == 3 review = Review(watching_simulation.movie, 'Fun', 8) watching_simulation.add_user_review(users[2], review) assert review not in watching_simulation.reviews and review not in watching_simulation.movie.reviews watching_simulation.watch_movie() movie = watching_simulation.movie assert watching_simulation.movie in users[0].watched_movies assert watching_simulation.movie not in users[1].watched_movies assert watching_simulation.movie in users[2].watched_movies assert watching_simulation.movie in user4.watched_movies reviews = [Review(movie, "good", 8), Review(movie, "great", 10), Review(movie, "boring", 1)] for i in range(len(reviews)): user = users[i] review = reviews[i] watching_simulation.add_user_review(user, review) assert reviews[0] in watching_simulation.reviews and reviews[0] in users[0].reviews and reviews[0] in movie.reviews assert reviews[1] not in watching_simulation.reviews and reviews[1] not in users[1].reviews \ and reviews[1] not in movie.reviews assert reviews[2] in watching_simulation.reviews and reviews[2] in users[2].reviews and reviews[2] in movie.reviews movie = Movie('Moana', 2016) review = Review(movie, 'Fun', 9) user4.add_review(review) watching_simulation.add_user_review(user4, review) assert review not in watching_simulation.reviews and review in user4.reviews review = Review(watching_simulation.movie, 'Fun', 9) watching_simulation.add_user_review(user4, review) assert review in watching_simulation.reviews and review in user4.reviews and \ review in watching_simulation.movie.reviews watching_simulation.remove_user_review(review) assert review not in watching_simulation.reviews and review not in user4.reviews and \ review not in watching_simulation.movie.reviews user5 = User('Tom', 'pw15567') user5.watch_movie(watching_simulation.movie) review = Review(watching_simulation.movie, 'Enjoyable', 8) watching_simulation.add_user_review(user5, review) assert review not in watching_simulation.reviews
def test_movie_constructor(movie, movie_invalid): assert repr(movie) == "<Movie Moana, 2016>" assert repr(movie_invalid) == "<Movie None, None>" assert movie_invalid.title is None assert movie_invalid.release_year is None movie3 = Movie(0, " ") assert movie3.title is None assert movie3.release_year is None
def test_repository_cannot_add_invalid_watchlist(in_memory_repo): watchlist = WatchList() watchlist.add_movie(Movie('Guardians of the Galaxy', 2014)) with pytest.raises(RepositoryException): in_memory_repo.add_watchlist(watchlist) with pytest.raises(RepositoryException): in_memory_repo.add_watchlist(Director('Joe')) user = User('John', 'pw0135') user.watchlist.add_movie(Movie('A Movie', 2020)) with pytest.raises(RepositoryException): in_memory_repo.add_watchlist(user.watchlist) user.watchlist.remove_movie(Movie('A Movie', 2020)) in_memory_repo.add_watchlist(user.watchlist)
def test_repository_can_add_review(in_memory_repo): user = User("Martin", "pw12345") movie = Movie('Sing', 2016) review = Review(movie, "This movie was very enjoyable.", 8) user.add_review(review) in_memory_repo.add_review(review) assert in_memory_repo.get_review(review.id) is review
def test_repository_can_get_movies_with_genres(in_memory_repo): genre_names = 'Action, Adventure, Fantasy' genres = [Genre(name) for name in genre_names.split(',')] movie_ranks = in_memory_repo.get_movie_ranks_by_genres(genres) movies_with_genres = in_memory_repo.get_movies_by_rank(movie_ranks) assert Movie('Suicide Squad', 2016) in movies_with_genres assert Movie('The Great Wall', 2016) in movies_with_genres assert len(movies_with_genres) == 2 genres = [Genre('Horror')] movie_ranks = in_memory_repo.get_movie_ranks_by_genres(genres) movies_with_genres = in_memory_repo.get_movies_by_rank(movie_ranks) assert Movie('Split', 2016) in movies_with_genres assert Movie('Slither', 2006) in movies_with_genres assert len(movies_with_genres) == 2
def test_user_file_reader(user_file_reader): user_file_reader.read_csv_file() users = [user for user in user_file_reader.dataset_of_users] assert len(users) == 3 assert User('Martin', 'pw12345') == users[0] assert User('Ian', 'pw67890') == users[1] assert User('Daniel', 'pw87465') == users[2] assert User('Bob', 'pw01234') not in users assert Movie('Prometheus', 2012) in users[0].watched_movies assert users[1].time_spent_watching_movies_minutes == 0 assert Movie('Sing', 2016) in users[2].watched_movies for user in user_file_reader.dataset_of_users: assert user.user_name is not None and user.password is not None assert user.id is not None assert sum(1 for _ in user.reviews) == 0 assert sum(1 for _ in user.watchlist) == 0 assert user.watchlist.user is user
def test_watchlist_select(watchlist, movie, movie_invalid): movies = [ movie, movie_invalid, (" ", 1899), Movie("Jaws", 2020), Movie("Moana", 2016), Movie("Moana", 1900) ] for movie_x in movies: watchlist.add_movie(movie_x) assert watchlist.select_movie_to_watch(-1) is None assert watchlist.select_movie_to_watch(0) == movie assert watchlist.select_movie_to_watch(1) == movies[3] assert watchlist.select_movie_to_watch(2) == movies[5] assert watchlist.select_movie_to_watch(3) is None assert watchlist.select_movie_to_watch(0.5) is None assert watchlist.select_movie_to_watch('a') is None
def test_movie_file_reader_movies(movie_from_file): movie = Movie("Guardians of the Galaxy", 2014) director = Director("James Gunn") actor = Actor("Chris Pratt") genre = Genre("Action") assert repr(movie_from_file) == repr(movie) assert repr(movie_from_file.director) == repr(director) assert actor in movie_from_file.actors assert genre in movie_from_file.genres assert movie_from_file.runtime_minutes == 121
def test_repository_can_get_watching_sims_for_movie(in_memory_repo): movie = in_memory_repo.get_movie_by_rank(6) watching_sims_for_movie = in_memory_repo.get_watching_sims_for_movie(movie) assert in_memory_repo.get_watching_sim(1) in watching_sims_for_movie movie = in_memory_repo.get_movie_by_rank(4) watching_sims_for_movie = in_memory_repo.get_watching_sims_for_movie(movie) assert in_memory_repo.get_watching_sim(3) in watching_sims_for_movie assert in_memory_repo.get_watching_sim(5) in watching_sims_for_movie movie = Movie('Moana', 2016) assert len(in_memory_repo.get_watching_sims_for_movie(movie)) == 0
def test_repository_can_get_watching_sim(in_memory_repo): watching_sim = in_memory_repo.get_watching_sim(1) assert watching_sim.movie == Movie('The Great Wall', 2016) assert watching_sim.movie is in_memory_repo.get_movie_by_rank(6) assert sum(1 for _ in watching_sim.users) == 2 assert in_memory_repo.get_user_by_id(1) in watching_sim.users assert in_memory_repo.get_user_by_id(3) in watching_sim.users assert sum(1 for _ in watching_sim.reviews) == 1 assert in_memory_repo.get_review(5) in watching_sim.reviews for user in watching_sim.users: assert watching_sim.movie in user.watched_movies
def test_movie_review_relationship(movie, review, user): movie.add_review(review) assert review not in movie.reviews user.add_review(review) assert review in user.reviews assert review in movie.reviews assert review.movie == movie movie.add_review(review) movie2 = Movie('Batman', 1989) movie2.add_review(review) assert review not in movie2.reviews assert review.movie == movie user.remove_review(review) assert review not in user.reviews assert review not in movie.reviews movie.remove_review(review) user2 = User('Ian', 'pw67890') user2.add_review(review) assert review not in user2.reviews assert review.user == user
def test_repository_cannot_add_invalid_watching_sim(in_memory_repo): with pytest.raises(RepositoryException): in_memory_repo.add_watching_sim(Movie('A Movie', 2020)) watching_simulation = MovieWatchingSimulation(Movie('A Movie', 2020)) with pytest.raises(RepositoryException): in_memory_repo.add_watching_sim(watching_simulation) watching_simulation = MovieWatchingSimulation( in_memory_repo.get_movie_by_rank(1)) users = [ in_memory_repo.get_user_by_id(1), User('Bob', 'pw1235'), in_memory_repo.get_user_by_id(3), in_memory_repo.get_user_by_id(2) ] for user in users: watching_simulation.add_user(user) with pytest.raises(RepositoryException): in_memory_repo.add_watching_sim(watching_simulation) watching_simulation.remove_user(User('Bob', 'pw1235')) review = Review(watching_simulation.movie, 'Cool', 6) users[3].add_review(review) reviews = [ in_memory_repo.get_review(3), review, in_memory_repo.get_review(4) ] watching_simulation.watch_movie() for review in reviews: watching_simulation.add_user_review(review.user, review) with pytest.raises(RepositoryException): in_memory_repo.add_watching_sim(watching_simulation) watching_simulation.remove_user_review(reviews[1]) for user in users: watching_simulation.remove_user(user) with pytest.raises(RepositoryException): in_memory_repo.add_watching_sim(watching_simulation)
def test_repository_can_get_review(in_memory_repo): review = in_memory_repo.get_review(1) assert review.user == User('Ian', 'pw67890') assert review.movie == Movie('Suicide Squad', 2016) assert "loved" in review.review_text assert review.rating == 10 assert review in review.user.reviews and review in review.movie.reviews user = in_memory_repo.get_user(review.user.user_name) assert review in user.reviews movie = in_memory_repo.get_movie_by_rank(review.movie.rank) assert review in movie.reviews
def test_watchlist_file_reader(watchlist_file_reader): watchlist_file_reader.read_csv_file() watch_lists = [ watchlist for watchlist in watchlist_file_reader.dataset_of_watch_lists ] assert len(watch_lists) == 3 assert watch_lists[0].user == User('Daniel', 'pw87465') assert watch_lists[1].user == User('Martin', 'pw12345') assert watch_lists[2].user == User('Ian', 'pw67890') assert watch_lists[0].size() == 5 assert watch_lists[1].size() == 6 assert watch_lists[2].size() == 0 assert watch_lists[0].first_movie_in_watchlist() == Movie( 'Suicide Squad', 2016) assert watch_lists[1].first_movie_in_watchlist() == Movie('Split', 2016) assert watch_lists[2].first_movie_in_watchlist() is None assert watch_lists[0].select_movie_to_watch(5) is None assert watch_lists[1].select_movie_to_watch(5) == Movie( 'Suicide Squad', 2016) assert watch_lists[2].select_movie_to_watch(0) is None
def test_user_watch_movie(movie, user, movie_invalid, user_invalid): movie3 = Movie("Moana", 1900) movie3.runtime_minutes = 140 user.watch_movie(movie) user_invalid.watch_movie(movie_invalid) assert user.time_spent_watching_movies_minutes == 0 assert movie not in user.watched_movies assert user_invalid.time_spent_watching_movies_minutes == 0 assert movie_invalid not in user_invalid.watched_movies movie.runtime_minutes = 120 movie_invalid.runtime_minutes = 100 user.watch_movie(movie) user.watch_movie(movie) user.watch_movie(movie3) user_invalid.watch_movie(movie_invalid) user_invalid.watch_movie(movie3) user_invalid.watch_movie(movie3) assert user.time_spent_watching_movies_minutes == 380 assert sum(1 for _ in user.watched_movies) == 2 assert user_invalid.time_spent_watching_movies_minutes == 280 assert sum(1 for _ in user_invalid.watched_movies) == 1