pdfs: migrate config to Protocol with properties

allowes to remove a whole bunch of hacky crap from tests!
This commit is contained in:
Dima Gerasimov 2024-08-23 00:47:00 +01:00 committed by karlicoss
parent d154825591
commit 5a67f0bafe
3 changed files with 56 additions and 64 deletions

View file

@ -1,17 +1,16 @@
import inspect
from pathlib import Path
import pytest
from more_itertools import ilen
import pytest
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 crap. if module is imported too early (on the top level, it makes it super hard to override config)
# need to at least detect it...
from my.pdfs import annotations, annotated_pdfs
# todo check types etc as well
assert ilen(annotations()) >= 3
assert ilen(annotated_pdfs()) >= 1
@ -22,12 +21,13 @@ def test_with_error(with_config, tmp_path: Path) -> None:
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,)
from my.pdfs import annotations
annots = list(annotations())
[annot] = annots
assert isinstance(annot, Exception)
@ -35,9 +35,6 @@ def test_with_error(with_config, tmp_path: Path) -> None:
@pytest.fixture
def with_config():
from my.tests.common import reset_modules
reset_modules() # todo ugh.. getting boilerplaty.. need to make it a bit more automatic..
# 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
@ -47,13 +44,9 @@ def with_config():
testdata(),
]
import my.core.cfg as C
with C.tmp_config() as config:
with tmp_config() as config:
config.pdfs = user_config
try:
yield
finally:
reset_modules()
yield
EXPECTED_HIGHLIGHTS = {
@ -68,8 +61,6 @@ def test_get_annots() -> None:
Test get_annots, with a real PDF file
get_annots should return a list of three Annotation objects
"""
from my.pdfs import get_annots
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
@ -80,12 +71,9 @@ 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
"""
from my.pdfs import annotated_pdfs
filelist = [testdata() / 'pdfs' / 'Information Architecture for the World Wide Web.pdf']
annotations_generator = annotated_pdfs(filelist=filelist)
import inspect
assert inspect.isgeneratorfunction(annotated_pdfs)
highlights_from_pdfs = []