Example #1
0
def test_config_get_validators_fail_bad_file(temp_dir):
    tf = _write_validator_config({}, temp_dir)
    os.remove(tf)
    with raises(Exception) as got:
        get_validators('file://' + tf)
    assert_exception_correct(
        got.value,
        ValueError(
            f"Failed to open validator configuration file at file://{tf}: " +
            f"[Errno 2] No such file or directory: '{tf}'"))
Example #2
0
def test_config_get_validators_fail_bad_yaml(temp_dir):
    # calling str() on ValidationErrors returns more detailed into about the error
    tf = tempfile.mkstemp('.tmp.cfg', 'config_test_bad_yaml', dir=temp_dir)
    os.close(tf[0])
    with open(tf[1], 'w') as temp:
        temp.write('[bad yaml')
    with raises(Exception) as got:
        get_validators('file://' + tf[1])
    assert_exception_correct(
        got.value,
        ValueError(
            f'Failed to open validator configuration file at file://{tf[1]}: while parsing a '
            +
            'flow sequence\n  in "<urllib response>", line 1, column 1\nexpected \',\' or \']\', '
            +
            'but got \'<stream end>\'\n  in "<urllib response>", line 1, column 10'
        ))
Example #3
0
def test_config_get_validators(temp_dir):
    cfg = {
        'validators': {
            'key1': {
                'validators': [{
                    'module': 'core.config_test_vals',
                    'callable_builder': 'val1'
                }],
                'key_metadata': {
                    'a': 'b',
                    'c': 1.56
                }
            },
            'key2': {
                'validators': [{
                    'module': 'core.config_test_vals',
                    'callable_builder': 'val2',
                    'parameters': {
                        'max-len': 7,
                        'foo': 'bar'
                    }
                }, {
                    'module': 'core.config_test_vals',
                    'callable_builder': 'val2',
                    'parameters': {
                        'max-len': 5,
                        'foo': 'bar'
                    },
                }],
            },
            'key3': {
                'validators': [{
                    'module': 'core.config_test_vals',
                    'callable_builder': 'val1',
                    'parameters': {
                        'foo': 'bat'
                    }
                }],
                'key_metadata': {
                    'f': None,
                    'g': 1
                }
            }
        },
        'prefix_validators': {
            'key4': {
                'validators': [{
                    'module': 'core.config_test_vals',
                    'callable_builder': 'pval1',
                }],
            },
            # check key3 doesn't interfere with above key3
            'key3': {
                'validators': [{
                    'module': 'core.config_test_vals',
                    'callable_builder': 'pval2',
                    'parameters': {
                        'max-len': 7,
                        'foo': 'bar'
                    },
                }, {
                    'module': 'core.config_test_vals',
                    'callable_builder': 'pval2',
                    'parameters': {
                        'max-len': 5,
                        'foo': 'bar'
                    }
                }],
                'key_metadata': {
                    'h': True,
                    'i': 1000
                }
            },
            'key5': {
                'validators': [{
                    'module': 'core.config_test_vals',
                    'callable_builder': 'pval1',
                    'parameters': {
                        'foo': 'bat'
                    }
                }],
                'key_metadata': {
                    'a': 'f',
                    'c': 'l'
                }
            }
        }
    }
    tf = _write_validator_config(cfg, temp_dir)
    vals = get_validators('file://' + tf)
    assert len(vals.keys()) == 3
    assert len(vals.prefix_keys()) == 3
    # the test validators always fail
    assert vals.validator_count('key1') == 1
    assert vals.call_validator('key1', 0,
                               {'a': 'b'}) == "1, key1, {}, {'a': 'b'}"

    assert vals.validator_count('key2') == 2
    assert vals.call_validator(
        'key2', 0,
        {'a': 'd'}) == "2, key2, {'foo': 'bar', 'max-len': 7}, {'a': 'd'}"
    assert vals.call_validator(
        'key2', 1,
        {'a': 'd'}) == "2, key2, {'foo': 'bar', 'max-len': 5}, {'a': 'd'}"

    assert vals.validator_count('key3') == 1
    assert vals.call_validator(
        'key3', 0, {'a': 'c'}) == "1, key3, {'foo': 'bat'}, {'a': 'c'}"

    assert vals.prefix_validator_count('key4') == 1
    assert vals.call_prefix_validator(
        'key4', 0, 'key4stuff',
        {'a': 'b'}) == "1, key4, key4stuff, {}, {'a': 'b'}"

    assert vals.prefix_validator_count('key3') == 2
    assert vals.call_prefix_validator(
        'key3', 0, 'key3s',
        {'a': 'd'
         }) == "2, key3, key3s, {'foo': 'bar', 'max-len': 7}, {'a': 'd'}"
    assert vals.call_prefix_validator(
        'key3', 1, 'key3s1',
        {'a': 'd'
         }) == "2, key3, key3s1, {'foo': 'bar', 'max-len': 5}, {'a': 'd'}"

    assert vals.prefix_validator_count('key5') == 1
    assert vals.call_prefix_validator(
        'key5', 0, 'key5s',
        {'a': 'c'}) == "1, key5, key5s, {'foo': 'bat'}, {'a': 'c'}"

    # Test metadata
    assert vals.key_metadata(['key1', 'key2', 'key3']) == {
        'key1': {
            'a': 'b',
            'c': 1.56
        },
        'key2': {},
        'key3': {
            'f': None,
            'g': 1
        }
    }

    assert vals.prefix_key_metadata(['key3', 'key4', 'key5']) == {
        'key3': {
            'h': True,
            'i': 1000
        },
        'key4': {},
        'key5': {
            'a': 'f',
            'c': 'l'
        }
    }

    # noop entry
    cfg = {}
    tf = _write_validator_config(cfg, temp_dir)
    vals = get_validators('file://' + tf)
    assert len(vals.keys()) == 0
    assert len(vals.prefix_keys()) == 0
Example #4
0
def _config_get_validators_fail(cfg, temp_dir, expected):
    tf = _write_validator_config(cfg, temp_dir)
    with raises(Exception) as got:
        get_validators('file://' + tf)
    assert_exception_correct(got.value, expected)