File size: 2,197 Bytes
aad7814
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from __future__ import annotations

from pathlib import Path

import pytest

from backend.config import REPO_ROOT, settings

_BUNDLE_DIR = REPO_ROOT / "Master Standard report and paragraphs"
_PDF = _BUNDLE_DIR / settings.report_template_filename
_WORD = _BUNDLE_DIR / settings.standard_paragraphs_filename


@pytest.mark.skipif(not _PDF.is_file(), reason="Operator PDF not on disk")
def test_real_pdf_schema_has_expected_sections():
    from backend.core.template_discoverer import discover_report_template_schema

    from backend.core.rics_canonical_l3 import PARENT_SECTION_COUNT, valid_leaf_section_ids

    schema = discover_report_template_schema(_PDF)
    ids = schema.section_ids()
    assert len(ids) == len(valid_leaf_section_ids())
    assert schema.additional_metadata.get("parent_section_count") == PARENT_SECTION_COUNT
    assert "D1" in ids
    assert "A1" in ids
    assert "B1" in ids
    assert "AS" not in ids
    assert "OF" not in ids


@pytest.mark.skipif(not _WORD.is_file(), reason="Operator Word file not on disk")
def test_real_word_paragraphs_have_e1():
    from backend.core.template_discoverer import discover_standard_paragraph_titles

    titles = discover_standard_paragraph_titles(_WORD)
    assert "E1" in titles
    assert "chimney" in titles["E1"].lower()


@pytest.mark.skipif(
    not (_PDF.is_file() and _WORD.is_file()),
    reason="Operator bundle not on disk",
)
def test_real_bundle_alias_maps_d1_to_e1(tmp_path, monkeypatch):
    from backend.core import ingest, template_discoverer

    monkeypatch.setattr(settings, "data_dir", str(tmp_path / "data"))
    monkeypatch.setattr(settings, "pii_use_spacy", False)
    monkeypatch.setattr(settings, "master_template_dir", str(_BUNDLE_DIR))
    tenant = "bundle-smoke"

    ingest.ingest_report_template(tenant, _PDF)
    ingest.ingest_standard_paragraphs(tenant, _WORD)

    schema = template_discoverer.load_schema(tenant)
    assert schema is not None
    assert schema.report_template_source == _PDF.name
    assert schema.standard_paragraphs_source == _WORD.name
    if "D1" in schema.section_ids() and "E1" in schema.paragraph_section_titles:
        assert schema.section_alias_map.get("D1") == "E1"