Merge pull request #1436 from jotagesales/config_from_object_string

Config from object string
This commit is contained in:
7
2019-06-16 16:58:43 -07:00
committed by GitHub
5 changed files with 72 additions and 5 deletions

View File

@@ -18,17 +18,30 @@ def temp_path():
yield Path(td, "file")
def test_load_from_object(app):
class Config:
not_for_config = "should not be used"
CONFIG_VALUE = "should be used"
class ConfigTest:
not_for_config = 'should not be used'
CONFIG_VALUE = 'should be used'
app.config.from_object(Config)
def test_load_from_object(app):
app.config.from_object(ConfigTest)
assert "CONFIG_VALUE" in app.config
assert app.config.CONFIG_VALUE == "should be used"
assert "not_for_config" not in app.config
def test_load_from_object_string(app):
app.config.from_object('test_config.ConfigTest')
assert 'CONFIG_VALUE' in app.config
assert app.config.CONFIG_VALUE == 'should be used'
assert 'not_for_config' not in app.config
def test_load_from_object_string_exception(app):
with pytest.raises(ImportError):
app.config.from_object('test_config.Config.test')
def test_auto_load_env():
environ["SANIC_TEST_ANSWER"] = "42"
app = Sanic()

View File

@@ -1,4 +1,8 @@
import inspect
from sanic import helpers
from sanic.config import Config
import pytest
def test_has_message_body():
@@ -56,3 +60,18 @@ def test_remove_entity_headers():
for header, expected in tests:
assert helpers.remove_entity_headers(header) == expected
def test_import_string_class():
obj = helpers.import_string('sanic.config.Config')
assert isinstance(obj, Config)
def test_import_string_module():
module = helpers.import_string('sanic.config')
assert inspect.ismodule(module)
def test_import_string_exception():
with pytest.raises(ImportError):
helpers.import_string('test.test.test')