aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/tests/mixins.py
diff options
context:
space:
mode:
authorGitHub Merge Button2011-07-11 12:33:49 -0700
committerGitHub Merge Button2011-07-11 12:33:49 -0700
commit2b909062937c37134effc38cf9b98b6ad49f72e9 (patch)
tree4c3a7cddbc099fe6ea65424a9595b87104b55967 /djangorestframework/tests/mixins.py
parent5433cb3e27058c2f7da6118ffa9ee7712846048d (diff)
parent91b9d0b2a3fa55ff163f64bc689a59ca01cff8bb (diff)
downloaddjango-rest-framework-2b909062937c37134effc38cf9b98b6ad49f72e9.tar.bz2
Merge 91b9d0b2a3fa55ff163f64bc689a59ca01cff8bb into 5433cb3e27058c2f7da6118ffa9ee7712846048d
Diffstat (limited to 'djangorestframework/tests/mixins.py')
-rw-r--r--djangorestframework/tests/mixins.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/djangorestframework/tests/mixins.py b/djangorestframework/tests/mixins.py
index 100109ca..b9aa4c3b 100644
--- a/djangorestframework/tests/mixins.py
+++ b/djangorestframework/tests/mixins.py
@@ -5,6 +5,7 @@ from djangorestframework.compat import RequestFactory
from django.contrib.auth.models import Group, User
from djangorestframework.mixins import CreateModelMixin
from djangorestframework.resources import ModelResource
+from djangorestframework.tests.models import CustomUser
class TestModelCreation(TestCase):
@@ -53,4 +54,60 @@ class TestModelCreation(TestCase):
self.assertEquals(1, response.cleaned_content.groups.count())
self.assertEquals('foo', response.cleaned_content.groups.all()[0].name)
+ def test_creation_with_m2m_relation_through(self):
+ """
+ Tests creation where the m2m relation uses a through table
+ """
+ class UserResource(ModelResource):
+ model = CustomUser
+
+ def url(self, instance):
+ return "/customusers/%i" % instance.id
+
+ form_data = {'username': 'bar0', 'groups': []}
+ request = self.req.post('/groups', data=form_data)
+ cleaned_data = dict(form_data)
+ cleaned_data['groups'] = []
+ mixin = CreateModelMixin()
+ mixin.resource = UserResource
+ mixin.CONTENT = cleaned_data
+
+ response = mixin.post(request)
+ self.assertEquals(1, CustomUser.objects.count())
+ self.assertEquals(0, response.cleaned_content.groups.count())
+
+ group = Group(name='foo1')
+ group.save()
+
+ form_data = {'username': 'bar1', '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(2, CustomUser.objects.count())
+ self.assertEquals(1, response.cleaned_content.groups.count())
+ self.assertEquals('foo1', response.cleaned_content.groups.all()[0].name)
+
+
+ group2 = Group(name='foo2')
+ group2.save()
+
+ form_data = {'username': 'bar2', 'groups': [group.id, group2.id]}
+ request = self.req.post('/groups', data=form_data)
+ cleaned_data = dict(form_data)
+ cleaned_data['groups'] = [group, group2]
+ mixin = CreateModelMixin()
+ mixin.resource = UserResource
+ mixin.CONTENT = cleaned_data
+
+ response = mixin.post(request)
+ self.assertEquals(3, CustomUser.objects.count())
+ self.assertEquals(2, response.cleaned_content.groups.count())
+ self.assertEquals('foo', response.cleaned_content.groups.all()[0].name)
+ self.assertEquals('foo2', response.cleaned_content.groups.all()[1].name)
+