From 4cceccd7872a4c8b4a0f0bbfc8f6913864d180a6 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Mon, 4 May 2020 20:56:11 +0100 Subject: [PATCH] add test for dynamic config attributes (import my.cfg as config) --- lint | 1 + my/body/weight.py | 12 ++++++---- my/notes/orgmode.py | 1 + setup.py | 3 +++ tests/config.py | 58 +++++++++++++++++++++++++++++++++++++++++++++ tox.ini | 1 + 6 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 tests/config.py diff --git a/lint b/lint index 8061578..95c7e50 100755 --- a/lint +++ b/lint @@ -41,6 +41,7 @@ def core_modules() -> Iterable[str]: 'my.init', 'tests/misc.py', 'tests/get_files.py', + # 'tests/config.py', TODO hmm. unclear how to type check this module ] diff --git a/my/body/weight.py b/my/body/weight.py index f18f449..16d3a9e 100644 --- a/my/body/weight.py +++ b/my/body/weight.py @@ -29,16 +29,16 @@ def from_orgmode() -> Iterator[Result]: orgs = orgmode.query() for o in orgs.query_all(lambda o: o.with_tag('weight')): try: - # TODO ?? Result type? + # TODO can it throw? not sure created = o.created - heading = o.heading + assert created is not None except Exception as e: log.exception(e) yield e continue try: - w = float(heading) - except ValueError as e: + w = float(o.heading) + except Exception as e: log.exception(e) yield e continue @@ -69,3 +69,7 @@ def dataframe(): df.set_index('dt', inplace=True) df.index = pd.to_datetime(df.index, utc=True) return df + +# TODO move to a submodule? e.g. my.body.weight.orgmode? +# so there could be more sources +# not sure about my.body thing though diff --git a/my/notes/orgmode.py b/my/notes/orgmode.py index 6df688c..117f462 100644 --- a/my/notes/orgmode.py +++ b/my/notes/orgmode.py @@ -26,6 +26,7 @@ def _org_files_in(ppp: Path, archived: bool=False) -> Iterator[Path]: def org_files(roots=config.roots, archived: bool=False) -> Iterator[Path]: + # TODO rename to 'paths'? use get_files? for p in config.roots: yield from _org_files_in(Path(p), archived=archived) diff --git a/setup.py b/setup.py index 5672e05..991ea2a 100644 --- a/setup.py +++ b/setup.py @@ -50,6 +50,9 @@ def main(): 'pytest', 'pylint', 'mypy', + + # used in some tests + 'pandas', ], 'optional': [ # TODO document these? diff --git a/tests/config.py b/tests/config.py new file mode 100644 index 0000000..ba95017 --- /dev/null +++ b/tests/config.py @@ -0,0 +1,58 @@ +from pathlib import Path + + +def setup_notes_path(notes: Path) -> None: + # TODO reuse doc from my.cfg? + from my.cfg import config + + from types import SimpleNamespace + config.orgmode = SimpleNamespace( # type: ignore[misc,assignment] + roots=[notes], + ) + + +def test_dynamic_configuration(notes: Path) -> None: + setup_notes_path(notes) + + from my.body.weight import dataframe + weight_df = dataframe() + + assert list(weight_df['weight'].fillna(0.0)) == [ + 0.0, + 62.0, + 0.0, + 61.0, + 62.0, + 0.0, + ] + + +import pytest # type: ignore +@pytest.fixture +def notes(tmp_path: Path): + ndir = tmp_path / 'notes' + ndir.mkdir() + logs = ndir / 'logs.org' + logs.write_text(''' +#+TITLE: Stuff I'm logging + +* Weight (org-capture) :weight: +** [2020-05-01 Fri 09:00] 62 +** 63 + this should be ignored, got no timestmap +** [2020-05-03 Sun 08:00] 61 +** [2020-05-04 Mon 10:00] 62 + ''') + misc = ndir / 'misc.org' + misc.write_text(''' +Some misc stuff + +* unrelated note :weight:whatever: + ''') + try: + yield ndir + finally: + pass + + +# TODO test set_repo? diff --git a/tox.ini b/tox.ini index 8188028..52549a8 100644 --- a/tox.ini +++ b/tox.ini @@ -14,6 +14,7 @@ commands = python3 -c 'import my.init; from my.config import stub as config; print(config.key)' python3 -c 'import my.init; import my.config; import my.config.repos' # shouldn't fail at least python3 -m pytest tests/misc.py tests/get_files.py + # TODO add; once I figure out porg depdencency?? tests/config.py # TODO run demo.py? just make sure with_my is a bit cleverer? # TODO e.g. under CI, rely on installing