def test_remove_actor(self): movie1 = Movie("Moana", 2016, 2) movie1.add_actor(Actor("Some Actor")) assert movie1.number_of_actors == 1 movie1.remove_actor(Actor("Some Actor")) assert movie1.number_of_actors == 0 movie1.remove_actor(Actor("Some Actor"))
def test_repository_can_retrieve_movie_by_index(in_memory_repo): movie = in_memory_repo.get_movie_by_index(1) # Check that the movie has expected attributes assert movie.title == "Guardians of the Galaxy" assert movie.release_year == 2014 assert movie.description == "A group of intergalactic criminals are forced to work together to stop a fanatical warrior from taking control of the universe." assert movie.director == Director("James Gunn") assert movie.runtime_minutes == 121 actors = iter(movie.actors) assert next(actors) == Actor("Chris Pratt") assert next(actors) == Actor("Vin Diesel") assert next(actors) == Actor("Bradley Cooper") assert next(actors) == Actor("Zoe Saldana") # Check that the movie has the expected genre(s) assert movie.is_classified_as(Genre("Action")) assert movie.is_classified_as(Genre("Adventure")) assert movie.is_classified_as(Genre("Sci-Fi")) # Check that the movie has the correct reviews assert movie.number_of_reviews == 3 review_1 = [ review for review in movie.reviews if review.review_text == "This movie is great!" ][0] review_2 = [ review for review in movie.reviews if review.review_text == "This movie is awesome" ][0] assert review_1.review_author.username == "fmercury" assert review_2.review_author.username == "thorke"
def test_add_actors(self): movie1 = Movie("Moana", 2016, 3) actors = [Actor("Auli'i Cravalho"), Actor("Dwayne Johnson"), Actor("Rachel House"), Actor("Temuera Morrison")] i = iter(movie1.actors) for actor in actors: movie1.add_actor(actor) next(i) == actor
def test_check_if_this_actor_worked_with(self): actor1 = Actor("Angelina Jolie") actor2 = Actor("Brad Pitt") actor3 = Actor("Not an Actor") actor1.add_actor_colleague(actor2) assert actor1.check_if_this_actor_worked_with(actor2) == True assert actor2.check_if_this_actor_worked_with(actor1) == True assert actor1.check_if_this_actor_worked_with(actor3) == False
def test_equal(self): actor1 = Actor("Angelina Jolie") actor2 = Actor("") actor3 = Actor("Angelina Jolie") actor4 = Actor(5) assert actor1.__eq__(actor2) == False assert actor3.__eq__(actor1) == True assert actor2.__eq__(actor4) == False
def test_repository_can_get_a_list_of_actors_of_a_movie(session_factory): repo = SqlAlchemyRepository(session_factory) movie = repo.get_movie_by_index(1) list_of_actors = list(repo.get_movie_actors(movie)) assert Actor('Chris Pratt') in list_of_actors assert Actor('Vin Diesel') in list_of_actors assert Actor('Bradley Cooper') in list_of_actors assert Actor('Zoe Saldana') in list_of_actors assert Actor('Fake Actor') not in list_of_actors
def test_add_actor_colleague(self): actor1 = Actor("Angelina Jolie") actor2 = Actor("Brad Pitt") actor3 = Actor("Not an Actor") actor1.add_actor_colleague(actor2) assert actor1.get_number_of_colleagues() == 1 assert actor2.get_number_of_colleagues() == 1
def test_repository_can_add_an_actor(session_factory): repo = SqlAlchemyRepository(session_factory) actor = Actor("Keanu Reeves") repo.add_actor(actor) assert repo.get_actor("Keanu Reeves") == actor and repo.get_actor("Keanu Reeves") is actor
def test_repository_can_check_existence_of_actor(in_memory_repo): assert in_memory_repo.check_actor_existence_in_repo( Actor("Fake Actor")) is False assert in_memory_repo.check_actor_existence_in_repo( Actor("Matt Damon")) is True
def test_repository_can_add_an_actor(in_memory_repo): actor = Actor("Keanu Reeves") in_memory_repo.add_actor(actor) assert in_memory_repo.get_actor("Keanu Reeves") == actor
def read_csv_file(self): duplicated_genre = [] duplicated_actors = [] with open(self.__file_name, mode='r', encoding='utf-8-sig') as csvfile: movie_file_reader = csv.DictReader(csvfile) for row in movie_file_reader: # Append to the movie data set title = row['Title'] year = int(row['Year']) movie = Movie(title, year, 4) if (movie not in self.__dataset_of_movies): self.__dataset_of_movies.append(movie) # Store potential duplicated actors duplicated_actors.append( row['Actors'].split(',')) # This is a list of list # Store unique directors director = Director(row['Director']) if director not in self.__dataset_of_directors: self.__dataset_of_directors.append(director) # Store potential duplicated genres duplicated_genre.append(row['Genre'].split(',')) #Store description description = row['Description'] self.__dataset_of_description.append(description) # Store runtime runtime = row['Runtime (Minutes)'] try: runtime = int(runtime) self.__dataset_of_runtime.append(runtime) except ValueError: continue # Store ratings rating = row['Rating'] try: rating = float(rating) self.__dataset_of_ratings.append(rating) except ValueError: continue # Store votes vote = row['Votes'] try: vote = int(vote) self.__dataset_of_votes.append(vote) except ValueError: continue # Store revenue revenue = row["Revenue (Millions)"] try: revenue = float(revenue) self.__dataset_of_revenue.append(revenue) except ValueError: continue # Store Metascore meta_score = row["Metascore"] try: meta_score = float(meta_score) self.__dataset_of_metadata.append(meta_score) except ValueError: continue for actor_list in duplicated_actors: for current_actor in actor_list: current_actor = Actor(current_actor) if current_actor not in self.__dataset_of_actors: self.__dataset_of_actors.append(current_actor) for genre_list in duplicated_genre: for current_genre in genre_list: current_genre = Genre(current_genre) if current_genre not in self.__dataset_of_genres: self.__dataset_of_genres.append(current_genre)
def make_actor(): actor = Actor("Robert Downey Jr.") return actor
def test_hash(self): actor1 = Actor("Angelina Jolie") assert actor1.__hash__() == hash("Angelina Jolie")
def test_lt(self): actor1 = Actor("Angelina Jolie") actor2 = Actor("Brad Pitt") assert actor1.__lt__(actor2) == True
def test_repository_can_check_existence_of_actor(session_factory): repo = SqlAlchemyRepository(session_factory) assert repo.check_actor_existence_in_repo(Actor("Fake Actor")) is False assert repo.check_actor_existence_in_repo(Actor("Matt Damon")) is True
def test_repository_can_retrieve_an_actor(session_factory): repo = SqlAlchemyRepository(session_factory) actor = repo.get_actor("Chris Pratt") assert actor == Actor("Chris Pratt") assert Movie("Guardians of the Galaxy", 2014) in actor.played_movies
def load_movies_actors_directors_genre_description(data_path: str, repo: MemoryRepository): genres = dict() actors = dict() directors = dict() for data_row in read_csv_file(os.path.join(data_path, "movies.csv")): movie_index = int(data_row[0]) title = data_row[1] try: movie_revenue = float(data_row[10]) except ValueError: movie_revenue = 0 list_of_genre_names = data_row[2].split(",") list_of_genres = [Genre(genre_name) for genre_name in list_of_genre_names] # can have duplicate movie_description = data_row[3] director = Director(data_row[4]) # can have duplicate list_of_actor_names = data_row[5].split(",") list_of_actor_names = [name.strip() for name in list_of_actor_names] list_of_actors = [Actor(actor_full_name) for actor_full_name in list_of_actor_names] # can have duplicate release_year = int(data_row[6]) runtime = int(data_row[7]) # Create Movie object movie = Movie( title=title, release_year=release_year, id=movie_index ) movie.set_revenue(revenue=movie_revenue) # Add movie to repo repo.add_movie(movie) # Add any new genres to repo for genre in list_of_genres: if genre not in genres.keys(): genres[genre] = list() genres[genre].append(movie_index) for actor in list_of_actors: if actor not in actors.keys(): actors[actor] = list() actors[actor].append(movie_index) # Add any new directors to dict if director not in directors: directors[director] = list() directors[director].append(movie_index) # Connect the current movie with its attributes add_movie_attributes(movie=movie, list_of_genres=list_of_genres, description=movie_description, list_of_actors=list_of_actors, director=director, runtime=runtime) # Associate Genres with Movies and add them to the repository for genre in genres.keys(): for current_movie_index in genres[genre]: movie = repo.get_movie_by_index(current_movie_index) movie.add_genre(genre) genre.add_Movie(movie) repo.add_genre(genre) # Associate Actors with Movies and add them to the repository for actor in actors.keys(): for current_movie_index in actors[actor]: movie = repo.get_movie_by_index(current_movie_index) movie.add_actor(actor) actor.add_played_movies(movie) repo.add_actor(actor) # Associate Directors with Movies and add them to the repositor for director in directors.keys(): for current_movie_index in directors[director]: movie = repo.get_movie_by_index(current_movie_index) movie.set_director(director) director.add_directed_movies(movie) repo.add_director(director)
def test_init(self): actor1 = Actor("Angelina Jolie") actor2 = Actor("") assert repr(actor1) == "<Actor Angelina Jolie>" assert actor2.actor_full_name is None