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