def test_can_retrieve_a_movie_and_add_a_review_to_it(session_factory):
    repo = SqlAlchemyRepository(session_factory)

    # Fetch Movie and User.
    movie = repo.get_movie(5)
    author = repo.get_user('thorke')

    # Create a new Review, connecting it to the Movie and User.
    review = add_review('First death in Australia', author, movie)

    movie_fetched = repo.get_movie(5)
    author_fetched = repo.get_user('thorke')

    assert review in movie_fetched.reviews
    assert review in author_fetched.reviews
def test_repository_does_not_add_a_comment_without_a_user(session_factory):
    repo = SqlAlchemyRepository(session_factory)

    movie = repo.get_movie(2)
    review = Review(movie, 'LOL', None, None)

    with pytest.raises(RepositoryException):
        repo.add_review(review)
def test_repository_can_add_a_review(session_factory):
    repo = SqlAlchemyRepository(session_factory)

    user = User('thorke', '902fjsdf')
    movie = repo.get_movie(2)
    review = add_review("Highly recommended!", user, movie, 0)

    repo.add_review(review)

    assert review in repo.get_reviews()
def test_repository_can_add_movie(session_factory):
    repo = SqlAlchemyRepository(session_factory)

    number_of_movies = repo.get_number_of_movies()

    new_movie_id = number_of_movies + 1

    movie = Movie('Parasite', 2019, new_movie_id)
    repo.add_movie(movie)

    assert repo.get_movie(new_movie_id) == movie
def test_repository_can_retrieve_movie(session_factory):
    repo = SqlAlchemyRepository(session_factory)

    movie = repo.get_movie(1)

    # Check that the movie has the expected title.
    assert movie.title == 'Guardians of the Galaxy'

    # Check that the Movie is reviewed as expected.
    review_one = [
        review for review in movie.reviews
        if review.review_text == "I love this movie"
    ][0]
    review_two = [
        review for review in movie.reviews
        if review.review_text == "Master piece!"
    ][0]

    assert review_one.user.username == 'fmercury'
    assert review_two.user.username == "thorke"

    # Check that the Movie has the expected genre.
    assert movie.has_genre(Genre('Action'))
    assert movie.has_genre(Genre('Adventure'))
def test_repository_does_not_retrieve_a_non_existent_movie(session_factory):
    repo = SqlAlchemyRepository(session_factory)

    movie = repo.get_movie(1001)
    assert movie is None