Add m2m update test

This commit is contained in:
Tom Christie 2012-10-03 11:14:47 +01:00
parent 7c303411a9
commit 89ec0b2750

View File

@ -50,19 +50,19 @@ class BasicTests(TestCase):
} }
self.assertEquals(serializer.data, expected) self.assertEquals(serializer.data, expected)
def test_serialization(self): def test_retrieve(self):
serializer = CommentSerializer(instance=self.comment) serializer = CommentSerializer(instance=self.comment)
expected = self.data expected = self.data
self.assertEquals(serializer.data, expected) self.assertEquals(serializer.data, expected)
def test_deserialization_for_create(self): def test_create(self):
serializer = CommentSerializer(self.data) serializer = CommentSerializer(self.data)
expected = self.comment expected = self.comment
self.assertEquals(serializer.is_valid(), True) self.assertEquals(serializer.is_valid(), True)
self.assertEquals(serializer.object, expected) self.assertEquals(serializer.object, expected)
self.assertFalse(serializer.object is expected) self.assertFalse(serializer.object is expected)
def test_deserialization_for_update(self): def test_update(self):
serializer = CommentSerializer(self.data, instance=self.comment) serializer = CommentSerializer(self.data, instance=self.comment)
expected = self.comment expected = self.comment
self.assertEquals(serializer.is_valid(), True) self.assertEquals(serializer.is_valid(), True)
@ -83,12 +83,12 @@ class ValidationTests(TestCase):
'created': datetime.datetime(2012, 1, 1) 'created': datetime.datetime(2012, 1, 1)
} }
def test_deserialization_for_create(self): def test_create(self):
serializer = CommentSerializer(self.data) serializer = CommentSerializer(self.data)
self.assertEquals(serializer.is_valid(), False) self.assertEquals(serializer.is_valid(), False)
self.assertEquals(serializer.errors, {'content': [u'Ensure this value has at most 1000 characters (it has 1001).']}) self.assertEquals(serializer.errors, {'content': [u'Ensure this value has at most 1000 characters (it has 1001).']})
def test_deserialization_for_update(self): def test_update(self):
serializer = CommentSerializer(self.data, instance=self.comment) serializer = CommentSerializer(self.data, instance=self.comment)
self.assertEquals(serializer.is_valid(), False) self.assertEquals(serializer.is_valid(), False)
self.assertEquals(serializer.errors, {'content': [u'Ensure this value has at most 1000 characters (it has 1001).']}) self.assertEquals(serializer.errors, {'content': [u'Ensure this value has at most 1000 characters (it has 1001).']})
@ -127,11 +127,17 @@ class ManyToManyTests(TestCase):
self.data = {'id': 1, 'rel': [self.anchor.id]} self.data = {'id': 1, 'rel': [self.anchor.id]}
def test_retrieve(self): def test_retrieve(self):
"""
Serialize an instance of a model with a ManyToMany relationship.
"""
serializer = self.serializer_class(instance=self.instance) serializer = self.serializer_class(instance=self.instance)
expected = self.data expected = self.data
self.assertEquals(serializer.data, expected) self.assertEquals(serializer.data, expected)
def test_create(self): def test_create(self):
"""
Create an instance of a model with a ManyToMany relationship.
"""
data = {'rel': [self.anchor.id]} data = {'rel': [self.anchor.id]}
serializer = self.serializer_class(data) serializer = self.serializer_class(data)
self.assertEquals(serializer.is_valid(), True) self.assertEquals(serializer.is_valid(), True)
@ -139,7 +145,20 @@ class ManyToManyTests(TestCase):
self.assertEquals(len(ManyToManyModel.objects.all()), 2) self.assertEquals(len(ManyToManyModel.objects.all()), 2)
self.assertEquals(instance.pk, 2) self.assertEquals(instance.pk, 2)
self.assertEquals(list(instance.rel.all()), [self.anchor]) self.assertEquals(list(instance.rel.all()), [self.anchor])
# self.assertFalse(serializer.object is expected)
def test_update(self):
"""
Update an instance of a model with a ManyToMany relationship.
"""
new_anchor = Anchor()
new_anchor.save()
data = {'rel': [self.anchor.id, new_anchor.id]}
serializer = self.serializer_class(data, instance=self.instance)
self.assertEquals(serializer.is_valid(), True)
instance = serializer.save()
self.assertEquals(len(ManyToManyModel.objects.all()), 1)
self.assertEquals(instance.pk, 1)
self.assertEquals(list(instance.rel.all()), [self.anchor, new_anchor])
# def test_deserialization_for_update(self): # def test_deserialization_for_update(self):
# serializer = self.serializer_class(self.data, instance=self.instance) # serializer = self.serializer_class(self.data, instance=self.instance)