Spaces:
Configuration error
Configuration error
Download tests/test_index_db.py from ZainabEman/OCR-Based-Spare-Part-Localization: direct link, hf CLI and curl.
- Browser
- Download file 4.91 kB
-
https://huggingface.co/spaces/ZainabEman/OCR-Based-Spare-Part-Localization/resolve/main/tests/test_index_db.py
- Command line
-
hf download hf://spaces/ZainabEman/OCR-Based-Spare-Part-Localization/tests/test_index_db.py
-
curl -L -o test_index_db.py https://huggingface.co/spaces/ZainabEman/OCR-Based-Spare-Part-Localization/resolve/main/tests/test_index_db.py
4.91 kB
| """Database contents, retrieval, and rebuild behaviour.""" | |
| from spareparts import build_index, config, index_db | |
| from spareparts.table_parser import PageTable, PartRow | |
| def _row(part_no, name_de, row_index): | |
| return PartRow(part_no, name_de, "en", "fr", row_index, "", False) | |
| def test_counts_match_the_document(built): | |
| summary, out = built | |
| conn = index_db.connect(out / "catalog.db") | |
| try: | |
| assert index_db.counts(conn) == { | |
| "pages": config.EXPECTED_PAGE_COUNT, | |
| "parts": config.EXPECTED_PARTS, | |
| "occurrences": config.EXPECTED_OCCURRENCES, | |
| } | |
| assert index_db.integrity_problems(conn) == [] | |
| assert index_db.malformed_parts(conn) == ["351159"] | |
| assert index_db.parts_on_multiple_pages(conn) == 61 | |
| finally: | |
| conn.close() | |
| assert len(list((out / "pages").glob("*.png"))) == config.EXPECTED_PAGE_COUNT | |
| assert summary["problems"] == [] | |
| def test_lookup_returns_part_with_its_pages(built): | |
| _, out = built | |
| conn = index_db.connect(out / "catalog.db") | |
| try: | |
| part = conn.execute( | |
| "SELECT * FROM parts WHERE part_no = '30642'" | |
| ).fetchone() | |
| assert part["name_de"] == "Schnecke,kpl." | |
| assert part["name_en"] == "worm,cpl." | |
| assert part["is_malformed"] == 0 | |
| pages = [ | |
| r["pdf_page"] | |
| for r in conn.execute( | |
| "SELECT pdf_page FROM occurrences WHERE part_no = '30642'" | |
| ) | |
| ] | |
| assert pages == [34] | |
| # A part listed on nine pages keeps all nine occurrences. | |
| pages = [ | |
| r["pdf_page"] | |
| for r in conn.execute( | |
| "SELECT pdf_page FROM occurrences WHERE part_no = '73290'" | |
| " ORDER BY pdf_page" | |
| ) | |
| ] | |
| assert pages == [31, 35, 36, 37, 39, 42, 45, 47, 49] | |
| # Page 43 lists 33275 twice; both rows are preserved. | |
| assert ( | |
| conn.execute( | |
| "SELECT COUNT(*) FROM occurrences" | |
| " WHERE part_no = '33275' AND pdf_page = 43" | |
| ).fetchone()[0] | |
| == 2 | |
| ) | |
| finally: | |
| conn.close() | |
| def test_page_metadata_points_at_the_right_diagram(built): | |
| _, out = built | |
| conn = index_db.connect(out / "catalog.db") | |
| try: | |
| page = conn.execute("SELECT * FROM pages WHERE pdf_page = 34").fetchone() | |
| assert page["section_code"] == "07" | |
| assert page["section_en"] == "Motor-drive" | |
| assert (page["img_xref"], page["img_w"], page["img_h"]) == (145, 1689, 2352) | |
| path = index_db.resolve_path(page["png_path"]) | |
| assert path.name == "p34.png" | |
| assert index_db.png_size(path) == (1689, 2352) | |
| finally: | |
| conn.close() | |
| def test_rebuild_is_idempotent(built): | |
| """Re-running the build over an existing index reproduces it exactly.""" | |
| _, out = built | |
| db_path, pages_dir = out / "catalog.db", out / "pages" | |
| conn = index_db.connect(db_path) | |
| before = ( | |
| index_db.counts(conn), | |
| conn.execute( | |
| "SELECT part_no, pdf_page, row_index FROM occurrences" | |
| " ORDER BY part_no, pdf_page, row_index" | |
| ).fetchall(), | |
| ) | |
| conn.close() | |
| # A stale image from an earlier run must be cleaned up, not left behind. | |
| stale = pages_dir / "p99.png" | |
| stale.write_bytes(b"") | |
| summary = build_index.build(config.PDF_PATH, db_path, pages_dir) | |
| assert summary["pruned_images"] == 1 | |
| assert not stale.exists() | |
| assert summary["cached_images"] == config.EXPECTED_PAGE_COUNT | |
| conn = index_db.connect(db_path) | |
| try: | |
| assert index_db.counts(conn) == before[0] | |
| after = conn.execute( | |
| "SELECT part_no, pdf_page, row_index FROM occurrences" | |
| " ORDER BY part_no, pdf_page, row_index" | |
| ).fetchall() | |
| assert [tuple(r) for r in after] == [tuple(r) for r in before[1]] | |
| assert index_db.integrity_problems(conn) == [] | |
| finally: | |
| conn.close() | |
| def test_canonical_name_takes_the_majority_spelling(): | |
| """Page 48 prints 74012 as 'Zytindemchraube DIN 84 M6x16'; eight other pages | |
| agree on the correct spelling, so the majority must win.""" | |
| good, bad = "Zylinderschraube DIN 84 M3x8", "Zytindemchraube DIN 84 M6x16" | |
| tables = [ | |
| PageTable(48, "20", "de", "en", "", [_row("74012", bad, 2)], [], []), | |
| PageTable(26, "01", "de", "en", "", [_row("74012", good, 2)], [], []), | |
| PageTable(34, "07", "de", "en", "", [_row("74012", good, 2)], [], []), | |
| ] | |
| assert index_db.canonical_names(tables)["74012"][0] == good | |
| def test_canonical_name_breaks_ties_by_first_appearance(): | |
| tables = [ | |
| PageTable(26, "01", "de", "en", "", [_row("70000", "first", 2)], [], []), | |
| PageTable(34, "07", "de", "en", "", [_row("70000", "second", 2)], [], []), | |
| ] | |
| assert index_db.canonical_names(tables)["70000"][0] == "first" | |