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' '}')
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' '}')
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' '}')
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' '}')
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?"}')
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?"}')
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}', })
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}', })
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([]), [])
def test_to_python_with_empty_string(self): """Testing JSONFormField.to_python with empty string""" field = JSONFormField() self.assertIsNone(field.to_python(''))
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})
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}')