Update config (#1903)
* New aproach for uploading sanic app config. * Update config.rst Co-authored-by: tigerthelion <bjt.thompson@gmail.com> Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
This commit is contained in:
1
tests/static/app_test_config.py
Normal file
1
tests/static/app_test_config.py
Normal file
@@ -0,0 +1 @@
|
||||
TEST_SETTING_VALUE = 1
|
||||
35
tests/test_load_module_from_file_location.py
Normal file
35
tests/test_load_module_from_file_location.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from pathlib import Path
|
||||
from types import ModuleType
|
||||
|
||||
import pytest
|
||||
|
||||
from sanic.exceptions import LoadFileException
|
||||
from sanic.utils import load_module_from_file_location
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def loaded_module_from_file_location():
|
||||
return load_module_from_file_location(
|
||||
str(Path(__file__).parent / "static/app_test_config.py")
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.dependency(name="test_load_module_from_file_location")
|
||||
def test_load_module_from_file_location(loaded_module_from_file_location):
|
||||
assert isinstance(loaded_module_from_file_location, ModuleType)
|
||||
|
||||
|
||||
@pytest.mark.dependency(depends=["test_load_module_from_file_location"])
|
||||
def test_loaded_module_from_file_location_name(
|
||||
loaded_module_from_file_location,
|
||||
):
|
||||
assert loaded_module_from_file_location.__name__ == "app_test_config"
|
||||
|
||||
|
||||
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}")
|
||||
36
tests/test_update_config.py
Normal file
36
tests/test_update_config.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
_test_setting_as_dict = {"TEST_SETTING_VALUE": 1}
|
||||
_test_setting_as_class = type("C", (), {"TEST_SETTING_VALUE": 1})
|
||||
_test_setting_as_module = str(
|
||||
Path(__file__).parent / "static/app_test_config.py"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"conf_object",
|
||||
[
|
||||
_test_setting_as_dict,
|
||||
_test_setting_as_class,
|
||||
pytest.param(
|
||||
_test_setting_as_module,
|
||||
marks=pytest.mark.dependency(
|
||||
depends=["test_load_module_from_file_location"],
|
||||
scope="session",
|
||||
),
|
||||
),
|
||||
],
|
||||
ids=["from_dict", "from_class", "from_file"],
|
||||
)
|
||||
def test_update(app, conf_object):
|
||||
app.update_config(conf_object)
|
||||
assert app.config["TEST_SETTING_VALUE"] == 1
|
||||
|
||||
|
||||
def test_update_from_lowercase_key(app):
|
||||
d = {"test_setting_value": 1}
|
||||
app.update_config(d)
|
||||
assert "test_setting_value" not in app.config
|
||||
Reference in New Issue
Block a user