Skip to content

added a factory with relations and some basic tests #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion example/factories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,38 @@

import factory

from example.models import Blog
from example.models import Blog, Author, Entry


class BlogFactory(factory.django.DjangoModelFactory):
class Meta:
model = Blog

name = "Blog 1"


class AuthorFactory(factory.django.DjangoModelFactory):
class Meta:
model = Author

name = "Author 1"
email = "[email protected]"


class EntryFactory(factory.django.DjangoModelFactory):
class Meta:
model = Entry

headline = "Headline 1"
body_text = "Here goes the body text"

blog = factory.SubFactory(BlogFactory)

@factory.post_generation
def authors(self, create, extracted, **kwargs):
if extracted:
if isinstance(extracted, (list, tuple)):
for author in extracted:
self.authors.add(author)
else:
self.authors.add(extracted)
7 changes: 3 additions & 4 deletions example/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ def __str__(self):
class Entry(BaseModel):
blog = models.ForeignKey(Blog)
headline = models.CharField(max_length=255)
body_text = models.TextField()
pub_date = models.DateField()
mod_date = models.DateField()
body_text = models.TextField(null=True)
pub_date = models.DateField(null=True)
mod_date = models.DateField(null=True)
authors = models.ManyToManyField(Author)
n_comments = models.IntegerField(default=0)
n_pingbacks = models.IntegerField(default=0)
rating = models.IntegerField(default=0)

def __str__(self):
return self.headline

3 changes: 2 additions & 1 deletion example/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ class Meta:
model = Blog
fields = ('name', )


class EntrySerializer(serializers.ModelSerializer):
class Meta:
model = Entry
fields = ('blog', 'headline', 'body_text', 'pub_date', 'mod_date',
'authors',)


class AuthorSerializer(serializers.ModelSerializer):

class Meta:
model = Author
fields = ('name', 'email',)

5 changes: 4 additions & 1 deletion example/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest
from pytest_factoryboy import register

from example.factories import BlogFactory
from example.factories import BlogFactory, AuthorFactory, EntryFactory

register(BlogFactory)
register(AuthorFactory)
register(EntryFactory)
17 changes: 17 additions & 0 deletions example/tests/test_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,20 @@ def test_multiple_blog(blog_factory):

assert another_blog.name == 'Cool Blog'
assert new_blog.name == 'Awesome Blog'


def test_factories_with_relations(author_factory, entry_factory):

author = author_factory(name="Joel Spolsky")
entry = entry_factory(
headline=("The Absolute Minimum Every Software Developer"
"Absolutely, Positively Must Know About Unicode "
"and Character Sets (No Excuses!)"),
blog__name='Joel on Software', authors=(author, ))

assert entry.blog.name == 'Joel on Software'
assert entry.headline == ("The Absolute Minimum Every Software Developer"
"Absolutely, Positively Must Know About Unicode "
"and Character Sets (No Excuses!)")
assert entry.authors.all().count() == 1
assert entry.authors.all()[0].name == 'Joel Spolsky'
68 changes: 68 additions & 0 deletions example/tests/test_pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from django.core.urlresolvers import reverse

import pytest
from example.tests.utils import dump_json, redump_json

pytestmark = pytest.mark.django_db


@pytest.fixture
def single_entry(author_factory, entry_factory):

author = author_factory(name="Joel Spolsky")
entry = entry_factory(
headline=("The Absolute Minimum Every Software Developer"
"Absolutely, Positively Must Know About Unicode "
"and Character Sets (No Excuses!)"),
blog__name='Joel on Software',
authors=(author, )
)


def test_pagination_with_single_entry(single_entry, client):

expected = {
"data": [
{
"type": "posts",
"id": "1",
"attributes":
{
"headline": "The Absolute Minimum Every Software DeveloperAbsolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)",
"body-text": "Here goes the body text",
"pub-date": None,
"mod-date": None
},
"relationships":
{
"blog": {
"data": {"type": "blogs", "id": "1"}
},
"authors": {
"meta": {"count": 1},
"data": [{"type": "authors", "id": "1"}]
}
}
}],
"links": {
"first": "http://testserver/entries?page=1",
"last": "http://testserver/entries?page=1",
"next": None,
"prev": None,
},
"meta":
{
"pagination":
{
"page": 1,
"pages": 1,
"count": 1
}
}
}

response = client.get(reverse("entry-list"))
content_dump = redump_json(response.content)
expected_dump = dump_json(expected)

assert content_dump == expected_dump
6 changes: 4 additions & 2 deletions example/views.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
from rest_framework import viewsets
from example.models import Blog, Entry, Author
from example.serializers import BlogSerializer, EntrySerializer, AuthorSerializer
from example.serializers import (BlogSerializer, EntrySerializer,
AuthorSerializer)


class BlogViewSet(viewsets.ModelViewSet):

queryset = Blog.objects.all()
serializer_class = BlogSerializer


class EntryViewSet(viewsets.ModelViewSet):

queryset = Entry.objects.all()
serializer_class = EntrySerializer
resource_name = 'posts'


class AuthorViewSet(viewsets.ModelViewSet):

queryset = Author.objects.all()
serializer_class = AuthorSerializer