def test_attribute(self): prop_value = 'value1' properties = { 'prop1': prop_value, } model = Model(properties) result = model.prop1 assert result == prop_value
def test_attribute(self): prop_value = "value1" properties = { "prop1": prop_value, } model = Model(properties) result = model.prop1 assert result == prop_value
def test_dict(self): properties = { 'prop1': 'value1', 'prop2': 'value2', } model = Model(properties) result = model.__dict__ assert result == properties
def test_dict(self): properties = { "prop1": "value1", "prop2": "value2", } model = Model(properties) result = model.__dict__ assert result == properties
def test_dict_empty(self): model = Model() result = model.__dict__ assert result == {}
class TestSchemaValidate(object): @pytest.fixture def validator_factory(self): def create_validator(schema): format_checker = build_format_checker() return SchemaUnmarshallersFactory( format_checker=format_checker).create(schema) return create_validator @pytest.mark.parametrize('schema_type', [ 'boolean', 'array', 'integer', 'number', 'string', ]) def test_null(self, schema_type, validator_factory): spec = { 'type': schema_type, } schema = SpecPath.from_spec(spec) value = None with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('schema_type', [ 'boolean', 'array', 'integer', 'number', 'string', ]) def test_nullable(self, schema_type, validator_factory): spec = { 'type': schema_type, 'nullable': True, } schema = SpecPath.from_spec(spec) value = None result = validator_factory(schema).validate(value) assert result is None def test_string_format_custom_missing(self, validator_factory): custom_format = 'custom' spec = { 'type': 'string', 'format': custom_format, } schema = SpecPath.from_spec(spec) value = 'x' with pytest.raises(FormatterNotFoundError): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [False, True]) def test_boolean(self, value, validator_factory): spec = { 'type': 'boolean', } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [1, 3.14, u('true'), [True, False]]) def test_boolean_invalid(self, value, validator_factory): spec = { 'type': 'boolean', } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [(1, 2)]) def test_array_no_schema(self, value, validator_factory): spec = { 'type': 'array', } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [[1, 2]]) def test_array(self, value, validator_factory): spec = { 'type': 'array', 'items': { 'type': 'integer', }, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [False, 1, 3.14, u('true'), (3, 4)]) def test_array_invalid(self, value, validator_factory): spec = { 'type': 'array', } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [1, 3]) def test_integer(self, value, validator_factory): spec = { 'type': 'integer', } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [False, 3.14, u('true'), [1, 2]]) def test_integer_invalid(self, value, validator_factory): spec = { 'type': 'integer', } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [0, 1, 2]) def test_integer_minimum_invalid(self, value, validator_factory): spec = { 'type': 'integer', 'minimum': 3, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [4, 5, 6]) def test_integer_minimum(self, value, validator_factory): spec = { 'type': 'integer', 'minimum': 3, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [4, 5, 6]) def test_integer_maximum_invalid(self, value, validator_factory): spec = { 'type': 'integer', 'maximum': 3, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [0, 1, 2]) def test_integer_maximum(self, value, validator_factory): spec = { 'type': 'integer', 'maximum': 3, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [1, 2, 4]) def test_integer_multiple_of_invalid(self, value, validator_factory): spec = { 'type': 'integer', 'multipleOf': 3, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [3, 6, 18]) def test_integer_multiple_of(self, value, validator_factory): spec = { 'type': 'integer', 'multipleOf': 3, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [1, 3.14]) def test_number(self, value, validator_factory): spec = { 'type': 'number', } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [False, 'true', [1, 3]]) def test_number_invalid(self, value, validator_factory): spec = { 'type': 'number', } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [0, 1, 2]) def test_number_minimum_invalid(self, value, validator_factory): spec = { 'type': 'number', 'minimum': 3, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [3, 4, 5]) def test_number_minimum(self, value, validator_factory): spec = { 'type': 'number', 'minimum': 3, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [1, 2, 3]) def test_number_exclusive_minimum_invalid(self, value, validator_factory): spec = { 'type': 'number', 'minimum': 3, 'exclusiveMinimum': True, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [4, 5, 6]) def test_number_exclusive_minimum(self, value, validator_factory): spec = { 'type': 'number', 'minimum': 3, 'exclusiveMinimum': True, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [4, 5, 6]) def test_number_maximum_invalid(self, value, validator_factory): spec = { 'type': 'number', 'maximum': 3, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [1, 2, 3]) def test_number_maximum(self, value, validator_factory): spec = { 'type': 'number', 'maximum': 3, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [3, 4, 5]) def test_number_exclusive_maximum_invalid(self, value, validator_factory): spec = { 'type': 'number', 'maximum': 3, 'exclusiveMaximum': True, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [0, 1, 2]) def test_number_exclusive_maximum(self, value, validator_factory): spec = { 'type': 'number', 'maximum': 3, 'exclusiveMaximum': True, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [1, 2, 4]) def test_number_multiple_of_invalid(self, value, validator_factory): spec = { 'type': 'number', 'multipleOf': 3, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [3, 6, 18]) def test_number_multiple_of(self, value, validator_factory): spec = { 'type': 'number', 'multipleOf': 3, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [u('true'), b('test')]) def test_string(self, value, validator_factory): spec = { 'type': 'string', } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [False, 1, 3.14, [1, 3]]) def test_string_invalid(self, value, validator_factory): spec = { 'type': 'string', } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ b('true'), u('test'), False, 1, 3.14, [1, 3], datetime.datetime(1989, 1, 2), ]) def test_string_format_date_invalid(self, value, validator_factory): spec = { 'type': 'string', 'format': 'date', } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ u('1989-01-02'), u('2018-01-02'), ]) def test_string_format_date(self, value, validator_factory): spec = { 'type': 'string', 'format': 'date', } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [ u('12345678-1234-5678-1234-567812345678'), ]) def test_string_format_uuid(self, value, validator_factory): spec = { 'type': 'string', 'format': 'uuid', } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [ b('true'), u('true'), False, 1, 3.14, [1, 3], datetime.date(2018, 1, 2), datetime.datetime(2018, 1, 2, 23, 59, 59), ]) def test_string_format_uuid_invalid(self, value, validator_factory): spec = { 'type': 'string', 'format': 'uuid', } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ b('true'), u('true'), False, 1, 3.14, [1, 3], u('1989-01-02'), ]) def test_string_format_datetime_invalid(self, value, validator_factory): spec = { 'type': 'string', 'format': 'date-time', } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ u('1989-01-02T00:00:00Z'), u('2018-01-02T23:59:59Z'), ]) @mock.patch('openapi_schema_validator._format.' 'DATETIME_HAS_STRICT_RFC3339', True) @mock.patch('openapi_schema_validator._format.' 'DATETIME_HAS_ISODATE', False) def test_string_format_datetime_strict_rfc3339(self, value, validator_factory): spec = { 'type': 'string', 'format': 'date-time', } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [ u('1989-01-02T00:00:00Z'), u('2018-01-02T23:59:59Z'), ]) @mock.patch('openapi_schema_validator._format.' 'DATETIME_HAS_STRICT_RFC3339', False) @mock.patch('openapi_schema_validator._format.' 'DATETIME_HAS_ISODATE', True) def test_string_format_datetime_isodate(self, value, validator_factory): spec = { 'type': 'string', 'format': 'date-time', } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [ u('true'), False, 1, 3.14, [1, 3], u('1989-01-02'), u('1989-01-02T00:00:00Z'), ]) def test_string_format_binary_invalid(self, value, validator_factory): spec = { 'type': 'string', 'format': 'binary', } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ b('stream'), b('text'), ]) def test_string_format_binary(self, value, validator_factory): spec = { 'type': 'string', 'format': 'binary', } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [ b('dGVzdA=='), u('dGVzdA=='), ]) def test_string_format_byte(self, value, validator_factory): spec = { 'type': 'string', 'format': 'byte', } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [ u('tsssst'), b('tsssst'), b('tesddddsdsdst'), ]) def test_string_format_byte_invalid(self, value, validator_factory): spec = { 'type': 'string', 'format': 'byte', } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ u('test'), b('stream'), datetime.date(1989, 1, 2), datetime.datetime(1989, 1, 2, 0, 0, 0), ]) def test_string_format_unknown(self, value, validator_factory): unknown_format = 'unknown' spec = { 'type': 'string', 'format': unknown_format, } schema = SpecPath.from_spec(spec) with pytest.raises(FormatterNotFoundError): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [u(""), u("a"), u("ab")]) def test_string_min_length_invalid(self, value, validator_factory): spec = { 'type': 'string', 'minLength': 3, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [u("abc"), u("abcd")]) def test_string_min_length(self, value, validator_factory): spec = { 'type': 'string', 'minLength': 3, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [ u(""), ]) def test_string_max_length_invalid_schema(self, value, validator_factory): spec = { 'type': 'string', 'maxLength': -1, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [u("ab"), u("abc")]) def test_string_max_length_invalid(self, value, validator_factory): spec = { 'type': 'string', 'maxLength': 1, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [u(""), u("a")]) def test_string_max_length(self, value, validator_factory): spec = { 'type': 'string', 'maxLength': 1, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [u("foo"), u("bar")]) def test_string_pattern_invalid(self, value, validator_factory): spec = { 'type': 'string', 'pattern': 'baz', } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [u("bar"), u("foobar")]) def test_string_pattern(self, value, validator_factory): spec = { 'type': 'string', 'pattern': 'bar', } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', ['true', False, 1, 3.14, [1, 3]]) def test_object_not_an_object(self, value, validator_factory): spec = { 'type': 'object', } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ Model(), ]) def test_object_multiple_one_of(self, value, validator_factory): one_of = [ { 'type': 'object', }, { 'type': 'object', }, ] spec = { 'type': 'object', 'oneOf': one_of, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ {}, ]) def test_object_different_type_one_of(self, value, validator_factory): one_of = [ { 'type': 'integer', }, { 'type': 'string', }, ] spec = { 'type': 'object', 'oneOf': one_of, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ {}, ]) def test_object_no_one_of(self, value, validator_factory): one_of = [ { 'type': 'object', 'required': [ 'test1', ], 'properties': { 'test1': { 'type': 'string', }, }, }, { 'type': 'object', 'required': [ 'test2', ], 'properties': { 'test2': { 'type': 'string', }, }, }, ] spec = { 'type': 'object', 'oneOf': one_of, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ { 'foo': u("FOO"), }, { 'foo': u("FOO"), 'bar': u("BAR"), }, ]) def test_unambiguous_one_of(self, value, validator_factory): one_of = [ { 'type': 'object', 'required': [ 'foo', ], 'properties': { 'foo': { 'type': 'string', }, }, 'additionalProperties': False, }, { 'type': 'object', 'required': ['foo', 'bar'], 'properties': { 'foo': { 'type': 'string', }, 'bar': { 'type': 'string', }, }, 'additionalProperties': False, }, ] spec = { 'type': 'object', 'oneOf': one_of, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [ {}, ]) def test_object_default_property(self, value, validator_factory): spec = { 'type': 'object', 'default': 'value1', } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [ {}, ]) def test_object_min_properties_invalid_schema(self, value, validator_factory): spec = { 'type': 'object', 'minProperties': 2, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ { 'a': 1 }, { 'a': 1, 'b': 2 }, { 'a': 1, 'b': 2, 'c': 3 }, ]) def test_object_min_properties_invalid(self, value, validator_factory): spec = { 'type': 'object', 'properties': {k: { 'type': 'number' } for k in ['a', 'b', 'c']}, 'minProperties': 4, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ { 'a': 1 }, { 'a': 1, 'b': 2 }, { 'a': 1, 'b': 2, 'c': 3 }, ]) def test_object_min_properties(self, value, validator_factory): spec = { 'type': 'object', 'properties': {k: { 'type': 'number' } for k in ['a', 'b', 'c']}, 'minProperties': 1, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [ {}, ]) def test_object_max_properties_invalid_schema(self, value, validator_factory): spec = { 'type': 'object', 'maxProperties': -1, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ { 'a': 1 }, { 'a': 1, 'b': 2 }, { 'a': 1, 'b': 2, 'c': 3 }, ]) def test_object_max_properties_invalid(self, value, validator_factory): spec = { 'type': 'object', 'properties': {k: { 'type': 'number' } for k in ['a', 'b', 'c']}, 'maxProperties': 0, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ { 'a': 1 }, { 'a': 1, 'b': 2 }, { 'a': 1, 'b': 2, 'c': 3 }, ]) def test_object_max_properties(self, value, validator_factory): spec = { 'type': 'object', 'properties': {k: { 'type': 'number' } for k in ['a', 'b', 'c']}, 'maxProperties': 3, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [ { 'additional': 1 }, ]) def test_object_additional_properties(self, value, validator_factory): spec = { 'type': 'object', } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [ { 'additional': 1 }, ]) def test_object_additional_properties_false(self, value, validator_factory): spec = { 'type': 'object', 'additionalProperties': False, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ { 'additional': 1 }, ]) def test_object_additional_properties_object(self, value, validator_factory): additional_properties = { 'type': 'integer', } spec = { 'type': 'object', 'additionalProperties': additional_properties, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [[], [1], [1, 2]]) def test_list_min_items_invalid(self, value, validator_factory): spec = { 'type': 'array', 'items': { 'type': 'number', }, 'minItems': 3, } schema = SpecPath.from_spec(spec) with pytest.raises(Exception): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [[], [1], [1, 2]]) def test_list_min_items(self, value, validator_factory): spec = { 'type': 'array', 'items': { 'type': 'number', }, 'minItems': 0, } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [ [], ]) def test_list_max_items_invalid_schema(self, value, validator_factory): spec = { 'type': 'array', 'items': { 'type': 'number', }, 'maxItems': -1, } schema = SpecPath.from_spec(spec) with pytest.raises(InvalidSchemaValue): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [[1, 2], [2, 3, 4]]) def test_list_max_items_invalid(self, value, validator_factory): spec = { 'type': 'array', 'items': { 'type': 'number', }, 'maxItems': 1, } schema = SpecPath.from_spec(spec) with pytest.raises(Exception): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [[1, 2, 1], [2, 2]]) def test_list_unique_items_invalid(self, value, validator_factory): spec = { 'type': 'array', 'items': { 'type': 'number', }, 'uniqueItems': True, } schema = SpecPath.from_spec(spec) with pytest.raises(Exception): validator_factory(schema).validate(value) @pytest.mark.parametrize('value', [ { 'someint': 123, }, { 'somestr': u('content'), }, { 'somestr': u('content'), 'someint': 123, }, ]) def test_object_with_properties(self, value, validator_factory): spec = { 'type': 'object', 'properties': { 'somestr': { 'type': 'string', }, 'someint': { 'type': 'integer', } } } schema = SpecPath.from_spec(spec) result = validator_factory(schema).validate(value) assert result is None @pytest.mark.parametrize('value', [ { 'somestr': {}, 'someint': 123, }, { 'somestr': ['content1', 'content2'], 'someint': 123, }, { 'somestr': 123, 'someint': 123, }, { 'somestr': 'content', 'someint': 123, 'not_in_scheme_prop': 123, }, ]) def test_object_with_invalid_properties(self, value, validator_factory): spec = { 'type': 'object', 'properties': { 'somestr': { 'type': 'string', }, 'someint': { 'type': 'integer', } }, 'additionalProperties': False, } schema = SpecPath.from_spec(spec) with pytest.raises(Exception): validator_factory(schema).validate(value)
class TestSchemaValidate(object): @pytest.mark.parametrize('schema_type', [ 'boolean', 'array', 'integer', 'number', 'string', ]) def test_null(self, schema_type): schema = Schema(schema_type) value = None with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('schema_type', [ 'boolean', 'array', 'integer', 'number', 'string', ]) def test_nullable(self, schema_type): schema = Schema(schema_type, nullable=True) value = None result = schema.validate(value) assert result is None @pytest.mark.xfail( reason="validation does not care about custom formats atm") def test_string_format_custom_missing(self): custom_format = 'custom' schema = Schema('string', schema_format=custom_format) value = 'x' with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [False, True]) def test_boolean(self, value): schema = Schema('boolean') result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [1, 3.14, u('true'), [True, False]]) def test_boolean_invalid(self, value): schema = Schema('boolean') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [(1, 2)]) def test_array_no_schema(self, value): schema = Schema('array') with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [[1, 2]]) def test_array(self, value): schema = Schema('array', items=Schema('integer')) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [False, 1, 3.14, u('true'), (3, 4)]) def test_array_invalid(self, value): schema = Schema('array') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [1, 3]) def test_integer(self, value): schema = Schema('integer') result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [False, 3.14, u('true'), [1, 2]]) def test_integer_invalid(self, value): schema = Schema('integer') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [0, 1, 2]) def test_integer_minimum_invalid(self, value): schema = Schema('integer', minimum=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [4, 5, 6]) def test_integer_minimum(self, value): schema = Schema('integer', minimum=3) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [4, 5, 6]) def test_integer_maximum_invalid(self, value): schema = Schema('integer', maximum=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [0, 1, 2]) def test_integer_maximum(self, value): schema = Schema('integer', maximum=3) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [1, 2, 4]) def test_integer_multiple_of_invalid(self, value): schema = Schema('integer', multiple_of=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [3, 6, 18]) def test_integer_multiple_of(self, value): schema = Schema('integer', multiple_of=3) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [1, 3.14]) def test_number(self, value): schema = Schema('number') result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [False, 'true', [1, 3]]) def test_number_invalid(self, value): schema = Schema('number') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [0, 1, 2]) def test_number_minimum_invalid(self, value): schema = Schema('number', minimum=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [3, 4, 5]) def test_number_minimum(self, value): schema = Schema('number', minimum=3) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [1, 2, 3]) def test_number_exclusive_minimum_invalid(self, value): schema = Schema('number', minimum=3, exclusive_minimum=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [4, 5, 6]) def test_number_exclusive_minimum(self, value): schema = Schema('number', minimum=3) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [4, 5, 6]) def test_number_maximum_invalid(self, value): schema = Schema('number', maximum=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [1, 2, 3]) def test_number_maximum(self, value): schema = Schema('number', maximum=3) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [3, 4, 5]) def test_number_exclusive_maximum_invalid(self, value): schema = Schema('number', maximum=3, exclusive_maximum=True) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [0, 1, 2]) def test_number_exclusive_maximum(self, value): schema = Schema('number', maximum=3, exclusive_maximum=True) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [1, 2, 4]) def test_number_multiple_of_invalid(self, value): schema = Schema('number', multiple_of=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [3, 6, 18]) def test_number_multiple_of(self, value): schema = Schema('number', multiple_of=3) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [u('true'), b('test')]) def test_string(self, value): schema = Schema('string') result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [False, 1, 3.14, [1, 3]]) def test_string_invalid(self, value): schema = Schema('string') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ b('true'), u('test'), False, 1, 3.14, [1, 3], datetime.datetime(1989, 1, 2), ]) def test_string_format_date_invalid(self, value): schema = Schema('string', schema_format='date') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ u('1989-01-02'), u('2018-01-02'), ]) def test_string_format_date(self, value): schema = Schema('string', schema_format='date') result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [ u('12345678-1234-5678-1234-567812345678'), ]) def test_string_format_uuid(self, value): schema = Schema('string', schema_format='uuid') result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [ b('true'), u('true'), False, 1, 3.14, [1, 3], datetime.date(2018, 1, 2), datetime.datetime(2018, 1, 2, 23, 59, 59), ]) def test_string_format_uuid_invalid(self, value): schema = Schema('string', schema_format='uuid') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ b('true'), u('true'), False, 1, 3.14, [1, 3], u('1989-01-02'), ]) def test_string_format_datetime_invalid(self, value): schema = Schema('string', schema_format='date-time') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ u('1989-01-02T00:00:00Z'), u('2018-01-02T23:59:59Z'), ]) @mock.patch( 'openapi_core.schema.schemas._format.' 'DATETIME_HAS_STRICT_RFC3339', True ) @mock.patch( 'openapi_core.schema.schemas._format.' 'DATETIME_HAS_ISODATE', False ) def test_string_format_datetime_strict_rfc3339(self, value): schema = Schema('string', schema_format='date-time') result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [ u('1989-01-02T00:00:00Z'), u('2018-01-02T23:59:59Z'), ]) @mock.patch( 'openapi_core.schema.schemas._format.' 'DATETIME_HAS_STRICT_RFC3339', False ) @mock.patch( 'openapi_core.schema.schemas._format.' 'DATETIME_HAS_ISODATE', True ) def test_string_format_datetime_isodate(self, value): schema = Schema('string', schema_format='date-time') result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [ u('true'), False, 1, 3.14, [1, 3], u('1989-01-02'), u('1989-01-02T00:00:00Z'), ]) def test_string_format_binary_invalid(self, value): schema = Schema('string', schema_format='binary') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ b('stream'), b('text'), ]) def test_string_format_binary(self, value): schema = Schema('string', schema_format='binary') result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [ b('dGVzdA=='), u('dGVzdA=='), ]) def test_string_format_byte(self, value): schema = Schema('string', schema_format='byte') result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [ u('tsssst'), b('tsssst'), b('tesddddsdsdst'), ]) def test_string_format_byte_invalid(self, value): schema = Schema('string', schema_format='byte') with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [ u('test'), b('stream'), datetime.date(1989, 1, 2), datetime.datetime(1989, 1, 2, 0, 0, 0), ]) def test_string_format_unknown(self, value): unknown_format = 'unknown' schema = Schema('string', schema_format=unknown_format) with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [u(""), u("a"), u("ab")]) def test_string_min_length_invalid(self, value): schema = Schema('string', min_length=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [u("abc"), u("abcd")]) def test_string_min_length(self, value): schema = Schema('string', min_length=3) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [u(""), ]) def test_string_max_length_invalid_schema(self, value): schema = Schema('string', max_length=-1) with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [u("ab"), u("abc")]) def test_string_max_length_invalid(self, value): schema = Schema('string', max_length=1) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [u(""), u("a")]) def test_string_max_length(self, value): schema = Schema('string', max_length=1) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [u("foo"), u("bar")]) def test_string_pattern_invalid(self, value): schema = Schema('string', pattern='baz') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [u("bar"), u("foobar")]) def test_string_pattern(self, value): schema = Schema('string', pattern='bar') result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', ['true', False, 1, 3.14, [1, 3]]) def test_object_not_an_object(self, value): schema = Schema('object') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [Model(), ]) def test_object_multiple_one_of(self, value): one_of = [ Schema('object'), Schema('object'), ] schema = Schema('object', one_of=one_of) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [{}, ]) def test_object_defferent_type_one_of(self, value): one_of = [ Schema('integer'), Schema('string'), ] schema = Schema('object', one_of=one_of) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [{}, ]) def test_object_no_one_of(self, value): one_of = [ Schema( 'object', properties={'test1': Schema('string')}, required=['test1', ], ), Schema( 'object', properties={'test2': Schema('string')}, required=['test2', ], ), ] schema = Schema('object', one_of=one_of) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ { 'foo': u("FOO"), }, { 'foo': u("FOO"), 'bar': u("BAR"), }, ]) def test_unambiguous_one_of(self, value): one_of = [ Schema( 'object', properties={ 'foo': Schema('string'), }, additional_properties=False, required=['foo'], ), Schema( 'object', properties={ 'foo': Schema('string'), 'bar': Schema('string'), }, additional_properties=False, required=['foo', 'bar'], ), ] schema = Schema('object', one_of=one_of) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [{}, ]) def test_object_default_property(self, value): schema = Schema('object', default='value1') result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [Model(), ]) def test_object_min_properties_invalid_schema(self, value): schema = Schema('object', min_properties=-1) with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [ {'a': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 2, 'c': 3}, ]) def test_object_min_properties_invalid(self, value): schema = Schema( 'object', properties={k: Schema('number') for k in ['a', 'b', 'c']}, min_properties=4, ) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ {'a': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 2, 'c': 3}, ]) def test_object_min_properties(self, value): schema = Schema( 'object', properties={k: Schema('number') for k in ['a', 'b', 'c']}, min_properties=1, ) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [Model(), ]) def test_object_max_properties_invalid_schema(self, value): schema = Schema('object', max_properties=-1) with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [ {'a': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 2, 'c': 3}, ]) def test_object_max_properties_invalid(self, value): schema = Schema( 'object', properties={k: Schema('number') for k in ['a', 'b', 'c']}, max_properties=0, ) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ {'a': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 2, 'c': 3}, ]) def test_object_max_properties(self, value): schema = Schema( 'object', properties={k: Schema('number') for k in ['a', 'b', 'c']}, max_properties=3, ) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [{'additional': 1}, ]) def test_object_additional_propetries(self, value): schema = Schema('object') result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [{'additional': 1}, ]) def test_object_additional_propetries_false(self, value): schema = Schema('object', additional_properties=False) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [{'additional': 1}, ]) def test_object_additional_propetries_object(self, value): additional_properties = Schema('integer') schema = Schema('object', additional_properties=additional_properties) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [[], [1], [1, 2]]) def test_list_min_items_invalid(self, value): schema = Schema( 'array', items=Schema('number'), min_items=3, ) with pytest.raises(Exception): schema.validate(value) @pytest.mark.parametrize('value', [[], [1], [1, 2]]) def test_list_min_items(self, value): schema = Schema( 'array', items=Schema('number'), min_items=0, ) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [[], ]) def test_list_max_items_invalid_schema(self, value): schema = Schema( 'array', items=Schema('number'), max_items=-1, ) with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [[1, 2], [2, 3, 4]]) def test_list_max_items_invalid(self, value): schema = Schema( 'array', items=Schema('number'), max_items=1, ) with pytest.raises(Exception): schema.validate(value) @pytest.mark.parametrize('value', [[1, 2, 1], [2, 2]]) def test_list_unique_items_invalid(self, value): schema = Schema( 'array', items=Schema('number'), unique_items=True, ) with pytest.raises(Exception): schema.validate(value) @pytest.mark.parametrize('value', [ { 'someint': 123, }, { 'somestr': u('content'), }, { 'somestr': u('content'), 'someint': 123, }, ]) def test_object_with_properties(self, value): schema = Schema( 'object', properties={ 'somestr': Schema('string'), 'someint': Schema('integer'), }, ) result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [ { 'somestr': {}, 'someint': 123, }, { 'somestr': [ 'content1', 'content2' ], 'someint': 123, }, { 'somestr': 123, 'someint': 123, }, { 'somestr': 'content', 'someint': 123, 'not_in_scheme_prop': 123, }, ]) def test_object_with_invalid_properties(self, value): schema = Schema( 'object', properties={ 'somestr': Schema('string'), 'someint': Schema('integer'), }, additional_properties=False, ) with pytest.raises(Exception): schema.validate(value)
class TestSchemaValidate(object): @pytest.mark.parametrize('schema_type', [ 'boolean', 'array', 'integer', 'number', 'string', ]) def test_null(self, schema_type): schema = Schema(schema_type) value = None with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('schema_type', [ 'boolean', 'array', 'integer', 'number', 'string', ]) def test_nullable(self, schema_type): schema = Schema(schema_type, nullable=True) value = None result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [False, True]) def test_boolean(self, value): schema = Schema('boolean') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [1, 3.14, u('true'), [True, False]]) def test_boolean_invalid(self, value): schema = Schema('boolean') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [[1, 2], (3, 4)]) def test_array_no_schema(self, value): schema = Schema('array') with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [[1, 2], (3, 4)]) def test_array(self, value): schema = Schema('array', items=Schema('integer')) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [False, 1, 3.14, u('true')]) def test_array_invalid(self, value): schema = Schema('array') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [1, 3]) def test_integer(self, value): schema = Schema('integer') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [False, 3.14, u('true'), [1, 2]]) def test_integer_invalid(self, value): schema = Schema('integer') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [1, 3.14]) def test_number(self, value): schema = Schema('number') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [False, 'true', [1, 3]]) def test_number_invalid(self, value): schema = Schema('number') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ u('true'), ]) def test_string(self, value): schema = Schema('string') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [b('test'), False, 1, 3.14, [1, 3]]) def test_string_invalid(self, value): schema = Schema('string') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ b('true'), u('test'), False, 1, 3.14, [1, 3], datetime.datetime(1989, 1, 2), ]) def test_string_format_date_invalid(self, value): schema = Schema('string', schema_format='date') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ datetime.date(1989, 1, 2), datetime.date(2018, 1, 2), ]) def test_string_format_date(self, value): schema = Schema('string', schema_format='date') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ b('true'), u('true'), False, 1, 3.14, [1, 3], datetime.date(1989, 1, 2), ]) def test_string_format_datetime_invalid(self, value): schema = Schema('string', schema_format='date-time') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ datetime.datetime(1989, 1, 2, 0, 0, 0), datetime.datetime(2018, 1, 2, 23, 59, 59), ]) def test_string_format_datetime(self, value): schema = Schema('string', schema_format='date-time') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ u('true'), False, 1, 3.14, [1, 3], datetime.date(1989, 1, 2), datetime.datetime(1989, 1, 2, 0, 0, 0), ]) def test_string_format_binary_invalid(self, value): schema = Schema('string', schema_format='binary') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ b('stream'), b('text'), ]) def test_string_format_binary(self, value): schema = Schema('string', schema_format='binary') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ u('test'), b('stream'), datetime.date(1989, 1, 2), datetime.datetime(1989, 1, 2, 0, 0, 0), ]) def test_string_format_unknown(self, value): unknown_format = 'unknown' schema = Schema('string', schema_format=unknown_format) with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', ['true', False, 1, 3.14, [1, 3]]) def test_object_not_an_object(self, value): schema = Schema('object') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ Model(), ]) def test_object_multiple_one_of(self, value): one_of = [ Schema('object'), Schema('object'), ] schema = Schema('object', one_of=one_of) with pytest.raises(MultipleOneOfSchema): schema.validate(value) @pytest.mark.parametrize('value', [ Model(), ]) def test_object_defferent_type_one_of(self, value): one_of = [ Schema('integer'), Schema('string'), ] schema = Schema('object', one_of=one_of) with pytest.raises(MultipleOneOfSchema): schema.validate(value) @pytest.mark.parametrize('value', [ Model(), ]) def test_object_no_one_of(self, value): one_of = [ Schema( 'object', properties={'test1': Schema('string')}, required=[ 'test1', ], ), Schema( 'object', properties={'test2': Schema('string')}, required=[ 'test2', ], ), ] schema = Schema('object', one_of=one_of) with pytest.raises(NoOneOfSchema): schema.validate(value) @pytest.mark.parametrize('value', [ Model(), ]) def test_object_default_property(self, value): schema = Schema('object', default='value1') result = schema.validate(value) assert result == value
class TestSchemaValidate(object): @pytest.mark.parametrize('schema_type', [ 'boolean', 'array', 'integer', 'number', 'string', ]) def test_null(self, schema_type): schema = Schema(schema_type) value = None with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('schema_type', [ 'boolean', 'array', 'integer', 'number', 'string', ]) def test_nullable(self, schema_type): schema = Schema(schema_type, nullable=True) value = None result = schema.validate(value) assert result is None @pytest.mark.parametrize('value', [False, True]) def test_boolean(self, value): schema = Schema('boolean') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [1, 3.14, u('true'), [True, False]]) def test_boolean_invalid(self, value): schema = Schema('boolean') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [[1, 2], (3, 4)]) def test_array_no_schema(self, value): schema = Schema('array') with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [[1, 2], (3, 4)]) def test_array(self, value): schema = Schema('array', items=Schema('integer')) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [False, 1, 3.14, u('true')]) def test_array_invalid(self, value): schema = Schema('array') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [1, 3]) def test_integer(self, value): schema = Schema('integer') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [False, 3.14, u('true'), [1, 2]]) def test_integer_invalid(self, value): schema = Schema('integer') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [0, 1, 2]) def test_integer_minimum_invalid(self, value): schema = Schema('integer', minimum=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [4, 5, 6]) def test_integer_minimum(self, value): schema = Schema('integer', minimum=3) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [4, 5, 6]) def test_integer_maximum_invalid(self, value): schema = Schema('integer', maximum=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [0, 1, 2]) def test_integer_maximum(self, value): schema = Schema('integer', maximum=3) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [1, 2, 4]) def test_integer_multiple_of_invalid(self, value): schema = Schema('integer', multiple_of=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [3, 6, 18]) def test_integer_multiple_of(self, value): schema = Schema('integer', multiple_of=3) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [1, 3.14]) def test_number(self, value): schema = Schema('number') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [False, 'true', [1, 3]]) def test_number_invalid(self, value): schema = Schema('number') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [0, 1, 2]) def test_number_minimum_invalid(self, value): schema = Schema('number', minimum=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [3, 4, 5]) def test_number_minimum(self, value): schema = Schema('number', minimum=3) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [1, 2, 3]) def test_number_exclusive_minimum_invalid(self, value): schema = Schema('number', minimum=3, exclusive_minimum=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [4, 5, 6]) def test_number_exclusive_minimum(self, value): schema = Schema('number', minimum=3) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [4, 5, 6]) def test_number_maximum_invalid(self, value): schema = Schema('number', maximum=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [1, 2, 3]) def test_number_maximum(self, value): schema = Schema('number', maximum=3) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [3, 4, 5]) def test_number_exclusive_maximum_invalid(self, value): schema = Schema('number', maximum=3, exclusive_maximum=True) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [0, 1, 2]) def test_number_exclusive_maximum(self, value): schema = Schema('number', maximum=3, exclusive_maximum=True) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [1, 2, 4]) def test_number_multiple_of_invalid(self, value): schema = Schema('number', multiple_of=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [3, 6, 18]) def test_number_multiple_of(self, value): schema = Schema('number', multiple_of=3) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ u('true'), ]) def test_string(self, value): schema = Schema('string') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [b('test'), False, 1, 3.14, [1, 3]]) def test_string_invalid(self, value): schema = Schema('string') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ b('true'), u('test'), False, 1, 3.14, [1, 3], datetime.datetime(1989, 1, 2), ]) def test_string_format_date_invalid(self, value): schema = Schema('string', schema_format='date') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ datetime.date(1989, 1, 2), datetime.date(2018, 1, 2), ]) def test_string_format_date(self, value): schema = Schema('string', schema_format='date') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ uuid.UUID('{12345678-1234-5678-1234-567812345678}'), ]) def test_string_format_uuid(self, value): schema = Schema('string', schema_format='uuid') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ b('true'), u('true'), False, 1, 3.14, [1, 3], datetime.date(2018, 1, 2), datetime.datetime(2018, 1, 2, 23, 59, 59), ]) def test_string_format_uuid_invalid(self, value): schema = Schema('string', schema_format='uuid') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ b('true'), u('true'), False, 1, 3.14, [1, 3], datetime.date(1989, 1, 2), ]) def test_string_format_datetime_invalid(self, value): schema = Schema('string', schema_format='date-time') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ datetime.datetime(1989, 1, 2, 0, 0, 0), datetime.datetime(2018, 1, 2, 23, 59, 59), ]) def test_string_format_datetime(self, value): schema = Schema('string', schema_format='date-time') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ u('true'), False, 1, 3.14, [1, 3], datetime.date(1989, 1, 2), datetime.datetime(1989, 1, 2, 0, 0, 0), ]) def test_string_format_binary_invalid(self, value): schema = Schema('string', schema_format='binary') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ b('stream'), b('text'), ]) def test_string_format_binary(self, value): schema = Schema('string', schema_format='binary') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ b('tsssst'), b('dGVzdA=='), ]) def test_string_format_byte_invalid(self, value): schema = Schema('string', schema_format='byte') with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [ u('tsssst'), u('dGVzdA=='), ]) def test_string_format_byte(self, value): schema = Schema('string', schema_format='byte') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ u('test'), b('stream'), datetime.date(1989, 1, 2), datetime.datetime(1989, 1, 2, 0, 0, 0), ]) def test_string_format_unknown(self, value): unknown_format = 'unknown' schema = Schema('string', schema_format=unknown_format) with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [ u(""), ]) def test_string_min_length_invalid_schema(self, value): schema = Schema('string', min_length=-1) with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [u(""), u("a"), u("ab")]) def test_string_min_length_invalid(self, value): schema = Schema('string', min_length=3) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [u("abc"), u("abcd")]) def test_string_min_length(self, value): schema = Schema('string', min_length=3) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ u(""), ]) def test_string_max_length_invalid_schema(self, value): schema = Schema('string', max_length=-1) with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [u("ab"), u("abc")]) def test_string_max_length_invalid(self, value): schema = Schema('string', max_length=1) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [u(""), u("a")]) def test_string_max_length(self, value): schema = Schema('string', max_length=1) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [u("foo"), u("bar")]) def test_string_pattern_invalid(self, value): schema = Schema('string', pattern='baz') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [u("bar"), u("foobar")]) def test_string_pattern(self, value): schema = Schema('string', pattern='bar') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', ['true', False, 1, 3.14, [1, 3]]) def test_object_not_an_object(self, value): schema = Schema('object') with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ Model(), ]) def test_object_multiple_one_of(self, value): one_of = [ Schema('object'), Schema('object'), ] schema = Schema('object', one_of=one_of) with pytest.raises(MultipleOneOfSchema): schema.validate(value) @pytest.mark.parametrize('value', [ Model(), ]) def test_object_defferent_type_one_of(self, value): one_of = [ Schema('integer'), Schema('string'), ] schema = Schema('object', one_of=one_of) with pytest.raises(MultipleOneOfSchema): schema.validate(value) @pytest.mark.parametrize('value', [ Model(), ]) def test_object_no_one_of(self, value): one_of = [ Schema( 'object', properties={'test1': Schema('string')}, required=[ 'test1', ], ), Schema( 'object', properties={'test2': Schema('string')}, required=[ 'test2', ], ), ] schema = Schema('object', one_of=one_of) with pytest.raises(NoOneOfSchema): schema.validate(value) @pytest.mark.parametrize('value', [ Model({ 'foo': u("FOO"), }), Model({ 'foo': u("FOO"), 'bar': u("BAR"), }), ]) def test_unambiguous_one_of(self, value): one_of = [ Schema( 'object', properties={ 'foo': Schema('string'), }, additional_properties=False, required=['foo'], ), Schema( 'object', properties={ 'foo': Schema('string'), 'bar': Schema('string'), }, additional_properties=False, required=['foo', 'bar'], ), ] schema = Schema('object', one_of=one_of) schema.validate(value) @pytest.mark.parametrize('value', [ Model(), ]) def test_object_default_property(self, value): schema = Schema('object', default='value1') result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ Model(), ]) def test_object_min_properties_invalid_schema(self, value): schema = Schema('object', min_properties=-1) with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [ Model({'a': 1}), Model({ 'a': 1, 'b': 2 }), Model({ 'a': 1, 'b': 2, 'c': 3 }) ]) def test_object_min_properties_invalid(self, value): schema = Schema( 'object', properties={k: Schema('number') for k in ['a', 'b', 'c']}, min_properties=4, ) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ Model({'a': 1}), Model({ 'a': 1, 'b': 2 }), Model({ 'a': 1, 'b': 2, 'c': 3 }) ]) def test_object_min_properties(self, value): schema = Schema( 'object', properties={k: Schema('number') for k in ['a', 'b', 'c']}, min_properties=1, ) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ Model(), ]) def test_object_max_properties_invalid_schema(self, value): schema = Schema('object', max_properties=-1) with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [ Model({'a': 1}), Model({ 'a': 1, 'b': 2 }), Model({ 'a': 1, 'b': 2, 'c': 3 }) ]) def test_object_max_properties_invalid(self, value): schema = Schema( 'object', properties={k: Schema('number') for k in ['a', 'b', 'c']}, max_properties=0, ) with pytest.raises(InvalidSchemaValue): schema.validate(value) @pytest.mark.parametrize('value', [ Model({'a': 1}), Model({ 'a': 1, 'b': 2 }), Model({ 'a': 1, 'b': 2, 'c': 3 }) ]) def test_object_max_properties(self, value): schema = Schema( 'object', properties={k: Schema('number') for k in ['a', 'b', 'c']}, max_properties=3, ) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ Model({'additional': 1}), ]) def test_object_additional_propetries(self, value): schema = Schema('object') schema.validate(value) @pytest.mark.parametrize('value', [ Model({'additional': 1}), ]) def test_object_additional_propetries_false(self, value): schema = Schema('object', additional_properties=False) with pytest.raises(UndefinedSchemaProperty): schema.validate(value) @pytest.mark.parametrize('value', [ Model({'additional': 1}), ]) def test_object_additional_propetries_object(self, value): additional_properties = Schema('integer') schema = Schema('object', additional_properties=additional_properties) schema.validate(value) @pytest.mark.parametrize('value', [ [], ]) def test_list_min_items_invalid_schema(self, value): schema = Schema( 'array', items=Schema('number'), min_items=-1, ) with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [[], [1], [1, 2]]) def test_list_min_items_invalid(self, value): schema = Schema( 'array', items=Schema('number'), min_items=3, ) with pytest.raises(Exception): schema.validate(value) @pytest.mark.parametrize('value', [[], [1], [1, 2]]) def test_list_min_items(self, value): schema = Schema( 'array', items=Schema('number'), min_items=0, ) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ [], ]) def test_list_max_items_invalid_schema(self, value): schema = Schema( 'array', items=Schema('number'), max_items=-1, ) with pytest.raises(OpenAPISchemaError): schema.validate(value) @pytest.mark.parametrize('value', [[1, 2], [2, 3, 4]]) def test_list_max_items_invalid(self, value): schema = Schema( 'array', items=Schema('number'), max_items=1, ) with pytest.raises(Exception): schema.validate(value) @pytest.mark.parametrize('value', [[1, 2, 1], [2, 2]]) def test_list_unique_items_invalid(self, value): schema = Schema( 'array', items=Schema('number'), unique_items=True, ) with pytest.raises(Exception): schema.validate(value) @pytest.mark.parametrize('value', [ Model({ 'someint': 123, }), Model({ 'somestr': u('content'), }), Model({ 'somestr': u('content'), 'someint': 123, }), ]) def test_object_with_properties(self, value): schema = Schema( 'object', properties={ 'somestr': Schema('string'), 'someint': Schema('integer'), }, ) result = schema.validate(value) assert result == value @pytest.mark.parametrize('value', [ Model({ 'somestr': Model(), 'someint': 123, }), Model({ 'somestr': {}, 'someint': 123, }), Model({ 'somestr': ['content1', 'content2'], 'someint': 123, }), Model({ 'somestr': 123, 'someint': 123, }), Model({ 'somestr': 'content', 'someint': 123, 'not_in_scheme_prop': 123, }), ]) def test_object_with_invalid_properties(self, value): schema = Schema( 'object', properties={ 'somestr': Schema('string'), 'someint': Schema('integer'), }, additional_properties=False, ) with pytest.raises(Exception): schema.validate(value)
class TestSchemaValidate: @pytest.fixture def validator_factory(self): def create_validator(schema): format_checker = build_format_checker() return SchemaUnmarshallersFactory( format_checker=format_checker).create(schema) return create_validator @pytest.mark.parametrize( "schema_type", [ "boolean", "array", "integer", "number", "string", ], ) def test_null(self, schema_type, validator_factory): schema = { "type": schema_type, } spec = Spec.from_dict(schema) value = None with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "schema_type", [ "boolean", "array", "integer", "number", "string", ], ) def test_nullable(self, schema_type, validator_factory): schema = { "type": schema_type, "nullable": True, } spec = Spec.from_dict(schema) value = None result = validator_factory(spec).validate(value) assert result is None def test_string_format_custom_missing(self, validator_factory): custom_format = "custom" schema = { "type": "string", "format": custom_format, } spec = Spec.from_dict(schema) value = "x" with pytest.raises(FormatterNotFoundError): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [False, True]) def test_boolean(self, value, validator_factory): schema = { "type": "boolean", } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [1, 3.14, "true", [True, False]]) def test_boolean_invalid(self, value, validator_factory): schema = { "type": "boolean", } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [(1, 2)]) def test_array_no_schema(self, value, validator_factory): schema = { "type": "array", } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [[1, 2]]) def test_array(self, value, validator_factory): schema = { "type": "array", "items": { "type": "integer", }, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [False, 1, 3.14, "true", (3, 4)]) def test_array_invalid(self, value, validator_factory): schema = { "type": "array", } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [1, 3]) def test_integer(self, value, validator_factory): schema = { "type": "integer", } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [False, 3.14, "true", [1, 2]]) def test_integer_invalid(self, value, validator_factory): schema = { "type": "integer", } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [0, 1, 2]) def test_integer_minimum_invalid(self, value, validator_factory): schema = { "type": "integer", "minimum": 3, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [4, 5, 6]) def test_integer_minimum(self, value, validator_factory): schema = { "type": "integer", "minimum": 3, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [4, 5, 6]) def test_integer_maximum_invalid(self, value, validator_factory): schema = { "type": "integer", "maximum": 3, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [0, 1, 2]) def test_integer_maximum(self, value, validator_factory): schema = { "type": "integer", "maximum": 3, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [1, 2, 4]) def test_integer_multiple_of_invalid(self, value, validator_factory): schema = { "type": "integer", "multipleOf": 3, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [3, 6, 18]) def test_integer_multiple_of(self, value, validator_factory): schema = { "type": "integer", "multipleOf": 3, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [1, 3.14]) def test_number(self, value, validator_factory): schema = { "type": "number", } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [False, "true", [1, 3]]) def test_number_invalid(self, value, validator_factory): schema = { "type": "number", } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [0, 1, 2]) def test_number_minimum_invalid(self, value, validator_factory): schema = { "type": "number", "minimum": 3, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [3, 4, 5]) def test_number_minimum(self, value, validator_factory): schema = { "type": "number", "minimum": 3, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [1, 2, 3]) def test_number_exclusive_minimum_invalid(self, value, validator_factory): schema = { "type": "number", "minimum": 3, "exclusiveMinimum": True, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [4, 5, 6]) def test_number_exclusive_minimum(self, value, validator_factory): schema = { "type": "number", "minimum": 3, "exclusiveMinimum": True, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [4, 5, 6]) def test_number_maximum_invalid(self, value, validator_factory): schema = { "type": "number", "maximum": 3, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [1, 2, 3]) def test_number_maximum(self, value, validator_factory): schema = { "type": "number", "maximum": 3, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [3, 4, 5]) def test_number_exclusive_maximum_invalid(self, value, validator_factory): schema = { "type": "number", "maximum": 3, "exclusiveMaximum": True, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [0, 1, 2]) def test_number_exclusive_maximum(self, value, validator_factory): schema = { "type": "number", "maximum": 3, "exclusiveMaximum": True, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [1, 2, 4]) def test_number_multiple_of_invalid(self, value, validator_factory): schema = { "type": "number", "multipleOf": 3, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [3, 6, 18]) def test_number_multiple_of(self, value, validator_factory): schema = { "type": "number", "multipleOf": 3, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", ["true", b"test"]) def test_string(self, value, validator_factory): schema = { "type": "string", } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [False, 1, 3.14, [1, 3]]) def test_string_invalid(self, value, validator_factory): schema = { "type": "string", } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ b"true", "test", False, 1, 3.14, [1, 3], datetime.datetime(1989, 1, 2), ], ) def test_string_format_date_invalid(self, value, validator_factory): schema = { "type": "string", "format": "date", } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ "1989-01-02", "2018-01-02", ], ) def test_string_format_date(self, value, validator_factory): schema = { "type": "string", "format": "date", } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize( "value", [ "12345678-1234-5678-1234-567812345678", ], ) def test_string_format_uuid(self, value, validator_factory): schema = { "type": "string", "format": "uuid", } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize( "value", [ b"true", "true", False, 1, 3.14, [1, 3], datetime.date(2018, 1, 2), datetime.datetime(2018, 1, 2, 23, 59, 59), ], ) def test_string_format_uuid_invalid(self, value, validator_factory): schema = { "type": "string", "format": "uuid", } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ b"true", "true", False, 1, 3.14, [1, 3], "1989-01-02", ], ) def test_string_format_datetime_invalid(self, value, validator_factory): schema = { "type": "string", "format": "date-time", } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ "1989-01-02T00:00:00Z", "2018-01-02T23:59:59Z", ], ) @mock.patch("openapi_schema_validator._format." "DATETIME_HAS_STRICT_RFC3339", True) @mock.patch("openapi_schema_validator._format." "DATETIME_HAS_ISODATE", False) def test_string_format_datetime_strict_rfc3339(self, value, validator_factory): schema = { "type": "string", "format": "date-time", } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize( "value", [ "1989-01-02T00:00:00Z", "2018-01-02T23:59:59Z", ], ) @mock.patch( "openapi_schema_validator._format." "DATETIME_HAS_STRICT_RFC3339", False, ) @mock.patch("openapi_schema_validator._format." "DATETIME_HAS_ISODATE", True) def test_string_format_datetime_isodate(self, value, validator_factory): schema = { "type": "string", "format": "date-time", } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize( "value", [ "true", False, 1, 3.14, [1, 3], "1989-01-02", "1989-01-02T00:00:00Z", ], ) def test_string_format_binary_invalid(self, value, validator_factory): schema = { "type": "string", "format": "binary", } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ b"stream", b"text", ], ) def test_string_format_binary(self, value, validator_factory): schema = { "type": "string", "format": "binary", } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize( "value", [ b"dGVzdA==", "dGVzdA==", ], ) def test_string_format_byte(self, value, validator_factory): schema = { "type": "string", "format": "byte", } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize( "value", [ "tsssst", b"tsssst", b"tesddddsdsdst", ], ) def test_string_format_byte_invalid(self, value, validator_factory): schema = { "type": "string", "format": "byte", } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ "test", b"stream", datetime.date(1989, 1, 2), datetime.datetime(1989, 1, 2, 0, 0, 0), ], ) def test_string_format_unknown(self, value, validator_factory): unknown_format = "unknown" schema = { "type": "string", "format": unknown_format, } spec = Spec.from_dict(schema) with pytest.raises(FormatterNotFoundError): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", ["", "a", "ab"]) def test_string_min_length_invalid(self, value, validator_factory): schema = { "type": "string", "minLength": 3, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", ["abc", "abcd"]) def test_string_min_length(self, value, validator_factory): schema = { "type": "string", "minLength": 3, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize( "value", [ "", ], ) def test_string_max_length_invalid_schema(self, value, validator_factory): schema = { "type": "string", "maxLength": -1, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", ["ab", "abc"]) def test_string_max_length_invalid(self, value, validator_factory): schema = { "type": "string", "maxLength": 1, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", ["", "a"]) def test_string_max_length(self, value, validator_factory): schema = { "type": "string", "maxLength": 1, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", ["foo", "bar"]) def test_string_pattern_invalid(self, value, validator_factory): schema = { "type": "string", "pattern": "baz", } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", ["bar", "foobar"]) def test_string_pattern(self, value, validator_factory): schema = { "type": "string", "pattern": "bar", } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", ["true", False, 1, 3.14, [1, 3]]) def test_object_not_an_object(self, value, validator_factory): schema = { "type": "object", } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ Model(), ], ) def test_object_multiple_one_of(self, value, validator_factory): one_of = [ { "type": "object", }, { "type": "object", }, ] schema = { "type": "object", "oneOf": one_of, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ {}, ], ) def test_object_different_type_one_of(self, value, validator_factory): one_of = [ { "type": "integer", }, { "type": "string", }, ] schema = { "type": "object", "oneOf": one_of, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ {}, ], ) def test_object_no_one_of(self, value, validator_factory): one_of = [ { "type": "object", "required": [ "test1", ], "properties": { "test1": { "type": "string", }, }, }, { "type": "object", "required": [ "test2", ], "properties": { "test2": { "type": "string", }, }, }, ] schema = { "type": "object", "oneOf": one_of, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ { "foo": "FOO", }, { "foo": "FOO", "bar": "BAR", }, ], ) def test_unambiguous_one_of(self, value, validator_factory): one_of = [ { "type": "object", "required": [ "foo", ], "properties": { "foo": { "type": "string", }, }, "additionalProperties": False, }, { "type": "object", "required": ["foo", "bar"], "properties": { "foo": { "type": "string", }, "bar": { "type": "string", }, }, "additionalProperties": False, }, ] schema = { "type": "object", "oneOf": one_of, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize( "value", [ {}, ], ) def test_object_default_property(self, value, validator_factory): schema = { "type": "object", "default": "value1", } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize( "value", [ {}, ], ) def test_object_min_properties_invalid_schema(self, value, validator_factory): schema = { "type": "object", "minProperties": 2, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ { "a": 1 }, { "a": 1, "b": 2 }, { "a": 1, "b": 2, "c": 3 }, ], ) def test_object_min_properties_invalid(self, value, validator_factory): schema = { "type": "object", "properties": {k: { "type": "number" } for k in ["a", "b", "c"]}, "minProperties": 4, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ { "a": 1 }, { "a": 1, "b": 2 }, { "a": 1, "b": 2, "c": 3 }, ], ) def test_object_min_properties(self, value, validator_factory): schema = { "type": "object", "properties": {k: { "type": "number" } for k in ["a", "b", "c"]}, "minProperties": 1, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize( "value", [ {}, ], ) def test_object_max_properties_invalid_schema(self, value, validator_factory): schema = { "type": "object", "maxProperties": -1, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ { "a": 1 }, { "a": 1, "b": 2 }, { "a": 1, "b": 2, "c": 3 }, ], ) def test_object_max_properties_invalid(self, value, validator_factory): schema = { "type": "object", "properties": {k: { "type": "number" } for k in ["a", "b", "c"]}, "maxProperties": 0, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ { "a": 1 }, { "a": 1, "b": 2 }, { "a": 1, "b": 2, "c": 3 }, ], ) def test_object_max_properties(self, value, validator_factory): schema = { "type": "object", "properties": {k: { "type": "number" } for k in ["a", "b", "c"]}, "maxProperties": 3, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize( "value", [ { "additional": 1 }, ], ) def test_object_additional_properties(self, value, validator_factory): schema = { "type": "object", } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize( "value", [ { "additional": 1 }, ], ) def test_object_additional_properties_false(self, value, validator_factory): schema = { "type": "object", "additionalProperties": False, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ { "additional": 1 }, ], ) def test_object_additional_properties_object(self, value, validator_factory): additional_properties = { "type": "integer", } schema = { "type": "object", "additionalProperties": additional_properties, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize("value", [[], [1], [1, 2]]) def test_list_min_items_invalid(self, value, validator_factory): schema = { "type": "array", "items": { "type": "number", }, "minItems": 3, } spec = Spec.from_dict(schema) with pytest.raises(Exception): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [[], [1], [1, 2]]) def test_list_min_items(self, value, validator_factory): schema = { "type": "array", "items": { "type": "number", }, "minItems": 0, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize( "value", [ [], ], ) def test_list_max_items_invalid_schema(self, value, validator_factory): schema = { "type": "array", "items": { "type": "number", }, "maxItems": -1, } spec = Spec.from_dict(schema) with pytest.raises(InvalidSchemaValue): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [[1, 2], [2, 3, 4]]) def test_list_max_items_invalid(self, value, validator_factory): schema = { "type": "array", "items": { "type": "number", }, "maxItems": 1, } spec = Spec.from_dict(schema) with pytest.raises(Exception): validator_factory(spec).validate(value) @pytest.mark.parametrize("value", [[1, 2, 1], [2, 2]]) def test_list_unique_items_invalid(self, value, validator_factory): schema = { "type": "array", "items": { "type": "number", }, "uniqueItems": True, } spec = Spec.from_dict(schema) with pytest.raises(Exception): validator_factory(spec).validate(value) @pytest.mark.parametrize( "value", [ { "someint": 123, }, { "somestr": "content", }, { "somestr": "content", "someint": 123, }, ], ) def test_object_with_properties(self, value, validator_factory): schema = { "type": "object", "properties": { "somestr": { "type": "string", }, "someint": { "type": "integer", }, }, } spec = Spec.from_dict(schema) result = validator_factory(spec).validate(value) assert result is None @pytest.mark.parametrize( "value", [ { "somestr": {}, "someint": 123, }, { "somestr": ["content1", "content2"], "someint": 123, }, { "somestr": 123, "someint": 123, }, { "somestr": "content", "someint": 123, "not_in_scheme_prop": 123, }, ], ) def test_object_with_invalid_properties(self, value, validator_factory): schema = { "type": "object", "properties": { "somestr": { "type": "string", }, "someint": { "type": "integer", }, }, "additionalProperties": False, } spec = Spec.from_dict(schema) with pytest.raises(Exception): validator_factory(spec).validate(value)