1 | from django.core.serializers import serialize, deserialize
|
---|
2 | from mysite.polls.models import Poll
|
---|
3 | from datetime import datetime
|
---|
4 |
|
---|
5 | from django.core.validators import ValidationError
|
---|
6 |
|
---|
7 | from unittest import TestCase
|
---|
8 |
|
---|
9 | class JsonSerializerShouldDealWithNonePrimaryKeysTest(TestCase):
|
---|
10 | def testAlreadyHandlesNonNullPKs(self):
|
---|
11 | m = self.model()
|
---|
12 | m.save()
|
---|
13 |
|
---|
14 | try:
|
---|
15 | list(deserialize('json', serialize('json', [m])))[0].object
|
---|
16 | except ValidationError:
|
---|
17 | self.fail()
|
---|
18 |
|
---|
19 | def testBlowsUpBecauseNoneInAStringIsNotNull(self):
|
---|
20 | m = self.model()
|
---|
21 | # no save.
|
---|
22 |
|
---|
23 | serialized = serialize('json', [m])
|
---|
24 | self.assertFalse('null' in serialized)
|
---|
25 | self.assertTrue('"None"' in serialized)
|
---|
26 |
|
---|
27 | try:
|
---|
28 | list(deserialize('json', serialized))[0].object
|
---|
29 | self.fail()
|
---|
30 | except ValidationError, va:
|
---|
31 | self.assertEquals(['This value must be an integer.'], va.messages)
|
---|
32 |
|
---|
33 | def model(self):
|
---|
34 | return Poll(question="What's up?", pub_date=datetime.now())
|
---|