def test_subschemas(): schema1 = Schema({'foo': str, str: int}) schema2 = Schema( {'key1': schema1, 'key2': schema1, str: schema1}) assert schema2.validates( {'key1': {'foo': 'bar'}, 'key2': {'foo': 'qux', 'baz': 43}, 'whatever': {'foo': 'doo', 'fsck': 22, 'tsk': 2992}}) assert not schema2.validates( {'key1': {'doo': 'bar'}, 'key2': {'foo': 'qux', 'baz': 43}, 'whatever': {'foo': 'doo', 'fsck': 22, 'tsk': 2992}}) assert not schema2.validates( {'key1': {'doo': 'bar'}, 'key2': {'foo': 'qux', 12: 43}, 'whatever': {'foo': 'doo', 'fsck': 22, 'tsk': 2992}}) assert not schema2.validates( {'key1': {'foo': 'bar'}, 'key2': {'foo': 'qux', 'baz': 'derp'}, 'whatever': {'foo': 'doo', 'fsck': 22, 'tsk': 2992}}) assert not schema2.validates( {'key1': {'foo': 'bar'}, 'key2': {'foo': 'qux', 'baz': 'derp'}, 12: {'foo': 'doo', 'fsck': 22, 'tsk': 2992}}) assert not schema2.validates( {'key1': {'foo': 'bar'}, 'key2': {'foo': 'qux', 'baz': 'derp'}, 'whatever': {}})
def test_schema_with_additional_validators(): def total_greater_than_12(value): """foo + bar > 12.""" return value['foo'] + value['bar'] > 12 schema = Schema({ 'foo': int, 'bar': int}, additional_validators=(total_greater_than_12,)) assert schema.validates({'foo': 7, 'bar': 7}) with pytest.raises(NotValid) as ctx: schema.validate({'foo': 5, 'bar': 7}) assert ctx.value.args[0].endswith( "not validated by additional validator 'foo + bar > 12.'")
def test_list_not_found(): schema = Schema(['1', '2', '3']) assert not schema.validates('12')
def test_list_data_multiple_types(): schema = Schema([str, int]) assert schema.validates(['1', '2', 3])
def test_list_data_wrong_type(): schema = Schema([str]) assert not schema.validates(['1', '2', 3])
def test_list_data(): schema = Schema([str]) assert schema.validates(['1', '2', '3'])