def test_length_validator_value_error(): class BadSchema(Schema): bob = fields.Integer(validate=validate.Length(min=1, max=3)) schema = BadSchema(strict=True) json_schema = JSONSchema() with pytest.raises(ValueError): json_schema.dump(schema)
def test_nested_custom_field_by_subclassing(): class Colour(fields.String): def __init__(self, default='red', **kwargs): super(Colour, self).__init__(default=default, **kwargs) class ColoursSchema(Schema): favourite_colour = Colour() class UserSchema(Schema): name = fields.String(required=True) colours = fields.Nested(ColoursSchema) schema = UserSchema() json_schema = JSONSchema() with pytest.raises(ValueError): # The custom Color field is not registered dumped = json_schema.dump(schema) # Provide the custom field to the default mappings class CustomJSONSchema(JSONSchema): def get_custom_mappings(self): return {Colour: text_type} # Register the field json_schema = CustomJSONSchema() dumped = json_schema.dump(schema).data assert dumped['definitions']['ColoursSchema']['properties']['favourite_colour'] == { 'default': 'red', 'title': 'favourite_colour', 'type': 'string'}
def test_sorting_properties(): class TestSchema(Schema): class Meta: ordered = True d = fields.Str() c = fields.Str() a = fields.Str() # Should be sorting of fields schema = TestSchema() json_schema = JSONSchema() data = json_schema.dump(schema) sorted_keys = sorted( data["definitions"]["TestSchema"]["properties"].keys()) properties_names = [k for k in sorted_keys] assert properties_names == ["a", "c", "d"] # Should be saving ordering of fields schema = TestSchema() json_schema = JSONSchema(props_ordered=True) data = json_schema.dump(schema) keys = data["definitions"]["TestSchema"]["properties"].keys() properties_names = [k for k in keys] assert properties_names == ["d", "c", "a"]
def test_range_non_number_error(): class TestSchema(Schema): foo = fields.String(validate=validate.Range(max=4)) schema = TestSchema() json_schema = JSONSchema() with pytest.raises(UnsupportedValueError): json_schema.dump(schema)
def test_unknown_typed_field_throws_valueerror(): class Invalid(fields.Field): def _serialize(self, value, attr, obj): return value class UserSchema(Schema): favourite_colour = Invalid() schema = UserSchema() json_schema = JSONSchema() with pytest.raises(ValueError): json_schema.dump(schema).data
def test_additional_properties_invalid_value(): class TestSchema(Schema): class Meta: additional_properties = "foo" foo = fields.Integer() schema = TestSchema() json_schema = JSONSchema() with pytest.raises(UnsupportedValueError): json_schema.dump(schema)
def test_handle_range_no_minimum(): class SchemaMin(Schema): floor = fields.Integer(validate=validate.Range(min=1, max=4)) class SchemaNoMin(Schema): floor = fields.Integer(validate=validate.Range(max=4)) schema1 = SchemaMin(strict=True) schema2 = SchemaNoMin(strict=True) json_schema = JSONSchema() dumped1 = json_schema.dump(schema1).data['definitions']['SchemaMin'] dumped2 = json_schema.dump(schema2).data['definitions']['SchemaNoMin'] dumped1['properties']['floor']['minimum'] == 1 dumped1['properties']['floor']['exclusiveMinimum'] is True dumped2['properties']['floor']['minimum'] == 0 dumped2['properties']['floor']['exclusiveMinimum'] is False
def test_handle_range_not_number_returns_same_instance(): class SchemaWithStringRange(Schema): floor = fields.String(validate=validate.Range(min=1, max=4)) class SchemaWithNoRange(Schema): floor = fields.String() class SchemaWithIntRangeValidate(Schema): floor = fields.Integer(validate=validate.Range(min=1, max=4)) class SchemaWithIntRangeNoValidate(Schema): floor = fields.Integer() schema1 = SchemaWithStringRange(strict=True) schema2 = SchemaWithNoRange(strict=True) schema3 = SchemaWithIntRangeValidate(strict=True) schema4 = SchemaWithIntRangeNoValidate(strict=True) json_schema = JSONSchema() json_schema.dump(schema1) == json_schema.dump(schema2) json_schema.dump(schema3) != json_schema.dump(schema4)
def test_unknown_typed_field(self): class Colour(fields.Field): def _jsonschema_type_mapping(self): return { 'type': 'string', } def _serialize(self, value, attr, obj): r, g, b = value r = hex(r)[2:] g = hex(g)[2:] b = hex(b)[2:] return '#' + r + g + b class UserSchema(Schema): name = fields.String(required=True) favourite_colour = Colour() schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data self.assertEqual(dumped['properties']['favourite_colour'], {'type': 'string'})
def test_nested_descriptions(): class TestSchema(Schema): myfield = fields.String(metadata={'description': 'Brown Cow'}) yourfield = fields.Integer(required=True) class TestNestedSchema(Schema): nested = fields.Nested(TestSchema, metadata={ 'description': 'Nested 1', 'title': 'Title1' }) yourfield_nested = fields.Integer(required=True) schema = TestNestedSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) nested_def = dumped['definitions']['TestSchema'] nested_dmp = dumped['definitions']['TestNestedSchema']['properties'][ 'nested'] assert nested_def['properties']['myfield']['description'] == 'Brown Cow' assert nested_dmp['$ref'] == '#/definitions/TestSchema' assert nested_dmp['description'] == 'Nested 1' assert nested_dmp['title'] == 'Title1'
def validate_and_dump(schema): json_schema = JSONSchema() data = json_schema.dump(schema) _validate_schema(data) # ensure last version assert data["$schema"] == "http://json-schema.org/draft-07/schema#" return data
def test_range_validator(): schema = Address() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped['properties']['floor']['minimum'] == 1 assert dumped['properties']['floor']['maximum'] == 4
def test_default(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) props = dumped['definitions']['UserSchema']['properties'] assert props['id']['default'] == 'no-id'
def get_type_schema(annotation_type): try: Schema = get_schema(annotation_type) except UnknownAnnotationTypeException: abort(404) json_schema = JSONSchema() return json_schema.dump(Schema())
def test_range_validator(): schema = Address() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped["properties"]["floor"]["minimum"] == 1 assert dumped["properties"]["floor"]["maximum"] == 4
def test_unknown_typed_field(): class Colour(fields.Field): def _jsonschema_type_mapping(self): return { 'type': 'string', } def _serialize(self, value, attr, obj): r, g, b = value r = hex(r)[2:] g = hex(g)[2:] b = hex(b)[2:] return '#' + r + g + b class UserSchema(Schema): name = fields.String(required=True) favourite_colour = Colour() schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data assert dumped['definitions']['UserSchema']['properties'][ 'favourite_colour'] == { 'type': 'string' }
def test_dump_schema(self): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data self._validate_schema(dumped) self.assertGreater(len(schema.fields), 1) for field_name, field in schema.fields.items(): self.assertIn(field_name, dumped['properties'])
def test_dump_schema(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert len(schema.fields) > 1 for field_name, field in schema.fields.items(): assert field_name in dumped['properties']
def validate_and_dump(schema): json_schema = JSONSchema() dumped = json_schema.dump(schema) data = dot_data_backwards_compatible(dumped) _validate_schema(data) # ensure last version assert data["$schema"] == "http://json-schema.org/draft-07/schema#" return data
def test_dump_schema(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert len(schema.fields) > 1 for field_name, field in schema.fields.items(): assert field_name in dumped["properties"]
def test_required_excluded_when_empty(): class TestSchema(Schema): optional_value = fields.String() schema = TestSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data assert 'required' not in dumped['definitions']['TestSchema']
def test_union_based(): class TestNestedSchema(Schema): field_1 = fields.String() field_2 = fields.Integer() class TestSchema(Schema): union_prop = Union([ fields.String(), fields.Integer(), fields.Nested(TestNestedSchema) ]) # Should be sorting of fields schema = TestSchema() json_schema = JSONSchema() data = json_schema.dump(schema) # Expect only the `anyOf` key assert "anyOf" in data["definitions"]["TestSchema"]["properties"][ "union_prop"] assert len( data["definitions"]["TestSchema"]["properties"]["union_prop"]) == 1 string_schema = {"type": "string", "title": ""} integer_schema = {"type": "string", "title": ""} referenced_nested_schema = { "type": "object", "$ref": "#/definitions/TestNestedSchema", } actual_nested_schema = { "type": "object", "properties": { "field_1": { "type": "string", "title": "field_1" }, "field_2": { "type": "number", "title": "field_2", "format": "integer" }, }, "additionalProperties": False, } assert (string_schema in data["definitions"]["TestSchema"]["properties"] ["union_prop"]["anyOf"]) assert (integer_schema in data["definitions"]["TestSchema"]["properties"] ["union_prop"]["anyOf"]) assert (referenced_nested_schema in data["definitions"]["TestSchema"] ["properties"]["union_prop"]["anyOf"]) assert data["definitions"]["TestNestedSchema"] == actual_nested_schema # Expect three possible schemas for the union type assert (len(data["definitions"]["TestSchema"]["properties"]["union_prop"] ["anyOf"]) == 3)
def test_descriptions(): class TestSchema(Schema): myfield = fields.String(metadata={'description': 'Brown Cow'}) yourfield = fields.Integer(required=True) schema = TestSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped['properties']['myfield']['description'] == 'Brown Cow'
def test_one_of_validator(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert ( dumped['definitions']['UserSchema']['properties']['sex']['enum'] == [ 'male', 'female' ])
def test_descriptions(): class TestSchema(Schema): myfield = fields.String(metadata={"description": "Brown Cow"}) yourfield = fields.Integer(required=True) schema = TestSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped["properties"]["myfield"]["description"] == "Brown Cow"
def get_input_configuration(self): """ Get name and type json value for input parameters required by this plugin to operate. """ json_schema = JSONSchema() schema_blue_print = self.input_params.generate_schema(self.name + 'InputParams') schema_desc = schema_blue_print() return json_schema.dump(schema_desc).data
def test_length_validator(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped['properties']['name']['minLength'] == 1 assert dumped['properties']['name']['maxLength'] == 255 assert dumped['properties']['addresses']['minItems'] == 1 assert dumped['properties']['addresses']['maxItems'] == 3 assert dumped['properties']['const']['minLength'] == 50 assert dumped['properties']['const']['maxLength'] == 50
def test_one_of_validator(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert ( dumped['definitions']['UserSchema']['properties']['sex']['enum'] == [ 'male', 'female', 'non_binary', 'other' ]) assert (dumped['definitions']['UserSchema']['properties']['sex'] ['enumNames'] == ['Male', 'Female', 'Non-binary/fluid', 'Other'])
def test_length_validator(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped["properties"]["name"]["minLength"] == 1 assert dumped["properties"]["name"]["maxLength"] == 255 assert dumped["properties"]["addresses"]["minItems"] == 1 assert dumped["properties"]["addresses"]["maxItems"] == 3 assert dumped["properties"]["const"]["minLength"] == 50 assert dumped["properties"]["const"]["maxLength"] == 50
def test_nested_recursive(): """A self-referential schema should not cause an infinite recurse.""" class RecursiveSchema(Schema): foo = fields.Integer(required=True) children = fields.Nested('RecursiveSchema', many=True) schema = RecursiveSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) props = dumped['definitions']['RecursiveSchema']['properties'] assert 'RecursiveSchema' in props['children']['items']['$ref']
def test_readonly(): class TestSchema(Schema): id = fields.Integer(required=True) readonly_fld = fields.String(dump_only=True) schema = TestSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data assert dumped['properties']['readonly_fld'] == { 'title': 'readonly_fld', 'type': 'string', 'readonly': True, }
def test_list(): class ListSchema(Schema): foo = fields.List(fields.String(min), required=True) schema = ListSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) nested_json = dumped['definitions']['ListSchema']['properties']['foo'] assert nested_json['type'] == 'array' assert 'items' in nested_json item_schema = nested_json['items'] assert item_schema['type'] == 'string'
def test_metadata(): """Metadata should be available in the field definition.""" class TestSchema(Schema): myfield = fields.String(metadata={'foo': 'Bar'}) yourfield = fields.Integer(required=True, baz="waz") schema = TestSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) props = dumped['definitions']['TestSchema']['properties'] assert props['myfield']['foo'] == 'Bar' assert props['yourfield']['baz'] == 'waz' assert 'metadata' not in props['myfield'] assert 'metadata' not in props['yourfield'] # repeat process to assure idempotency json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) props = dumped['definitions']['TestSchema']['properties'] assert props['myfield']['foo'] == 'Bar' assert props['yourfield']['baz'] == 'waz'
def test_nested_string_to_cls(): class TestSchema(Schema): foo = fields.Integer(required=True) class TestNestedSchema(Schema): foo2 = fields.Integer(required=True) nested = fields.Nested('TestSchema') schema = TestNestedSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) nested_json = dumped['properties']['nested'] assert nested_json['properties']['foo']['format'] == 'integer' assert nested_json['type'] == 'object'
def test_handle_range_no_minimum(): class SchemaMin(Schema): floor = fields.Integer(validate=validate.Range(min=1, max=4)) class SchemaNoMin(Schema): floor = fields.Integer(validate=validate.Range(max=4)) schema1 = SchemaMin(strict=True) schema2 = SchemaNoMin(strict=True) json_schema = JSONSchema() dumped1 = json_schema.dump(schema1) dumped2 = json_schema.dump(schema2) dumped1.data['properties']['floor']['minimum'] == 1 dumped1.data['properties']['floor']['exclusiveMinimum'] is True dumped2.data['properties']['floor']['minimum'] == 0 dumped2.data['properties']['floor']['exclusiveMinimum'] is False
def test_metadata_direct_from_field(): """Should be able to get metadata without accessing metadata kwarg.""" class TestSchema(Schema): id = fields.Integer(required=True) metadata_field = fields.String(description='Directly on the field!') schema = TestSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data assert dumped['definitions']['TestSchema']['properties'][ 'metadata_field'] == { 'title': 'metadata_field', 'type': 'string', 'description': 'Directly on the field!', }
def spec(): if self.cached_spec is None: json_schema = JSONSchema() definitions = dict( (k, list(json_schema.dump(v)['definitions'].values())[0]) for k, v in self.schemas.items()) spec = flask_swagger.swagger( flask, template=dict(definitions=definitions, info=self.info, securityDefinitions=self.securities)) self.cached_spec = json.dumps(spec, cls=JSONEncoder2, allow_nan=False) return self.cached_spec
def test_nested_descriptions(): class TestSchema(Schema): myfield = fields.String(metadata={'description': 'Brown Cow'}) yourfield = fields.Integer(required=True) class TestNestedSchema(Schema): nested = fields.Nested( TestSchema, metadata={'description': 'Nested 1', 'title': 'Title1'}) yourfield_nested = fields.Integer(required=True) schema = TestNestedSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) nested_dmp = dumped['properties']['nested'] assert nested_dmp['properties']['myfield']['description'] == 'Brown Cow' assert nested_dmp['description'] == 'Nested 1' assert nested_dmp['title'] == 'Title1'
def test_nested_descriptions(): class TestSchema(Schema): myfield = fields.String(metadata={"description": "Brown Cow"}) yourfield = fields.Integer(required=True) class TestNestedSchema(Schema): nested = fields.Nested(TestSchema, metadata={"description": "Nested 1", "title": "Title1"}) yourfield_nested = fields.Integer(required=True) schema = TestNestedSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) nested_dmp = dumped["properties"]["nested"] assert nested_dmp["properties"]["myfield"]["description"] == "Brown Cow" assert nested_dmp["description"] == "Nested 1" assert nested_dmp["title"] == "Title1"
def test_unknown_typed_field(): class Colour(fields.Field): def _jsonschema_type_mapping(self): return {"type": "string"} def _serialize(self, value, attr, obj): r, g, b = value r = hex(r)[2:] g = hex(g)[2:] b = hex(b)[2:] return "#" + r + g + b class UserSchema(Schema): name = fields.String(required=True) favourite_colour = Colour() schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data assert dumped["properties"]["favourite_colour"] == {"type": "string"}
def test_nested_only_exclude(self): schema = Group() json_schema = JSONSchema() dumped = json_schema.dump(schema).data for key, field in schema.declared_fields.items(): original_fields = field.schema.declared_fields.keys() nested_dict = dumped['properties'][key]['items']['properties'] actual_properties = list(nested_dict.keys()) if field.only: # only takes precedence over exclude if isinstance(field.only, str): expected_properties = [field.only] else: expected_properties = list(field.only) elif field.exclude: expected_properties = list(original_fields - set(field.exclude)) else: expected_properties = original_fields self.assertEqual(sorted(expected_properties), sorted(actual_properties))
def test_one_of_validator(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped['properties']['sex']['enum'] == ['male', 'female']
def test_default(self): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data self._validate_schema(dumped) self.assertEqual(dumped['properties']['id']['default'], 'no-id')
def test_default(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped['properties']['id']['default'] == 'no-id'
def test_default(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped["properties"]["id"]["default"] == "no-id"
def test_one_of_validator(): schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data _validate_schema(dumped) assert dumped["properties"]["sex"]["enum"] == ["male", "female"]