infoxtractor/src/ix/ocr/fake.py
Dirk Riemann 118a9abd09
All checks were successful
tests / test (push) Successful in 1m0s
tests / test (pull_request) Successful in 1m1s
feat(clients): OCRClient + GenAIClient protocols + fakes (spec §6.2, §6.3)
Adds the two Protocol-based client contracts the pipeline steps depend on,
plus test-oriented fakes. Real engines (Surya, Ollama) land in Chunk 4.

- ix.ocr.client.OCRClient — runtime_checkable Protocol with async ocr().
- ix.genai.client.GenAIClient — runtime_checkable Protocol with async
  invoke(); GenAIInvocationResult + GenAIUsage dataclasses carry the
  parsed model, token usage, and model name.
- FakeOCRClient / FakeGenAIClient: return canned results; both expose a
  raise_on_call hook for error-path tests.

8 unit tests across tests/unit/test_ocr_fake.py + test_genai_fake.py
confirm protocol conformance, canned-return behaviour, usage/model-name
defaults, and raise_on_call propagation.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 11:08:24 +02:00

40 lines
1.1 KiB
Python

"""FakeOCRClient — returns a canned :class:`OCRResult` for hermetic tests.
Used by every pipeline unit test to avoid booting Surya / CUDA. The
``raise_on_call`` hook lets error-path tests exercise ``IX_002_000``-style
code paths without needing to forge network errors.
"""
from __future__ import annotations
from ix.contracts import OCRResult, Page
class FakeOCRClient:
"""Satisfies :class:`~ix.ocr.client.OCRClient` structurally.
Parameters
----------
canned:
The :class:`OCRResult` to return from every :meth:`ocr` call.
raise_on_call:
If set, :meth:`ocr` raises this exception instead of returning.
"""
def __init__(
self,
canned: OCRResult,
*,
raise_on_call: BaseException | None = None,
) -> None:
self._canned = canned
self._raise_on_call = raise_on_call
async def ocr(self, pages: list[Page]) -> OCRResult:
"""Return the canned result or raise the configured error."""
if self._raise_on_call is not None:
raise self._raise_on_call
return self._canned
__all__ = ["FakeOCRClient"]