my.pdfs: reorganize tests a bit, fix mypy

This commit is contained in:
Dima Gerasimov 2021-03-30 20:44:49 +01:00 committed by karlicoss
parent 5c38872efc
commit e7604c188e
6 changed files with 23 additions and 17 deletions

View file

@ -38,8 +38,8 @@ def test_old_db() -> None:
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def prepare(): def prepare():
testdata = Path(__file__).absolute().parent.parent / 'testdata' from .common import testdata
bmdata = testdata / 'hpi-testdata' / 'bluemaestro' bmdata = testdata() / 'hpi-testdata' / 'bluemaestro'
assert bmdata.exists(), bmdata assert bmdata.exists(), bmdata
class bluemaestro: class bluemaestro:

View file

@ -1,4 +1,5 @@
import os import os
from pathlib import Path
import pytest import pytest
@ -18,3 +19,9 @@ def reset_modules() -> None:
to_unload = [m for m in sys.modules if re.match(r'my[.]?', m)] to_unload = [m for m in sys.modules if re.match(r'my[.]?', m)]
for m in to_unload: for m in to_unload:
del sys.modules[m] del sys.modules[m]
def testdata() -> Path:
d = Path(__file__).absolute().parent.parent / 'testdata'
assert d.exists(), d
return d

View file

@ -32,10 +32,8 @@ def prepare(tmp_path: Path):
def _prepare_google_config(tmp_path: Path): def _prepare_google_config(tmp_path: Path):
testdata = Path(__file__).absolute().parent.parent / 'testdata' from .common import testdata
assert testdata.exists(), testdata track = one(testdata().rglob('italy-slovenia-2017-07-29.json'))
track = one(testdata.rglob('italy-slovenia-2017-07-29.json'))
# todo ugh. unnecessary zipping, but at the moment takeout provider doesn't support plain dirs # todo ugh. unnecessary zipping, but at the moment takeout provider doesn't support plain dirs
import zipfile import zipfile

View file

@ -1,34 +1,34 @@
#!/usr/bin/env python3
import inspect import inspect
from pathlib import Path from pathlib import Path
import tempfile import tempfile
from my.pdfs import get_annots, annotated_pdfs from my.pdfs import get_annots, annotated_pdfs
from .common import testdata
ROOT = Path(__file__).parent.absolute() EXPECTED_HIGHLIGHTS = {
EXPECTED_HIGHLIGHTS = set(['Since 1994, when we first began organizing web sites, we have enjoyed a rare opportunity to participate in the birth of a new discipline. ', 'Since 1994, when we first began organizing web sites, we have enjoyed a rare opportunity to participate in the birth of a new discipline. ',
'And yet, unlearn we must, ', 'And yet, unlearn we must, ',
'', '',
]) }
def test_get_annots():
def test_get_annots() -> None:
""" """
Test get_annots, with a real PDF file Test get_annots, with a real PDF file
get_annots should return a list of three Annotation objects get_annots should return a list of three Annotation objects
""" """
annotations = get_annots(Path(ROOT / 'Information Architecture for the World Wide Web.pdf')) annotations = get_annots(testdata() / 'pdfs' / 'Information Architecture for the World Wide Web.pdf')
assert len(annotations) == 3 assert len(annotations) == 3
assert set([a.highlight for a in annotations]) == EXPECTED_HIGHLIGHTS assert set([a.highlight for a in annotations]) == EXPECTED_HIGHLIGHTS
def test_annotated_pdfs_with_filelist(): def test_annotated_pdfs_with_filelist() -> None:
""" """
Test annotated_pdfs, with a real PDF file Test annotated_pdfs, with a real PDF file
annotated_pdfs should return a list of one Pdf object, with three Annotations annotated_pdfs should return a list of one Pdf object, with three Annotations
""" """
filelist = [Path(ROOT / 'Information Architecture for the World Wide Web.pdf')] filelist = [testdata() / 'pdfs' / 'Information Architecture for the World Wide Web.pdf']
annotations_generator = annotated_pdfs(filelist=filelist, roots=None) annotations_generator = annotated_pdfs(filelist=filelist, roots=None)
assert inspect.isgeneratorfunction(annotated_pdfs) assert inspect.isgeneratorfunction(annotated_pdfs)
@ -36,6 +36,7 @@ def test_annotated_pdfs_with_filelist():
highlights_from_pdfs = [] highlights_from_pdfs = []
for pdf_object in list(annotations_generator): 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]) highlights_from_pdfs.extend([a.highlight for a in pdf_object.annotations])
assert len(highlights_from_pdfs) == 3 assert len(highlights_from_pdfs) == 3

View file