core: add tmp_config helper for test & adhoc patching

bluemaestro: cleanup tests
This commit is contained in:
Dima Gerasimov 2021-02-18 22:40:05 +00:00 committed by karlicoss
parent 42399f6250
commit 5313984d8f
3 changed files with 21 additions and 17 deletions

View file

@ -40,3 +40,11 @@ def override_config(config: F) -> Iterator[F]:
# ugh. __dict__ of type objects isn't writable..
for k, v in orig_properties.items():
setattr(config, k, v)
# helper for tests? not sure if could be useful elsewhere
@contextmanager
def tmp_config():
import my.config as C
with override_config(C):
yield C # todo not sure?

@ -1 +1 @@
Subproject commit 977691c8577634ff7bc7c6ce9db21f6773b0d009
Subproject commit bcd45b64669bc94849a585b14a09a5d07f60915a

View file

@ -2,18 +2,14 @@
from pathlib import Path
from more_itertools import one
# TODO hmm uses testdata so maybe ok to uncomment
from .common import skip_if_not_karlicoss
import pytest # type: ignore
@skip_if_not_karlicoss
def test() -> None:
from my.bluemaestro import measurements
res = list(measurements())
res2020 = [m for m in measurements() if '2020' in str(m.dt)]
tp = [x for x in res if x.temp == 2.1]
tp = [x for x in res2020 if x.temp == 2.1]
assert len(tp) > 0
for p in tp:
print(p)
@ -24,10 +20,9 @@ def test() -> None:
assert len(tp) == 1 # should be unique
# 2.5 K + 4 K datapoints, somwhat overlapping
assert len(res) < 6000
assert len(res2020) < 6000
@pytest.mark.skip(reason='todo add old database to the testdata')
def test_old_db() -> None:
from my.bluemaestro import measurements
res = list(measurements())
@ -36,9 +31,9 @@ def test_old_db() -> None:
r2 = one(x for x in res if x.dt.strftime('%Y%m%d %H:%M:%S') == '20181003 09:19:00')
assert r1.temp == 16.8
assert r2.temp == 18.3
assert r1.pressure == 1025.8
assert r2.pressure == 1009.9
assert r2.temp == 18.5
assert r1.pressure == 1024.5
assert r2.pressure == 1009.8
@pytest.fixture(autouse=True)
@ -47,9 +42,10 @@ def prepare():
bmdata = testdata / 'hpi-testdata' / 'bluemaestro'
assert bmdata.exists(), bmdata
from my.cfg import config
class user_config:
class bluemaestro:
export_path = bmdata
config.bluemaestro = user_config # type: ignore
yield
# TODO restore the config!
from my.core.cfg import tmp_config
with tmp_config() as config:
config.bluemaestro = bluemaestro
yield