示例#1
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],
    }
示例#2
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,
    }
示例#3
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,
        },
    }
示例#4
0
def test_var_repr():
    """Test repr of the Var class."""
    var = Var(
        use=str,
        validate=bool,
        load_from='some_var',
    )

    expected = ('<Var use=<class \'str\'>, '
                'validate=[<class \'bool\'>], '
                'load_from=some_var, '
                'default=None>')

    assert repr(var) == expected
示例#5
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!',
    }
示例#6
0
def test_simple_envvar_parsing():
    """Simple test for preset envvars."""
    args = {
        'a_var': Var(
            use=int,
            load_from='A_VAR',
        ),
    }

    os.environ['A_VAR'] = '0'

    assert parse_env(args) == {
        'a_var': 0,
    }
示例#7
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,
    }
示例#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,
    }
示例#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,
    }
示例#10
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,
    }
示例#11
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,
    }
示例#12
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',)"
示例#13
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.'
示例#14
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',
    }
示例#15
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',
    }
示例#16
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,
    }
示例#17
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,
    }