Env custom type casting (#2330)

This commit is contained in:
Adam Hopkins
2021-12-21 00:50:45 +02:00
committed by GitHub
parent d799c5f03c
commit 080d41627a
2 changed files with 77 additions and 3 deletions

View File

@@ -1,3 +1,5 @@
import logging
from contextlib import contextmanager
from os import environ
from pathlib import Path
@@ -32,6 +34,11 @@ class ConfigTest:
return self.not_for_config
class UltimateAnswer:
def __init__(self, answer):
self.answer = int(answer)
def test_load_from_object(app):
app.config.load(ConfigTest)
assert "CONFIG_VALUE" in app.config
@@ -137,6 +144,32 @@ def test_env_prefix_string_value():
del environ["MYAPP_TEST_TOKEN"]
def test_env_w_custom_converter():
environ["SANIC_TEST_ANSWER"] = "42"
config = Config(converters=[UltimateAnswer])
app = Sanic(name=__name__, config=config)
assert isinstance(app.config.TEST_ANSWER, UltimateAnswer)
assert app.config.TEST_ANSWER.answer == 42
del environ["SANIC_TEST_ANSWER"]
def test_add_converter_multiple_times(caplog):
def converter():
...
message = (
"Configuration value converter 'converter' has already been registered"
)
config = Config()
config.register_type(converter)
with caplog.at_level(logging.WARNING):
config.register_type(converter)
assert ("sanic.error", logging.WARNING, message) in caplog.record_tuples
assert len(config._converters) == 5
def test_load_from_file(app):
config = dedent(
"""