Files
mtusi2026lab3/student-tests/test_coverage.py
T
ricoromaandClaude Opus 5.5 af4bca08c9 feat: carry lab 2 over and add X-Instance-Id and idempotency keys
- carry the lab 2 solution over and plan lab 3 (PLAN.md, OpenSpec
  change instance-id-idempotency)
- rename the compose project to mtusi-lab3 and the image to :lab3
- add X-Instance-Id to every response and instance_id to every log
  record ([app] instance_id, hostname by default)
- require Idempotency-Key on POST messages: keys table in Postgres,
  key and message in one transaction, replay returns the same 201,
  another body gets 409, TTL from [idempotency] ttl_seconds
- send the key from student-tests, k6 scenarios and the skill driver;
  add idempotency, X-Instance-Id, key race and fault tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 19:42:36 +03:00

99 lines
3.4 KiB
Python

"""У каждого сценария основных спеков есть тест или исключение."""
from pathlib import Path
import pytest
from spec_coverage import (
CAPABILITIES,
COLLECTED,
EXCLUDED,
Exclusion,
Scenario,
all_scenarios,
coverage_problems,
spec_scenarios,
)
TESTS_DIR = Path(__file__).parent
def complete_marks(scenarios: set[Scenario]) -> dict[Scenario, list[str]]:
"""Маркеры, при которых покрыто всё, кроме исключений."""
return {scenario: ["test_x"] for scenario in scenarios - EXCLUDED.keys()}
def test_every_scenario_is_covered(request: pytest.FixtureRequest) -> None:
collected = request.config.stash[COLLECTED]
modules = {path.name for path in TESTS_DIR.glob("test_*.py")}
if not modules <= collected.modules:
pytest.skip(
"coverage is checked only when the whole suite is collected: "
"make student_test",
)
scenarios = all_scenarios()
problems = coverage_problems(scenarios, collected.marks, EXCLUDED)
tested = len(scenarios & collected.marks.keys())
# тесты fault выполняет только make failure_checks
by_fault = len(scenarios & collected.fault)
print(
f"coverage: {len(scenarios)} scenarios in "
f"{len(CAPABILITIES)} specs, {tested} tested "
f"({by_fault} by make failure_checks), {len(EXCLUDED)} excluded",
)
assert not problems, "\n".join(problems)
def test_every_spec_has_scenarios() -> None:
for capability in CAPABILITIES:
assert spec_scenarios(capability), f"no scenarios in {capability}"
def test_exclusions_explain_reason_and_check() -> None:
for (capability, scenario), exclusion in EXCLUDED.items():
assert exclusion.reason.strip(), f"{capability} / {scenario}"
assert exclusion.check.strip(), f"{capability} / {scenario}"
def test_scenario_parser(tmp_path: Path) -> None:
spec = tmp_path / "demo" / "spec.md"
spec.parent.mkdir()
spec.write_text(
"### Requirement: R\n\n#### Scenario: Первый\n\n- **WHEN** x\n\n"
"#### Scenario: Второй \n",
encoding="utf-8",
)
assert spec_scenarios("demo", tmp_path) == ["Первый", "Второй"]
def test_new_scenario_without_test_fails() -> None:
scenarios = all_scenarios()
marks = complete_marks(scenarios)
assert coverage_problems(scenarios, marks, EXCLUDED) == []
grown = scenarios | {("users", "Новый сценарий")}
assert coverage_problems(grown, marks, EXCLUDED) == [
"no test and no exclusion: users / Новый сценарий",
]
def test_marker_typo_fails() -> None:
scenarios = all_scenarios()
marks = complete_marks(scenarios)
marks[("users", "Повтор usernme")] = ["test_typo"]
assert coverage_problems(scenarios, marks, EXCLUDED) == [
"marker points to unknown scenario users / Повтор usernme: test_typo",
]
def test_stale_exclusion_fails() -> None:
scenarios = all_scenarios()
marks = complete_marks(scenarios)
excluded = {
**EXCLUDED,
("health", "Удалённый сценарий"): Exclusion(reason="r", check="c"),
}
assert coverage_problems(scenarios, marks, excluded) == [
"exclusion points to unknown scenario: health / Удалённый сценарий",
]