예제 #1
0
 def test_has_changed(self):
     field = JSONField()
     self.assertIs(field.has_changed({'a': True}, '{"a": 1}'), True)
     self.assertIs(field.has_changed({
         'a': 1,
         'b': 2
     }, '{"b": 2, "a": 1}'), False)
예제 #2
0
 def test_converted_value(self):
     field = JSONField(required=False)
     tests = [
         '["a", "b", "c"]',
         '{"a": 1, "b": 2}',
         '1',
         '1.5',
         '"foo"',
         'true',
         'false',
         'null',
     ]
     for json_string in tests:
         with self.subTest(json_string=json_string):
             val = field.clean(json_string)
             self.assertEqual(field.clean(val), val)
예제 #3
0
    def test_custom_encoder_decoder(self):
        class CustomDecoder(json.JSONDecoder):
            def __init__(self, object_hook=None, *args, **kwargs):
                return super().__init__(object_hook=self.as_uuid,
                                        *args,
                                        **kwargs)

            def as_uuid(self, dct):
                if 'uuid' in dct:
                    dct['uuid'] = uuid.UUID(dct['uuid'])
                return dct

        value = {'uuid': uuid.UUID('{c141e152-6550-4172-a784-05448d98204b}')}
        encoded_value = '{"uuid": "c141e152-6550-4172-a784-05448d98204b"}'
        field = JSONField(encoder=DjangoJSONEncoder, decoder=CustomDecoder)
        self.assertEqual(field.prepare_value(value), encoded_value)
        self.assertEqual(field.clean(encoded_value), value)
예제 #4
0
 def test_prepare_value(self):
     field = JSONField()
     self.assertEqual(field.prepare_value({'a': 'b'}), '{"a": "b"}')
     self.assertEqual(field.prepare_value(None), 'null')
     self.assertEqual(field.prepare_value('foo'), '"foo"')
     self.assertEqual(field.prepare_value('你好,世界'), '"你好,世界"')
     self.assertEqual(field.prepare_value({'a': '😀🐱'}), '{"a": "😀🐱"}')
     self.assertEqual(
         field.prepare_value(["你好,世界", "jaźń"]),
         '["你好,世界", "jaźń"]',
     )
예제 #5
0
 def test_prepare_value(self):
     field = JSONField()
     self.assertEqual(field.prepare_value({"a": "b"}), '{"a": "b"}')
     self.assertEqual(field.prepare_value(None), "null")
     self.assertEqual(field.prepare_value("foo"), '"foo"')
     self.assertEqual(field.prepare_value("你好,世界"), '"你好,世界"')
     self.assertEqual(field.prepare_value({"a": "😀🐱"}), '{"a": "😀🐱"}')
     self.assertEqual(
         field.prepare_value(["你好,世界", "jaźń"]),
         '["你好,世界", "jaźń"]',
     )
예제 #6
0
 class JSONForm(Form):
     json_field = JSONField(disabled=True)
예제 #7
0
 def test_custom_widget_kwarg(self):
     field = JSONField(widget=TextInput)
     self.assertIsInstance(field.widget, TextInput)
예제 #8
0
 def test_widget(self):
     field = JSONField()
     self.assertIsInstance(field.widget, Textarea)
예제 #9
0
 def test_prepare_value(self):
     field = JSONField()
     self.assertEqual(field.prepare_value({'a': 'b'}), '{"a": "b"}')
     self.assertEqual(field.prepare_value(None), 'null')
     self.assertEqual(field.prepare_value('foo'), '"foo"')
예제 #10
0
 def test_invalid(self):
     field = JSONField()
     with self.assertRaisesMessage(ValidationError, 'Enter a valid JSON.'):
         field.clean('{some badly formed: json}')
예제 #11
0
 def test_valid_empty(self):
     field = JSONField(required=False)
     self.assertIsNone(field.clean(''))
     self.assertIsNone(field.clean(None))
예제 #12
0
 def test_valid(self):
     field = JSONField()
     value = field.clean('{"a": "b"}')
     self.assertEqual(value, {'a': 'b'})
예제 #13
0
 class JSONForm(Form):
     name = CharField(max_length=2)
     json_field = JSONField()
예제 #14
0
 def test_valid_empty(self):
     field = JSONField(required=False)
     value = field.clean('')
     self.assertIsNone(value)
예제 #15
0
 class JSONForm(Form):
     json_field = JSONField(required=True)