def test_complex_settings_set(self): merger = SettingMerger( None, [ DictProvider({"X": 1}, name="d1"), DictProvider({"Y": {"{X}"}}, name="d2"), ], ) merger.process() self.assertEqual({"X": 1, "Y": {"1"}}, merger.settings)
def test_complex_settings_ref(self): merger = SettingMerger( None, [ DictProvider({"X": 1}, name="d1"), DictProvider({"Y": SettingReference("X")}, name="d2"), ], ) merger.process() self.assertEqual({"X": 1, "Y": 1}, merger.settings)
def test_complex_settings_ordereddict(self): merger = SettingMerger( None, [ DictProvider({"X": 1}, name="d1"), DictProvider({"Y": OrderedDict([("{X}", "{X}")])}, name="d2"), ], ) merger.process() self.assertEqual({"X": 1, "Y": {"1": "1"}}, merger.settings) self.assertIsInstance(merger.settings["Y"], OrderedDict)
def test_dynamic_setting(self): merger = SettingMerger( None, [ DictProvider({"X": 1}, name="d1"), DictProvider({"X": RawValue("{Y}")}, name="d2"), ], ) merger.process() self.assertEqual({"X": "{Y}"}, merger.settings) self.assertEqual(OrderedDict([("d1", 1), ("d2", RawValue("{Y}"))]), merger.raw_settings["X"])
def test_priority(self): merger = SettingMerger( None, [ DictProvider({"X": 1}, name="d1"), DictProvider({"X": 2}, name="d2") ], ) merger.process() merger.post_process() self.assertEqual({"X": 2}, merger.settings) self.assertEqual(OrderedDict([("d1", 1), ("d2", 2)]), merger.raw_settings["X"])
def test_complex_settings_defaultdict(self): values = defaultdict(lambda: []) values["{X}"].append("{X}") merger = SettingMerger( None, [ DictProvider({"X": 1}, name="d1"), DictProvider({"Y": values}, name="d2") ], ) merger.process() self.assertEqual({"X": 1, "Y": {"1": ["1"]}}, merger.settings) self.assertIsInstance(merger.settings["Y"], defaultdict) merger.settings["Y"]["2"].append(1)
def test_parse(self): merger = SettingMerger( None, [ DictProvider({ "X": 1, "Y": "x{X}" }, name="1"), DictProvider({"X": 2}, name="2"), ], ) merger.process() self.assertEqual({ "X": 2, "Y": "x2", }, merger.settings)
def test_postprocess(self): merger = SettingMerger( None, [ DictProvider( { "INSTALLED_APPS": [ "df_config", "django.contrib.auth", "df_config", ] }, name="d1", ) ], ) merger.process() self.assertEqual( { "INSTALLED_APPS": ["df_config", "django.contrib.auth", "df_config"] }, merger.settings, ) merger.post_process() self.assertEqual( {"INSTALLED_APPS": ["df_config", "django.contrib.auth"]}, merger.settings)
def get_merger_from_env() -> SettingMerger: """ Should be used after set_env(); determine all available settings in this order: * df_config.config.defaults * {project_name}.defaults (overrides df_config.config.defaults) * {root}/etc/{project_name}/settings.ini (overrides {project_name}.settings) * {root}/etc/{project_name}/settings.py (overrides {root}/etc/{project_name}/settings.ini) * ./local_settings.ini (overrides {root}/etc/{project_name}/settings.py) * ./local_settings.py (overrides ./local_settings.ini) * environment variables (overrides ./local_settings.py) """ # required if set_env is not called module_name = set_env() prefix = os.path.abspath(sys.prefix) if prefix == "/usr": prefix = "" mapping = "%s.iniconf:INI_MAPPING" % module_name config_providers = [ DictProvider({"DF_MODULE_NAME": module_name}, name="default values"), PythonModuleProvider("df_config.config.defaults"), PythonModuleProvider("%s.defaults" % module_name), IniConfigProvider("%s/etc/%s/settings.ini" % ( prefix, module_name, )), PythonFileProvider("%s/etc/%s/settings.py" % ( prefix, module_name, )), IniConfigProvider(os.path.abspath("local_settings.ini")), PythonFileProvider(os.path.abspath("local_settings.py")), EnvironmentConfigProvider("%s_" % module_name.upper()), ] fields_provider = PythonConfigFieldsProvider(mapping) return SettingMerger(fields_provider, config_providers)
def test_loop(self): merger = SettingMerger( None, [DictProvider({ "X": "{Y}", "Y": "{Z}", "Z": "{X}" }, name="1")], ) self.assertRaises(ValueError, merger.process)
def check( self, dynamic_setting: DynamicSettting, expected_value, pre_collectstatic=False, pre_migrate=False, post_collectstatic=False, post_migrate=False, ): p_values = [x for x in settings_check_results] settings_check_results[:] = [] setting_name = "X" provider = DictProvider({ setting_name: dynamic_setting, "OTHER": "42" }, name="d1") merger = SettingMerger( None, [provider], ) merger.load_raw_settings() actual_value = dynamic_setting.get_value(merger, provider.name, setting_name) self.assertEqual(expected_value, actual_value) if pre_collectstatic: dynamic_setting.pre_collectstatic(merger, provider.name, setting_name, actual_value) if pre_migrate: dynamic_setting.pre_migrate(merger, provider.name, setting_name, actual_value) if post_collectstatic: dynamic_setting.post_collectstatic(merger, provider.name, setting_name, actual_value) if post_migrate: dynamic_setting.post_migrate(merger, provider.name, setting_name, actual_value) n_values = [x for x in settings_check_results] settings_check_results[:] = p_values return n_values
def get_provider(self): values = OrderedDict() values["UNITTEST"] = True values["UNITTEST_3"] = False return DictProvider(values)