Spaces:
Sleeping
Sleeping
Download tests/test_parser.py from DerXanity/farient-deckcheck: direct link, hf CLI and curl.
- Browser
- Download file 12.3 kB
-
https://huggingface.co/spaces/DerXanity/farient-deckcheck/resolve/main/tests/test_parser.py
- Command line
-
hf download hf://spaces/DerXanity/farient-deckcheck/tests/test_parser.py
-
curl -L -o test_parser.py https://huggingface.co/spaces/DerXanity/farient-deckcheck/resolve/main/tests/test_parser.py
12.3 kB
| """Tests for services.parser and services.notes_xml. | |
| Both test decks are required for the full suite. If they are absent (CI | |
| without the binary assets), each test that needs them is skipped via | |
| ``pytest.skip``. | |
| Test decks: | |
| - JACOBS: 33 slides (Jacobs Assessment of Compensation Peers) | |
| - IDEXX: 24 slides (IDEXX Peer Group Assessment, annotated speaker notes) | |
| """ | |
| from __future__ import annotations | |
| import hashlib | |
| import os | |
| from pathlib import Path | |
| import pytest | |
| # --------------------------------------------------------------------------- | |
| # Deck paths β resolved relative to this file's location (apps/api/tests/) | |
| # The decks live at <repo_root>/Doc Reference/ | |
| # --------------------------------------------------------------------------- | |
| _REPO_ROOT = Path(__file__).parent.parent.parent.parent # β project root | |
| _DOC_REF = _REPO_ROOT / "Doc Reference" | |
| JACOBS_PATH = _DOC_REF / "20260429 Jacobs Assessment of Compensation Peers - DRAFT 20260327 (3).pptx" | |
| IDEXX_PATH = _DOC_REF / "Annotated_20260511 IDEXX Peer Group Assessment - DRAFT 20260330 (3).pptx" | |
| def _skip_if_absent(path: Path) -> None: | |
| if not path.exists(): | |
| pytest.skip(f"Test deck not found (CI): {path.name}") | |
| def _sha256(path: Path) -> str: | |
| h = hashlib.sha256() | |
| with path.open("rb") as f: | |
| for chunk in iter(lambda: f.read(65536), b""): | |
| h.update(chunk) | |
| return h.hexdigest() | |
| # --------------------------------------------------------------------------- | |
| # Lazy imports so missing deps don't break collection | |
| # --------------------------------------------------------------------------- | |
| def _import_parser(): | |
| import sys | |
| api_dir = Path(__file__).parent.parent | |
| if str(api_dir) not in sys.path: | |
| sys.path.insert(0, str(api_dir)) | |
| from services.parser import parse_deck | |
| return parse_deck | |
| def _import_notes_xml(): | |
| import sys | |
| api_dir = Path(__file__).parent.parent | |
| if str(api_dir) not in sys.path: | |
| sys.path.insert(0, str(api_dir)) | |
| from services.notes_xml import read_notes_xml | |
| return read_notes_xml | |
| # =========================================================================== | |
| # Slide count tests | |
| # =========================================================================== | |
| def test_idexx_slide_count(): | |
| _skip_if_absent(IDEXX_PATH) | |
| parse_deck = _import_parser() | |
| deck = parse_deck(IDEXX_PATH) | |
| assert deck.slide_count == 24, f"Expected 24, got {deck.slide_count}" | |
| def test_jacobs_slide_count(): | |
| _skip_if_absent(JACOBS_PATH) | |
| parse_deck = _import_parser() | |
| deck = parse_deck(JACOBS_PATH) | |
| assert deck.slide_count == 33, f"Expected 33, got {deck.slide_count}" | |
| # =========================================================================== | |
| # Deck dimensions | |
| # =========================================================================== | |
| def test_deck_dimensions_jacobs(): | |
| _skip_if_absent(JACOBS_PATH) | |
| parse_deck = _import_parser() | |
| deck = parse_deck(JACOBS_PATH) | |
| assert deck.slide_width_emu > 0, "slide_width_emu must be positive" | |
| assert deck.slide_height_emu > 0, "slide_height_emu must be positive" | |
| assert isinstance(deck.slide_width_emu, int) | |
| assert isinstance(deck.slide_height_emu, int) | |
| def test_deck_dimensions_idexx(): | |
| _skip_if_absent(IDEXX_PATH) | |
| parse_deck = _import_parser() | |
| deck = parse_deck(IDEXX_PATH) | |
| assert deck.slide_width_emu > 0 | |
| assert deck.slide_height_emu > 0 | |
| # =========================================================================== | |
| # File immutability (byte-hash before vs after parsing) | |
| # =========================================================================== | |
| def test_parser_does_not_modify_jacobs(): | |
| _skip_if_absent(JACOBS_PATH) | |
| parse_deck = _import_parser() | |
| before = _sha256(JACOBS_PATH) | |
| parse_deck(JACOBS_PATH) | |
| after = _sha256(JACOBS_PATH) | |
| assert before == after, "Parser modified the Jacobs source file!" | |
| def test_parser_does_not_modify_idexx(): | |
| _skip_if_absent(IDEXX_PATH) | |
| parse_deck = _import_parser() | |
| before = _sha256(IDEXX_PATH) | |
| parse_deck(IDEXX_PATH) | |
| after = _sha256(IDEXX_PATH) | |
| assert before == after, "Parser modified the IDEXX source file!" | |
| # =========================================================================== | |
| # Acceptance-criteria text checks (Jacobs) | |
| # =========================================================================== | |
| def test_jacobs_slide1_contains_draft(): | |
| _skip_if_absent(JACOBS_PATH) | |
| parse_deck = _import_parser() | |
| deck = parse_deck(JACOBS_PATH) | |
| slide1 = deck.slides[0] | |
| combined = slide1.extracted_text + " " + slide1.notes_text | |
| assert "DRAFT" in combined, f"'DRAFT' not found in slide 1. Text: {slide1.extracted_text[:200]!r}" | |
| def test_jacobs_slide6_contains_tw_question(): | |
| _skip_if_absent(JACOBS_PATH) | |
| parse_deck = _import_parser() | |
| deck = parse_deck(JACOBS_PATH) | |
| slide6 = deck.slides[5] # 0-indexed | |
| combined = slide6.extracted_text + " " + slide6.notes_text | |
| assert "TW question" in combined, ( | |
| f"'TW question' not found in slide 6. Text: {slide6.extracted_text[:200]!r}" | |
| ) | |
| def test_jacobs_slide7_contains_tw_question(): | |
| _skip_if_absent(JACOBS_PATH) | |
| parse_deck = _import_parser() | |
| deck = parse_deck(JACOBS_PATH) | |
| slide7 = deck.slides[6] # 0-indexed | |
| combined = slide7.extracted_text + " " + slide7.notes_text | |
| assert "TW question" in combined, ( | |
| f"'TW question' not found in slide 7. Text: {slide7.extracted_text[:200]!r}" | |
| ) | |
| def test_jacobs_slide23_contains_page_to_be_updated(): | |
| _skip_if_absent(JACOBS_PATH) | |
| parse_deck = _import_parser() | |
| deck = parse_deck(JACOBS_PATH) | |
| slide23 = deck.slides[22] # 0-indexed | |
| combined = slide23.extracted_text + " " + slide23.notes_text | |
| assert "Page to be updated" in combined, ( | |
| f"'Page to be updated' not found in slide 23. Text: {slide23.extracted_text[:200]!r}" | |
| ) | |
| def test_jacobs_slide24_contains_page_to_be_updated(): | |
| _skip_if_absent(JACOBS_PATH) | |
| parse_deck = _import_parser() | |
| deck = parse_deck(JACOBS_PATH) | |
| slide24 = deck.slides[23] # 0-indexed | |
| combined = slide24.extracted_text + " " + slide24.notes_text | |
| assert "Page to be updated" in combined, ( | |
| f"'Page to be updated' not found in slide 24. Text: {slide24.extracted_text[:200]!r}" | |
| ) | |
| # =========================================================================== | |
| # Shape IDs and group flattening | |
| # =========================================================================== | |
| def test_shape_ids_are_stable_and_unique(): | |
| """Every ShapeInfo has a unique shape_id within a slide.""" | |
| _skip_if_absent(JACOBS_PATH) | |
| parse_deck = _import_parser() | |
| deck = parse_deck(JACOBS_PATH) | |
| for slide in deck.slides: | |
| ids = [s.shape_id for s in slide.shapes] | |
| assert len(ids) == len(set(ids)), ( | |
| f"Duplicate shape IDs on slide {slide.slide_number}: {ids}" | |
| ) | |
| for sid in ids: | |
| assert sid.startswith(f"s{slide.slide_number}."), ( | |
| f"shape_id {sid!r} does not start with 's{slide.slide_number}.'" | |
| ) | |
| def test_group_shapes_flattened(): | |
| """Deck containing GROUP shapes returns all leaf shapes (no GROUP entries).""" | |
| _skip_if_absent(JACOBS_PATH) | |
| parse_deck = _import_parser() | |
| deck = parse_deck(JACOBS_PATH) | |
| for slide in deck.slides: | |
| for shape in slide.shapes: | |
| assert shape.shape_type != "GROUP", ( | |
| f"Slide {slide.slide_number}: GROUP shape not flattened: {shape.shape_id}" | |
| ) | |
| def test_group_children_have_slide_relative_coords(): | |
| """Children of a group have coords >= the group's slide-relative offset.""" | |
| _skip_if_absent(JACOBS_PATH) | |
| from pptx import Presentation | |
| from pptx.enum.shapes import MSO_SHAPE_TYPE | |
| parse_deck = _import_parser() | |
| prs = Presentation(JACOBS_PATH) | |
| # Find slide 9 which we know has a GROUP shape | |
| target_slide_idx = 8 # 0-indexed β slide 9 | |
| group_slide = list(prs.slides)[target_slide_idx] | |
| group_shape = None | |
| for sh in group_slide.shapes: | |
| if sh.shape_type == MSO_SHAPE_TYPE.GROUP: | |
| group_shape = sh | |
| break | |
| assert group_shape is not None, "Expected a GROUP on slide 9 of Jacobs" | |
| deck = parse_deck(JACOBS_PATH) | |
| slide9 = deck.slides[target_slide_idx] | |
| # Each child's left should be >= group.left (since child.left in group frame is >= 0) | |
| # and each child's top should be >= group.top | |
| g_left = group_shape.left or 0 | |
| g_top = group_shape.top or 0 | |
| # We can't directly identify which parsed shapes came from the group, | |
| # but we can check that all shapes on this slide have non-negative coords | |
| # (negative would indicate broken transform). | |
| # Also verify at least one shape has left near or beyond g_left. | |
| for shape in slide9.shapes: | |
| assert shape.left >= 0, f"Negative left on {shape.shape_id}: {shape.left}" | |
| assert shape.top >= 0, f"Negative top on {shape.shape_id}: {shape.top}" | |
| # =========================================================================== | |
| # notes_xml.py β raw zip reader | |
| # =========================================================================== | |
| def test_notes_xml_idexx_at_least_15_slides(): | |
| """IDEXX notes are parseable via raw XML reader on at least 15 slides.""" | |
| _skip_if_absent(IDEXX_PATH) | |
| read_notes_xml = _import_notes_xml() | |
| notes = read_notes_xml(IDEXX_PATH) | |
| assert len(notes) >= 15, ( | |
| f"Expected >=15 slides with notes via notes_xml, got {len(notes)}: {sorted(notes.keys())}" | |
| ) | |
| def test_notes_xml_returns_dict_of_strings(): | |
| _skip_if_absent(IDEXX_PATH) | |
| read_notes_xml = _import_notes_xml() | |
| notes = read_notes_xml(IDEXX_PATH) | |
| assert isinstance(notes, dict) | |
| for k, v in notes.items(): | |
| assert isinstance(k, int), f"Key {k!r} is not int" | |
| assert isinstance(v, str), f"Value for key {k} is not str" | |
| assert v.strip(), f"Slide {k} notes value is blank" | |
| def test_notes_xml_does_not_modify_idexx(): | |
| _skip_if_absent(IDEXX_PATH) | |
| read_notes_xml = _import_notes_xml() | |
| before = _sha256(IDEXX_PATH) | |
| read_notes_xml(IDEXX_PATH) | |
| after = _sha256(IDEXX_PATH) | |
| assert before == after, "notes_xml reader modified the IDEXX source file!" | |
| def test_notes_xml_slide_numbers_are_1indexed(): | |
| _skip_if_absent(IDEXX_PATH) | |
| read_notes_xml = _import_notes_xml() | |
| notes = read_notes_xml(IDEXX_PATH) | |
| for k in notes: | |
| assert k >= 1, f"Slide number {k} is not 1-indexed" | |
| assert k <= 24, f"Slide number {k} exceeds deck size of 24" | |
| # =========================================================================== | |
| # Pydantic model validation | |
| # =========================================================================== | |
| def test_parsed_deck_model_is_valid(): | |
| """ParsedDeck parses without Pydantic errors.""" | |
| _skip_if_absent(JACOBS_PATH) | |
| parse_deck = _import_parser() | |
| deck = parse_deck(JACOBS_PATH) | |
| # Re-serialize to dict and back to exercise model_validate | |
| from schemas.parsed import ParsedDeck | |
| data = deck.model_dump() | |
| deck2 = ParsedDeck.model_validate(data) | |
| assert deck2.slide_count == 33 | |
| def test_extra_fields_forbidden(): | |
| """ParsedDeck has extra='forbid' β extra fields raise ValidationError.""" | |
| import sys | |
| api_dir = Path(__file__).parent.parent | |
| if str(api_dir) not in sys.path: | |
| sys.path.insert(0, str(api_dir)) | |
| from schemas.parsed import ParsedDeck | |
| import pydantic | |
| with pytest.raises(pydantic.ValidationError): | |
| ParsedDeck(slide_count=1, slide_width_emu=100, slide_height_emu=100, | |
| slides=[], unexpected_field="oops") | |
| # =========================================================================== | |
| # IDEXX parser smoke test | |
| # =========================================================================== | |
| def test_idexx_notes_via_parser(): | |
| """Parser-level notes extraction also works for IDEXX (has_notes_slide guard).""" | |
| _skip_if_absent(IDEXX_PATH) | |
| parse_deck = _import_parser() | |
| deck = parse_deck(IDEXX_PATH) | |
| non_empty_notes = [s for s in deck.slides if s.notes_text.strip()] | |
| assert len(non_empty_notes) >= 10, ( | |
| f"Expected >=10 slides with notes via parser, got {len(non_empty_notes)}" | |
| ) | |