Skip to content

Commit

Permalink
Merge pull request #1338 from hramezani/improve_config_test
Browse files Browse the repository at this point in the history
Check error message and fix some lint error in test config.
  • Loading branch information
sjsadowski committed Oct 3, 2018
2 parents f742512 + df7f63d commit 1498baa
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@ class Config:
assert app.config.CONFIG_VALUE == 'should be used'
assert 'not_for_config' not in app.config


def test_auto_load_env():
environ["SANIC_TEST_ANSWER"] = "42"
app = Sanic()
assert app.config.TEST_ANSWER == 42
del environ["SANIC_TEST_ANSWER"]


def test_dont_load_env():
environ["SANIC_TEST_ANSWER"] = "42"
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"]


def test_load_env_prefix():
environ["MYAPP_TEST_ANSWER"] = "42"
app = Sanic(load_env='MYAPP_')
assert app.config.TEST_ANSWER == 42
del environ["MYAPP_TEST_ANSWER"]


def test_load_from_file(app):
config = b"""
VALUE = 'some value'
Expand Down Expand Up @@ -68,12 +72,16 @@ def test_load_from_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')
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):
app.config.DEFAULT = 1

class Config:
DEFAULT = 2

Expand All @@ -82,5 +90,6 @@ class Config:


def test_missing_config(app):
with pytest.raises(AttributeError):
with pytest.raises(AttributeError) as e:
app.config.NON_EXISTENT
assert str(e.value) == ("Config has no 'NON_EXISTENT'")

0 comments on commit 1498baa

Please sign in to comment.