def test_to_json_schema_invalid_field(): field = CustomField() with pytest.raises(ValueError) as exc_info: to_json_schema(field) expected = "Cannot convert field type 'CustomField' to JSON Schema" assert str(exc_info.value) == expected
def test_to_json_schema_complex_regular_expression(): field = typesystem.String(pattern=re.compile("foo", re.IGNORECASE | re.VERBOSE)) with pytest.raises(ValueError) as exc_info: to_json_schema(field) expected = ("Cannot convert regular expression with non-standard flags " "to JSON schema: RegexFlag.") assert str(exc_info.value).startswith(expected)
def test_to_from_json_schema(schema, data, is_valid, description): """ Test that marshalling to and from JSON schema doens't affect the behavior of the validation. Ie. `new_field = from_json_schema(to_json_schema(initial_field))` should always result in `new_field` having the same behavior as `initial_field`. """ validator = from_json_schema(schema) value_before_convert, error_before_convert = validator.validate_or_error( data, strict=True) schema_after = to_json_schema(validator) validator = from_json_schema(schema_after) value_after_convert, error_after_convert = validator.validate_or_error( data, strict=True) assert error_before_convert == error_after_convert assert value_before_convert == value_after_convert
def test_schema_to_json_schema(): class BookingSchema(typesystem.Schema): start_date = typesystem.Date(title="Start date") end_date = typesystem.Date(title="End date") room = typesystem.Choice( title="Room type", choices=[ ("double", "Double room"), ("twin", "Twin room"), ("single", "Single room"), ], ) include_breakfast = typesystem.Boolean(title="Include breakfast", default=False) schema = to_json_schema(BookingSchema) assert schema == { "type": "object", "properties": { "start_date": { "type": "string", "format": "date", "minLength": 1 }, "end_date": { "type": "string", "format": "date", "minLength": 1 }, "room": { "enum": ["double", "twin", "single"] }, "include_breakfast": { "type": "boolean", "default": False }, }, "required": ["start_date", "end_date", "room"], }