def test_load_module_from_file_location_with_non_existing_env_variable(): with pytest.raises( LoadFileException, match="The following environment variables are not set: MuuMilk", ): load_module_from_file_location("${MuuMilk}")
def test_loaded_module_from_file_location_name(): module = load_module_from_file_location( str(Path(__file__).parent / "static" / "app_test_config.py") ) name = module.__name__ if "C:\\" in name: name = name.split("\\")[-1] assert name == "app_test_config"
def update_config(self, config: Union[bytes, str, dict, Any]): """ Update app.config. .. note:: Only upper case settings are considered You can upload app config by providing path to py file holding settings. .. code-block:: python # /some/py/file A = 1 B = 2 .. code-block:: python config.update_config("${some}/py/file") Yes you can put environment variable here, but they must be provided in format: ``${some_env_var}``, and mark that ``$some_env_var`` is treated as plain string. You can upload app config by providing dict holding settings. .. code-block:: python d = {"A": 1, "B": 2} config.update_config(d) You can upload app config by providing any object holding settings, but in such case config.__dict__ will be used as dict holding settings. .. code-block:: python class C: A = 1 B = 2 config.update_config(C) `See user guide re: config <https://sanicframework.org/guide/deployment/configuration.html>`__ """ if isinstance(config, (bytes, str, Path)): config = load_module_from_file_location(location=config) if not isinstance(config, dict): cfg = {} if not isclass(config): cfg.update({ key: getattr(config, key) for key in config.__class__.__dict__.keys() }) config = dict(config.__dict__) config.update(cfg) config = dict(filter(lambda i: i[0].isupper(), config.items())) self.update(config)
def loaded_module_from_file_location(): return load_module_from_file_location( str(Path(__file__).parent / "static" / "app_test_config.py") )
def test_load_module_from_file_location_using_env(): environ["APP_TEST_CONFIG"] = "static/app_test_config.py" location = str(Path(__file__).parent / "${APP_TEST_CONFIG}") module = load_module_from_file_location(location) assert isinstance(module, ModuleType)
def test_load_module_from_file_location(location): module = load_module_from_file_location(location) assert isinstance(module, ModuleType)