def test_save_commented_movie(empty_session): # Create Article User objects. movie = make_movie() user = make_user() # Create a new Comment that is bidirectionally linked with the User and Article. review_text = "Some comment text." review = make_comment(review_text, user, movie) # Save the new Article. empty_session.add(movie) empty_session.commit() # Test test_saving_of_article() checks for insertion into the articles table. rows = list(empty_session.execute('SELECT rank 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 comments table has a new record that links to the articles 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 load_comments(data_path: str, repo: MemoryRepository, users): for data_row in read_csv_file(os.path.join(data_path, 'comments.csv')): comment = make_comment( comment_text=data_row[3], user=users[data_row[1]], movie=repo.get_movie(int(data_row[2])), ) repo.add_review(comment)
def test_repository_can_add_a_comment(in_memory_repo): user = in_memory_repo.get_user('thorke') article = in_memory_repo.get_movie(2) comment = make_comment("Trump's onto it!", user, article) in_memory_repo.add_review(comment) assert comment in in_memory_repo.get_reviews()
def test_repository_can_add_a_reviews(session_factory): repo = SqlAlchemyRepository(session_factory) user = repo.get_user('thorke') movie = repo.get_movie(2) review = make_comment("Trump's onto it!", user, movie) repo.add_review(review) assert review in repo.get_reviews()
def test_make_comment_establishes_relationships(movie, user): comment_text = 'COVID-19 in the USA!' comment = make_comment(comment_text, user, movie) # Check that the User object knows about the Comment. assert comment in user.reviews # Check that the Comment knows about the User. assert comment.user is user # Check that Article knows about the Comment. assert comment in movie.review # Check that the Comment knows about the Article. assert comment.movie is movie
def test_can_retrieve_a_movie_and_add_a_review_to_it(session_factory): repo = SqlAlchemyRepository(session_factory) # Fetch Article and User. movie = repo.get_movie(2) author = repo.get_user('thorke') # Create a new Comment, connecting it to the Article and User. review = make_comment('First death in Australia', author, movie) movie_fetched = repo.get_movie(2) author_fetched = repo.get_user('thorke') assert review in movie_fetched.review assert review in author_fetched.reviews
def add_review(article_id: int, comment_text: str, username: str, repo: AbstractRepository): # Check that the article exists. article = repo.get_movie(article_id) if article is None: raise NonExistentArticleException user = repo.get_user(username) if user is None: raise UnknownUserException # Create comment. comment = make_comment(comment_text, user, article) # Update the repository. repo.add_review(comment)
def test_saving_of_reviews(empty_session): movie_key = insert_movie(empty_session) user_key = insert_user(empty_session, ("Andrew", "1234")) rows = empty_session.query(Movie).all() movie = rows[0] user = empty_session.query(User).filter(User.user_name == "Andrew").one() # Create a new Comment that is bidirectionally linked with the User and Article. review_text = "Some comment text." review = make_comment(review_text, user, movie) # Note: if the bidirectional links between the new Comment and the User and # Article objects hadn't been established in memory, they would exist following # committing the addition of the Comment to the database. empty_session.add(review) empty_session.commit() rows = list( empty_session.execute('SELECT user_id, movie_id, review FROM reviews')) assert rows == [(user_key, movie_key, review_text)]