def test_save_commented_article(empty_session): # Create Article User objects. article = make_article() user = make_user() rating = 5 # Create a new Comment that is bidirectionally linked with the User and Article. comment_text = "Some comment text." comment = make_comment(comment_text, user, article, rating) # Save the new Article. empty_session.add(article) empty_session.commit() # Test test_saving_of_article() checks for insertion into the articles table. rows = list(empty_session.execute('SELECT id FROM articles')) article_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, article_id, comment FROM comments')) assert rows == [(user_key, article_key, comment_text)]
def test_repository_can_add_a_comment(in_memory_repo): user = in_memory_repo.get_user('thorke') article = in_memory_repo.get_article(2) comment = make_comment("Trump's onto it!", user, article, 10) in_memory_repo.add_comment(comment) assert comment in in_memory_repo.get_comments()
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]], article=repo.get_article(int(data_row[2])), timestamp=datetime.fromisoformat(data_row[4]), rating=int(data_row[5]) ) repo.add_comment(comment)
def test_repository_can_add_a_comment(session_factory): repo = SqlAlchemyRepository(session_factory) user = repo.get_user('thorke') article = repo.get_article(2) comment = make_comment("Trump's onto it!", user, article) repo.add_comment(comment) assert comment in repo.get_comments()
def add_comment(article_id: int, comment_text: str, username: str, repo: AbstractRepository, rating_in: int): # Check that the article exists. article = repo.get_article(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, rating_in) # Update the repository. repo.add_comment(comment)
def test_can_retrieve_an_article_and_add_a_comment_to_it(session_factory): repo = SqlAlchemyRepository(session_factory) # Fetch Article and User. article = repo.get_article(5) author = repo.get_user('thorke') # Create a new Comment, connecting it to the Article and User. comment = make_comment('First death in Australia', author, article) article_fetched = repo.get_article(5) author_fetched = repo.get_user('thorke') assert comment in article_fetched.comments assert comment in author_fetched.comments
def test_make_comment_establishes_relationships(article, user): comment_text = 'COVID-19 in the USA!' rating = 4 comment = make_comment(comment_text, user, article, rating) # Check that the User object knows about the Comment. assert comment in user.comments # Check that the Comment knows about the User. assert comment.user is user # Check that Article knows about the Comment. assert comment in article.comments # Check that the Comment knows about the Article. assert comment.article is article