Ejemplo n.º 1
0
def repository():
    repo = MemoryRepository()
    user1 = User("Myles Kennedy", "123")
    user2 = User("Mark Tremonti", "456")
    director1 = Director("Taika Waititi")
    director2 = Director("Steven Speilberg")
    actor1 = Actor("Chris Pratt")
    actor2 = Actor("Emma Watson")
    genre1 = Genre("Comedy")
    genre2 = Genre("Animation")
    movie1 = Movie("Moana", 2016)
    movie2 = Movie("Ice Age", 2002)
    movie1.director = director1
    movie2.director = director2
    movie1.add_actor(actor1)
    movie2.add_actor(actor2)
    movie1.add_genre(genre1)
    movie2.add_genre(genre2)
    review1 = Review(movie1, "very nice", 9)
    review2 = Review(movie2, "incredible", 10)
    user1.add_review(review1)
    user2.add_review(review2)
    repo.add_movie(movie1)
    repo.add_movie(movie2)
    repo.add_director(director1)
    repo.add_director(director2)
    repo.add_actor(actor1)
    repo.add_actor(actor2)
    repo.add_genre(genre1)
    repo.add_genre(genre2)
    repo.add_review(review1)
    repo.add_review(review2)
    repo.add_user(user1)
    repo.add_user(user2)
    return repo
def test_get_movie_ids_for_genre(in_memory_repo):
    genre = Genre("Action")
    id_list = in_memory_repo.get_movie_ids_for_genre(genre)
    assert len(id_list) == 4
    genre2 = Genre("Racing")  # invalid genre
    id_list2 = in_memory_repo.get_movie_ids_for_genre(genre2)
    assert len(id_list2) == 0
Ejemplo n.º 3
0
def test_genre_init():
    genre1 = Genre("Comedy")
    assert repr(genre1) == "<Genre Comedy>"
    genre2 = Genre("")
    assert genre2.genre is None
    genre3 = Genre(42)
    assert genre3.genre is None
def test_get_movies_by_genre(session_factory):
    repository = SqlAlchemyRepository(session_factory)
    movie1 = Movie("Maquia", 2018)
    movie2 = Movie("Ice Age", 2002)
    genre1 = Genre("Japanese Anime")
    genre2 = Genre("Emotional")
    repository.add_genre(genre1)
    repository.add_genre(genre2)
    movie1.description = "The Iorph people live far away from the world of humans, spending their days weaving " \
                         "Hibiol, a special cloth serving as a written chronicle every time. They age more slowly " \
                         "than humans and have the capacity to live for hundreds of years."
    movie1.runtime_minutes = 115
    movie2.description = "A Cronopio known as Scrat attempts to find a place to store his acorn for the winter. " \
                         "Eventually, as he tries to stomp it into the ground, he inadvertently causes a large " \
                         "crack to form in the ice that extends for miles before setting off a large avalanche " \
                         "which nearly crushes him."
    movie2.runtime_minutes = 81
    movie1.add_genre(genre1)
    movie1.add_genre(genre2)
    movie2.add_genre(genre2)
    repository.add_movie(movie1)
    repository.add_movie(movie2)
    assert repository.get_movies_by_genre("Japanese Anime") == [movie1]
    assert repository.get_movies_by_genre("xyz") == []
    assert repository.get_movies_by_genre("Emotional") == [movie2, movie1]
    assert repository.get_movies_by_genre(1) == []
def test_add_and_get_genres(session_factory):
    repository = SqlAlchemyRepository(session_factory)
    repository.add_genre(Genre("Japanese Anime"))
    repository.add_genre(Genre("Japanese Anime"))
    repository.add_genre(Genre("Emotional"))
    genres = repository.get_genres()
    assert Genre("Emotional") in genres and Genre("Japanese Anime") in genres
Ejemplo n.º 6
0
def test_movie_genre_property():
    movie1 = Movie("Princess and The Frog", 2009)
    genre1 = Genre("Animation")
    genre2 = Genre("Musical")
    movie1.genres = genre1
    movie1.genres = genre2
    assert movie1.genres == [genre1, genre2]
Ejemplo n.º 7
0
def test_tv_show_genre_property():
    tv_show1 = TV_Show("Sailor Moon: Crystal", 2014)
    genre1 = Genre("Animation")
    genre2 = Genre("Action")
    tv_show1.genres = genre1
    tv_show1.genres = genre2
    assert tv_show1.genres == [genre1, genre2]
Ejemplo n.º 8
0
def test_movie_remove_genre():
    movie1 = Movie("Princess and The Frog", 2009)
    genre1 = Genre("Animation")
    genre2 = Genre("Musical")
    movie1.add_genre(genre1)
    movie1.add_genre(genre2)
    movie1.remove_genre(genre1)
    assert movie1.return_genre() == [genre2]
Ejemplo n.º 9
0
def test_tv_show_remove_genre():
    tv_show1 = TV_Show("Sailor Moon: Crystal", 2014)
    genre1 = Genre("Animation")
    genre2 = Genre("Action")
    tv_show1.add_genre(genre1)
    tv_show1.add_genre(genre2)
    tv_show1.remove_genre(genre1)
    assert tv_show1.return_genre() == [genre2]
Ejemplo n.º 10
0
def test_get_movies_by_genre(repository):
    movie1 = Movie("Moana", 2016)
    movie1.add_genre(Genre("Comedy"))
    movie1.add_genre(Genre("Animated"))
    movie2 = Movie("Ice Age", 2002)
    movie2.add_genre(Genre("Animated"))
    repository.add_movie(movie1)
    repository.add_movie(movie2)
    assert repository.get_movies() == [movie2, movie1]
    assert repository.get_movies_by_genre("Comedy") == [movie1]
    assert repository.get_movies_by_genre("xyz") == []
    assert repository.get_movies_by_genre("Animated") == [movie2, movie1]
    assert repository.get_movies_by_genre(1) == []
Ejemplo n.º 11
0
def test_genre_methods():
    genre1 = Genre('Horror')
    assert genre1.__repr__() == '<Genre Horror>'
    genre2 = Genre('Comedy')
    assert genre1.__lt__(genre2) == False
    assert genre1.__lt__(genre1) == False
    assert genre2.__lt__(genre1) == True
    genre3 = Genre(56)
    assert genre3.genre is None

    assert genre1.__eq__(genre1) == True
Ejemplo n.º 12
0
 def read_csv_file(self):
     with open(self.__file_name, newline="", encoding="utf-8-sig") as csv_file:
         csv_reader = csv.DictReader(csv_file)
         for row in csv_reader:
             movie = Movie(row["Title"], int(row["Year"]))
             movie.description = row["Description"]
             movie.runtime_minutes = int(row["Runtime (Minutes)"])
             if movie not in self.__dataset_of_movies:
                 self.__dataset_of_movies.append(movie)
             actors = row["Actors"].split(",")
             for actor in actors:
                 actor_true = Actor(actor.strip())
                 if actor_true not in self.__dataset_of_actors:
                     self.__dataset_of_actors.append(actor_true)
                 movie.add_actor(actor_true)
             director = Director(row["Director"])
             movie.director = director
             if director not in self.__dataset_of_directors:
                 self.__dataset_of_directors.append(director)
             genres = row["Genre"].split(",")
             for genre in genres:
                 genre_true = Genre(genre.strip())
                 if genre_true not in self.__dataset_of_genres:
                     self.__dataset_of_genres.append(genre_true)
                 movie.add_genre(genre_true)
Ejemplo n.º 13
0
def load_movies_and_genres_and_directors_and_actors(data_path, repo):
    for row in read_csv_file(os.path.join(data_path, 'Data1000Movies.csv')):
        title = row[1]
        release_year = int(row[6])
        runtime = int(row[7])
        description = row[3]

        genre_list = row[2].split(',')
        for i in range(len(genre_list)):
            genre_list[i] = Genre(genre_list[i])

        director = Director(row[4])

        actor_list = row[5].split(',')
        for i in range(len(actor_list)):
            actor_list[i] = Actor(actor_list[i])
        for actor_obj in actor_list:
            for colleague in actor_list:
                if colleague not in actor_obj.actor_colleagues:
                    actor_obj.add_actor_colleague(colleague)

        rating = float(row[8])
        votes = int(row[9])

        if row[10] == "N/A":
            revenue = "N/A"
        else:
            revenue = float(row[10])

        if row[11] == "N/A":
            metascore = "N/A"
        else:
            metascore = int(row[11])

        id = int(row[0])

        # create movie object and assign everything
        movie = Movie(title, release_year)
        movie.actors = actor_list
        movie.genres = genre_list
        movie.director = director
        movie.description = description
        movie.runtime_minutes = runtime
        movie.rating = rating
        movie.votes = votes
        movie.revenue = revenue
        movie.metascore = metascore
        movie.id = id

        repo.add_movie(movie)
        for genre in movie.genres:
            repo.add_genre(genre)
        for actor in movie.actors:
            repo.add_actor(actor)
        repo.add_director(movie.director)
Ejemplo n.º 14
0
def test_can_get_movie(in_memory_repo):
    movie_id = 1
    movie_as_dict = movies_services.get_movie(movie_id, in_memory_repo)

    assert movie_id == movie_as_dict['id']
    assert movie_as_dict['title'] == "Guardians of the Galaxy"
    assert movie_as_dict[
               'description'] == 'A group of intergalactic criminals are forced to work together to stop a fanatical ' \
                                 'warrior from taking control of the universe. '
    assert movie_as_dict['director'] == Director("James Gunn")
    actors = movie_as_dict['actors']
    assert Actor("Chris Pratt") in actors
    assert Actor("Vin Diesel") in actors
    genres = movie_as_dict['genres']
    assert Genre('Action') in genres
    assert Genre('Adventure') in genres
    assert movie_as_dict['release_year'] == 2014
    assert movie_as_dict["runtime_minutes"] == 121
    assert movie_as_dict["rating"] == 8.1
    assert movie_as_dict["votes"] == 757074
    assert movie_as_dict["revenue"] == 333.13
    assert movie_as_dict["metascore"] == 76
def test_repository_can_retrieve_movie(in_memory_repo):
    movie = in_memory_repo.get_movie_by_id(1)
    assert movie.title == "Guardians of the Galaxy"
    assert movie.genres == [
        Genre("Action"), Genre("Adventure"),
        Genre("Sci-Fi")
    ]
    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.actors == [
        Actor("Chris Pratt"),
        Actor("Vin Diesel"),
        Actor("Bradley Cooper"),
        Actor("Zoe Saldana")
    ]
    assert movie.release_year == 2014
    assert movie.runtime_minutes == 121
    assert movie.rating == 8.1
    assert movie.votes == 757074
    assert movie.revenue == 333.13
    assert movie.metascore == 76
    assert movie.reviews == []
Ejemplo n.º 16
0
def test_add_and_get_genres(repository):
    repository.add_genre(Genre("Comedy"))
    repository.add_genre(Genre("Comedy"))
    repository.add_genre(Genre("Animated"))
    assert repository.get_genres() == [Genre("Animated"), Genre("Comedy")]
Ejemplo n.º 17
0
def make_genre():
    genre = Genre("Comedy")
    return genre
def test_repository_can_add_a_genre(in_memory_repo):
    genre = Genre("Racing")
    in_memory_repo.add_genre(genre)
    assert genre in in_memory_repo.get_genres()
Ejemplo n.º 19
0
def test_movie_add_genre():
    movie1 = Movie("Princess and The Frog", 2009)
    genre1 = Genre("Animation")
    movie1.add_genre(genre1)
    assert movie1.return_genre() == [genre1]
Ejemplo n.º 20
0
def test_genre_lt_function():
    genre1 = Genre("Comedy")
    genre2 = Genre("Action")
    assert genre2.__lt__(genre1) is True
Ejemplo n.º 21
0
def load_movies_and_other_info(data_path: str, repo: MemoryRepository):
    actors = dict()
    directors = dict()
    genres = dict()

    for data_row in read_csv_file(os.path.join(data_path,
                                               'Data1000Movies.csv')):

        movie_rank = int(data_row['Rank'])
        movie_genres = data_row['Genres'].split(", ")
        movie_actors = data_row['Actors'].split(", ")
        movie_directors = data_row['Director'].split(", ")

        # Add any new tags; associate the current article with tags.
        for actor in movie_actors:
            if actor not in actors.keys():
                actors[actor] = list()
            actors[actor].append(movie_key)
        del data_row[-number_of_actors:]

        for genre in movie_genres:
            if genre not in genres.keys():
                genres[genre] = list()
            genres[genre].append(movie_key)
        del data_row[-number_of_genres:]

        for director in movie_directors:
            if director not in directors.keys():
                directors[director] = list()
            directors[director].append(movie_key)
        del data_row[-number_of_directors:]

        # Create Movie object.
        print(data_row)
        movie = Movie(title=data_row[2],
                      date=date.fromisoformat(data_row[1]),
                      id=movie_key)

        # Add the Article to the repository.
        repo.add_movie(movie)

    # Create Tag objects, associate them with Articles and add them to the repository.
    for actor_name in actors.keys():
        actor = Actor(actor_name)
        for movie_id in actors[actor_name]:
            movie = repo.get_movie(movie_id)
            make_actor_association(movie, actor)
        repo.add_actor(actor)

    for director_name in directors.keys():
        director = Director(director_name)
        for movie_id in Directors[Director_name]:
            movie = repo.get_movie(movie_id)
            make_director_association(movie, director)
        repo.add_director(director)

    for genre_name in genres.keys():
        genre = Genre(genre_name)
        for movie_id in genres[genre_name]:
            movie = repo.get_movie(movie_id)
            make_genre_association(movie, genre)
        repo.add_genre(genre)
Ejemplo n.º 22
0
def test_genre_false_eq_function():
    genre1 = Genre("Comedy")
    genre2 = Genre(" ")
    assert genre1.__eq__(genre2) is False
def test_repository_can_retrieve_genres(in_memory_repo):
    assert in_memory_repo.get_genres() == {
        Genre("Fantasy"),
        Genre("Horror"),
        Genre("Mystery"),
        Genre("Action"),
        Genre("Drama"),
        Genre("Comedy"),
        Genre("Music"),
        Genre("Sci-Fi"),
        Genre("Animation"),
        Genre("Family"),
        Genre("Biography"),
        Genre("Adventure"),
        Genre("Romance"),
        Genre("Thriller")
    }
    assert len(in_memory_repo.get_genres()) == 14
Ejemplo n.º 24
0
def test_genre_false_lt_function():
    genre1 = Genre("Comedy")
    genre2 = Genre("Action")
    assert genre1.__lt__(genre2) is False
Ejemplo n.º 25
0
def movies_by_genre():
    username = None
    try:
        username = session['username']
    except:
        pass
    recommendations = utilities.get_recommendations(username)

    movies_per_page = 3
    # Read query parameters.
    genre_name = request.args.get('genre')
    cursor = request.args.get('cursor')
    movie_to_show_reviews = request.args.get('view_reviews_for')

    if movie_to_show_reviews is None:
        # No view-reviews query parameter, so set to a non-existent movie id.
        movie_to_show_reviews = -1
    else:
        # Convert movie_to_show_reviews from string to int.
        movie_to_show_reviews = int(movie_to_show_reviews)

    if cursor is None:
        # No cursor query parameter, so initialise cursor to start at the beginning.
        cursor = 0
    else:
        # Convert cursor from string to int.
        cursor = int(cursor)

    # Retrieve movie ids for movies that are tagged with genre_name.
    movie_ids = services.get_movie_ids_for_genre(Genre(genre_name),
                                                 repo.repo_instance)

    # Retrieve the batch of movies to display on the Web page.
    movies = services.get_movies_by_id(
        movie_ids[cursor:cursor + movies_per_page],
        repo.repo_instance)  # list of movies as dicts

    first_movie_url = None
    last_movie_url = None
    next_movie_url = None
    prev_movie_url = None

    if cursor > 0:
        # There are preceding movies, so generate URLs for the 'previous' and 'first' navigation buttons.
        prev_movie_url = url_for('movies_bp.movies_by_genre',
                                 genre=genre_name,
                                 cursor=cursor - movies_per_page)
        first_movie_url = url_for('movies_bp.movies_by_genre',
                                  genre=genre_name)

    if cursor + movies_per_page < len(movie_ids):
        # There are further movies, so generate URLs for the 'next' and 'last' navigation buttons.
        next_movie_url = url_for('movies_bp.movies_by_genre',
                                 genre=genre_name,
                                 cursor=cursor + movies_per_page)

        last_cursor = movies_per_page * int(len(movie_ids) / movies_per_page)
        if len(movie_ids) % movies_per_page == 0:
            last_cursor -= movies_per_page
        last_movie_url = url_for('movies_bp.movies_by_genre',
                                 genre=genre_name,
                                 cursor=last_cursor)

    # Construct urls for viewing movie reviews and adding reviews.
    for movie in movies:
        movie['view_reviews_url'] = url_for('movies_bp.movies_by_genre',
                                            genre=genre_name,
                                            cursor=cursor,
                                            view_reviews_for=movie['id'])
        movie['add_review_url'] = url_for('movies_bp.review_movie',
                                          movie=movie['id'])
        movie['display_movie_url'] = url_for('movies_bp.display_movie',
                                             movie=movie['id'])

    # Generate the webpage to display the movies.
    return render_template('movies/movies.html',
                           title='Movies',
                           movies_title='Movies of genre ' + genre_name,
                           movies=movies,
                           selected_movies=utilities.get_selected_movies(
                               len(movies) * 2),
                           genre_urls=utilities.get_genres_and_urls(),
                           first_movie_url=first_movie_url,
                           last_movie_url=last_movie_url,
                           prev_movie_url=prev_movie_url,
                           next_movie_url=next_movie_url,
                           show_reviews_for_movie=movie_to_show_reviews,
                           recommendations=recommendations)