Skip to content

Commit 56f1a4e

Browse files
committed
test commentbox_props_response() and it's behavior with non-ID primary keys
1 parent a58336e commit 56f1a4e

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Generated by Django 4.0.6 on 2022-07-08 11:28
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('tests', '0002_auto_20170523_1624'),
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='UUIDDiary',
16+
fields=[
17+
('diary_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, to='tests.diary')),
18+
('uuid', models.UUIDField(editable=False, primary_key=True, serialize=False, verbose_name='uuid')),
19+
],
20+
bases=('tests.diary',),
21+
),
22+
]

django_comments_xtd/tests/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class Meta:
5252
ordering = ('-publish',)
5353

5454

55+
class UUIDDiary(Diary):
56+
"""Diary, that accepts comments."""
57+
uuid = models.UUIDField("uuid", editable=False, primary_key=True)
58+
59+
5560
class DiaryCommentModerator(XtdCommentModerator):
5661
email_notification = True
5762
enable_field = 'allow_comments'
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from django.test import TestCase, RequestFactory
2+
from django.contrib.auth.models import AnonymousUser
3+
from django_comments_xtd.api.frontend import commentbox_props_response
4+
from django_comments_xtd.tests.models import UUIDDiary, Diary
5+
from django_comments_xtd.models import XtdComment
6+
import uuid
7+
from pprint import pprint
8+
9+
10+
class CommentBoxTestCase(TestCase):
11+
def test_commentbox_props_response(self):
12+
request_factory = RequestFactory()
13+
request = request_factory.get("/")
14+
user = AnonymousUser()
15+
diary = Diary.objects.create(
16+
body='Lorem ipsum',
17+
allow_comments=True
18+
)
19+
XtdComment.objects.create(
20+
content_object=diary,
21+
site_id=1,
22+
)
23+
response = commentbox_props_response(diary, user, request)
24+
d = response.data
25+
pprint(d)
26+
self.assertEqual(d['comment_count'], 1)
27+
self.assertEqual(d['form']['object_pk'], str(diary.id))
28+
self.assertEqual(d['count_url'], '/comments/api/tests-diary/1/count/')
29+
self.assertEqual(d['list_url'], '/comments/api/tests-diary/1/')
30+
31+
def test_commentbox_props_response_uuid(self):
32+
request_factory = RequestFactory()
33+
request = request_factory.get("/")
34+
user = AnonymousUser()
35+
diary = UUIDDiary.objects.create(
36+
uuid=uuid.uuid4(),
37+
body='Lorem ipsum',
38+
allow_comments=True
39+
)
40+
XtdComment.objects.create(
41+
content_object=diary,
42+
site_id=1,
43+
)
44+
response = commentbox_props_response(diary, user, request)
45+
d = response.data
46+
pprint(d)
47+
self.assertEqual(d['comment_count'], 1)
48+
self.assertEqual(d['form']['object_pk'], str(diary.uuid))
49+
self.assertEqual(d['count_url'], f'/comments/api/tests-uuiddiary/{diary.uuid}/count/')
50+
self.assertEqual(d['list_url'], f'/comments/api/tests-uuiddiary/{diary.uuid}/')

0 commit comments

Comments
 (0)