Exemple #1
0
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', )
Exemple #2
0
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')
Exemple #3
0
def test_allow_unknown():
    assert normalize_dict(id_int, {
        'id': 3,
        'foo': 'bar'
    }, allow_unknown=True) == {
        'id': 3,
        'foo': 'bar'
    }
Exemple #4
0
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'
Exemple #5
0
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'
Exemple #6
0
def test_dict_of_int():
    sample = {'id': 3}
    assert normalize_dict(id_int, sample) == sample
Exemple #7
0
def test_disallow_unknown():
    with pytest.raises(E.UnknownFields) as ei:
        normalize_dict(id_int, {'id': 3, 'foo': 'bar'}, allow_unknown=False)
Exemple #8
0
def test_not_required():
    assert normalize_dict({'id': S.Integer(required=False)}, {}) == {}
Exemple #9
0
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 == ()
Exemple #10
0
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}