コード例 #1
0
ファイル: test_json_field.py プロジェクト: chipx86/djblets
    def test_init_with_custom_encoder_class(self):
        """Testing JSONFormField initialization with custom encoder class"""
        class MyEncoder(json.JSONEncoder):
            def __init__(self, default_msg, **kwargs):
                self.default_msg = default_msg

                super(MyEncoder, self).__init__(**kwargs)

            def default(self, o):
                return self.default_msg

        field = JSONFormField(
            encoder_cls=MyEncoder,
            encoder_kwargs={
                'default_msg': 'What even is this?',
            })

        self.assertEqual(
            field.prepare_value({
                'a': 1,
                'b': 2,
                'cls': MyEncoder,
            }),
            '{\n'
            '  "a": 1,\n'
            '  "b": 2,\n'
            '  "cls": "What even is this?"\n'
            '}')
コード例 #2
0
ファイル: test_json_field.py プロジェクト: sjl421/djblets
    def test_init_with_custom_encoder_class(self):
        """Testing JSONFormField initialization with custom encoder class"""
        class MyEncoder(json.JSONEncoder):
            def __init__(self, default_msg, **kwargs):
                self.default_msg = default_msg

                super(MyEncoder, self).__init__(**kwargs)

            def default(self, o):
                return self.default_msg

        field = JSONFormField(encoder_cls=MyEncoder,
                              encoder_kwargs={
                                  'default_msg': 'What even is this?',
                              })

        self.assertEqual(
            field.prepare_value({
                'a': 1,
                'b': 2,
                'cls': MyEncoder,
            }), '{\n'
            '  "a": 1,\n'
            '  "b": 2,\n'
            '  "cls": "What even is this?"\n'
            '}')
コード例 #3
0
ファイル: test_json_field.py プロジェクト: chipx86/djblets
 def test_prepare_value_with_deserialized(self):
     """Testing JSONFormField.prepare_value with deserialized data"""
     field = JSONFormField()
     self.assertEqual(field.prepare_value({'a': 1, 'b': 2}),
                      '{\n'
                      '  "a": 1,\n'
                      '  "b": 2\n'
                      '}')
コード例 #4
0
ファイル: test_json_field.py プロジェクト: sjl421/djblets
 def test_prepare_value_with_deserialized(self):
     """Testing JSONFormField.prepare_value with deserialized data"""
     field = JSONFormField()
     self.assertEqual(field.prepare_value({
         'a': 1,
         'b': 2
     }), '{\n'
                      '  "a": 1,\n'
                      '  "b": 2\n'
                      '}')
コード例 #5
0
ファイル: test_json_field.py プロジェクト: sjl421/djblets
    def test_init_with_custom_encoder_instance(self):
        """Testing JSONFormField initialization with custom encoder instance"""
        class MyEncoder(json.JSONEncoder):
            def default(self, o):
                return 'What even is this?'

        field = JSONFormField(encoder=MyEncoder())

        self.assertEqual(field.prepare_value({
            'cls': MyEncoder,
        }), '{"cls": "What even is this?"}')
コード例 #6
0
ファイル: test_json_field.py プロジェクト: chipx86/djblets
    def test_init_with_custom_encoder_instance(self):
        """Testing JSONFormField initialization with custom encoder instance"""
        class MyEncoder(json.JSONEncoder):
            def default(self, o):
                return 'What even is this?'

        field = JSONFormField(encoder=MyEncoder())

        self.assertEqual(
            field.prepare_value({
                'cls': MyEncoder,
            }),
            '{"cls": "What even is this?"}')
コード例 #7
0
ファイル: test_json_field.py プロジェクト: chipx86/djblets
    def test_to_python_with_validation_error(self):
        """Testing JSONFormField.to_python with bad JSON data triggering
        ValidationError
        """
        field = JSONFormField()

        with self.assertRaises(ValidationError) as cm:
            field.to_python('{a: 1}')

        self.assertEqual(cm.exception.message,
                         'Expecting property name enclosed in double quotes: '
                         'line 1 column 2 (char 1)')
        self.assertEqual(cm.exception.code, 'invalid')
        self.assertEqual(cm.exception.params, {
            'value': '{a: 1}',
        })
コード例 #8
0
ファイル: test_json_field.py プロジェクト: sjl421/djblets
    def test_to_python_with_validation_error(self):
        """Testing JSONFormField.to_python with bad JSON data triggering
        ValidationError
        """
        field = JSONFormField()

        with self.assertRaises(ValidationError) as cm:
            field.to_python('{a: 1}')

        self.assertEqual(
            cm.exception.message,
            'Expecting property name enclosed in double quotes: '
            'line 1 column 2 (char 1)')
        self.assertEqual(cm.exception.code, 'invalid')
        self.assertEqual(cm.exception.params, {
            'value': '{a: 1}',
        })
コード例 #9
0
ファイル: test_json_field.py プロジェクト: sjl421/djblets
 def test_to_python_with_empty_non_string(self):
     """Testing JSONFormField.to_python with empty non-string value"""
     field = JSONFormField()
     self.assertEqual(field.to_python({}), {})
     self.assertEqual(field.to_python([]), [])
コード例 #10
0
ファイル: test_json_field.py プロジェクト: sjl421/djblets
 def test_to_python_with_empty_string(self):
     """Testing JSONFormField.to_python with empty string"""
     field = JSONFormField()
     self.assertIsNone(field.to_python(''))
コード例 #11
0
ファイル: test_json_field.py プロジェクト: sjl421/djblets
 def test_to_python_with_deserialized(self):
     """Testing JSONFormField.to_python with deserialized data"""
     field = JSONFormField()
     self.assertEqual(field.to_python({"a": 1, "b": 2}), {'a': 1, 'b': 2})
コード例 #12
0
ファイル: test_json_field.py プロジェクト: sjl421/djblets
 def test_prepare_value_with_serialized(self):
     """Testing JSONFormField.prepare_value with serialized data"""
     field = JSONFormField()
     self.assertEqual(field.prepare_value('{"a": 1, "b": 2}'),
                      '{"a": 1, "b": 2}')
コード例 #13
0
ファイル: test_json_field.py プロジェクト: chipx86/djblets
 def test_to_python_with_empty_non_string(self):
     """Testing JSONFormField.to_python with empty non-string value"""
     field = JSONFormField()
     self.assertEqual(field.to_python({}), {})
     self.assertEqual(field.to_python([]), [])
コード例 #14
0
ファイル: test_json_field.py プロジェクト: chipx86/djblets
 def test_to_python_with_empty_string(self):
     """Testing JSONFormField.to_python with empty string"""
     field = JSONFormField()
     self.assertIsNone(field.to_python(''))
コード例 #15
0
ファイル: test_json_field.py プロジェクト: chipx86/djblets
 def test_to_python_with_deserialized(self):
     """Testing JSONFormField.to_python with deserialized data"""
     field = JSONFormField()
     self.assertEqual(field.to_python({"a": 1, "b": 2}),
                      {'a': 1, 'b': 2})
コード例 #16
0
ファイル: test_json_field.py プロジェクト: chipx86/djblets
 def test_prepare_value_with_serialized(self):
     """Testing JSONFormField.prepare_value with serialized data"""
     field = JSONFormField()
     self.assertEqual(field.prepare_value('{"a": 1, "b": 2}'),
                      '{"a": 1, "b": 2}')