def test_repository_does_not_retrieve_a_non_existent_movie(): mem_repo = MemoryRepository() movie = Movie("Up", 2009, 1) mem_repo.add_movie(movie) got_movie = mem_repo.get_movie(7) assert got_movie is None
def test_repository_can_retrieve_movie(): mem_repo = MemoryRepository() movie = Movie("Up", 2009, 1) mem_repo.add_movie(movie) got_movie = mem_repo.get_movie(1) assert got_movie == movie
def test_cannot_add_movie_with_non_unique_id(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 1) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) assert mem_repo.get_number_of_movies() == 1
def test_can_add_review(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) # ["Dolittle", "Klaus", "Up"] movie_id = 3 review_text = "Very good!" username = "******" rating = 5 mem_repo.add_user(User(username, "CS235")) # call the service layer to add the comment movie_services.add_review(movie_id, review_text, rating, username, mem_repo) # retrieve the reviews for the movie from the repository reviews_as_dict = movie_services.get_reviews_for_movie(movie_id, mem_repo) # check that the reviews inclue a review with the new review text. assert next((dictionary['review_text'] for dictionary in reviews_as_dict if dictionary['review_text'] == review_text), None) is not None
def test_get_number_of_movies(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) assert mem_repo.get_number_of_movies() == 3
def test_repository_does_not_retrieve_a_non_existent_id(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) movies_by_id = mem_repo.get_movies_by_id([1, 2, 4]) assert len(movies_by_id) == 2 assert movies_by_id == [up_movie, klaus_movie]
def test_repository_returns_year_of_next_movie(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) # ["Dolittle", "Klaus", "Up"] next_year = mem_repo.get_year_of_next_movie(klaus_movie) assert next_year is None
def test_repository_does_not_retrieve_a_non_existent_title(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) movies_by_title = mem_repo.get_movies_by_title(["Up", "Klaus", "Frozen"]) assert len(movies_by_title) == 2 assert movies_by_title == [up_movie, klaus_movie]
def test_get_last_movie(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) movie_as_dict = movie_services.get_last_movie(mem_repo) # ["Dolittle", "Klaus", "Up"] assert movie_as_dict["ID"] == 1
def test_repository_can_get_movies_by_id(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) # ["Dolittle", "Klaus", "Up"] movies_by_id = mem_repo.get_movies_by_id([1, 2, 3]) assert len(movies_by_id) == 3 assert movies_by_id == [up_movie, klaus_movie, dolittle_movie]
def test_cannot_get_movie_with_non_existent_id(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) # ["Dolittle", "Klaus", "Up"] movie_id = 7 with pytest.raises(movie_services.NonExistentMovieException): movie_services.get_movie(movie_id, mem_repo)
def test_get_movies_by_year_with_multiple_years(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) sonic_movie = Movie("Sonic the Hedgehog", 2020, 4) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) mem_repo.add_movie(sonic_movie) target_year = 2019 movies_as_dict, prev_date, next_date = movie_services.get_movies_by_year( target_year, mem_repo) assert len(movies_as_dict) == 2 movie_ids = [movie["ID"] for movie in movies_as_dict] assert {2, 3}.issubset(movie_ids) assert prev_date == 2009 assert next_date == 2020
def test_get_movies_by_year_with_non_existent_date(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) target_year = 2020 movies_as_dict, prev_date, next_date = movie_services.get_movies_by_year( target_year, mem_repo) assert len(movies_as_dict) == 0
def test_repository_can_get_movies_by_actor(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) the_avengers_movie = Movie("The Avengers", 2012, 2) dolittle_movie = Movie("Dolittle", 2019, 3) up_movie.add_actor(Actor("Ed Asner")) the_avengers_movie.add_actor(Actor("Robert Downey, Jr.")) dolittle_movie.add_actor(Actor("Robert Downey, Jr.")) mem_repo.add_movie(up_movie) mem_repo.add_movie(the_avengers_movie) mem_repo.add_movie(dolittle_movie) movies_by_actor = mem_repo.get_movies_by_actor(Actor("Robert Downey, Jr.")) assert len(movies_by_actor) == 2 assert movies_by_actor == [dolittle_movie, the_avengers_movie]
def test_can_get_movie(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) # ["Dolittle", "Klaus", "Up"] movie_id = 2 movie_as_dict = movie_services.get_movie(movie_id, mem_repo) assert movie_as_dict["ID"] == movie_id assert movie_as_dict["release_year"] == klaus_movie.release_year assert movie_as_dict["title"] == "Klaus"
def test_repository_can_get_movies_by_rating_low_to_high(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) up_movie.external_rating = 89 klaus_movie.external_rating = 90 dolittle_movie.external_rating = 76 mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) movies_by_low_to_high = mem_repo.get_movies_by_rating_low() assert len(movies_by_low_to_high) == 3 assert movies_by_low_to_high == [dolittle_movie, up_movie, klaus_movie]
def test_repository_does_not_retrieve_a_non_existent_actor(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) the_avengers_movie = Movie("The Avengers", 2012, 2) dolittle_movie = Movie("Dolittle", 2019, 3) up_movie.add_actor(Actor("Ed Asner")) the_avengers_movie.add_actor(Actor("Robert Downey, Jr.")) dolittle_movie.add_actor(Actor("Robert Downey, Jr.")) mem_repo.add_movie(up_movie) mem_repo.add_movie(the_avengers_movie) mem_repo.add_movie(dolittle_movie) movies_by_actor = mem_repo.get_movies_by_actor(Actor("Jim Carrey")) assert len(movies_by_actor) == 0 assert movies_by_actor == []
def test_repository_can_get_movies_by_genre(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) schindlers_list_movie = Movie("Schindler's List", 1993, 2) dolittle_movie = Movie("Dolittle", 2019, 3) up_movie.add_genre(Genre("Comedy")) schindlers_list_movie.add_genre(Genre("War")) dolittle_movie.add_genre(Genre("Comedy")) mem_repo.add_movie(up_movie) mem_repo.add_movie(schindlers_list_movie) mem_repo.add_movie(dolittle_movie) movies_by_genre = mem_repo.get_movies_by_genre(Genre("Comedy")) assert len(movies_by_genre) == 2 assert movies_by_genre == [dolittle_movie, up_movie]
def test_repository_does_not_retrieve_a_non_existent_genre(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) up_movie.add_genre(Genre("Comedy")) klaus_movie.add_genre(Genre("Comedy")) dolittle_movie.add_genre(Genre("Comedy")) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) movies_by_genre = mem_repo.get_movies_by_genre(Genre("War")) assert len(movies_by_genre) == 0 assert movies_by_genre == []
def test_repository_can_get_movies_by_director(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) inside_out_movie = Movie("Inside Out", 2015, 2) dolittle_movie = Movie("Dolittle", 2019, 3) up_movie.director = Director("Pete Docter") inside_out_movie.director = Director("Pete Docter") dolittle_movie.director = Director("Stephen Gaghan") mem_repo.add_movie(up_movie) mem_repo.add_movie(inside_out_movie) mem_repo.add_movie(dolittle_movie) movies_by_director = mem_repo.get_movies_by_director( Director("Pete Docter")) assert len(movies_by_director) == 2 assert movies_by_director == [inside_out_movie, up_movie]
def test_repository_does_not_retrieve_a_non_existent_director(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) inside_out_movie = Movie("Inside Out", 2015, 2) dolittle_movie = Movie("Dolittle", 2019, 3) up_movie.director = Director("Pete Docter") inside_out_movie.director = Director("Pete Docter") dolittle_movie.director = Director("Stephen Gaghan") mem_repo.add_movie(up_movie) mem_repo.add_movie(inside_out_movie) mem_repo.add_movie(dolittle_movie) movies_by_director = mem_repo.get_movies_by_director( Director("Sergio Pablos")) assert len(movies_by_director) == 0 assert movies_by_director == []
def test_get_movies_by_year_with_one_year(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) target_year = 2009 movies_as_dict, prev_date, next_date = movie_services.get_movies_by_year( target_year, mem_repo) assert len(movies_as_dict) == 1 assert movies_as_dict[0]["ID"] == 1 assert prev_date is None assert next_date == 2019
def test_get_movies_by_id(): mem_repo = MemoryRepository() up_movie = Movie("Up", 2009, 1) klaus_movie = Movie("Klaus", 2019, 2) dolittle_movie = Movie("Dolittle", 2019, 3) mem_repo.add_movie(up_movie) mem_repo.add_movie(klaus_movie) mem_repo.add_movie(dolittle_movie) target_movie_ids = [2, 1, 4, 5] movies_as_dict = movie_services.get_movies_by_ids(target_movie_ids, mem_repo) # check that 2 movies were returned from the query assert len(movies_as_dict) == 2 # check that the movie ids returned were 1 and 2 movie_ids = [movie["ID"] for movie in movies_as_dict] assert {1, 2}.issubset(movie_ids)
def test_repository_can_add_movie(): mem_repo = MemoryRepository() movie = Movie("Up", 2009, 1) mem_repo.add_movie(movie) assert mem_repo.get_movie(1) is movie