Instantly test-cover your Django REST Framework based API
from django.db import models
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
def __unicode__(self):
return self.name
class Author(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField()
def __unicode__(self):
return self.name
class Entry(models.Model):
blog = models.ForeignKey(Blog)
headline = models.CharField(max_length=255)
body_text = models.TextField()
pub_date = models.DateField()
mod_date = models.DateField(auto_now=True)
authors = models.ManyToManyField(Author)
n_comments = models.IntegerField()
n_pingbacks = models.IntegerField()
rating = models.IntegerField()
def __unicode__(self):
return self.headline
from rest_framework import serializers
from . import models
class Entry(serializers.ModelSerializer):
class Meta:
model = models.Entry
from rest_framework import viewsets
from . import models, serializers
class Entries(viewsets.ModelViewSet):
queryset = models.Entry.objects.all()
serializer_class = serializers.Entry
from django.conf.urls import url, include
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'entries', views.Entries)
urlpatterns = [
url(r'^', include(router.urls)),
]
import datetime
import factory
from factory import fuzzy
from django.contrib import auth
from . import models
class Blog(factory.DjangoModelFactory):
class Meta:
model = models.Blog
# fields...
class Author(factory.DjangoModelFactory):
class Meta:
model = models.Author
# fields...
class Entry(factory.DjangoModelFactory):
class Meta:
model = models.Entry
# fields...
class User(factory.DjangoModelFactory):
class Meta:
model = auth.get_user_model()
# fields...
import datetime
from rest_assured.testcases import ReadWriteRESTAPITestCaseMixin, BaseRESTAPITestCase
from . import factories
class EntryAPITestCase(ReadWriteRESTAPITestCaseMixin, BaseRESTAPITestCase):
base_name = 'entry' # this is the base_name generated by the DefaultRouter
factory_class = factories.Entry
user_factory = factories.User
update_data = {'rating': 5}
def setUp(self):
self.author = factories.Author.create()
super(EntryAPITestCase, self).setUp()
def get_object(self, factory):
return factory.create(authors=[self.author])
def get_create_data(self):
return {'headline': 'Lucifer Sam',
'body_text': 'is a song by British psychedelic rock band Pink Floyd.',
'authors': [self.author.pk],
'rating': 4,
'n_pingbacks': 0,
'n_comments': 0,
'pub_date': datetime.date(2014, 11, 12),
'blog': self.object.blog.pk}