def test_dictionary_optional_null_value(): schema = Schema({ 'key': str, Optional('key2', default='val2', null_values=(None,)): Or( str, None)}) assert ( schema.validate({'key': 'val', 'key2': None}) == {'key': 'val', 'key2': 'val2'})
def test_callable_gives_sensible_error(): def less_than_two(value): return value < 2 schema = Schema(less_than_two) with pytest.raises(NotValid) as ctx: schema.validate(12) assert ctx.value.args == ("12 not validated by 'less_than_two'",)
def test_issue_9_prioritized_key_comparison_in_dicts(): # http://stackoverflow.com/questions/14588098/docopt-schema-validation schema = Schema( {'ID': Convert(int), # , error='ID should be an int'), 'FILE': Or(None, Convert(open)), # , error='FILE not opened')), str: object}) # all type keys are optional data = {'ID': 10, 'FILE': None, 'other': 'other', 'other2': 'other2'} assert schema.validate(data) == data data = {'ID': 10, 'FILE': None} assert schema.validate(data) == data
def test_regression_validating_twice_works(): schema = Schema({ 'key': str, Optional('key2', default='val2', null_values=(None,)): Or( str, None)}) assert ( schema.validate({'key': 'val', 'key2': 'other_val'}) == {'key': 'val', 'key2': 'other_val'}) assert schema.validate( {'key': 'new_val'}) == {'key': 'new_val', 'key2': 'val2'}
def test_callable_gives_readable_error(): def less_than_two(value): """Must be less than two.""" return value < 2 schema = Schema(less_than_two) with pytest.raises(NotValid) as ctx: schema.validate(12) assert ctx.value.args == ( "12 not validated by 'Must be less than two.'",)
def test_dont_care_values_in_dict(): schema = Schema( {'foo': int, 'bar': str, str: object}) assert ( schema.validate({ 'foo': 12, 'bar': 'bar', 'qux': 'baz', 'fnord': [1, 2, 'donkey kong']}) == { 'foo': 12, 'bar': 'bar', 'qux': 'baz', 'fnord': [1, 2, 'donkey kong']})
def to_val(teleport_schema): """Convert a parsed teleport schema to a val schema.""" translated = _translate(teleport_schema) if isinstance(translated, BaseSchema): return translated return Schema(translated)
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_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_dictionary_optional_missing(): schema = Schema({'key': str, Optional('key2', default='val2'): str}) assert schema.validate( {'key': 'val'}) == {'key': 'val', 'key2': 'val2'}
def test_dictionary_optional(): schema = Schema({'key': str, Optional('key2'): str}) assert schema.validate({'key': 'val'}) == {'key': 'val'}
def test_dictionary_not_a_dict(): schema = Schema({'key': str}) with pytest.raises(NotValid): schema.validate('foo')
def test_list_data_multiple_types(): schema = Schema([str, int]) assert schema.validates(['1', '2', 3])
def test_issue_9_prioritized_key_comparison(): schema = Schema({'key': 42, object: 42}) assert schema.validate({'key': 42, 777: 42}) == {'key': 42, 777: 42}
def test_callable(): schema = Schema(lambda x: x < 2) assert schema.validate(1) == 1
def test_dictionary(): schema = Schema({'key': str}) assert schema.validate({'key': 'val'}) == {'key': 'val'}
def test_list_data(): schema = Schema([str]) assert schema.validates(['1', '2', '3'])
def test_dictionary_missing_key(): schema = Schema({'key': str, 'key2': str}) with pytest.raises(NotValid): schema.validate({'key': 'val'})
def test_dictionary_optional_not_missing(): schema = Schema({'key': str, Optional( 'key2', default='val2'): Or(str, None)}) assert schema.validate({ 'key': 'val', 'key2': None}) == {'key': 'val', 'key2': None}
def test_callable_exception(): schema = Schema(lambda x: x + 2) with pytest.raises(NotValid): schema.validate("foo")
def test_identity(): schema = Schema('test') assert schema.validate('test') == 'test'
def test_list_not_found(): schema = Schema(['1', '2', '3']) assert not schema.validates('12')
def test_list_data_wrong_type(): schema = Schema([str]) assert not schema.validates(['1', '2', 3])
def test_validate_object(): schema = Schema({object: str}) assert schema.validate({42: 'str'}) == {42: 'str'} with pytest.raises(NotValid): schema.validate({42: 777})
def test_non_identity(): schema = Schema('test') with pytest.raises(NotValid): schema.validate('bar')
def test_dictionary_wrong_key(): schema = Schema({'key': str}) with pytest.raises(NotValid): schema.validate({'not_key': 'val'})
def test_type_check(): schema = Schema(str) assert schema.validate('test'), 'test'
def test_failing_type_check(): schema = Schema(int) with pytest.raises(NotValid): schema.validate('test')
def test_dictionary_leftover_key(): schema = Schema({'key': str}) with pytest.raises(NotValid): schema.validate({'key': 'val', 'key2': 'val2'})