Beispiel #1
0
def test_fancy_validation_function():
    """Test that fails to validate with a real function."""
    def validation_function(value):
        if value == 1:
            raise errors.ValidationError(
                'Value not 1',
                value=value,
            )

    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
            validate=validation_function,
        ),
    }

    values = {
        'A_VAR': '1',
    }

    with raises(errors.ValidationError) as err:
        parse_dict(values, args)

    assert err.value.extra == {
        'value': 1,
    }
Beispiel #2
0
def test_custom_function_int_list():
    """Test that we can have more complex parsing functions."""
    def parse_list(value):
        return [
            int(each)
            for each in value.split(',')
        ]

    args = {
        'a_list': Var(
            use=parse_list,
            validate=(
                lambda parsed: isinstance(parsed, list),
                lambda parsed: all(isinstance(each, int) for each in parsed),
            ),
        ),
    }

    values = {
        'a_list': '1,2,3,4',
    }

    assert parse_dict(values, args) == {
        'a_list': [1, 2, 3, 4],
    }
Beispiel #3
0
def test_custom_function_int_list():
    """Test that we can have more complex parsing functions."""
    def parse_list(value):
        return [
            int(each)
            for each in value.split(',')
        ]

    args = {
        'a_list': Var(
            use=parse_list,
            validate=(
                lambda parsed: isinstance(parsed, list),
                lambda parsed: all(isinstance(each, int) for each in parsed),
            ),
        ),
    }

    values = {
        'a_list': '1,2,3,4',
    }

    assert parse_dict(values, args) == {
        'a_list': [1, 2, 3, 4],
    }
Beispiel #4
0
def test_err_msg():
    """Test that error messages bubble up, when you use them."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
            err_msg='A_VAR not valid',
        ),
    }

    values = {
        'A_VAR': 'abc',
    }

    with raises(errors.ParseError) as err:
        parse_dict(values, args)

    assert err.value.message == 'A_VAR not valid'
    assert repr(err.value) == "ParseError('A_VAR not valid',)"
Beispiel #5
0
def test_missing_value():
    """Test case that fails because of a missing value."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
            validate=lambda x: x == 0,
        ),
    }

    values = {}

    with raises(errors.ParseError) as err:
        parse_dict(values, args)

    assert err.value.extra == {
        'location': 'A_VAR',
    }
    assert str(err.value) == 'Required field "A_VAR" missing.'
Beispiel #6
0
def test_parse_fail():
    """Simple case that fails to parse."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
        ),
    }

    values = {
        'A_VAR': 'abc',
    }

    with raises(errors.ParseError) as err:
        parse_dict(values, args)

    assert err.value.extra == {
        'location': 'A_VAR',
        'value': 'abc',
    }
Beispiel #7
0
def test_validation_with_lambda_fail():
    """Simple case that fails to validate."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
            validate=lambda x: x == 0,
        ),
    }

    values = {
        'A_VAR': '1',
    }

    with raises(errors.ValidationError) as err:
        parse_dict(values, args)

    assert err.value.extra == {
        'value': 1,
        'location': 'A_VAR',
    }
Beispiel #8
0
def test_complex_defaulting():
    """Test that when defaulting, the functions are not used."""
    args = {
        'a_bool': Var(
            use=lambda x: x.lower() in {'1', 't', 'true'},
            validate=lambda x: isinstance(x, bool),
            default=False,
        ),
    }

    values = {}

    assert parse_dict(values, args) == {
        'a_bool': False,
    }
Beispiel #9
0
def test_default():
    """Test that setting a default works."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
            default=5,
        ),
    }

    values = {}

    assert parse_dict(values, args) == {
        'a_var': 5,
    }
Beispiel #10
0
def test_complex_defaulting():
    """Test that when defaulting, the functions are not used."""
    args = {
        'a_bool': Var(
            use=lambda x: x.lower() in {'1', 't', 'true'},
            validate=lambda x: isinstance(x, bool),
            default=False,
        ),
    }

    values = {}

    assert parse_dict(values, args) == {
        'a_bool': False,
    }
Beispiel #11
0
def test_default_name():
    """Test that using the default name works too."""
    args = {
        'a_var': Var(
            use=int,
        ),
    }

    values = {
        'a_var': '5',
    }

    assert parse_dict(values, args) == {
        'a_var': 5,
    }
Beispiel #12
0
def test_default_name():
    """Test that using the default name works too."""
    args = {
        'a_var': Var(
            use=int,
        ),
    }

    values = {
        'a_var': '5',
    }

    assert parse_dict(values, args) == {
        'a_var': 5,
    }
Beispiel #13
0
def test_default():
    """Test that setting a default works."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
            default=5,
        ),
    }

    values = {}

    assert parse_dict(values, args) == {
        'a_var': 5,
    }
Beispiel #14
0
def test_simple_dict_parsing():
    """Simple test for a dict."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
        ),
    }

    values = {
        'A_VAR': '0',
    }

    assert parse_dict(values, args) == {
        'a_var': 0,
    }
Beispiel #15
0
def test_simple_dict_parsing():
    """Simple test for a dict."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
        ),
    }

    values = {
        'A_VAR': '0',
    }

    assert parse_dict(values, args) == {
        'a_var': 0,
    }
Beispiel #16
0
def test_validation_with_lambda():
    """Test validation with a lambda."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
            validate=lambda x: x == 0
        ),
    }

    values = {
        'A_VAR': '0',
    }

    assert parse_dict(values, args) == {
        'a_var': 0,
    }
Beispiel #17
0
def test_validation_with_lambda():
    """Test validation with a lambda."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
            validate=lambda x: x == 0
        ),
    }

    values = {
        'A_VAR': '0',
    }

    assert parse_dict(values, args) == {
        'a_var': 0,
    }
Beispiel #18
0
def test_nesting():
    """Make sure we can parse nested structures."""
    args = {
        'nested': {
            'a_str': Var(use=str),
            'an_int': Var(use=int),
        },
    }

    values = {
        'a_str': 'this-is-my-string',
        'an_int': '10'
    }

    assert parse_dict(values, args) == {
        'nested': {
            'a_str': 'this-is-my-string',
            'an_int': 10,
        },
    }
Beispiel #19
0
def test_nesting():
    """Make sure we can parse nested structures."""
    args = {
        'nested': {
            'a_str': Var(use=str),
            'an_int': Var(use=int),
        },
    }

    values = {
        'a_str': 'this-is-my-string',
        'an_int': '10'
    }

    assert parse_dict(values, args) == {
        'nested': {
            'a_str': 'this-is-my-string',
            'an_int': 10,
        },
    }
Beispiel #20
0
def test_multi_validation():
    """Test multiple validation functions."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
            validate=(
                bool,
                lambda x: x > 9,
                lambda x: x < 11,
                lambda x: x == 10,
            ),
        ),
    }

    values = {
        'A_VAR': '10',
    }

    assert parse_dict(values, args) == {
        'a_var': 10,
    }
Beispiel #21
0
def test_multi_validation():
    """Test multiple validation functions."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
            validate=(
                bool,
                lambda x: x > 9,
                lambda x: x < 11,
                lambda x: x == 10,
            ),
        ),
    }

    values = {
        'A_VAR': '10',
    }

    assert parse_dict(values, args) == {
        'a_var': 10,
    }
Beispiel #22
0
def test_many_values():
    """Test parsing of several values work."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
            validate=lambda x: x == 0
        ),
        'some_string': Var(
            use=lambda x: x.lower(),
            load_from='SOME_STRING',
        ),
    }

    values = {
        'A_VAR': '0',
        'SOME_STRING': 'SOME VALUE!',
    }

    assert parse_dict(values, args) == {
        'a_var': 0,
        'some_string': 'some value!',
    }
Beispiel #23
0
def test_many_values():
    """Test parsing of several values work."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
            validate=lambda x: x == 0
        ),
        'some_string': Var(
            use=lambda x: x.lower(),
            load_from='SOME_STRING',
        ),
    }

    values = {
        'A_VAR': '0',
        'SOME_STRING': 'SOME VALUE!',
    }

    assert parse_dict(values, args) == {
        'a_var': 0,
        'some_string': 'some value!',
    }
Beispiel #24
0
def test_custom_function_boolean():
    """Test that we can use a custom function to parse with."""
    def parse_bool(value):
        return value.lower() in {
            'true',
            'yes',
            'y',
            '1',
        }

    args = {
        'a_bool': Var(
            use=parse_bool,
            validate=lambda parsed: isinstance(parsed, bool)
        ),
    }

    values = {
        'a_bool': 'true',
    }

    assert parse_dict(values, args) == {
        'a_bool': True,
    }
Beispiel #25
0
def test_custom_function_boolean():
    """Test that we can use a custom function to parse with."""
    def parse_bool(value):
        return value.lower() in {
            'true',
            'yes',
            'y',
            '1',
        }

    args = {
        'a_bool': Var(
            use=parse_bool,
            validate=lambda parsed: isinstance(parsed, bool)
        ),
    }

    values = {
        'a_bool': 'true',
    }

    assert parse_dict(values, args) == {
        'a_bool': True,
    }