Skip to content

Commit a71926b

Browse files
committed
Added a comment factory and added them to entry serializer
1 parent 63d32f4 commit a71926b

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

example/factories/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import factory
55
from faker import Factory as FakerFactory
6-
from example.models import Blog, Author, Entry
6+
from example.models import Blog, Author, Entry, Comment
77

88
faker = FakerFactory.create()
99
faker.seed(983843)
@@ -40,3 +40,13 @@ def authors(self, create, extracted, **kwargs):
4040
self.authors.add(author)
4141
else:
4242
self.authors.add(extracted)
43+
44+
45+
class CommentFactory(factory.django.DjangoModelFactory):
46+
class Meta:
47+
model = Comment
48+
49+
entry = factory.SubFactory(EntryFactory)
50+
body = factory.LazyAttribute(lambda x: faker.text())
51+
author = factory.SubFactory(AuthorFactory)
52+

example/serializers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from rest_framework import serializers
1+
from rest_framework_json_api import serializers, relations
22
from example.models import Blog, Entry, Author, Comment
33

44

@@ -10,10 +10,14 @@ class Meta:
1010

1111

1212
class EntrySerializer(serializers.ModelSerializer):
13+
14+
comments = relations.ResourceRelatedField(
15+
source='comment_set', many=True, read_only=True)
16+
1317
class Meta:
1418
model = Entry
1519
fields = ('blog', 'headline', 'body_text', 'pub_date', 'mod_date',
16-
'authors',)
20+
'authors', 'comments',)
1721

1822

1923
class AuthorSerializer(serializers.ModelSerializer):

example/tests/conftest.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
import pytest
22
from pytest_factoryboy import register
33

4-
from example.factories import BlogFactory, AuthorFactory, EntryFactory
4+
from example.factories import BlogFactory, AuthorFactory, EntryFactory, CommentFactory
55

66
register(BlogFactory)
77
register(AuthorFactory)
88
register(EntryFactory)
9+
register(CommentFactory)
910

1011

1112
@pytest.fixture
12-
def single_entry(blog, author, entry_factory):
13+
def single_entry(blog, author, entry_factory, comment_factory):
1314

14-
return entry_factory(blog=blog, authors=(author,))
15+
entry = entry_factory(blog=blog, authors=(author,))
16+
comment_factory(entry=entry)
17+
return entry
1518

1619

1720
@pytest.fixture
18-
def multiple_entries(blog_factory, author_factory, entry_factory):
21+
def multiple_entries(blog_factory, author_factory, entry_factory, comment_factory):
1922

20-
return [
23+
entries = [
2124
entry_factory(blog=blog_factory(), authors=(author_factory(),)),
2225
entry_factory(blog=blog_factory(), authors=(author_factory(),)),
2326
]
27+
comment_factory(entry=entries[0])
28+
comment_factory(entry=entries[1])
29+
return entries
2430

example/tests/integration/test_non_paginated_responses.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def test_multiple_entries_no_pagination(multiple_entries, rf):
3434
"authors": {
3535
"meta": {"count": 1},
3636
"data": [{"type": "authors", "id": "1"}]
37+
},
38+
"comments": {
39+
"meta": {"count": 1},
40+
"data": [{"type": "comments", "id": "1"}]
3741
}
3842
}
3943
},
@@ -55,6 +59,10 @@ def test_multiple_entries_no_pagination(multiple_entries, rf):
5559
"authors": {
5660
"meta": {"count": 1},
5761
"data": [{"type": "authors", "id": "2"}]
62+
},
63+
"comments": {
64+
"meta": {"count": 1},
65+
"data": [{"type": "comments", "id": "2"}]
5866
}
5967
}
6068
},

example/tests/integration/test_pagination.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def test_pagination_with_single_entry(single_entry, client):
2828
"authors": {
2929
"meta": {"count": 1},
3030
"data": [{"type": "authors", "id": "1"}]
31+
},
32+
"comments": {
33+
"meta": {"count": 1},
34+
"data": [{"type": "comments", "id": "1"}]
3135
}
3236
}
3337
}],

0 commit comments

Comments
 (0)