core/modules: switch away from using override_config to tmp_config in some tests & faka data generators

This commit is contained in:
Dima Gerasimov 2023-02-09 00:53:24 +00:00 committed by karlicoss
parent 5ac5636e7f
commit 0e884fe166
5 changed files with 48 additions and 25 deletions

View file

@ -28,7 +28,7 @@ F = TypeVar('F')
from contextlib import contextmanager
from typing import Iterator
@contextmanager
def override_config(config: F) -> Iterator[F]:
def _override_config(config: F) -> Iterator[F]:
'''
Temporary override for config's parameters, useful for testing/fake data/etc.
'''
@ -82,7 +82,7 @@ def tmp_config(*, modules: Optional[ModuleRegex]=None, config=None):
assert config is not None
import my.config
with ExitStack() as module_reload_stack, override_config(my.config) as new_config:
with ExitStack() as module_reload_stack, _override_config(my.config) as new_config:
if config is not None:
overrides = {k: v for k, v in vars(config).items() if not k.startswith('__')}
for k, v in overrides.items():
@ -104,3 +104,8 @@ def test_tmp_config() -> None:
# todo hmm. not sure what should do about new properties??
assert not hasattr(c, 'extra')
assert c.google != 'whatever'
###
# todo properly deprecate, this isn't really meant for public use
override_config = _override_config

View file

@ -123,8 +123,8 @@ from contextlib import contextmanager as ctx
@ctx
def _reset_config() -> Iterator[Config]:
# todo maybe have this decorator for the whole of my.config?
from .cfg import override_config
with override_config(config) as cc:
from .cfg import _override_config
with _override_config(config) as cc:
cc.enabled_modules = None
cc.disabled_modules = None
cc.cache_dir = None

View file

@ -3,6 +3,11 @@
Consumes data exported by https://github.com/karlicoss/emfitexport
"""
REQUIRES = [
'git+https://github.com/karlicoss/emfitexport',
]
from pathlib import Path
from typing import Dict, List, Iterable, Any, Optional
@ -140,16 +145,20 @@ def stats() -> Stats:
from contextlib import contextmanager
from typing import Iterator
@contextmanager
def fake_data(nights: int=500) -> Iterator[None]:
from ..core.cfg import override_config
def fake_data(nights: int=500) -> Iterator:
from my.core.cfg import tmp_config
from tempfile import TemporaryDirectory
with override_config(config) as cfg, TemporaryDirectory() as td:
with TemporaryDirectory() as td:
tdir = Path(td)
cfg.export_path = tdir
gen = dal.FakeData()
gen.fill(tdir, count=nights)
yield
class override:
class emfit:
export_path = tdir
with tmp_config(modules=__name__, config=override) as cfg:
yield cfg
# TODO remove/deprecate it? I think used by timeline

View file

@ -87,20 +87,24 @@ def stats() -> Stats:
# TODO make sure it's possible to 'advise' functions and override stuff
from contextlib import contextmanager
from typing import Iterator
@contextmanager
def fake_data(count: int=100):
from .core.cfg import override_config
def fake_data(count: int=100) -> Iterator:
from my.core.cfg import tmp_config
from tempfile import TemporaryDirectory
import json
with override_config(endomondo) as cfg, TemporaryDirectory() as td:
with TemporaryDirectory() as td:
tdir = Path(td)
cfg.export_path = tdir
# todo would be nice to somehow expose the generator so it's possible to hack from the outside?
fd = dal.FakeData()
data = fd.generate(count=count)
jf = tdir / 'data.json'
jf.write_text(json.dumps(data))
yield
class override:
class endomondo:
export_path = tdir
with tmp_config(modules=__name__, config=override) as cfg:
# todo would be nice to somehow expose the generator so it's possible to hack from the outside?
yield cfg

View file

@ -58,22 +58,27 @@ def stats() -> Stats:
# basically, hack config and populate it with fake data? fake data generated by DAL, but the rest is handled by this?
from typing import Iterator
from contextlib import contextmanager
from typing import Iterator
# todo take seed, or what?
@contextmanager
def fake_data(rows: int=1000) -> Iterator[None]:
def fake_data(rows: int=1000) -> Iterator:
# todo also disable cachew automatically for such things?
from .core.cachew import disabled_cachew
from .core.cfg import override_config
from my.core.cfg import tmp_config
from my.core.cachew import disabled_cachew
from tempfile import TemporaryDirectory
with disabled_cachew(), override_config(config) as cfg, TemporaryDirectory() as td:
tdir = Path(td)
cfg.export_path = tdir
f = tdir / 'rescuetime.json'
import json
with disabled_cachew(), TemporaryDirectory() as td:
tdir = Path(td)
f = tdir / 'rescuetime.json'
f.write_text(json.dumps(dal.fake_data_generator(rows=rows)))
yield
class override:
class rescuetime:
export_path = tdir
with tmp_config(modules=__name__, config=override) as cfg:
yield cfg
# TODO ok, now it's something that actually could run on CI!
# todo would be kinda nice if doctor could run against the fake data, to have a basic health check of the module?