def test_invalid(self): field = JSONField() with pytest.raises(exceptions.ValidationError) as excinfo: field.clean('{some badly formed: json}') assert ( excinfo.value.messages[0] == "'{some badly formed: json}' value must be valid JSON." )
def test_already_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: val = field.clean(json_string) assert field.clean(val) == val
def test_valid_empty(self): field = JSONField(required=False) value = field.clean('') assert value is None
def test_valid(self): field = JSONField() value = field.clean('{"a": "b"}') assert value == {'a': 'b'}