Exemplo n.º 1
0
def test_fails_with_missing_key():
    schema = Schema({ 'a': ok })

    try:
        schema({})
        raise AssertionError("Expected error.")
    except Invalid as e:
        assert e.path == ['a']
        assert "required" in e.message
Exemplo n.º 2
0
def test_entire_validator_gets_all_data():
    called = []
    def entire(data):
        assert data['a'] == 'a'
        assert data['b'] == 'b'
        called.append(True)
    schema = Schema({ 'a': ok, 'b': ok }, entire=entire)

    schema({ 'a': 'a', 'b': 'b' })
    assert called
Exemplo n.º 3
0
def test_fails_missing_custom_message():
    schema = Schema({ 'a': ok }, required_error="Bla")

    try:
        schema({})
        raise AssertionError("Expected error.")
    except Invalid as e:
        assert len(e) == 1
        assert e.path == ['a']
        assert e.message == "Bla"
Exemplo n.º 4
0
def test_fails_unknown_keys():
    schema = Schema({
        'a': ok,
    }, extra_keys=Schema.REJECT)

    try:
        schema({ 'a': 123, 'b': 456 })
        raise AssertionError("Expected error.")
    except Invalid as e:
        assert len(e) == 1
        assert e.path == ['b']
Exemplo n.º 5
0
def test_entire_validator_raises_invalid():
    def entire(data):
        raise Error("Nope")
    schema = Schema({ 'a': ok }, entire=entire)

    try:
        schema({ 'a': 123 })
        raise AssertionError("Expected error.")
    except Invalid as e:
        assert len(e) == 1
        assert e.message == "Nope"
Exemplo n.º 6
0
def test_fails_with_multiple_missing_keys():
    schema = Schema({ 'a': ok, 'b': ok, 'c': ok })

    try:
        schema({})
        raise AssertionError("Expected error.")
    except Invalid as e:
        assert len(e) == 3
        assert ['a'] in e.paths
        assert ['b'] in e.paths
        assert ['c'] in e.paths
        for message in e.messages:
            assert "required" in message
Exemplo n.º 7
0
def test_callable_error():
    def raiser(x):
        raise Error("Nope")
    schema = Schema({
        'test': raiser,
    })

    try:
        schema({ 'test': "abc" })
        raise AssertionError("Expected error.")
    except Invalid as e:
        assert len(e) == 1
        assert e.path == ['test']
        assert e.message == "Nope"
Exemplo n.º 8
0
def test_entire_validator_raises_with_failures():
    def entire(data):
        raise Error("Entire")
    def raiser(x):
        raise Error("Nope")
    schema = Schema({ 'a': raiser }, entire=entire)

    try:
        schema({ 'a': 123 })
        raise AssertionError("Expected error.")
    except Invalid as e:
        assert len(e) == 2
        assert "Entire" in e.messages
        assert "Nope" in e.messages
Exemplo n.º 9
0
def test_fails_missing_nested_schema():
    schema = Schema({
        'nested': Schema({
            'a': ok,
            'b': ok,
            'c': ok,
        }),
    })

    try:
        schema({})
        raise AssertionError("Expected error.")
    except Invalid as e:
        assert len(e) == 1
        assert e.path == ['nested']
Exemplo n.º 10
0
def test_nested_error():
    def raiser(x):
        raise Error("Nope")
    schema = Schema({
        'test': Schema({
            'inner': raiser,
        })
    })

    try:
        schema({ 'test': { 'inner': 123 } })
        raise AssertionError("Expected error.")
    except Invalid as e:
        assert len(e) == 1
        assert e.path == ['test', 'inner']
        assert e.message == "Nope"
Exemplo n.º 11
0
def test_entire_validator_mutates_data():
    def entire(data):
        data['a'], data['b'] = data['b'], data['a']
        return data
    schema = Schema({ 'a': ok, 'b': ok }, entire=entire)

    assert schema({ 'a': 'b', 'b': 'a' }) == { 'a': 'a', 'b': 'b' }
Exemplo n.º 12
0
def test_optional_keys_accepted():
    schema = Schema({
        Optional('b'): ok,
    })

    result = schema({ 'b': 456 })
    assert result['b'] == 456
Exemplo n.º 13
0
def test_callable_invalid():
    def raiser(x):
        first = Error("First")
        second = Error("Second")
        raise Invalid([first, second])
    schema = Schema({
        'test': raiser,
    })

    try:
        schema({ 'test': "abc" })
        raise AssertionError("Expected error.")
    except Invalid as e:
        assert len(e) == 2
        assert "First" in e.messages
        assert "Second" in e.messages
Exemplo n.º 14
0
def test_nested_valid():
    schema = Schema({
        'test': Schema({
            'inner': lambda x: x + 1,
        })
    })
    assert schema({ 'test': { 'inner': 1 } }) == { 'test': { 'inner': 2 } }
Exemplo n.º 15
0
def test_nested_default_value():
    schema = Schema({
        Default('test', {}): Schema({
            Default('inner', 123): ok,
        })
    })

    assert schema({}) == { 'test': { 'inner': 123 } }
Exemplo n.º 16
0
def test_optional_keys_missing():
    schema = Schema({
        'a': ok,
        Optional('b'): ok,
    })

    result = schema({ 'a': 123 })
    assert result['a'] == 123
    assert 'b' not in result
Exemplo n.º 17
0
def test_accepts_unknown_keys():
    schema = Schema({
        'a': ok,
    }, extra_keys=Schema.ACCEPT)

    result = schema({ 'a': 123, 'b': 456 })
    assert 'a' in result
    assert 'b' in result
    assert result['b'] == 456
Exemplo n.º 18
0
def test_entire_validator_called_with_failures():
    called = []
    def raiser(x):
        raise Error("Nope")
    def entire(data):
        called.append(1)
        assert data['a'] == 'a'
        # b failed before, so it shouldn't be included
        assert 'b' not in data
    schema = Schema({
        'a': ok,
        'b': raiser,
    }, entire=entire)

    try:
        schema({
            'a': 'a',
            'b': 123,
        })
        raise AssertionError("Expected error.")
    except Invalid as e:
        assert called
Exemplo n.º 19
0
def test_nested_multiple_errors():
    def raiser(x):
        raise Error("Nope")
    schema = Schema({
        'will_fail': raiser,
        'nested': Schema({
            'inner': raiser,
            'another': ok,
        })
    })

    try:
        schema({
            'will_fail': "Hello",
            'nested': {
                'inner': 123
            }
        })
        raise AssertionError("Expected error.")
    except Invalid as e:
        assert len(e) == 3
        assert ['will_fail'] in e.paths
        assert ['nested', 'inner'] in e.paths
        assert ['nested', 'another'] in e.paths
Exemplo n.º 20
0
def test_default_value():
    schema = Schema({
        Default('test', 123): ok,
    })

    assert schema({}) == { 'test': 123 }
Exemplo n.º 21
0
def test_empty_schema_invalid_value(value):
    schema = Schema({})
    with pytest.raises(Invalid):
        schema(value)
Exemplo n.º 22
0
def test_empty_schema_valid_value():
    schema = Schema({})
    assert schema({}) == {}
Exemplo n.º 23
0
def test_discards_unknown_keys():
    schema = Schema({ 'a': ok }, extra_keys=Schema.IGNORE)

    result = schema({ 'a': 123, 'b': 456 })
    assert 'a' in result
    assert 'b' not in result
Exemplo n.º 24
0
def test_callable_transforms_value():
    schema = Schema({
        'test': lambda x: x + 1
    })

    assert schema({ 'test': 1 }) == { 'test': 2 }