def preserves_basic_members(self): c1 = Config( defaults={"key": "default"}, overrides={"key": "override"}, system_prefix="global", user_prefix="user", project_location="project", runtime_path="runtime.yaml", ) c2 = c1.clone() # NOTE: expecting identical defaults also implicitly tests that # clone() passes in defaults= instead of doing an empty init + # copy. (When that is not the case, we end up with # global_defaults() being rerun and re-added to _defaults...) assert c2._defaults == c1._defaults assert c2._defaults is not c1._defaults assert c2._overrides == c1._overrides assert c2._overrides is not c1._overrides assert c2._system_prefix == c1._system_prefix assert c2._user_prefix == c1._user_prefix assert c2._project_prefix == c1._project_prefix assert c2.prefix == c1.prefix assert c2.file_prefix == c1.file_prefix assert c2.env_prefix == c1.env_prefix assert c2._runtime_path == c1._runtime_path
def resulting_clones_are_typed_as_new_class(self): class MyConfig(Config): pass c = Config() c2 = c.clone(into=MyConfig) assert type(c2) is MyConfig
def does_not_deepcopy(self): c = Config( defaults={ # Will merge_dicts happily "oh": {"dear": {"god": object()}}, # And shallow-copy compound values "shallow": {"objects": ["copy", "okay"]}, # Will preserve refrences to the innermost dict, sadly. Not # much we can do without incurring deepcopy problems (or # reimplementing it entirely) "welp": {"cannot": ["have", {"everything": "we want"}]}, } ) c2 = c.clone() # Basic identity assert c is not c2, "Clone had same identity as original!" # Dicts get recreated assert c.oh is not c2.oh, "Top level key had same identity!" assert ( c.oh.dear is not c2.oh.dear ), "Midlevel key had same identity!" # noqa # Basic values get copied err = "Leaf object() had same identity!" assert c.oh.dear.god is not c2.oh.dear.god, err assert c.shallow.objects == c2.shallow.objects err = "Shallow list had same identity!" assert c.shallow.objects is not c2.shallow.objects, err # Deeply nested non-dict objects are stil problematic, oh well err = "Huh, a deeply nested dict-in-a-list had different identity?" assert c.welp.cannot[1] is c2.welp.cannot[1], err err = "Huh, a deeply nested dict-in-a-list value had different identity?" # noqa assert ( c.welp.cannot[1]["everything"] is c2.welp.cannot[1]["everything"] ), err # noqa
def preserves_merged_config(self): c = Config( defaults={"key": "default"}, overrides={"key": "override"} ) assert c.key == "override" assert c._defaults["key"] == "default" c2 = c.clone() assert c2.key == "override" assert c2._defaults["key"] == "default" assert c2._overrides["key"] == "override"
def does_not_reload_file_data(self, load_yaml): path = join(CONFIGS_PATH, "yaml/") c = Config(system_prefix=path) c2 = c.clone() assert c2.outer.inner.hooray == "yaml" # Crummy way to say "only got called with this specific invocation # one time" (since assert_calls_with gets mad about other # invocations w/ different args) calls = load_yaml.call_args_list my_call = call("{}raft.yaml".format(path)) try: calls.remove(my_call) assert my_call not in calls except ValueError: err = "{} not found in {} even once!" assert False, err.format(my_call, calls)
def non_conflicting_values_are_merged(self): # NOTE: this is really just basic clone behavior. class MyConfig(Config): @staticmethod def global_defaults(): orig = Config.global_defaults() orig["new"] = {"data": "ohai"} return orig c = Config(defaults={"other": {"data": "hello"}}) c["runtime"] = {"modification": "sup"} c2 = c.clone(into=MyConfig) # New default data from MyConfig present assert c2.new.data == "ohai" # As well as old default data from the cloned instance assert c2.other.data == "hello" # And runtime user mods from the cloned instance assert c2.runtime.modification == "sup"
def is_not_required(self): c = Config(defaults={"meh": "okay"}) c2 = c.clone() assert c2.meh == "okay"
def preserves_env_data(self): os.environ["RAFT_FOO"] = "bar" c = Config(defaults={"foo": "notbar"}) c.load_shell_env() c2 = c.clone() assert c2.foo == "bar"
def preserves_file_data(self): c = Config(system_prefix=join(CONFIGS_PATH, "yaml/")) assert c.outer.inner.hooray == "yaml" c2 = c.clone() assert c2.outer.inner.hooray == "yaml" assert c2._system == {"outer": {"inner": {"hooray": "yaml"}}}