Skip to content

Commit 9818e89

Browse files
committed
added a factory with relations and basic tests
1 parent 5ca112e commit 9818e89

File tree

7 files changed

+128
-9
lines changed

7 files changed

+128
-9
lines changed

example/factories/__init__.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,40 @@
33

44
import factory
55

6-
from example.models import Blog
6+
from example.models import Blog, Author, Entry
77

88

99
class BlogFactory(factory.django.DjangoModelFactory):
1010
class Meta:
1111
model = Blog
1212

1313
name = "Blog 1"
14+
15+
16+
class AuthorFactory(factory.django.DjangoModelFactory):
17+
class Meta:
18+
model = Author
19+
20+
name = "Author 1"
21+
22+
23+
24+
class EntryFactory(factory.django.DjangoModelFactory):
25+
class Meta:
26+
model = Entry
27+
28+
headline = "Headline 1"
29+
body_text = "Here goes the body text"
30+
31+
blog = factory.SubFactory(BlogFactory)
32+
33+
@factory.post_generation
34+
def authors(self, create, extracted, **kwargs):
35+
if extracted:
36+
if isinstance(extracted, (list, tuple)):
37+
for author in extracted:
38+
print author
39+
self.authors.add(author)
40+
else:
41+
print extracted
42+
self.authors.add(extracted)

example/models.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ def __str__(self):
3838
class Entry(BaseModel):
3939
blog = models.ForeignKey(Blog)
4040
headline = models.CharField(max_length=255)
41-
body_text = models.TextField()
42-
pub_date = models.DateField()
43-
mod_date = models.DateField()
41+
body_text = models.TextField(null=True)
42+
pub_date = models.DateField(null=True)
43+
mod_date = models.DateField(null=True)
4444
authors = models.ManyToManyField(Author)
4545
n_comments = models.IntegerField(default=0)
4646
n_pingbacks = models.IntegerField(default=0)
4747
rating = models.IntegerField(default=0)
4848

4949
def __str__(self):
5050
return self.headline
51-

example/serializers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ class Meta:
88
model = Blog
99
fields = ('name', )
1010

11+
1112
class EntrySerializer(serializers.ModelSerializer):
1213
class Meta:
1314
model = Entry
1415
fields = ('blog', 'headline', 'body_text', 'pub_date', 'mod_date',
1516
'authors',)
1617

18+
1719
class AuthorSerializer(serializers.ModelSerializer):
1820

1921
class Meta:
2022
model = Author
2123
fields = ('name', 'email',)
22-

example/tests/conftest.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import pytest
12
from pytest_factoryboy import register
23

3-
from example.factories import BlogFactory
4+
from example.factories import BlogFactory, AuthorFactory, EntryFactory
45

56
register(BlogFactory)
7+
register(AuthorFactory)
8+
register(EntryFactory)

example/tests/test_factories.py

+17
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,20 @@ def test_multiple_blog(blog_factory):
2626

2727
assert another_blog.name == 'Cool Blog'
2828
assert new_blog.name == 'Awesome Blog'
29+
30+
31+
def test_factories_with_relations(author_factory, entry_factory):
32+
33+
author = author_factory(name="Joel Spolsky")
34+
entry = entry_factory(
35+
headline=("The Absolute Minimum Every Software Developer"
36+
"Absolutely, Positively Must Know About Unicode "
37+
"and Character Sets (No Excuses!)"),
38+
blog__name='Joel on Software', authors=(author, ))
39+
40+
assert entry.blog.name == 'Joel on Software'
41+
assert entry.headline == ("The Absolute Minimum Every Software Developer"
42+
"Absolutely, Positively Must Know About Unicode "
43+
"and Character Sets (No Excuses!)")
44+
assert entry.authors.all().count() == 1
45+
assert entry.authors.all()[0].name == 'Joel Spolsky'

example/tests/test_pagination.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from django.core.urlresolvers import reverse
2+
3+
import pytest
4+
from example.tests.utils import dump_json, redump_json
5+
6+
pytestmark = pytest.mark.django_db
7+
8+
9+
@pytest.fixture
10+
def single_entry(author_factory, entry_factory):
11+
12+
author = author_factory(name="Joel Spolsky")
13+
entry = entry_factory(
14+
headline=("The Absolute Minimum Every Software Developer"
15+
"Absolutely, Positively Must Know About Unicode "
16+
"and Character Sets (No Excuses!)"),
17+
blog__name='Joel on Software',
18+
authors=(author, )
19+
)
20+
21+
22+
def test_pagination_with_single_entry(single_entry, client):
23+
24+
expected = {
25+
"data": [
26+
{
27+
"type": "posts",
28+
"id": "1",
29+
"attributes":
30+
{
31+
"headline": "The Absolute Minimum Every Software DeveloperAbsolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)",
32+
"bodyText": "Here goes the body text",
33+
"pubDate": None,
34+
"modDate": None
35+
},
36+
"relationships":
37+
{
38+
"blog": {
39+
"data": {"type": "blogs", "id": "1"}
40+
},
41+
"authors": {
42+
"meta": {"count": 1},
43+
"data": [{"type": "authors", "id": "1"}]
44+
}
45+
}
46+
}],
47+
"links": {
48+
"first": "http://testserver/entries?page=1",
49+
"last": "http://testserver/entries?page=1",
50+
"next": None,
51+
"prev": None,
52+
},
53+
"meta":
54+
{
55+
"pagination":
56+
{
57+
"page": 1,
58+
"pages": 1,
59+
"count": 1
60+
}
61+
}
62+
}
63+
64+
response = client.get(reverse("entry-list"))
65+
content_dump = redump_json(response.content)
66+
expected_dump = dump_json(expected)
67+
68+
assert content_dump == expected_dump

example/views.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
from rest_framework import viewsets
22
from example.models import Blog, Entry, Author
3-
from example.serializers import BlogSerializer, EntrySerializer, AuthorSerializer
3+
from example.serializers import (BlogSerializer, EntrySerializer,
4+
AuthorSerializer)
45

56

67
class BlogViewSet(viewsets.ModelViewSet):
78

89
queryset = Blog.objects.all()
910
serializer_class = BlogSerializer
1011

12+
1113
class EntryViewSet(viewsets.ModelViewSet):
1214

1315
queryset = Entry.objects.all()
1416
serializer_class = EntrySerializer
1517
resource_name = 'posts'
1618

19+
1720
class AuthorViewSet(viewsets.ModelViewSet):
1821

1922
queryset = Author.objects.all()
2023
serializer_class = AuthorSerializer
21-

0 commit comments

Comments
 (0)