|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +import extract_dates as mod |
| 6 | + |
| 7 | + |
| 8 | +class FakeTextPage: |
| 9 | + def extractWORDS(self, delimiters: str | None = None) -> list[tuple]: # noqa: N802, ARG002 |
| 10 | + return [ |
| 11 | + (0, 0, 10, 10, "January"), |
| 12 | + (11, 0, 20, 10, "2024"), |
| 13 | + (30, 0, 40, 10, "February"), |
| 14 | + (41, 0, 50, 10, "2024"), |
| 15 | + (60, 0, 70, 10, "March"), |
| 16 | + (71, 0, 80, 10, "2024"), |
| 17 | + ] |
| 18 | + |
| 19 | + |
| 20 | +class FakePage: |
| 21 | + def get_pixmap(self) -> "PM": # noqa: F821 |
| 22 | + class PM: |
| 23 | + def tobytes(self, fmt: str) -> bytes: # noqa: ARG002 |
| 24 | + return b"\x89PNG\r\n\x1a\n" |
| 25 | + |
| 26 | + return PM() |
| 27 | + |
| 28 | + def get_textpage_ocr(self, tessdata=None) -> FakeTextPage: # noqa: ARG002, ANN001 |
| 29 | + return FakeTextPage() |
| 30 | + |
| 31 | + |
| 32 | +class FakeDoc: |
| 33 | + def __getitem__(self, idx: int) -> FakePage: |
| 34 | + return FakePage() |
| 35 | + |
| 36 | + |
| 37 | +def test_extract_png_calendars(monkeypatch) -> None: # noqa: ANN001 |
| 38 | + monkeypatch.setattr(mod.pymupdf, "open", lambda _: FakeDoc()) |
| 39 | + monkeypatch.setattr(mod.cv2, "imdecode", lambda *_: np.zeros((200, 300, 3))) |
| 40 | + |
| 41 | + calendars = mod.extract_png_calendars(Path("fake.png")) |
| 42 | + |
| 43 | + assert len(calendars) == 3 |
| 44 | + assert [(c.month, c.year) for c in calendars] == [(1, 2024), (2, 2024), (3, 2024)] |
0 commit comments