Skip to content

Commit

Permalink
core/modules: switch away from using override_config to tmp_config in…
Browse files Browse the repository at this point in the history
… some tests & faka data generators
  • Loading branch information
karlicoss committed Feb 9, 2023
1 parent 5ac5636 commit 0e884fe
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
9 changes: 7 additions & 2 deletions my/core/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def make_config(cls: Type[C], migration: Callable[[Attrs], Attrs]=lambda x: x) -
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.
'''
Expand Down Expand Up @@ -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():
Expand All @@ -104,3 +104,8 @@ class extra:
# 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
4 changes: 2 additions & 2 deletions my/core/core_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def matches(specs: Sequence[str]) -> Optional[str]:
@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
Expand Down
21 changes: 15 additions & 6 deletions my/emfit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions my/endomondo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 13 additions & 8 deletions my/rescuetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
import json
with disabled_cachew(), TemporaryDirectory() as td:
tdir = Path(td)
cfg.export_path = tdir
f = tdir / 'rescuetime.json'
import 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?

Expand Down

0 comments on commit 0e884fe

Please sign in to comment.