Ejemplo n.º 1
0
    def test_combining_keys(self):
        var_key1 = VariableKey("hello") / "world"
        var_key2 = VariableKey("more complicated key")
        combined_key = var_key1 / var_key2 / "ez"

        self.assertEqual(list(iter(combined_key)),
                         ["hello", "world", "more complicated key", "ez"])
Ejemplo n.º 2
0
class ConfigSample(BaseConfig):
    user_id: Variable[int] = Variable(loader, VariableKey("user_id"))

    @staticmethod
    @user_id.register_validator
    def validate_user_id(var, value):
        # Functions can return bool values or raise
        # config_framework.types.custom_exceptions.InvalidValueError
        # for more detailed description.
        if value != 2:
            raise ValueValidationError(f"User id must be 2 (got {value})")

        return True
Ejemplo n.º 3
0
        class BasicConfig(BaseConfig):
            rud: Variable[str] = Variable(config_data, "rud")
            is_author: Variable[str] = Variable(config_data, "is author")
            nested_data: Variable[str] = Variable(
                config_data,
                VariableKey("nested") / "data")

            @staticmethod
            @rud.register_validator
            def validate_rud(variable: Variable, value: Any):
                if value != "356":
                    raise types.custom_exceptions.ValueValidationError(
                        "Value for Rud must be 356 only")

                return True
Ejemplo n.º 4
0
class ConfigSample(BaseConfig):
    user_id: Variable[Uid] = Variable(loader, VariableKey("user_id"))

    @staticmethod
    @user_id.register_serializer
    def serialize_user_id(var, value: Uid) -> Any:
        # Here we cast value back to int
        return str(int(value))

    @staticmethod
    @user_id.register_deserializer
    def deserialize_user_id(var, value: Any) -> Uid:
        # And there we cast our raw value to
        # Uid type so we can use it everywhere
        uid_value = Uid(value)
        return uid_value
Ejemplo n.º 5
0
 def test_basic_variable_key(self):
     var_key = VariableKey("hello") / "world"
     self.assertEqual("hello[/]world", str(var_key))
     self.assertEqual(list(iter(var_key)), ["hello", "world"])
Ejemplo n.º 6
0
 def test_setting_invalid_type_to_var_key(self):
     with self.assertRaises(ValueError):
         VariableKey(123)  # noqa: testing
Ejemplo n.º 7
0
 def test_deleting_not_existing_key_from_loader(self):
     loader = loaders.Dict.load({"first layer": {"internal": ["values"]}})
     key = VariableKey("first layer") / "idk"
     with self.assertRaises(KeyError):
         del loader[key]
Ejemplo n.º 8
0
 def test_deleting_value_from_loader(self):
     loader = loaders.Dict.load({"first layer": {"internal": ["values"]}})
     key = VariableKey("first layer") / "internal"
     del loader[key]
     self.assertEqual(loader["first layer"], {})
Ejemplo n.º 9
0
 def test_setting_value(self):
     loader = loaders.Dict.load({"first layer": {"internal": ["values"]}})
     key = VariableKey("first layer") / "internal"
     loader[key] = "Example of changing var"
     self.assertEqual(loader[key], "Example of changing var")
Ejemplo n.º 10
0
 def test_getting_nested_vars(self):
     loader = loaders.Dict.load({"first layer": {"internal": ["values"]}})
     key = VariableKey("first layer") / "internal"
     self.assertEqual(loader[key], ["values"])
Ejemplo n.º 11
0
class ConfigSample(BaseConfig):
    path_to_variable = VariableKey("longer_nested_value") / "target"
    combined_key = VariableKey("nested_val") / path_to_variable

    var = Variable(loader, combined_key)