def test_ensure_dict(self): schema = EmptySchema() assert schema.spec() == {} # noinspection PyProtectedMember assert schema._ensure_dict('name') == {} assert schema.spec() == {'name': {}}
def test_field_set_no_name(self): schema = EmptySchema() def tag(): # noinspection PyProtectedMember schema._set('value') tag() assert schema.spec() == {'tag': 'value'}
def test_to_spec(self): assert Schema._to_spec(3) == 3 assert Schema._to_spec([1, 2, 3]) == [1, 2, 3] assert Schema._to_spec((1, 2, 3)) == [1, 2, 3] assert Schema._to_spec({'key': 'value'}) == {'key': 'value'} assert Schema._to_spec(EmptySchema()) == {} assert Schema._to_spec([IntegerSchema(), BooleanSchema()]) == [{ 'type': 'integer' }, { 'type': 'boolean' }] assert Schema._to_spec((IntegerSchema(), BooleanSchema())) == [{ 'type': 'integer' }, { 'type': 'boolean' }] assert Schema._to_spec({ 'one': IntegerSchema(), 'two': BooleanSchema() }) == { 'one': { 'type': 'integer' }, 'two': { 'type': 'boolean' } }
def test_ref_resolution(self): resolver = { 'http://example.com': EmptySchema().add_definition('port', _port_schema).spec() } validator = SchemaValidator( RefSchema('http://example.com#/definitions/port')) with FakeSchemaReader(resolver) as fsr: assert validator.validate(7) is True assert validator.validate(0) is False assert validator.error == _em('minimum', '0 is less than or equal to 1.') assert fsr.call_count == 1
_em('type', "it is not one of ['string', 'object']")), # Enum constraint tests. (1, dict(enum=[1, 2, 3]), None), (2, dict(enum=[1, 2, 3]), None), (3, dict(enum=[1, 2, 3]), None), (None, dict(enum=[1, None, 3]), None), (None, dict(enum=[1, 'null', 3]), None), ('null', dict(enum=[1, None, 3]), None), ('null', dict(enum=[1, 'null', 3]), None), (0, dict(enum=[1, 2, 3]), _em('enum', 'it is not one of [1, 2, 3].')), (None, dict(enum=[1, 2, 3]), _em('enum', 'it is not one of [1, 2, 3].')), ('null', dict(enum=[1, 2, 3]), _em('enum', 'it is not one of [1, 2, 3].')), # const constraint tests. (7, EmptySchema().const(7), None), (8, EmptySchema().const(7), _em('const', 'the value 8 is not 7.')), # String constraints tests. ('string', StringSchema(min_length=6), None), ('bobby', StringSchema(min_length=6), _em('minLength', 'the string is shorter than 6.')), ('string', StringSchema(max_length=6), None), ('string2', StringSchema(max_length=6), _em('maxLength', 'the string is longer than 6.')), ('good', StringSchema(pattern=r'^go'), None), ('bad', StringSchema(pattern=r'^go'), _em('pattern', "it does not match the '^go' pattern.")), ('1.2.3.4', StringSchema(str_format='ipv4'), None), ('1.2.3', StringSchema(str_format='ipv4'), _em('format', 'it does not follow the ipv4 format.')),
def test_as_json_text(self): schema = EmptySchema() assert schema.as_json_text() == "{}"
def test_field_set_with_name(self): schema = EmptySchema() # noinspection PyProtectedMember schema._set('value', name='name') assert schema.spec() == {'name': 'value'}
""" This file contains all the unit tests for our framework's schema creation support. """ from builder.schema import EmptySchema, BooleanSchema, IntegerSchema, NumberSchema, StringSchema, Schema, \ ObjectSchema, ArraySchema, AllOfSchema, AnyOfSchema, OneOfSchema, NotSchema, RefSchema _creation_test_cases = [ # Basic schema stuff. (EmptySchema(), {}), (EmptySchema().enum(1, 2, 3), { 'enum': [1, 2, 3] }), (EmptySchema().const('word'), { 'const': 'word' }), (EmptySchema().if_then_else(BooleanSchema(), then_schema=IntegerSchema(), else_schema=StringSchema()), { 'if': { "type": "boolean" }, 'then': { "type": "integer" }, 'else': { "type": "string" }, }), (EmptySchema().ref('#/path/to/thing'), { '$ref': '#/path/to/thing' }),