tests: move remaining tests from tests/ to my.tests, cleanup corresponding modules

This commit is contained in:
Dima Gerasimov 2024-08-26 03:34:45 +01:00 committed by karlicoss
parent a5643206a0
commit b87d1c970a
9 changed files with 120 additions and 134 deletions

View file

@ -1,59 +0,0 @@
from pathlib import Path
from typing import Iterator
from more_itertools import one
import pytest
from my.bluemaestro import measurements, Measurement
def ok_measurements() -> Iterator[Measurement]:
for m in measurements():
assert not isinstance(m, Exception)
yield m
def test() -> None:
res2020 = [m for m in ok_measurements() if '2020' in str(m.dt)]
tp = [x for x in res2020 if x.temp == 2.1]
assert len(tp) > 0
for p in tp:
print(p)
dts = p.dt.strftime('%Y%m%d %H')
# check that timezone is set properly
assert dts == '20200824 22'
assert len(tp) == 1 # should be unique
# 2.5 K + 4 K datapoints, somewhat overlapping
assert len(res2020) < 6000
def test_old_db() -> None:
res = list(ok_measurements())
r1 = one(x for x in res if x.dt.strftime('%Y%m%d %H:%M:%S') == '20181003 09:07:00')
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.5
assert r1.pressure == 1024.5
assert r2.pressure == 1009.8
@pytest.fixture(autouse=True)
def prepare():
from my.tests.common import testdata
bmdata = testdata() / 'hpi-testdata' / 'bluemaestro'
assert bmdata.exists(), bmdata
class bluemaestro:
export_path = bmdata
from my.core.cfg import tmp_config
with tmp_config() as config:
config.bluemaestro = bluemaestro
yield

View file

@ -1,89 +0,0 @@
from pathlib import Path
# TODO move this somewhere else -- there are more specific tests covering this now
def test_dynamic_configuration(notes: Path) -> None:
import pytz
from types import SimpleNamespace as NS
from my.core.cfg import tmp_config
with tmp_config() as C:
C.orgmode = NS(paths=[notes])
# TODO ugh. this belongs to tz provider or global config or something
C.weight = NS(default_timezone=pytz.timezone('Europe/London'))
from my.body.weight import from_orgmode
weights = [0.0 if isinstance(x, Exception) else x.value for x in from_orgmode()]
assert weights == [
0.0,
62.0,
0.0,
61.0,
62.0,
0.0,
]
import pytest
from dataclasses import dataclass
# TODO this test should probs be deprecated? it's more of a documentation?
def test_user_config() -> None:
from my.core.common import classproperty
class user_config:
param1 = 'abacaba'
# TODO fuck. properties don't work here???
@classproperty
def param2(cls) -> int:
return 456
extra = 'extra!'
@dataclass
class test_config(user_config):
param1: str
param2: int # type: ignore[assignment] # TODO need to figure out how to trick mypy for @classproperty
param3: str = 'default'
assert test_config.param1 == 'abacaba'
assert test_config.param2 == 456
assert test_config.param3 == 'default'
assert test_config.extra == 'extra!'
from my.core.cfg import make_config
c = make_config(test_config)
assert c.param1 == 'abacaba'
assert c.param2 == 456
assert c.param3 == 'default'
assert c.extra == 'extra!'
@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 timestamp
** [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

View file

@ -1,91 +0,0 @@
import inspect
from pathlib import Path
import pytest
from more_itertools import ilen
from my.core.cfg import tmp_config
from my.tests.common import testdata
from my.pdfs import annotated_pdfs, annotations, get_annots
def test_module(with_config) -> None:
# todo check types etc as well
assert ilen(annotations()) >= 3
assert ilen(annotated_pdfs()) >= 1
def test_with_error(with_config, tmp_path: Path) -> None:
"""should handle crappy files gracefully"""
root = tmp_path
g = root / 'garbage.pdf'
g.write_text('garbage')
from my.config import pdfs
# meh. otherwise legacy config value 'wins'
del pdfs.roots # type: ignore[attr-defined]
pdfs.paths = (root,)
annots = list(annotations())
[annot] = annots
assert isinstance(annot, Exception)
@pytest.fixture
def with_config():
# extra_data = Path(__file__).absolute().parent / 'extra/data/polar'
# assert extra_data.exists(), extra_data
# todo hmm, turned out no annotations in these ones.. whatever
class user_config:
roots = [
testdata(),
]
with tmp_config() as config:
config.pdfs = user_config
yield
EXPECTED_HIGHLIGHTS = {
'Since 1994, when we first began organizing web sites, we have enjoyed a rare oppor-tunity to participate in the birth of a new discipline.',
'And yet, unlearn we must,',
'',
}
def test_get_annots() -> None:
"""
Test get_annots, with a real PDF file
get_annots should return a list of three Annotation objects
"""
annotations = get_annots(testdata() / 'pdfs' / 'Information Architecture for the World Wide Web.pdf')
assert len(annotations) == 3
assert set([a.highlight for a in annotations]) == EXPECTED_HIGHLIGHTS
def test_annotated_pdfs_with_filelist() -> None:
"""
Test annotated_pdfs, with a real PDF file
annotated_pdfs should return a list of one Pdf object, with three Annotations
"""
filelist = [testdata() / 'pdfs' / 'Information Architecture for the World Wide Web.pdf']
annotations_generator = annotated_pdfs(filelist=filelist)
assert inspect.isgeneratorfunction(annotated_pdfs)
highlights_from_pdfs = []
for pdf_object in list(annotations_generator):
assert not isinstance(pdf_object, Exception)
highlights_from_pdfs.extend([a.highlight for a in pdf_object.annotations])
assert len(highlights_from_pdfs) == 3
assert set(highlights_from_pdfs) == EXPECTED_HIGHLIGHTS
# todo old test on my(karlicoss) computer:
# - mature-optimization_wtf.pdf: >3 annotations?
# - nonlinear2.pdf