"""volve_dia.ddr.rendered: the publisher HTML and chandra's blocks reduce to the same `Rendered` report.""" from volve_dia.ddr.rendered import Rendered, Table, parse_html, parse_ocr from volve_dia.ocr import LABEL_MAP, Block HTML = """

Summary report

Wellbore: 15/9-F-12

Period: 2007-12-31 00:00 - 2008-01-01 00:00

Status:normal
Hole Dia ():
Rig Name:MÆRSK INSPIRER

Summary of activities (24 Hours)

Installed the BOP.
Tested it.

Operations

Start timeRemark
00:00Moved the BOP.
03:30Tested.

Casing Liner Tubing

Start Time
Typeliner
""" EXPECTED = Rendered( header={"wellbore": "15/9-F-12", "period": "2007-12-31 00:00 - 2008-01-01 00:00"}, fields={"status": "normal", "hole dia ()": "", "rig name": "MÆRSK INSPIRER"}, texts={"Summary of activities (24 Hours)": "Installed the BOP.\nTested it."}, tables={"Operations": Table(["Start time", "Remark"], [["00:00", "Moved the BOP."], ["03:30", "Tested."]]), "Casing Liner Tubing": Table(None, [["Start Time", ""], ["Type", "liner"]])}, ) def block(page, label, html, text=None): return Block(page=page, label=LABEL_MAP.get(label.lower(), "paragraph"), raw_label=label, text=text or html, bbox=[], reading_order=0, html=html) OPS_HEAD = "Start timeRemark" BLOCKS = [ block(1, "Page-Header", "

Summary report

", "Summary report\nWellbore: 15/9-F-12 Period: 2007-12-31 00:00 - 2008-01-01 00:00"), block(1, "Table", "" "
Status: normal
Hole Dia ():
Rig Name:MÆRSK INSPIRER
"), block(1, "Section-Header", "Summary of activities (24 Hours)"), block(1, "Text", "

Installed the BOP.

", "Installed the BOP."), block(2, "Text", "

Tested it.

", "Tested it."), # the paragraph goes on block(2, "Text", "

Operations

", "Operations"), # bold throughout: a title block(2, "Table", f"{OPS_HEAD}
00:00Moved the BOP.
"), block(3, "Page-Footer", "

Page 2 of 3

", "Page 2 of 3"), block(3, "Table", f"{OPS_HEAD}
03:30Tested.
"), # header repeated block(3, "Section-Header", "Casing Liner Tubing"), block(3, "Table", "
Start Time
"), block(4, "Table", "
Typeliner
"), # a data row read as a header ] def test_parse_html(): assert parse_html(HTML) == EXPECTED def test_parse_ocr_undoes_the_page_layout(): r = parse_ocr(BLOCKS) assert (r.header, r.fields, r.texts, r.tables) == (EXPECTED.header, EXPECTED.fields, EXPECTED.texts, EXPECTED.tables) assert r.notes == ["table continued: Operations", "table continued (header row kept as data): Casing Liner Tubing"] def test_parse_ocr_repairs_the_table_structure(): period = "2007-12-31 00:00 - 2008-01-01 00:00" blocks = [ block(1, "Table", "" f"" "
Summary reportPrinted: 2008-10-04 00:00
Wellbore: 15/9-F-12Period: {period}
Status:normal
"), # header lines as rows block(1, "Section-Header", "Operations"), block(1, "Table", f"{OPS_HEAD}" "
00:00Moved
the BOP.
"), # a two-line cell as rows block(2, "Table", "
03:30okTested.
"), # a column split ] r = parse_ocr(blocks) assert r.header == {"wellbore": "15/9-F-12", "period": period, "other": "Printed: 2008-10-04 00:00"} assert r.fields == {"status": "normal"} assert r.tables == {"Operations": Table(["Start time", "Remark"], [["00:00", "Moved\nthe BOP."], ["03:30", "ok", "Tested."]])} assert r.notes == ["table continued, columns differ: Operations", "rows merged into the row above: Operations (1)"] def test_round_trip(): assert Rendered.from_dict(EXPECTED.to_dict()) == EXPECTED