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_repository_can_get_expected_movie_genres(in_memory_repo):
    movie_genres = iter(
        in_memory_repo.get_movie_genres(in_memory_repo.get_movie_by_index(10)))
    assert next(movie_genres) == Genre("Adventure")
    assert next(movie_genres) == Genre("Drama")
    assert next(movie_genres) == Genre("Romance")
    with pytest.raises(StopIteration):
        assert repr(next(movie_genres)) == StopIteration
def test_repository_can_get_expected_movie_genres(session_factory):
    repo = SqlAlchemyRepository(session_factory)
    movie_genres = iter(repo.get_movie_genres(repo.get_movie_by_index(10)))
    assert next(movie_genres) == Genre("Adventure")
    assert next(movie_genres) == Genre("Drama")
    assert next(movie_genres) == Genre("Romance")
    with pytest.raises(StopIteration):
        assert repr(next(movie_genres)) == StopIteration
 def test_eq(self):
     genre1 = Genre("Horror")
     genre2 = Genre("")
     genre3 = Genre("Horror")
     genre4 = Genre(1)
     assert genre1.__eq__(genre2) == False
     assert genre1.__eq__(genre3) == True
     assert genre2.__eq__(genre4) == False
Example #5
0
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_repository_can_check_existence_of_genre(in_memory_repo):
    assert in_memory_repo.check_genre_existence(Genre("Fake Genre")) is False
    assert in_memory_repo.check_genre_existence(Genre("Animation")) is True
    assert in_memory_repo.check_genre_existence(Genre("Family")) is True
    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_genre():
    genre = Genre('Testing Genre')
    return genre
 def test_init(self):
     genre1 = Genre("Horror")
     assert repr(genre1) == "<Genre Horror>"
     genre2 = Genre("")
     assert genre2.genre_name is None
 def test_hash(self):
     genre1 = Genre("Horror")
     assert hash(genre1) == hash("Horror")
 def test_lt(self):
     genre1 = Genre("Horror")
     genre2 = Genre("Romantic")
     assert genre1.__lt__(genre2) == True
def test_repository_can_add_a_genre(session_factory):
    repo = SqlAlchemyRepository(session_factory)
    genre = Genre('Some New Genre')
    repo.add_genre(genre)
    assert repo.get_genre('Some New Genre') == genre and repo.get_genre('Some New Genre') is genre
def test_repository_can_check_existence_of_genre(session_factory):
    repo = SqlAlchemyRepository(session_factory)
    assert repo.check_genre_existence(Genre("Fake Genre")) is False
    assert repo.check_genre_existence(Genre("Animation")) is True
    assert repo.check_genre_existence(Genre("Family")) is True
def test_repository_can_retrieve_a_genre(session_factory):
    repo = SqlAlchemyRepository(session_factory)
    genre = repo.get_genre('Sci-Fi')
    assert genre == Genre('Sci-Fi')
    assert Movie("Guardians of the Galaxy", 2014) in genre.classified_movies