|
| 1 | +from datetime import timedelta |
| 2 | + |
| 3 | +from django.apps import apps |
| 4 | +from django.contrib.auth.models import User |
| 5 | +from django.template import Context, Template |
| 6 | +from django.utils import timezone |
| 7 | + |
| 8 | +from blog.models import Category, Post |
| 9 | +from .base import CommentDataTestCase |
| 10 | +from ..forms import CommentForm |
| 11 | +from ..models import Comment |
| 12 | +from ..templatetags.comments_extras import show_comment_form, show_comments |
| 13 | + |
| 14 | + |
| 15 | +class CommentExtraTestCase(CommentDataTestCase): |
| 16 | + def setUp(self) -> None: |
| 17 | + super().setUp() |
| 18 | + self.ctx = Context() |
| 19 | + |
| 20 | + def test_show_comment_form_with_empty_form(self): |
| 21 | + template = Template("{% load comments_extras %}" "{% show_comment_form post %}") |
| 22 | + form = CommentForm() |
| 23 | + context = Context(show_comment_form(self.ctx, self.post)) |
| 24 | + expected_html = template.render(context) |
| 25 | + for field in form: |
| 26 | + label = '<label for="{}">{}:</label>'.format( |
| 27 | + field.id_for_label, field.label |
| 28 | + ) |
| 29 | + self.assertInHTML(label, expected_html) |
| 30 | + self.assertInHTML(str(field), expected_html) |
| 31 | + |
| 32 | + def test_show_comment_form_with_invalid_bound_form(self): |
| 33 | + template = Template( |
| 34 | + "{% load comments_extras %}" "{% show_comment_form post form %}" |
| 35 | + ) |
| 36 | + invalid_data = { |
| 37 | + "email": "invalid_email", |
| 38 | + } |
| 39 | + form = CommentForm(data=invalid_data) |
| 40 | + self.assertFalse(form.is_valid()) |
| 41 | + context = Context(show_comment_form(self.ctx, self.post, form=form)) |
| 42 | + expected_html = template.render(context) |
| 43 | + for field in form: |
| 44 | + label = '<label for="{}">{}:</label>'.format( |
| 45 | + field.id_for_label, field.label |
| 46 | + ) |
| 47 | + self.assertInHTML(label, expected_html) |
| 48 | + self.assertInHTML(str(field), expected_html) |
| 49 | + self.assertInHTML(str(field.errors), expected_html) |
| 50 | + |
| 51 | + def test_show_comments_without_any_comment(self): |
| 52 | + template = Template("{% load comments_extras %}" "{% show_comments post %}") |
| 53 | + ctx_dict = show_comments(self.ctx, self.post) |
| 54 | + ctx_dict["post"] = self.post |
| 55 | + context = Context(ctx_dict) |
| 56 | + expected_html = template.render(context) |
| 57 | + self.assertInHTML("<h3>评论列表,共 <span>0</span> 条评论</h3>", expected_html) |
| 58 | + self.assertInHTML("暂无评论", expected_html) |
| 59 | + |
| 60 | + def test_show_comments_with_comments(self): |
| 61 | + comment1 = Comment.objects.create( |
| 62 | + name="评论者1", email="[email protected]", text="评论内容1", post=self. post, |
| 63 | + ) |
| 64 | + comment2 = Comment.objects.create( |
| 65 | + name="评论者2", |
| 66 | + |
| 67 | + text="评论内容2", |
| 68 | + post=self.post, |
| 69 | + created_time=timezone.now() - timedelta(days=1), |
| 70 | + ) |
| 71 | + template = Template("{% load comments_extras %}" "{% show_comments post %}") |
| 72 | + ctx_dict = show_comments(self.ctx, self.post) |
| 73 | + ctx_dict["post"] = self.post |
| 74 | + context = Context(ctx_dict) |
| 75 | + expected_html = template.render(context) |
| 76 | + self.assertInHTML("<h3>评论列表,共 <span>2</span> 条评论</h3>", expected_html) |
| 77 | + self.assertInHTML(comment1.name, expected_html) |
| 78 | + self.assertInHTML(comment1.text, expected_html) |
| 79 | + self.assertInHTML(comment2.name, expected_html) |
| 80 | + self.assertInHTML(comment2.text, expected_html) |
| 81 | + self.assertQuerysetEqual( |
| 82 | + ctx_dict["comment_list"], [repr(c) for c in [comment1, comment2]] |
| 83 | + ) |
0 commit comments