bhyve-workflow / tests /test_engine_gate.py
akashdahad's picture
Deploy bhyve-workflow (FastAPI + embedded Postgres)
74d4552 verified
Raw History Blame Contribute Delete
3.18 kB
"""Pins the per-row required-field gate in app/engine.py:run_workflow.
Fail-closed must mean "never emit an unverified ROW", not "reject the whole DOCUMENT
for one bad row". Extraction is stubbed so these tests are deterministic (no LLM).
"""
import app.engine as eng
from app.engine import run_workflow
from app.parsing import ParsedDoc, Page
DEF = {
"steps": [
{"id": "items", "type": "extract", "cardinality": "many",
"item_label": "obs", "fields": [{"name": "Title"}, {"name": "Grade"}]},
],
"output": {"row_source": "items", "columns": [
{"name": "Title", "from": "items.Title"},
{"name": "Grade", "from": "items.Grade"},
]},
"required_fields": ["items.Title", "items.Grade"],
}
def _cell(v, grounded=True):
return {"value": v if grounded else "", "grounded": grounded,
"source_quote": "q" if grounded else "", "page": "1"}
def _doc():
return ParsedDoc(filename="x.pdf", pages=[Page(1, "enough real text to pass the has_text guard " * 3)])
def _stub(monkeypatch, records):
monkeypatch.setattr(eng, "extract_block", lambda doc, fields, **kw: list(records))
def test_drops_unverified_row_and_keeps_the_rest(monkeypatch):
_stub(monkeypatch, [
{"Title": _cell("A"), "Grade": _cell("Major")},
{"Title": _cell("B"), "Grade": _cell("", grounded=False)}, # required Grade not citable -> dropped
{"Title": _cell("C"), "Grade": _cell("Low")},
])
res = run_workflow(DEF, _doc(), {})
assert res.status == "succeeded"
assert [r["Title"]["value"] for r in res.rows] == ["A", "C"]
assert res.stats["dropped"] == 1
assert any("dropped" in w for w in res.warnings)
def test_fails_only_when_no_row_can_be_verified(monkeypatch):
_stub(monkeypatch, [{"Title": _cell("A"), "Grade": _cell("", grounded=False)}])
res = run_workflow(DEF, _doc(), {})
assert res.status == "failed"
assert "No row" in res.failure_reason
DEF_NUMBERED = {
"steps": DEF["steps"],
"output": {"row_source": "items", "columns": [
{"name": "No.", "from": "__row_number__"},
{"name": "Title", "from": "items.Title"},
{"name": "Grade", "from": "items.Grade"},
]},
"required_fields": ["items.Title", "items.Grade"],
}
def test_row_numbering_is_contiguous_after_a_drop(monkeypatch):
_stub(monkeypatch, [
{"Title": _cell("A"), "Grade": _cell("Major")},
{"Title": _cell("B"), "Grade": _cell("", grounded=False)}, # dropped
{"Title": _cell("C"), "Grade": _cell("Low")},
])
res = run_workflow(DEF_NUMBERED, _doc(), {})
assert [r["Title"]["value"] for r in res.rows] == ["A", "C"]
assert [r["No."]["value"] for r in res.rows] == ["1", "2"] # output position, NOT source index (1,3)
def test_all_verified_keeps_all_with_no_drop_warning(monkeypatch):
_stub(monkeypatch, [
{"Title": _cell("A"), "Grade": _cell("Major")},
{"Title": _cell("B"), "Grade": _cell("Low")},
])
res = run_workflow(DEF, _doc(), {})
assert res.status == "succeeded" and len(res.rows) == 2
assert res.stats["dropped"] == 0
assert not any("dropped" in w for w in res.warnings)