def test_invalid_key(): """Test invalid configuration key.""" TEST_CONFIG = {0: "test"} TEST_REQ = {} with pytest.raises(TypeError): validate_config(TEST_CONFIG, TEST_REQ)
def test_sub_dict(): """Test sub-configuration in dict.""" TEST_CONFIG = {"myStuff": {"value": 42}} TEST_CONFIG_ERR = {"myStuff": {"value": -50}} SUB_REQ = {"value": validate_positive_integer} TEST_CONFIG_REQ = {"myStuff": SubDictValidator(SUB_REQ)} validate_config(TEST_CONFIG, TEST_CONFIG_REQ) with pytest.raises(ConfigurationError): validate_config(TEST_CONFIG_ERR, TEST_CONFIG_REQ)
def test_sub_list(): """Test sub-configuration in list.""" TEST_CONFIG = {"myStuff": [{"value": 42}, {"value": 46}]} TEST_CONFIG_ERR = {"myStuff": [{"value": 42}, {"value": -50}]} SUB_REQ = {"value": validate_positive_integer} TEST_CONFIG_REQ = {"myStuff": SubListValidator(SUB_REQ)} validate_config(TEST_CONFIG, TEST_CONFIG_REQ) with pytest.raises(ConfigurationError): validate_config(TEST_CONFIG_ERR, TEST_CONFIG_REQ)
def test_dependency(): """Test key dependency.""" TEST_CONFIG = {"myKey": "someValue", "myDependency": 42} TEST_CONFIG_ERR = {"myKey": "someValue"} # missing dependency TEST_CONFIG_REQ = {"myKey": KeyDependency("myDependency")} TEST_CONFIG_OPT = {"myDependency": int} validate_config(TEST_CONFIG, TEST_CONFIG_REQ, TEST_CONFIG_OPT) with pytest.raises(ConfigurationError): validate_config(TEST_CONFIG_ERR, TEST_CONFIG_REQ, TEST_CONFIG_OPT)
def test_fragment_replace_fail(): """Test fragment replacement failure.""" TEST_CONFIG = { "my_key": "my_value", "my_other_key": "${my_key2}_dontreplace", } TEST_REQ = {"my_key": str, "my_other_key": AutoFragmentReplace()} with pytest.raises(FragmentError): validate_config(TEST_CONFIG, TEST_REQ) return validate_config(TEST_CONFIG, TEST_REQ, my_key2="bla")
def test_invalid_req(): """Test invalid key declaration.""" TEST_CONFIG = {0: "test"} TEST_REQ = {0: str} TEST_OPT = {1: str} with pytest.raises(KeyDeclarationError): validate_config(TEST_CONFIG, TEST_REQ) with pytest.raises(KeyDeclarationError): validate_config(TEST_CONFIG, {}, TEST_OPT)
def test_dependency_map(): """Test key dependency.""" TEST_CONFIG = {"myKey": "someValue", "someKey": 42, "otherKey": "bla"} TEST_CONFIG_ERR = {"myKey": "someValue"} # missing dependency TEST_CONFIG_REQ = { "myKey": KeyDependencyMap(someValue="someKey", otherValue="otherKey") } TEST_CONFIG_OPT = {"someKey": int, "otherKey": str} validate_config(TEST_CONFIG, TEST_CONFIG_REQ, TEST_CONFIG_OPT) with pytest.raises(ConfigurationError): validate_config(TEST_CONFIG_ERR, TEST_CONFIG_REQ, TEST_CONFIG_OPT)
def test_dependency_decorator(): """Test dependency decorator.""" @KeyDependency("myDependency") def my_validator(myval, **kwargs): """Custom validator.""" return kwargs["myDependency"] == 42 TEST_CONFIG = {"myKey": "someValue", "myDependency": 42} TEST_CONFIG_ERR = {"myKey": "someValue"} # missing dependency TEST_CONFIG_REQ = {"myKey": my_validator} TEST_CONFIG_OPT = {"myDependency": int} validate_config(TEST_CONFIG, TEST_CONFIG_REQ, TEST_CONFIG_OPT) with pytest.raises(ConfigurationError): validate_config(TEST_CONFIG_ERR, TEST_CONFIG_REQ, TEST_CONFIG_OPT)
def test_auto_fragment_replace(): """Test automatic fragment replacement.""" TEST_CONFIG = { "my_key": "my_value", "my_other_key": "${my_key}_dontreplace", } TEST_REQ = {"my_key": str, "my_other_key": AutoFragmentReplace()} return validate_config(TEST_CONFIG, TEST_REQ)
def test_fragment_key(): """Test fragments which access keys.""" TEST_CONFIG = { "my_key": { "foo": "bar" }, "my_other_key": "${my_key::foo}_dontreplace", } TEST_CONFIG_2 = { "my_key": { "foo": "bar" }, "my_other_key": "${:my_key::foo}_dontreplace", } TEST_REQ = {"my_key": dict, "my_other_key": AutoFragmentReplace()} validate_config(TEST_CONFIG, TEST_REQ) return validate_config(TEST_CONFIG_2, TEST_REQ)
def test_fragment_replace(): """Test manual fragment replacement.""" TEST_CONFIG = { "my_key": "my_value", "my_other_key": "REPLACETHIS_dontreplace", } TEST_REQ = { "my_key": str, "my_other_key": FragmentReplace({"REPLACETHIS": "my_key"}), } return validate_config(TEST_CONFIG, TEST_REQ)
def test_toplevel_fragment_replace(): """Test auto replacement with value from toplevel config.""" TEST_CONFIG = { "my_key": "my_value", "other_keys": [{ "my_other_key": "${:my_key}_blabla" }], } TEST_REQ = { "my_key": str, "other_keys": SubListValidator({"my_other_key": AutoFragmentReplace()}), } return validate_config(TEST_CONFIG, TEST_REQ)
def test_union(): """Test union.""" TEST_CONFIG_1 = {"myKey": 1} TEST_CONFIG_2 = {"myKey": False} TEST_CONFIG_REQ = {"myKey": ValidateUnion(int, bool)} validate_config(TEST_CONFIG_1, TEST_CONFIG_REQ) validate_config(TEST_CONFIG_2, TEST_CONFIG_REQ) with pytest.raises(ValidationError): validate_config({"myKey": []}, TEST_CONFIG_REQ)
def test_invalid_config(): """Test invalid config description.""" TEST_CONFIG = [1, 2, 3] TEST_CONFIG_1 = {"some": "key"} TEST_REQ = {} TEST_REQ_1 = [] TEST_OPT_1 = [] with pytest.raises(TypeError): validate_config(TEST_CONFIG, TEST_REQ) with pytest.raises(TypeError): validate_config(TEST_CONFIG_1, TEST_REQ_1) with pytest.raises(TypeError): validate_config(TEST_CONFIG_1, TEST_REQ, TEST_OPT_1)
def test_negate(): """Test negation.""" TEST_CONFIG_1 = {"myKey": False} TEST_CONFIG_REQ_1 = {"myKey": InvertValidation(int)} TEST_CONFIG_REQ_2 = {"myKey": InvertValidation(bool)} validate_config(TEST_CONFIG_1, TEST_CONFIG_REQ_1) with pytest.raises(ValidationError): validate_config(TEST_CONFIG_1, TEST_CONFIG_REQ_2) @InvertValidation() @validate_string def _validator(_value, **kwargs): return _value validate_config(TEST_CONFIG_1, {"myKey": _validator})
def validate_trigger_config(trigger_config): """Validate trigger configuration.""" return validate_config(trigger_config, CONFIGURATION_REQ_KEYS, CONFIGURATION_OPT_KEYS)
def validate_input_config(input_config): """Validate input configuration.""" return validate_config(input_config, INPUT_REQ, INPUT_OPT)