Debian Codegraph @

tests/serializers/test_json.py

raw
1 import datetime
2 import decimal
3 import json
4 import re
5
6 from django.core import serializers
7 from django.core.serializers.base import DeserializationError
8 from django.core.serializers.json import DjangoJSONEncoder
9 from django.db import models
10 from django.test import SimpleTestCase, TestCase, TransactionTestCase
11 from django.test.utils import isolate_apps
12 from django.utils.translation import gettext_lazy, override
13
14 from .models import Score
15 from .tests import SerializersTestBase, SerializersTransactionTestBase
16
17
18 class JsonSerializerTestCase(SerializersTestBase, TestCase):
19 serializer_name = "json"
20 pkless_str = """[
21 {
22 "pk": null,
23 "model": "serializers.category",
24 "fields": {"name": "Reference"}
25 }, {
26 "model": "serializers.category",
27 "fields": {"name": "Non-fiction"}
28 }]"""
29 mapping_ordering_str = """[
30 {
31 "model": "serializers.article",
32 "pk": %(article_pk)s,
33 "fields": {
34 "author": %(author_pk)s,
35 "headline": "Poker has no place on ESPN",
36 "pub_date": "2006-06-16T11:00:00",
37 "categories": [
38 %(first_category_pk)s,
39 %(second_category_pk)s
40 ],
41 "meta_data": [],
42 "topics": []
43 }
44 }
45 ]
46 """
47
48 @staticmethod
49 def _validate_output(serial_str):
50 try:
51 json.loads(serial_str)
52 except Exception:
53 return False
54 else:
55 return True
56
57 @staticmethod
58 def _get_pk_values(serial_str):
59 serial_list = json.loads(serial_str)
60 return [obj_dict["pk"] for obj_dict in serial_list]
61
62 @staticmethod
63 def _get_field_values(serial_str, field_name):
64 serial_list = json.loads(serial_str)
65 return [
66 obj_dict["fields"][field_name]
67 for obj_dict in serial_list
68 if field_name in obj_dict["fields"]
69 ]
70
71 def test_indentation_whitespace(self):
72 s = serializers.json.Serializer()
73 json_data = s.serialize([Score(score=5.0), Score(score=6.0)], indent=2)
74 for line in json_data.splitlines():
75 if re.search(r".+,\s*$", line):
76 self.assertEqual(line, line.rstrip())
77
78 @isolate_apps("serializers")
79 def test_custom_encoder(self):
80 class ScoreDecimal(models.Model):
81 score = models.DecimalField()
82
83 class CustomJSONEncoder(json.JSONEncoder):
84 def default(self, o):
85 if isinstance(o, decimal.Decimal):
86 return str(o)
87 return super().default(o)
88
89 s = serializers.json.Serializer()
90 json_data = s.serialize(
91 [ScoreDecimal(score=decimal.Decimal(1.0))], cls=CustomJSONEncoder
92 )
93 self.assertIn('"fields": {"score": "1"}', json_data)
94
95 def test_json_deserializer_exception(self):
96 with self.assertRaises(DeserializationError):
97 for obj in serializers.deserialize("json", """[{"pk":1}"""):
98 pass
99
100 def test_helpful_error_message_invalid_pk(self):
101 """
102 If there is an invalid primary key, the error message should contain
103 the model associated with it.
104 """
105 test_string = """[{
106 "pk": "badpk",
107 "model": "serializers.player",
108 "fields": {
109 "name": "Bob",
110 "rank": 1,
111 "team": "Team"
112 }
113 }]"""
114 with self.assertRaisesMessage(
115 DeserializationError, "(serializers.player:pk=badpk)"
116 ):
117 list(serializers.deserialize("json", test_string))
118
119 def test_helpful_error_message_invalid_field(self):
120 """
121 If there is an invalid field value, the error message should contain
122 the model associated with it.
123 """
124 test_string = """[{
125 "pk": "1",
126 "model": "serializers.player",
127 "fields": {
128 "name": "Bob",
129 "rank": "invalidint",
130 "team": "Team"
131 }
132 }]"""
133 expected = "(serializers.player:pk=1) field_value was 'invalidint'"
134 with self.assertRaisesMessage(DeserializationError, expected):
135 list(serializers.deserialize("json", test_string))
136
137 def test_helpful_error_message_for_foreign_keys(self):
138 """
139 Invalid foreign keys with a natural key should throw a helpful error
140 message, such as what the failing key is.
141 """
142 test_string = """[{
143 "pk": 1,
144 "model": "serializers.category",
145 "fields": {
146 "name": "Unknown foreign key",
147 "meta_data": [
148 "doesnotexist",
149 "metadata"
150 ]
151 }
152 }]"""
153 key = ["doesnotexist", "metadata"]
154 expected = "(serializers.category:pk=1) field_value was '%r'" % key
155 with self.assertRaisesMessage(DeserializationError, expected):
156 list(serializers.deserialize("json", test_string))
157
158 def test_helpful_error_message_for_many2many_non_natural(self):
159 """
160 Invalid many-to-many keys should throw a helpful error message.
161 """
162 test_string = """[{
163 "pk": 1,
164 "model": "serializers.article",
165 "fields": {
166 "author": 1,
167 "headline": "Unknown many to many",
168 "pub_date": "2014-09-15T10:35:00",
169 "categories": [1, "doesnotexist"]
170 }
171 }, {
172 "pk": 1,
173 "model": "serializers.author",
174 "fields": {
175 "name": "Agnes"
176 }
177 }, {
178 "pk": 1,
179 "model": "serializers.category",
180 "fields": {
181 "name": "Reference"
182 }
183 }]"""
184 expected = "(serializers.article:pk=1) field_value was 'doesnotexist'"
185 with self.assertRaisesMessage(DeserializationError, expected):
186 list(serializers.deserialize("json", test_string))
187
188 def test_helpful_error_message_for_many2many_natural1(self):
189 """
190 Invalid many-to-many keys should throw a helpful error message.
191 This tests the code path where one of a list of natural keys is invalid.
192 """
193 test_string = """[{
194 "pk": 1,
195 "model": "serializers.categorymetadata",
196 "fields": {
197 "kind": "author",
198 "name": "meta1",
199 "value": "Agnes"
200 }
201 }, {
202 "pk": 1,
203 "model": "serializers.article",
204 "fields": {
205 "author": 1,
206 "headline": "Unknown many to many",
207 "pub_date": "2014-09-15T10:35:00",
208 "meta_data": [
209 ["author", "meta1"],
210 ["doesnotexist", "meta1"],
211 ["author", "meta1"]
212 ]
213 }
214 }, {
215 "pk": 1,
216 "model": "serializers.author",
217 "fields": {
218 "name": "Agnes"
219 }
220 }]"""
221 key = ["doesnotexist", "meta1"]
222 expected = "(serializers.article:pk=1) field_value was '%r'" % key
223 with self.assertRaisesMessage(DeserializationError, expected):
224 for obj in serializers.deserialize("json", test_string):
225 obj.save()
226
227 def test_helpful_error_message_for_many2many_natural2(self):
228 """
229 Invalid many-to-many keys should throw a helpful error message. This
230 tests the code path where a natural many-to-many key has only a single
231 value.
232 """
233 test_string = """[{
234 "pk": 1,
235 "model": "serializers.article",
236 "fields": {
237 "author": 1,
238 "headline": "Unknown many to many",
239 "pub_date": "2014-09-15T10:35:00",
240 "meta_data": [1, "doesnotexist"]
241 }
242 }, {
243 "pk": 1,
244 "model": "serializers.categorymetadata",
245 "fields": {
246 "kind": "author",
247 "name": "meta1",
248 "value": "Agnes"
249 }
250 }, {
251 "pk": 1,
252 "model": "serializers.author",
253 "fields": {
254 "name": "Agnes"
255 }
256 }]"""
257 expected = "(serializers.article:pk=1) field_value was 'doesnotexist'"
258 with self.assertRaisesMessage(DeserializationError, expected):
259 for obj in serializers.deserialize("json", test_string, ignore=False):
260 obj.save()
261
262 def test_helpful_error_message_for_many2many_not_iterable(self):
263 """
264 Not iterable many-to-many field value throws a helpful error message.
265 """
266 test_string = """[{
267 "pk": 1,
268 "model": "serializers.m2mdata",
269 "fields": {"data": null}
270 }]"""
271
272 expected = "(serializers.m2mdata:pk=1) field_value was 'None'"
273 with self.assertRaisesMessage(DeserializationError, expected):
274 next(serializers.deserialize("json", test_string, ignore=False))
275
276
277 class JsonSerializerTransactionTestCase(
278 SerializersTransactionTestBase, TransactionTestCase
279 ):
280 serializer_name = "json"
281 fwd_ref_str = """[
282 {
283 "pk": 1,
284 "model": "serializers.article",
285 "fields": {
286 "headline": "Forward references pose no problem",
287 "pub_date": "2006-06-16T15:00:00",
288 "categories": [1],
289 "author": 1
290 }
291 },
292 {
293 "pk": 1,
294 "model": "serializers.category",
295 "fields": {
296 "name": "Reference"
297 }
298 },
299 {
300 "pk": 1,
301 "model": "serializers.author",
302 "fields": {
303 "name": "Agnes"
304 }
305 }]"""
306
307
308 class DjangoJSONEncoderTests(SimpleTestCase):
309 def test_lazy_string_encoding(self):
310 self.assertEqual(
311 json.dumps({"lang": gettext_lazy("French")}, cls=DjangoJSONEncoder),
312 '{"lang": "French"}',
313 )
314 with override("fr"):
315 self.assertEqual(
316 json.dumps({"lang": gettext_lazy("French")}, cls=DjangoJSONEncoder),
317 '{"lang": "Fran\\u00e7ais"}',
318 )
319
320 def test_timedelta(self):
321 duration = datetime.timedelta(days=1, hours=2, seconds=3)
322 self.assertEqual(
323 json.dumps({"duration": duration}, cls=DjangoJSONEncoder),
324 '{"duration": "P1DT02H00M03S"}',
325 )
326 duration = datetime.timedelta(0)
327 self.assertEqual(
328 json.dumps({"duration": duration}, cls=DjangoJSONEncoder),
329 '{"duration": "P0DT00H00M00S"}',
330 )