def test_cannot_add_comment_for_non_existent_article(in_memory_repo): article_id = 101 comment_text = "COVID-19 - what's that?" username = '******' # Call the service layer to attempt to add the comment. with pytest.raises(news_services.NonExistentArticleException): news_services.add_comment(article_id, comment_text, username, in_memory_repo, 3)
def test_cannot_add_comment_by_unknown_user(in_memory_repo): article_id = 3 comment_text = 'The loonies are stripping the supermarkets bare!' username = '******' # Call the service layer to attempt to add the comment. with pytest.raises(news_services.UnknownUserException): news_services.add_comment(article_id, comment_text, username, in_memory_repo, 3)
def test_can_add_comment(in_memory_repo): article_id = 3 comment_text = 'The loonies are stripping the supermarkets bare!' username = '******' # Call the service layer to add the comment. news_services.add_comment(article_id, comment_text, username, in_memory_repo, 3) # Retrieve the comments for the article from the repository. comments_as_dict = news_services.get_comments_for_article(article_id, in_memory_repo) # Check that the comments include a comment with the new comment text. assert next( (dictionary['comment_text'] for dictionary in comments_as_dict if dictionary['comment_text'] == comment_text), None) is not None
def comment_on_article(): # Obtain the username of the currently logged in user. username = session['username'] # Create form. The form maintains state, e.g. when this method is called with a HTTP GET request and populates # the form with an article id, when subsequently called with a HTTP POST request, the article id remains in the # form. form = CommentForm() if form.validate_on_submit(): # Successful POST, i.e. the comment text has passed data validation. # Extract the article id, representing the commented article, from the form. article_id = int(form.article_id.data) # Use the service layer to store the new comment. services.add_comment(article_id, form.comment.data, username, repo.repo_instance, form.rating.data) # Retrieve the article in dict form. article = services.get_article(article_id, repo.repo_instance) # Cause the web browser to display the page of all articles that have the same date as the commented article, # and display all comments, including the new comment. return redirect(url_for('news_bp.articles', article_input=article_id)) if request.method == 'GET': # Request is a HTTP GET to display the form. # Extract the article id, representing the article to comment, from a query parameter of the GET request. article_id = int(request.args.get('article')) # Store the article id in the form. form.article_id.data = article_id else: # Request is a HTTP POST where form validation has failed. # Extract the article id of the article being commented from the form. article_id = int(form.article_id.data) # For a GET or an unsuccessful POST, retrieve the article to comment in dict form, and return a Web page that allows # the user to enter a comment. The generated Web page includes a form object. article = services.get_article(article_id, repo.repo_instance) return render_template('news/comment_on_article.html', title='Edit article', article=article, form=form, handler_url=url_for('news_bp.comment_on_article'), selected_articles=utilities.get_selected_articles(), tag_urls=utilities.get_tags_and_urls())