net-kingdom/tools/contract-tests/tests/test_excerpt.py

68 lines
2.1 KiB
Python
Raw Normal View History

"""tools/contract-tests/excerpt.py (NK-WP-0058-T08): worker excerpts from the contract table."""
import importlib.util
from pathlib import Path
import pytest
REPO = Path(__file__).resolve().parents[3]
spec = importlib.util.spec_from_file_location('contract_excerpt', REPO / 'tools/contract-tests/excerpt.py')
excerpt = importlib.util.module_from_spec(spec)
spec.loader.exec_module(excerpt)
SYNTHETIC = """## Part 1
| ID | Interface | Owner |
| --- | --- | --- |
| E1 | lanes | platform |
| O2 | lane child | warden |
### E1: lanes
- role `r`, path `p`
### O2 and O3: sessions
- silent child
## Contract CI
### Coverage by row (2026-11-06)
| Row | Contract test | Runs in |
| --- | --- | --- |
| E1 | `tests/test_lanes.py` | CI |
| O2–O4 | `tests/test_bx.py` | CI |
"""
def test_ranges_and_lists():
assert excerpt.ids_in('O2–O4, F1, F2') == {'O2', 'O3', 'O4', 'F1', 'F2'}
assert excerpt.ids_in('E2, E3') == {'E2', 'E3'}
def test_row_details_and_tests():
text = excerpt.excerpt(['E1', 'O2'], SYNTHETIC)
assert '| E1 | lanes | platform |' in text and 'role `r`, path `p`' in text
assert '`tests/test_lanes.py`' in text
assert '### O2 and O3: sessions' in text and '`tests/test_bx.py`' in text
def test_unknown_rows_fail():
with pytest.raises(SystemExit, match='add the interface to the table first'):
excerpt.excerpt(['C9'], SYNTHETIC)
with pytest.raises(SystemExit, match='not a row id'):
excerpt.excerpt(['nonsense'], SYNTHETIC)
def test_every_row_of_the_real_table_has_details_and_a_contract_test():
text = excerpt.TABLE.read_text()
rows = [line.split('|')[1].strip() for line in text.splitlines()
if line.startswith('| ') and excerpt.ROW.match(line.split('|')[1].strip())
and '## Routing' not in text[:text.index(line)][-2000:]]
rows = sorted({r for r in rows})
assert rows, 'no rows found'
for row in rows:
out = excerpt.excerpt([row], text)
assert f'### {row}' in out or f' {row}:' in out or f'and {row}' in out, f'{row}: no detail section'
assert 'none recorded' not in out, f'{row}: no contract test recorded'