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