def test_init(self): genre1 = Genre("Comedy") assert repr(genre1) == "<Genre Comedy>" genre2 = Genre("") assert genre2.genre_name is None genre3 = Genre(42) assert genre3.genre_name is None
def test_equality(): genre1 = Genre("Horror") genre2 = Genre("") genre3 = Genre("Horror") assert genre1 != genre2 assert genre1 == genre3
def test_sort(): genre1 = Genre("a") genre2 = Genre("aa") genre3 = Genre("b") assert genre1 < genre2 assert genre1 < genre3 assert genre2 < genre3
def test_init(): genre1 = Genre("Horror") genre2 = Genre("") genre3 = Genre(12) assert repr(genre1) == "<Genre Horror>" assert repr(genre2) == "<Genre None>" assert genre2.genre_name is None assert genre3.genre_name is None
def test_lt(self): genre1 = Genre("Comedy") genre4 = Genre("Horror") genre_list = [genre4, genre1] genre_list.sort() assert genre_list[1] == genre4 and genre_list[0] == genre1 genre5 = Genre("Comedian") genre_list2 = [genre1, genre5] genre_list2.sort() assert genre_list2[0] == genre5 and genre_list2[1] == genre1
def test_hash(): genre1 = Genre("a") genre2 = Genre("a") genre3 = Genre("b") genres = set([genre1, genre2, genre3]) assert len(genres) == 2 assert genre1 in genres assert genre2 in genres assert genre3 in genres
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_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 load_movies(data_path, repo): filename = os.path.join(data_path, "movies.csv") with open(filename, mode='r', encoding='utf-8-sig') as csvfile: movie_file_reader = csv.DictReader(csvfile) for row in movie_file_reader: media_id = row["Id"] title = row['Title'] release_year = int(row['Year']) description = row["Description"] runtime_minutes = row["Runtime (Minutes)"] director = Director(row["Director"]) genres = set([Genre(x) for x in row["Genre"].split(',')]) actors = set([Actor(x) for x in row["Actors"].split(',')]) movie = Movie(title, release_year) movie.description = description movie.runtime_minutes = runtime_minutes movie.director = director movie.media_id = media_id for genre in genres: movie.add_genre(genre) for actor in actors: movie.add_actor(actor) repo.add_media(movie)
def read_csv_file(self): with open(self.__file_name, mode='r', encoding='utf-8-sig') as csvfile: movie_file_reader = csv.DictReader(csvfile) index = 0 for row in movie_file_reader: title = row['Title'] release_year = int(row['Year']) actors = row['Actors'].split(",") genres = row['Genre'].split(",") director = Director(row['Director']) rating = row['Rating'] votes = row['Votes'] revenue = row['Revenue (Millions)'] metascore = row['Metascore'] movie = Movie(title, release_year, index + 1) movie.director = director # extension attribute application movie.external_rating = rating movie.rating_votes = votes movie.revenue = revenue movie.metascores = metascore # adding the actors, genres, director and the movie to the corresponding datasets for i in range(len(actors)): actor = Actor(actors[i]) movie.add_actor(actor) if actor not in self.__dataset_of_actors: # adding only unique actors to the dataset self.__dataset_of_actors.add(actor) for i in range(len(genres)): genre = Genre(genres[i]) movie.add_genre(genre) if genre not in self.__dataset_of_genres: # adding only unique genres to the dataset self.__dataset_of_genres.add(genre) if movie.director not in self.__dataset_of_directors: # adding only unique directors to the dataset self.__dataset_of_directors.add(movie.director) self.__dataset_of_movies.append(movie) index += 1
def test_repo_get_media_by_fake_genre(in_memory_repo): media = in_memory_repo.get_media_by_genre(Genre("zzzz")) assert media == []
def test_repo_get_media_by_different_genre(in_memory_repo): media = in_memory_repo.get_media_by_genre(Genre("Sci-Fi")) assert media == [ Media("Guardians of the Galaxy", 2014), Media("Prometheus", 2012) ]
def test_repo_get_media_by_genre(in_memory_repo): media = in_memory_repo.get_media_by_genre(Genre("Horror")) assert media == [Media("Split", 2016)]