Add test demonstrating unloading the module during dynamic configuration

This commit is contained in:
Dima Gerasimov 2020-05-10 21:57:55 +01:00
parent 0ac78143f2
commit 1d0ef82d32
3 changed files with 63 additions and 6 deletions

View file

@ -1,6 +1,8 @@
from pathlib import Path
# TODO switch these from using SimpleNamespace
def setup_notes_path(notes: Path) -> None:
# TODO reuse doc from my.cfg?
from my.cfg import config

View file

@ -1,10 +1,11 @@
import sys
from pathlib import Path
from more_itertools import ilen
# TODO NOTE: this wouldn't work because of an early my.config.demo import
# from my.demo import items
def test_dynamic_config(tmp_path: Path) -> None:
def test_dynamic_config_1(tmp_path: Path) -> None:
import my.config
class user_config:
@ -17,7 +18,46 @@ def test_dynamic_config(tmp_path: Path) -> None:
assert item1.username == 'user'
# exactly the same test, but using a different config, to test out the behavious w.r.t. import order
def test_dynamic_config_2(tmp_path: Path) -> None:
# doesn't work without it!
# because the config from test_dybamic_config_1 is cached in my.demo.demo
del sys.modules['my.demo']
import my.config
class user_config:
username = 'user2'
data_path = f'{tmp_path}/*.json'
my.config.demo = user_config # type: ignore[misc, assignment]
from my.demo import items
[item1, item2] = items()
assert item1.username == 'user2'
import pytest # type: ignore
@pytest.mark.skip(reason="won't work at the moment because of inheritance")
def test_dynamic_config_simplenamespace(tmp_path: Path) -> None:
# doesn't work without it!
# because the config from test_dybamic_config_1 is cached in my.demo.demo
del sys.modules['my.demo']
import my.config
from types import SimpleNamespace
user_config = SimpleNamespace(
username='user3',
data_path=f'{tmp_path}/*.json',
)
my.config.demo = user_config # type: ignore[misc, assignment]
from my.demo import config
assert config().username == 'user3'
@pytest.fixture(autouse=True)
def prepare(tmp_path: Path):
(tmp_path / 'data.json').write_text('''