def test_save_reviewed_movie(empty_session): # Create Movie User objects. movie = make_movie() user = make_user() # Create a new Review that is bidirectionally linked with the User and Movie. review_text = "Some review text." review = make_review(review_text, user, movie) # Save the new Movie. empty_session.add(movie) empty_session.commit() # Test test_saving_of_movie() checks for insertion into the movies table. rows = list(empty_session.execute('SELECT id FROM movies')) movie_key = rows[0][0] # Test test_saving_of_users() checks for insertion into the users table. rows = list(empty_session.execute('SELECT id FROM users')) user_key = rows[0][0] # Check that the reviews table has a new record that links to the movies and users # tables. rows = list( empty_session.execute('SELECT user_id, movie_id, review FROM reviews')) assert rows == [(user_key, movie_key, review_text)]
def test_repository_can_add_a_review(in_memory_repo): user = in_memory_repo.get_user('thorke') movie = in_memory_repo.get_movie(2) review = make_review("Trump's onto it!", user, movie) in_memory_repo.add_review(review) assert review in in_memory_repo.get_reviews()
def load_reviews(data_path: str, repo: MemoryRepository, users): for data_row in read_csv_file(os.path.join(data_path, 'comments.csv')): review = make_review( review_text=data_row[3], user=users[data_row[1]], movie=repo.get_movie(int(data_row[2])), timestamp=datetime.fromisoformat(data_row[4]) ) repo.add_review(review)
def test_repository_can_add_a_review(session_factory): repo = SqlAlchemyRepository(session_factory) user = repo.get_user('thorke') movie = repo.get_movie(2) review = make_review("Trump's onto it!", user, movie) repo.add_review(review) assert review in repo.get_reviews()
def add_review(movie_id: int, review_text: str, username: str, repo: AbstractRepository): # Check that the movie exists. movie = repo.get_movie(movie_id) if movie is None: raise NonExistentMovieException user = repo.get_user(username) if user is None: raise UnknownUserException # Create review. review = make_review(review_text, user, movie) # Update the repository. repo.add_review(review)
def test_can_retrieve_an_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 = make_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_make_review_establishes_relationships(movie, user): review_text = 'COVID-19 in the USA!' review = make_review(review_text, user, movie) # Check that the User object knows about the Review. assert review in user.reviews # Check that the Review knows about the User. assert review.user is user # Check that Movie knows about the Review. assert review in movie.reviews # Check that the Review knows about the Movie. assert review.movie is movie