core/modules: switch away from using override_config to tmp_config in some tests & faka data generators
This commit is contained in:
parent
5ac5636e7f
commit
0e884fe166
5 changed files with 48 additions and 25 deletions
|
@ -28,7 +28,7 @@ F = TypeVar('F')
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
@contextmanager
|
@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.
|
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
|
assert config is not None
|
||||||
|
|
||||||
import my.config
|
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:
|
if config is not None:
|
||||||
overrides = {k: v for k, v in vars(config).items() if not k.startswith('__')}
|
overrides = {k: v for k, v in vars(config).items() if not k.startswith('__')}
|
||||||
for k, v in overrides.items():
|
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??
|
# todo hmm. not sure what should do about new properties??
|
||||||
assert not hasattr(c, 'extra')
|
assert not hasattr(c, 'extra')
|
||||||
assert c.google != 'whatever'
|
assert c.google != 'whatever'
|
||||||
|
|
||||||
|
|
||||||
|
###
|
||||||
|
# todo properly deprecate, this isn't really meant for public use
|
||||||
|
override_config = _override_config
|
||||||
|
|
|
@ -123,8 +123,8 @@ from contextlib import contextmanager as ctx
|
||||||
@ctx
|
@ctx
|
||||||
def _reset_config() -> Iterator[Config]:
|
def _reset_config() -> Iterator[Config]:
|
||||||
# todo maybe have this decorator for the whole of my.config?
|
# todo maybe have this decorator for the whole of my.config?
|
||||||
from .cfg import override_config
|
from .cfg import _override_config
|
||||||
with override_config(config) as cc:
|
with _override_config(config) as cc:
|
||||||
cc.enabled_modules = None
|
cc.enabled_modules = None
|
||||||
cc.disabled_modules = None
|
cc.disabled_modules = None
|
||||||
cc.cache_dir = None
|
cc.cache_dir = None
|
||||||
|
|
|
@ -3,6 +3,11 @@
|
||||||
|
|
||||||
Consumes data exported by https://github.com/karlicoss/emfitexport
|
Consumes data exported by https://github.com/karlicoss/emfitexport
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
REQUIRES = [
|
||||||
|
'git+https://github.com/karlicoss/emfitexport',
|
||||||
|
]
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict, List, Iterable, Any, Optional
|
from typing import Dict, List, Iterable, Any, Optional
|
||||||
|
|
||||||
|
@ -140,16 +145,20 @@ def stats() -> Stats:
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def fake_data(nights: int=500) -> Iterator[None]:
|
def fake_data(nights: int=500) -> Iterator:
|
||||||
from ..core.cfg import override_config
|
from my.core.cfg import tmp_config
|
||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory
|
||||||
with override_config(config) as cfg, TemporaryDirectory() as td:
|
with TemporaryDirectory() as td:
|
||||||
tdir = Path(td)
|
tdir = Path(td)
|
||||||
cfg.export_path = tdir
|
|
||||||
|
|
||||||
gen = dal.FakeData()
|
gen = dal.FakeData()
|
||||||
gen.fill(tdir, count=nights)
|
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
|
# TODO remove/deprecate it? I think used by timeline
|
||||||
|
|
|
@ -87,20 +87,24 @@ def stats() -> Stats:
|
||||||
# TODO make sure it's possible to 'advise' functions and override stuff
|
# TODO make sure it's possible to 'advise' functions and override stuff
|
||||||
|
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
from typing import Iterator
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def fake_data(count: int=100):
|
def fake_data(count: int=100) -> Iterator:
|
||||||
from .core.cfg import override_config
|
from my.core.cfg import tmp_config
|
||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory
|
||||||
import json
|
import json
|
||||||
with override_config(endomondo) as cfg, TemporaryDirectory() as td:
|
with TemporaryDirectory() as td:
|
||||||
tdir = Path(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()
|
fd = dal.FakeData()
|
||||||
data = fd.generate(count=count)
|
data = fd.generate(count=count)
|
||||||
|
|
||||||
jf = tdir / 'data.json'
|
jf = tdir / 'data.json'
|
||||||
jf.write_text(json.dumps(data))
|
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
|
||||||
|
|
|
@ -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?
|
# 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 contextlib import contextmanager
|
||||||
|
from typing import Iterator
|
||||||
# todo take seed, or what?
|
# todo take seed, or what?
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def fake_data(rows: int=1000) -> Iterator[None]:
|
def fake_data(rows: int=1000) -> Iterator:
|
||||||
# todo also disable cachew automatically for such things?
|
# todo also disable cachew automatically for such things?
|
||||||
from .core.cachew import disabled_cachew
|
from my.core.cfg import tmp_config
|
||||||
from .core.cfg import override_config
|
from my.core.cachew import disabled_cachew
|
||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory
|
||||||
with disabled_cachew(), override_config(config) as cfg, TemporaryDirectory() as td:
|
import json
|
||||||
|
with disabled_cachew(), TemporaryDirectory() as td:
|
||||||
tdir = Path(td)
|
tdir = Path(td)
|
||||||
cfg.export_path = tdir
|
|
||||||
f = tdir / 'rescuetime.json'
|
f = tdir / 'rescuetime.json'
|
||||||
import json
|
|
||||||
f.write_text(json.dumps(dal.fake_data_generator(rows=rows)))
|
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 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?
|
# todo would be kinda nice if doctor could run against the fake data, to have a basic health check of the module?
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue