aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/tests/mixins.py
diff options
context:
space:
mode:
authorFernando Zunino2011-07-07 23:33:39 -0300
committerFernando Zunino2011-07-07 23:33:39 -0300
commit8a2944acdf66a966f0bd4ea4770f012f2597fb4e (patch)
tree8cfca9b5701f8bccbd154e2cf7f59017cf695746 /djangorestframework/tests/mixins.py
parenta634d10cbff798b07690eff332bee862b7fae211 (diff)
downloaddjango-rest-framework-8a2944acdf66a966f0bd4ea4770f012f2597fb4e.tar.bz2
Tests for CreateModelMixin (unit and integration test via ListOrCreateModelView).
Diffstat (limited to 'djangorestframework/tests/mixins.py')
-rw-r--r--djangorestframework/tests/mixins.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/djangorestframework/tests/mixins.py b/djangorestframework/tests/mixins.py
new file mode 100644
index 00000000..100109ca
--- /dev/null
+++ b/djangorestframework/tests/mixins.py
@@ -0,0 +1,56 @@
+"""Tests for the status module"""
+from django.test import TestCase
+from djangorestframework import status
+from djangorestframework.compat import RequestFactory
+from django.contrib.auth.models import Group, User
+from djangorestframework.mixins import CreateModelMixin
+from djangorestframework.resources import ModelResource
+
+
+class TestModelCreation(TestCase):
+ """Tests on CreateModelMixin"""
+
+ def setUp(self):
+ self.req = RequestFactory()
+
+ def test_creation(self):
+ self.assertEquals(0, Group.objects.count())
+
+ class GroupResource(ModelResource):
+ model = Group
+
+ form_data = {'name': 'foo'}
+ request = self.req.post('/groups', data=form_data)
+ mixin = CreateModelMixin()
+ mixin.resource = GroupResource
+ mixin.CONTENT = form_data
+
+ response = mixin.post(request)
+ self.assertEquals(1, Group.objects.count())
+ self.assertEquals('foo', response.cleaned_content.name)
+
+
+ def test_creation_with_m2m_relation(self):
+ class UserResource(ModelResource):
+ model = User
+
+ def url(self, instance):
+ return "/users/%i" % instance.id
+
+ group = Group(name='foo')
+ group.save()
+
+ form_data = {'username': 'bar', 'password': 'baz', 'groups': [group.id]}
+ request = self.req.post('/groups', data=form_data)
+ cleaned_data = dict(form_data)
+ cleaned_data['groups'] = [group]
+ mixin = CreateModelMixin()
+ mixin.resource = UserResource
+ mixin.CONTENT = cleaned_data
+
+ response = mixin.post(request)
+ self.assertEquals(1, User.objects.count())
+ self.assertEquals(1, response.cleaned_content.groups.count())
+ self.assertEquals('foo', response.cleaned_content.groups.all()[0].name)
+
+