def test_bad_type(): sample = {'id': '3'} with pytest.raises(E.BadType) as ei: normalize_dict(id_int, sample) assert ei.value.value == '3' assert ei.value.type_ == 'integer' assert ei.value.stack == ('id', )
def test_nested_error(): schema = {'nested': S.Dict(schema={'num': S.Integer()})} with pytest.raises(E.BadType) as ei: normalize_dict(schema, {'nested': {'num': 'three!'}}) assert ei.value.value == 'three!' assert ei.value.type_ == 'integer' assert ei.value.stack == ('nested', 'num')
def test_allow_unknown(): assert normalize_dict(id_int, { 'id': 3, 'foo': 'bar' }, allow_unknown=True) == { 'id': 3, 'foo': 'bar' }
def test_validator_error(): def val(field, value, error): error(field, "heyo") schema = {'key': {'validator': val}} with pytest.raises(E.CustomValidatorError) as ei: assert normalize_dict(schema, {'key': 'hi'}) == {'key': 'hi'} assert ei.value.field == 'key' assert ei.value.msg == 'heyo'
def test_validator(): called = [] def val(field, value, error): called.append((field, value, error)) schema = {'key': {'validator': val}} assert normalize_dict(schema, {'key': 'hi'}) == {'key': 'hi'} assert len(called) == 1 assert called[0][0] == 'key' assert called[0][1] == 'hi'
def test_dict_of_int(): sample = {'id': 3} assert normalize_dict(id_int, sample) == sample
def test_disallow_unknown(): with pytest.raises(E.UnknownFields) as ei: normalize_dict(id_int, {'id': 3, 'foo': 'bar'}, allow_unknown=False)
def test_not_required(): assert normalize_dict({'id': S.Integer(required=False)}, {}) == {}
def test_field_not_found(): with pytest.raises(E.DictFieldNotFound) as ei: normalize_dict({'id': S.Integer(required=True)}, {}) assert ei.value.key == 'id' assert ei.value.value == {} assert ei.value.stack == ()
def test_default(): old_dict = {} schema = {'num': S.Integer(default=0)} new_dict = normalize_dict(schema, old_dict) assert old_dict == {} assert new_dict == {'num': 0}