def test_valid_json_field(): field = JSONSchemaField(schema=schema) assert field.to_internal_value({ 'a': 'foo', 'b': 3.1 }) == { 'a': 'foo', 'b': 3.1 }
class TestSerializer(serializers.Serializer): json = JSONSchemaField(schema=schema)
def test_json_field_invalid_format_check_but_format_checking_disabled(): field = JSONSchemaField(schema=format_schema, format_checker=None) assert field.to_internal_value({'a': 'noemail'}) == {'a': 'noemail'}
def test_json_field_invalid_format_check(): field = JSONSchemaField(schema=format_schema) with pytest.raises(serializers.ValidationError) as e: field.run_validation({'a': 'noemail'}) assert e.value.detail == ["'noemail' is not a 'email'"]
def test_json_field_valid_format_check(): field = JSONSchemaField(schema=format_schema) assert field.to_internal_value({'a': '*****@*****.**'}) == { 'a': '*****@*****.**' }
def test_invalid_json_field(): field = JSONSchemaField(schema=schema) with pytest.raises(serializers.ValidationError) as e: field.run_validation({'a': 'foo', 'b': 'nonumber'}) assert e.value.detail == ["'nonumber' is not of type 'number'"]