tests: thinking about external repositories

This commit is contained in:
Dima Gerasimov 2020-05-18 20:42:10 +01:00
parent 41c5b34006
commit 44aa062756
2 changed files with 30 additions and 2 deletions

View file

@ -2,23 +2,42 @@
Just a demo module for testing and documentation purposes
'''
from .core import Paths
from .core import Paths, PathIsh
from typing import Optional
from datetime import tzinfo
import pytz
from my.config import demo as user_config
from dataclasses import dataclass
@dataclass
class demo(user_config):
data_path: Paths
username: str
timezone: tzinfo = pytz.utc
external: Optional[PathIsh] = None
@property
def external_module(self):
rpath = self.external
if rpath is not None:
from .cfg import set_repo
set_repo('external', rpath)
import my.config.repos.external as m # type: ignore
return m
from .core import make_config
config = make_config(demo)
# TODO not sure about type checking?
external = config.external_module
from pathlib import Path
from typing import Sequence, Iterable
from datetime import datetime
@ -46,6 +65,6 @@ def items() -> Iterable[Item]:
for raw in j:
yield Item(
username=config.username,
raw=raw,
raw=external.identity(raw),
dt=dt,
)

View file

@ -11,6 +11,7 @@ def test_dynamic_config_1(tmp_path: Path) -> None:
class user_config:
username = 'user'
data_path = f'{tmp_path}/*.json'
external = f'{tmp_path}/external'
my.config.demo = user_config # type: ignore[misc, assignment]
from my.demo import items
@ -29,6 +30,7 @@ def test_dynamic_config_2(tmp_path: Path) -> None:
class user_config:
username = 'user2'
data_path = f'{tmp_path}/*.json'
external = f'{tmp_path}/external'
my.config.demo = user_config # type: ignore[misc, assignment]
from my.demo import items
@ -75,6 +77,7 @@ def test_attribute_handling(tmp_path: Path) -> None:
username = 'UUU'
data_path = f'{tmp_path}/*.json'
external = f'{tmp_path}/external'
my.config.demo = user_config # type: ignore[misc, assignment]
@ -99,4 +102,10 @@ def prepare(tmp_path: Path):
{"key2": 2}
]
''')
ext = tmp_path / 'external'
ext.mkdir()
(ext / '__init__.py').write_text('identity = lambda x: x')
yield
ex = 'my.config.repos.external'
if ex in sys.modules:
del sys.modules[ex]