def load_movies(data_path: str, repo: MemoryRepository): movies = dict() for data_row in read_csv_file(os.path.join(data_path, 'Data1000Movies.csv')): movie = Movie( rank=int(data_row[0]), title=data_row[1].strip(), release_year=int(data_row[6]), description=data_row[3].strip() ) actors = data_row[5] actors = actors.split(',') for actor in actors: actor = actor.strip() if Actor(actor) not in movie.actors: movie.add_actor(Actor(actor)) genres = data_row[2] genres = genres.split(',') for genre in genres: genre = genre.strip() if Genre(genre) not in movie.genres: movie.add_genre(Genre(genre)) director = data_row[4] director = director.strip() movie.director = Director(director) repo.add_movie(movie) movies[data_row[0]] = movie return movies
def test_get_movie_by_genre(in_memory_repo): assert in_memory_repo.get_movie_by_genre('Action') == [ Movie( 1, 'Guardians of the Galaxy', 2014, "A group of \ intergalactic criminals are forced to work together\ to stop a fanatical warrior from taking control of\ the universe."), Movie( 5, 'Suicide Squad', 2016, "A secret government agency\ recruits some of the most dangerous incarcerated \ super-villains to form a defensive task force. \ Their first mission: save the world from the apocalypse." ), Movie( 6, 'The Great Wall', 2016, "European mercenaries \ searching for black powder become embroiled in the defense\ of the Great Wall of China against a horde of monstrous\ creatures."), Movie( 9, 'The Lost City of Z', 2016, "A true-life drama, \ centering on British explorer Col. Percival Fawcett, who\ disappeared while searching for a mysterious city in the\ Amazon in the 1920s." ) ] movie1 = Movie(11, '1', 2000, "a") movie2 = Movie(12, '2', 3000, "a") genre1 = Genre('a') movie1.add_genre(genre1) movie2.add_genre(genre1) in_memory_repo.add_movie(movie1) in_memory_repo.add_movie(movie2) assert in_memory_repo.get_movie_by_genre('a') == [movie1, movie2]
def remove_genre(): movie1 = Movie("Spirited Away", 2000) movie1.genres = [Genre("Comedy")] movie1.add_genre(Genre("Horror")) movie1.add_genre(Genre("Happy ending")) assert movie1.genres == [ Genre("Comedy"), Genre("Horror"), Genre("Happy ending") ] movie1.remove_genre("Scifi") movie1.remove_genre(Genre("Hhiya")) movie1.remove_genre("") movie1.remove_genre(None) movie1.remove_genre(21345648) movie1.remove_genre(Genre("Happy ending")) assert movie1.genres == [Genre("Comedy"), Genre("Horror")] movie1.remove_genre(Genre("Comedy")) assert movie1.genres == [Genre("Horror")] movie1.remove_genre(Genre("Horror")) assert movie1.genres == []
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: # Read in Title title = row['Title'] # Read in Year release_year = int(row['Year']) movie = Movie(title, release_year) self.__dataset_of_movies.append(movie) # Read in description description = row["Description"] movie.description = description # Read in runtime runtime = row["Runtime (Minutes)"] movie.runtime_minutes = int(runtime) # Read in Actors actors = row['Actors'] actor = actors.split(",") for i in actor: actor = i.strip() movie.add_actor(Actor(actor)) self.__dataset_of_actors.add(Actor(actor)) # Read in Directors director = row['Director'] movie.director = Director(director) self.__dataset_of_directors.add(Director(director)) # Read in Genre genres = row['Genre'] genre = genres.split(",") for i in genre: a = i.strip() movie.add_genre(Genre(a)) self.__dataset_of_genres.add(Genre(a)) # Read in Rank rank = row["Rank"] movie.rank = rank # Read in Rating rating = row["Rating"] movie.rating = rating # Read in Votes vote = row["Votes"] movie.votes = vote # Read in revenue revenue = row["Revenue (Millions)"] movie.revenue = revenue # Read in meta_scores metascore = row["Metascore"] movie.metascore = metascore index += 1
def test_add_genre(): movie1 = Movie("Spirited Away", 2000) movie1.genres = [Genre("Comedy")] movie1.add_genre(Genre("Horror")) assert movie1.genres == [Genre("Comedy"), Genre("Horror")] movie1.add_genre("Scifi") movie1.add_genre(Genre("Hhiya")) movie1.add_genre("") movie1.add_genre(None) movie1.add_genre(21345648) assert movie1.genres == [Genre("Comedy"), Genre("Horror"), Genre("Hhiya")]