Spaces:
Configuration error
Configuration error
Download tests/test_table_parser.py from ZainabEman/OCR-Based-Spare-Part-Localization: direct link, hf CLI and curl.
- Browser
- Download file 2.85 kB
-
https://huggingface.co/spaces/ZainabEman/OCR-Based-Spare-Part-Localization/resolve/main/tests/test_table_parser.py
- Command line
-
hf download hf://spaces/ZainabEman/OCR-Based-Spare-Part-Localization/tests/test_table_parser.py
-
curl -L -o test_table_parser.py https://huggingface.co/spaces/ZainabEman/OCR-Based-Spare-Part-Localization/resolve/main/tests/test_table_parser.py
2.85 kB
| """Table extraction and part-number validation.""" | |
| from spareparts import config, pdf_loader, table_parser | |
| def test_page_26_matches_the_printed_table(doc): | |
| parsed = table_parser.parse_page(doc[25]) | |
| assert parsed.pdf_page == 26 | |
| assert parsed.section_code == "01" | |
| assert parsed.section_de == "Abdeckhaube" | |
| assert parsed.section_en == "Cover" | |
| assert parsed.models == "PU-ELA-Z" | |
| assert len(parsed.rows) == 36 | |
| first = parsed.rows[0] | |
| assert first.part_no == "30302" | |
| assert first.name_de == "Drehknopf,kpl." | |
| assert first.name_en == "revoling button,cpl." | |
| assert first.name_fr == "bouton tournant,cpl." | |
| assert parsed.rows[-1].part_no == "79038" | |
| def test_part_numbers_are_strings(doc): | |
| parsed = table_parser.parse_page(doc[25]) | |
| assert all(isinstance(row.part_no, str) for row in parsed.rows) | |
| # A number that would lose meaning as an int must survive verbatim. | |
| assert table_parser.parse_page(doc[46]).rows # page 47, checked below | |
| def test_malformed_part_number_is_flagged_not_corrupted(doc): | |
| # Page 47 lists '351159' (6 digits) where the diagram prints '35159'. | |
| rows = {row.part_no: row for row in table_parser.parse_page(doc[46]).rows} | |
| assert "351159" in rows | |
| assert rows["351159"].is_malformed is True | |
| assert rows["351159"].name_de == "Kipperhebel" | |
| assert sum(row.is_malformed for row in rows.values()) == 1 | |
| def test_page_52_footnotes_filtered_but_later_parts_kept(doc): | |
| parsed = table_parser.parse_page(doc[51]) | |
| # The '*', '**' and '***' definition rows are skipped, plus one blank row. | |
| assert [raw for _, raw in parsed.skipped_rows] == ["*", "**", "***", ""] | |
| assert parsed.rejected_rows == [] | |
| numbers = [row.part_no for row in parsed.rows] | |
| assert len(numbers) == 24 | |
| # Real parts listed *after* the footnote block must still be present. | |
| for part_no in ("30343", "30346", "32569", "79123"): | |
| assert part_no in numbers | |
| def test_footnote_marker_is_split_off_the_german_name(doc): | |
| rows = {row.part_no: row for row in table_parser.parse_page(doc[51]).rows} | |
| assert rows["76744"].footnote == "*" | |
| assert rows["76744"].name_de == "Handkamm" | |
| assert rows["76752"].footnote == "**" | |
| assert rows["76752"].name_de == "Bürstenwalze" | |
| assert rows["30074"].footnote == "" | |
| def test_document_wide_totals(doc): | |
| rows = 0 | |
| distinct = set() | |
| skipped = 0 | |
| for pdf_page in pdf_loader.find_catalogue_pages(doc): | |
| parsed = table_parser.parse_page(doc[pdf_page - 1]) | |
| assert parsed.rejected_rows == [], f"page {pdf_page}" | |
| rows += len(parsed.rows) | |
| distinct.update(row.part_no for row in parsed.rows) | |
| skipped += len(parsed.skipped_rows) | |
| assert rows == config.EXPECTED_OCCURRENCES | |
| assert len(distinct) == config.EXPECTED_PARTS | |
| assert skipped == 13 | |