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"])
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
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
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
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"])
def test_setting_invalid_type_to_var_key(self): with self.assertRaises(ValueError): VariableKey(123) # noqa: testing
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]
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"], {})
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")
def test_getting_nested_vars(self): loader = loaders.Dict.load({"first layer": {"internal": ["values"]}}) key = VariableKey("first layer") / "internal" self.assertEqual(loader[key], ["values"])
class ConfigSample(BaseConfig): path_to_variable = VariableKey("longer_nested_value") / "target" combined_key = VariableKey("nested_val") / path_to_variable var = Variable(loader, combined_key)