Beispiel #1
0
def test_loading_of_reviews(empty_session):
    users = list()
    users.append(("Andrew", "1234"))
    users.append(("Cindy", "1111"))
    insert_users(empty_session, users)
    users = empty_session.query(User).all()

    movies = list()
    movies.append(("Andrew", 2020))
    movies.append(("Cindy", 1999))
    insert_movies(empty_session, movies)
    movies = empty_session.query(Movie).all()

    # Precision of microseconds in the database can differ
    now = datetime.utcnow().replace(microsecond=0)

    reviews = list()
    reviews.append((users[0].id, movies[0].id, "Description 1", 7, now))
    reviews.append((users[1].id, movies[1].id, "Description 2", 1, now))

    insert_reviews(empty_session, reviews)

    expected = [
        Review(movies[0], "Description 1", 7, now),
        Review(movies[1], "Description 2", 1, now)
    ]

    assert empty_session.query(Review).all() == expected
    assert expected[0].movie == movies[0]
    assert expected[1].movie == movies[1]
def test_equality_when_not_equal(review):
    # Check not equal when reviews are partially equal
    movie = Movie("Test", 2000, 1)
    review_text = review.review_text
    rating = review.rating
    other = Review(movie, review_text, rating)
    assert review != other

    movie = review.movie
    review_text = "Test"
    rating = review.rating
    other = Review(movie, review_text, rating)
    assert review != other

    movie = review.movie
    review_text = review.review_text
    rating = 5
    other = Review(movie, review_text, rating)
    assert review != other

    # Check not equal when reviews are completely different
    movie = Movie("Test", 2000, 1)
    review_text = "Test"
    rating = 5
    other = Review(movie, review_text, rating)
    assert review != other
def test_constructor(movie):
    review_text = "   Text    with   spaces  \n"

    review_text_expected = "Text    with   spaces"
    rating = 1
    review = Review(movie, review_text, rating)

    assert review.movie == movie
    assert review.review_text == review_text_expected
    assert review.rating == rating
    assert review.timestamp.timestamp() == pytest.approx(datetime.utcnow().timestamp(), abs=_TIMESTAMP_TOLERANCE)

    review_text = "   Text    with   spaces  \n"
    review_text_expected = "Text    with   spaces"
    rating = 10
    review = Review(movie, review_text, rating)

    assert review.movie == movie
    assert review.review_text == review_text_expected
    assert review.rating == rating
    assert review.timestamp.timestamp() == pytest.approx(datetime.utcnow().timestamp(), abs=_TIMESTAMP_TOLERANCE)

    review_text = "   Text    with   spaces  \n"
    review_text_expected = "Text    with   spaces"
    rating = 10
    timestamp = datetime(2020, 1, 1)
    review = Review(movie, review_text, rating, timestamp)

    assert review.movie == movie
    assert review.review_text == review_text_expected
    assert review.rating == rating
    assert review.timestamp == timestamp
def test_constructor_invalid_movie_type():
    movie = 42
    review_text = "   Text    with   spaces  \n"
    rating = 1

    with pytest.raises(TypeError):
        _ = Review(movie, review_text, rating)
def test_repr_no_rating(movie):
    review_text = "Test"
    rating = 1.1
    review = Review(movie, review_text, rating)

    expected = f'<Review <Movie 1, TestMovie, 2020>, Test, None, {review.timestamp.isoformat()}, None>'
    assert repr(review) == expected
def test_construction_invalid_timestamp_type(movie):
    review_text = "   Text    with   spaces  \n"
    rating = 10
    timestamp = 123

    with pytest.raises(TypeError):
        _ = Review(movie, review_text, rating, timestamp)
    def simulate(
            self,
            num_users: int = _DEFAULT_USER_COUNT,
            min_num_movies: int = _DEFAULT_MIN_MOVIES_PER_USER,
            max_num_movies: int = _DEFAULT_MAX_MOVIES_PER_USER) -> 'State':

        self._validate_params(num_users, min_num_movies, max_num_movies)

        users = []
        reviews = []

        num_movies = len(self._movies)
        upper_bound = min(max_num_movies or num_movies, num_movies)
        population = list(range(0, num_movies))
        now = datetime.utcnow().timestamp()

        for i in range(num_users):
            username = _rand_string(1, 4)
            # Hash the password. We use method='plain' here to save time.
            password = generate_password_hash(_rand_string(1, 1),
                                              method='plain')
            user = User(username, password)

            # Pick n distinct movies
            movies = [
                self._movies[idx] for idx in sample(
                    population, randint(min_num_movies, upper_bound))
            ]

            # Add those movies to the user's watchlist
            for movie in movies:
                user.add_to_watchlist(movie)

            # Watch a random number of movies on the user's watchlist
            for j in range(randint(0, user.watchlist_size())):
                user.watch_movie(movies[j])

            # Review a random number of the movies the user watched
            for j in range(randint(0, len(user.watched_movies))):
                movie = movies[j]
                review_text = _rand_string(spaces=True)
                rating = randint(1, 10)

                release_date = datetime(movie.release_date, 1, 1).timestamp()
                delta = randint(
                    0, int(now - release_date)
                )  # float -> int will be lossy but not a big deal here
                delta += random()  # Add a random number of milliseconds
                timestamp = datetime.fromtimestamp(now - delta)

                review = Review(movie, review_text, rating, timestamp, user)
                user.add_review(review)
                reviews.append(review)

            users.append(user)

        return self.State(users, reviews)
def test_constructor_invalid_review_text_type(movie):
    review_text = 42
    rating = 1
    review = Review(movie, review_text, rating)

    assert review.movie == movie
    assert review.review_text is None
    assert review.rating == rating
    assert review.timestamp.timestamp() == pytest.approx(datetime.utcnow().timestamp(), abs=_TIMESTAMP_TOLERANCE)
def test_constructor_invalid_rating_type(movie):
    review_text = "   Text    with   spaces  \n"
    review_text_expected = "Text    with   spaces"
    rating = 1.0
    review = Review(movie, review_text, rating)

    assert review.movie == movie
    assert review.review_text == review_text_expected
    assert review.rating is None
    assert review.timestamp.timestamp() == pytest.approx(datetime.utcnow().timestamp(), abs=_TIMESTAMP_TOLERANCE)
Beispiel #10
0
def add_review(repo: AbstractRepository, movie: Movie, review_text: str, rating: int, user: Union[User, None] = None):
    """
    Adds a review to this repository. If a user is specified it is treated as being the review's creator, otherwise the
    review is considered to be posted anonymously.
    """
    review = Review(movie, review_text, rating, user=user)
    repo.add_review(review, user)

    if user:
        user.add_review(review)
Beispiel #11
0
def reviews(movies):
    return [Review(movie, f'Review{i}', 1) for i, movie in enumerate(movies)]
Beispiel #12
0
def review(movie, user):
    return Review(movie, "Text  with  some  spaces", 1, user=user)