def test_dictconfig(): conf = DictConfig( {"TEST": "value", "PREFIX_ONE": "foo", "PREFIX_TWO": "bar"} ) assert conf.get("FOO") is NOT_PROVIDED assert dict(conf.iterprefixed("NOPREFIX_")) == {} assert conf.get("TEST") == "value" assert dict(conf.iterprefixed("PREFIX_")) == { "PREFIX_ONE": "foo", "PREFIX_TWO": "bar", }
def test_reference(): class RefSettings(Settings): KEY1 = Value(int) KEY2 = Value(int, default=ref("KEY1")) s = RefSettings(DictConfig({"KEY1": "1", "KEY2": "2"})) assert s.KEY1 == 1 assert s.KEY2 == 2 s = RefSettings(DictConfig({"KEY1": "1"})) assert s.KEY1 == 1 assert s.KEY2 == 1
def test_dict_iter_config(): conf = DictConfig( { "TEST_KEY_1": 1, "TEST_KEY_2": 2, "TEST_FOO_3": 3, "TEST_KEX_1": 1, "TEST_KEY_3": 3, "TEST_BAR_1": 1, }, prefix="TEST_", ) items = conf.iterprefixed("KEY_") assert sorted(items) == [("KEY_1", 1), ("KEY_2", 2), ("KEY_3", 3)]
def test_load_appconfig_custom(): settings_class = make_django_settings({"INSTALLED_APPS": []}) s = settings_class(DictConfig({"APP_KEY": "appconfig_custom_val"})) # Load empty apps s.load_apps() s.load_apps([]) class AppSettings(Settings): APP_KEY = Value(str) class AppConfig(DjangoAppConfig): name = "my_test_appconfig_custom.module.submodule" settings = "some_module.custom_settings.CustomSettings" with pytest.raises(AttributeError): s.APP_KEY install_module("my_test_appconfig_custom.module.submodule.apps", AppConfig=AppConfig) install_module("some_module.custom_settings", CustomSettings=AppSettings) s.load_apps([ "my_non_test_app", # Does not exist "my_test_appconfig_custom.module.submodule.apps.AppConfig", ]) assert s.APP_KEY == "appconfig_custom_val"
def test_iter(): class SimpleSettings(Settings): KEY1 = Value(int) KEY2 = Value(int) KEY3 = Value(int) KEY4 = Value(int) KEY5 = Value(int) s = SimpleSettings( DictConfig( { "KEY1": "10", "KEY2": "20", "KEY3": "30", "KEY4": "40", "KEY5": "50", } ) ) assert list(s.keys()) == ["KEY1", "KEY2", "KEY3", "KEY4", "KEY5"] assert list(s.items()) == [ ("KEY1", 10), ("KEY2", 20), ("KEY3", 30), ("KEY4", 40), ("KEY5", 50), ] assert s.as_dict() == { "KEY1": 10, "KEY2": 20, "KEY3": 30, "KEY4": 40, "KEY5": 50, }
def test_dict_value(): class DictSettings(Settings): DICTKEY = DictValue(str) assert isinstance(DictSettings.DICTKEY, BoundValue) s = DictSettings( DictConfig({"DICTKEY_KEY1": 1, "DICTKEY_KEY2": 2, "DICTKE_KEY3": 3}) ) assert s.DICTKEY == {"KEY1": "1", "KEY2": "2"}
def test_mixin(): class TestMixin: KEY = Value(int) class SimpleSettings(TestMixin, Settings): pass class SimpleSettingsOverride(TestMixin, Settings): KEY = Value(str) assert isinstance(SimpleSettings.KEY, BoundValue) assert isinstance(SimpleSettingsOverride.KEY, BoundValue) s = SimpleSettings(DictConfig({"KEY": "1"})) assert s.KEY == 1 s = SimpleSettingsOverride(DictConfig({"KEY": "string"})) assert s.KEY == "string"
def test_iter(): settings_class = make_django_settings({ "INSTALLED_APPS": [], "ROOT_URLCONF": "test.urls" }) settings = settings_class(DictConfig({"SECRET_KEY": "test-secret-key"})) confdict = settings.as_dict() assert confdict["INSTALLED_APPS"] == [] assert confdict["ROOT_URLCONF"] == "test.urls"
def test_env_config(): class DefaultSettings(Settings): SECRET_KEY = Value(str) DEBUG = Value(types.boolean, default=False) DB_URL = Value(types.sqlalchemy_url) LOCALES = Value(types.list(str)) settings = DefaultSettings(DictConfig(os.environ, prefix="APP_")) assert settings.config_provider.__class__ == DictConfig assert settings.config_provider._conf_dict == os.environ
def test_readonly(): class SimpleSettings(Settings): KEY = Value(int) s = SimpleSettings(DictConfig({"KEY": "1"})) assert s.KEY == 1 with pytest.raises(AttributeError): s.KEY = 2 assert s.KEY == 1
def test_load_settings_multiple_providers(): class AppSettings(Settings): APP_KEY = Value(str) APP2_KEY = Value(str) install_module("my_loaded_app.settings", AppSettings=AppSettings) providers = [ DictConfig({"APP_KEY": "app_val"}), DictConfig({"APP2_KEY": "app_val2"}), ] static_config = { "INSTALLED_APPS": ["my_loaded_app"], "ROOT_URLCONF": "test.app.urls", } load_django_settings(providers, static_config, name="test_settings") import test_settings assert test_settings.INSTALLED_APPS == ["my_loaded_app"] assert test_settings.ROOT_URLCONF == "test.app.urls" assert test_settings.APP_KEY == "app_val" assert test_settings.APP2_KEY == "app_val2"
def test_fallbackprovider(): conf = FallbackProvider([ DictConfig({ "TEST1": "value", "PREFIX_ONE": "foo", "PREFIX_TWO": "bar" }), DictConfig({ "TEST1": "wrongvalue", "TEST2": "rightvalue", "PREFIX_ONE": "wrongfoo", "PREFIX_THREE": "rightbar", }), ]) assert conf.get("FOO") is NOT_PROVIDED assert dict(conf.iterprefixed("NOPREFIX_")) == {} assert conf.get("TEST1") == "value" assert conf.get("TEST2") == "rightvalue" assert dict(conf.iterprefixed("PREFIX_")) == { "PREFIX_ONE": "foo", "PREFIX_TWO": "bar", "PREFIX_THREE": "rightbar", }
def test_install(): s = BaseDjangoSettings(DictConfig({})) with unset_key(os.environ, "DJANGO_SETTINGS_MODULE"): with pytest.raises(KeyError): s.install() with set_key(os.environ, "DJANGO_SETTINGS_MODULE", "my_test_settings"): s.install() import my_test_settings assert my_test_settings is s s.install(name="my_other_test_settings") import my_other_test_settings assert my_other_test_settings is s # And let's overwrite it s2 = BaseDjangoSettings(DictConfig({})) s2.install(name="my_other_test_settings") import my_other_test_settings assert my_other_test_settings is s2
def test_simple(): class SimpleSettings(Settings): INTKEY = Value(int) STRKEY = Value(str) NOKEY = Value(str) DEFKEY = Value(str, default="hello") assert isinstance(SimpleSettings.INTKEY, BoundValue) s = SimpleSettings(DictConfig({"INTKEY": "1", "STRKEY": "string"})) assert s.INTKEY == 1 assert s.STRKEY == "string" assert s.DEFKEY == "hello" with pytest.raises(ImproperlyConfigured): s.NOKEY
def test_merge(): class SimpleSettings(Settings): ORIGINAL_KEY = Value(int) class MergeSettings(Settings): MERGED_KEY = Value(int) s = SimpleSettings(DictConfig({"ORIGINAL_KEY": 1, "MERGED_KEY": 2})) assert s.ORIGINAL_KEY == 1 with pytest.raises(AttributeError): s.MERGED_KEY s.merge(MergeSettings) assert s.ORIGINAL_KEY == 1 assert s.MERGED_KEY == 2
def test_dictconfig(): conf = DictConfig({ "TEST": "value", "PREFIX_ONE": "foo", "PREFIX_TWO": "bar" }) assert conf.get("FOO") is NOT_PROVIDED assert dict(conf.iterprefixed("NOPREFIX_")) == {} assert conf.get("TEST") == "value" assert dict(conf.iterprefixed("PREFIX_")) == { "PREFIX_ONE": "foo", "PREFIX_TWO": "bar", }
def test_load_apps(): settings_class = make_django_settings({"INSTALLED_APPS": []}) s = settings_class(DictConfig({"APP_KEY": "app_val"})) # Load empty apps s.load_apps() s.load_apps([]) class AppSettings(Settings): APP_KEY = Value(str) with pytest.raises(AttributeError): s.APP_KEY install_module("my_test_app.module.submodule.settings", AppSettings=AppSettings) s.load_apps(["my_non_test_app", "my_test_app.module.submodule"]) # Does not exist assert s.APP_KEY == "app_val"
def test_dict_get_config(): conf = DictConfig({"TEST_KEY": 1}, prefix="TEST_") assert conf.get("KEY") == 1 assert conf.get("NOKEY") is NOT_PROVIDED