Merge pull request #1338 from hramezani/improve_config_test

Check error message and fix some lint error in test config.
This commit is contained in:
Stephen Sadowski 2018-10-03 09:18:46 -05:00 committed by GitHub
commit 1498baab0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,24 +15,28 @@ def test_load_from_object(app):
assert app.config.CONFIG_VALUE == 'should be used' assert app.config.CONFIG_VALUE == 'should be used'
assert 'not_for_config' not in app.config assert 'not_for_config' not in app.config
def test_auto_load_env(): def test_auto_load_env():
environ["SANIC_TEST_ANSWER"] = "42" environ["SANIC_TEST_ANSWER"] = "42"
app = Sanic() app = Sanic()
assert app.config.TEST_ANSWER == 42 assert app.config.TEST_ANSWER == 42
del environ["SANIC_TEST_ANSWER"] del environ["SANIC_TEST_ANSWER"]
def test_dont_load_env(): def test_dont_load_env():
environ["SANIC_TEST_ANSWER"] = "42" environ["SANIC_TEST_ANSWER"] = "42"
app = Sanic(load_env=False) app = Sanic(load_env=False)
assert getattr(app.config, 'TEST_ANSWER', None) == None assert getattr(app.config, 'TEST_ANSWER', None) is None
del environ["SANIC_TEST_ANSWER"] del environ["SANIC_TEST_ANSWER"]
def test_load_env_prefix(): def test_load_env_prefix():
environ["MYAPP_TEST_ANSWER"] = "42" environ["MYAPP_TEST_ANSWER"] = "42"
app = Sanic(load_env='MYAPP_') app = Sanic(load_env='MYAPP_')
assert app.config.TEST_ANSWER == 42 assert app.config.TEST_ANSWER == 42
del environ["MYAPP_TEST_ANSWER"] del environ["MYAPP_TEST_ANSWER"]
def test_load_from_file(app): def test_load_from_file(app):
config = b""" config = b"""
VALUE = 'some value' VALUE = 'some value'
@ -68,12 +72,16 @@ def test_load_from_envvar(app):
def test_load_from_missing_envvar(app): def test_load_from_missing_envvar(app):
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError) as e:
app.config.from_envvar('non-existent variable') app.config.from_envvar('non-existent variable')
assert str(e.value) == ("The environment variable 'non-existent "
"variable' is not set and thus configuration "
"could not be loaded.")
def test_overwrite_exisiting_config(app): def test_overwrite_exisiting_config(app):
app.config.DEFAULT = 1 app.config.DEFAULT = 1
class Config: class Config:
DEFAULT = 2 DEFAULT = 2
@ -82,5 +90,6 @@ def test_overwrite_exisiting_config(app):
def test_missing_config(app): def test_missing_config(app):
with pytest.raises(AttributeError): with pytest.raises(AttributeError) as e:
app.config.NON_EXISTENT app.config.NON_EXISTENT
assert str(e.value) == ("Config has no 'NON_EXISTENT'")