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_movie_add_genres(director, genre, movie, genre_invalid, movie_invalid): genres = [ Genre("Action"), Genre("Romance"), Genre("Horror"), Genre("Mystery") ] for genre_x in genres: movie.add_genre(genre_x) assert [genre for genre in movie.genres] == genres genres = [genre, genre_invalid, director, genre] for genre_x in genres: movie_invalid.add_genre(genre_x) assert genre_invalid not in movie_invalid.genres assert director not in movie_invalid.genres assert sum(1 for _ in movie_invalid.genres) == 1
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_movie_remove_genres(director, genre, movie, genre_invalid): genres = [ Genre("Action"), Genre("Romance"), Genre("Horror"), Genre("Mystery") ] for genre_x in genres: movie.add_genre(genre_x) assert [genre for genre in movie.genres] == genres movie.remove_genre(genre) assert [genre for genre in movie.genres] == genres movie.remove_genre(genre_invalid) assert [genre for genre in movie.genres] == genres movie.remove_genre(genres[3]) assert genres[3] not in movie.genres and [genre for genre in movie.genres ] != genres movie.remove_genre(director) assert sum(1 for _ in movie.genres) == 3
def test_repository_can_get_most_common_genres(in_memory_repo): with pytest.raises(RepositoryException): in_memory_repo.get_most_common_genres(0) with pytest.raises(RepositoryException): in_memory_repo.get_most_common_genres('') most_common_genres = in_memory_repo.get_most_common_genres(1) assert Genre('Adventure') in most_common_genres most_common_genres = in_memory_repo.get_most_common_genres(20) assert len(most_common_genres) == sum( 1 for _ in in_memory_repo.get_movie_file_csv_reader().dataset_of_genres)
def test_repository_cannot_add_invalid_genre(in_memory_repo): with pytest.raises(RepositoryException): in_memory_repo.add_genre(0) with pytest.raises(RepositoryException): in_memory_repo.add_genre(Genre(''))
def test_repository_can_add_genre(in_memory_repo): genre = Genre("Historical Drama") in_memory_repo.add_genre(genre) assert genre in in_memory_repo.get_genres()
def movies_by_genres(): movies_per_page = 3 # Read query parameters genre_names_string = request.args.get('genres') genre_names = genre_names_string.split('/') cursor = request.args.get('cursor') movie_to_show_reviews = request.args.get('view_reviews_for') try: # Try converting movie_to_show_reviews from string to int. movie_to_show_reviews = int(movie_to_show_reviews) except TypeError: # No view_reviews_for query parameter, so set to a non-existent movie rank. movie_to_show_reviews = -1 try: # Try converting cursor from string to int. cursor = int(cursor) except TypeError: # No cursor query parameter, so initialise cursor to start at the beginning. cursor = 0 # Retrieve Movie ranks for Movies with the given genres. genres = [Genre(genre_name) for genre_name in genre_names] movie_ranks = services.get_movie_ranks_by_genres(genres, repo.repo_instance) # Retrieve only a batch of Movies to display on the Web page movies = services.get_movies_by_rank( movie_ranks[cursor:cursor + movies_per_page], repo.repo_instance) first_movie_url = None last_movie_url = None next_movie_url = None prev_movie_url = None director_urls = dict() actor_urls = dict() genre_urls = dict() view_review_urls = dict() add_review_urls = dict() add_watchlist_urls = dict() image_urls = dict() for movie in movies: director_full_name = movie.director.director_full_name director_urls[director_full_name] = url_for( 'movie_bp.movies_by_director', director=director_full_name) actor_urls.update(utilities.get_actor_urls_for_movie(movie)) genre_urls.update(utilities.get_genre_urls_for_movie(movie)) view_review_urls[movie.rank] = url_for('movie_bp.movies_by_genres', genres=genre_names_string, cursor=cursor, view_reviews_for=movie.rank) add_review_urls[movie.rank] = url_for('movie_bp.create_movie_review', add_review_for=movie.rank) add_watchlist_urls[movie.rank] = url_for( 'user_activity_bp.browse_watchlist', movie=movie.rank) image_urls[movie.rank] = utilities.get_image_url_for_movie(movie.title) if cursor > 0: # There are preceding Movies, so generate URLs for the 'previous' and 'first' navigation buttons. prev_movie_url = url_for('movie_bp.movies_by_genres', genres=genre_names_string, cursor=cursor - movies_per_page) first_movie_url = url_for('movie_bp.movies_by_genres', genres=genre_names_string) if cursor + movies_per_page < len(movie_ranks): # There are further Movies, so generate URLs for the 'next' and 'last' navigation buttons. next_movie_url = url_for('movie_bp.movies_by_genres', genres=genre_names_string, cursor=cursor + movies_per_page) last_cursor = movies_per_page * int(len(movie_ranks) / movies_per_page) if len(movie_ranks) % movies_per_page == 0: last_cursor -= movies_per_page last_movie_url = url_for('movie_bp.movies_by_genres', genres=genre_names_string, cursor=last_cursor) # Generate the webpage to display the Movies. return render_template( 'movies/movies.html', title='Movies', movies_title=f"Movies with genres: {', '.join(genre_names)}", movies=movies, random_movies=utilities.get_random_movies(len(movies) * 2), first_movie_url=first_movie_url, last_movie_url=last_movie_url, prev_movie_url=prev_movie_url, next_movie_url=next_movie_url, director_urls=director_urls, actor_urls=actor_urls, genre_urls=genre_urls, view_review_urls=view_review_urls, add_review_urls=add_review_urls, show_reviews_for_movie=movie_to_show_reviews, image_urls=image_urls, add_watchlist_urls=add_watchlist_urls)
def genre_invalid(): return Genre(" ")
def genre(): return Genre("Comedy")
def test_genre_hash(genre, genre_invalid): genres = [Genre(0), Genre("Action"), Genre("Comedy")] assert hash(genre) == hash(genres[2]) assert hash(genre) != hash(genres[1]) assert hash(genre_invalid) == hash(genres[0])
def test_genre_lt(genre, genre_invalid): genres = [Genre(0), Genre("Action"), Genre("Comedy")] assert genres[1] < genres[2] assert not genre < genres[2] assert genre_invalid < genre assert not genre_invalid < genres[0]
def test_genre_eq(genre, genre_invalid): genres = [Genre(0), Genre("Action"), Genre("Comedy")] assert genre_invalid == genres[0] assert genre != genres[1] assert genre == genres[2]
def test_genre_constructor(genre, genre_invalid): assert repr(genre) == "<Genre Comedy>" assert repr(genre_invalid) == "<Genre None>" assert genre_invalid.genre_name is None genre3 = Genre(0) assert genre3.genre_name is None