chore: carry over lab 1 solution into lab 2 skeleton
Copy the committed lab 1 sources as-is: src/, student-tests/, bench/, Dockerfile, docker-compose.yml, pyproject.toml, uv.lock, .python-version, config_example.toml, AGENTS.md, CLAUDE.md, .claude/, openspec/, .graphifyignore and KNOWN_ISSUES.md. Lab 1 evidence stays in the lab 1 repository. Merge the new skeleton files with own sections: Makefile (lab 2 targets plus own run, docker, bench and common targets), .gitignore, .dockerignore and .env.example. Rename the image to mtusi-messenger:lab2 and point AGENTS.md at lab 2. The application is still the in-memory lab 1 implementation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
"""Сверка тестов со сценариями основных спеков `openspec/specs/`."""
|
||||
|
||||
import re
|
||||
from collections.abc import Iterable, Mapping
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
SPECS_DIR = Path(__file__).resolve().parents[1] / "openspec" / "specs"
|
||||
# основные спеки поведения API; спеки контейнера, логов и конфига
|
||||
# проверяются evidence, а не HTTP-тестами
|
||||
CAPABILITIES = (
|
||||
"api-conventions",
|
||||
"identification",
|
||||
"users",
|
||||
"chats",
|
||||
"messages",
|
||||
"pagination",
|
||||
"data-storage",
|
||||
"health",
|
||||
)
|
||||
SCENARIO_RE = re.compile(r"^#### Scenario: (.+?)\s*$", re.MULTILINE)
|
||||
|
||||
type Scenario = tuple[str, str]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Exclusion:
|
||||
"""Сценарий, который не воспроизвести по HTTP против сервера."""
|
||||
|
||||
reason: str
|
||||
check: str
|
||||
|
||||
|
||||
EXCLUDED: dict[Scenario, Exclusion] = {
|
||||
("api-conventions", "Ответ 500"): Exclusion(
|
||||
reason="500 возникает только при сбое внутри сервиса, "
|
||||
"штатным запросом его не вызвать",
|
||||
check="временно бросить исключение в ручке и запросить её; "
|
||||
"обработчик Exception — src/api/errors.py",
|
||||
),
|
||||
("api-conventions", "Исключение внутри обработки"): Exclusion(
|
||||
reason="нужен сбой внутри сервиса, как для «Ответ 500»; "
|
||||
"traceback виден только в логе процесса",
|
||||
check="тот же ручной сбой, затем make logs: запись error с "
|
||||
"traceback и тем же request_id",
|
||||
),
|
||||
("data-storage", "Прежний пользователь после рестарта"): Exclusion(
|
||||
reason="нужен рестарт процесса, тесты сервер не трогают",
|
||||
check="evidence/container/lifecycle.txt: после рестарта "
|
||||
"прежний X-User-Id получает 401",
|
||||
),
|
||||
("health", "Хранилище не готово"): Exclusion(
|
||||
reason="хранилище создаётся при старте, у запущенного сервера "
|
||||
"оно готово всегда",
|
||||
check="ветка 503 в src/api/routers/health.py, проверка чтением кода",
|
||||
),
|
||||
("identification", "Кнопка Authorize в Swagger"): Exclusion(
|
||||
reason="это интерфейс /docs в браузере",
|
||||
check="схему devUser, из которой строится кнопка, проверяет "
|
||||
"тест сценария «Схема в openapi.json»; кнопку — вручную",
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class Collected:
|
||||
"""Что собрал pytest: маркеры `spec` и модули тестов."""
|
||||
|
||||
marks: dict[Scenario, list[str]] = field(default_factory=dict)
|
||||
modules: set[str] = field(default_factory=set)
|
||||
|
||||
|
||||
COLLECTED = pytest.StashKey[Collected]()
|
||||
|
||||
|
||||
def spec_scenarios(capability: str, specs_dir: Path = SPECS_DIR) -> list[str]:
|
||||
text = (specs_dir / capability / "spec.md").read_text(encoding="utf-8")
|
||||
return SCENARIO_RE.findall(text)
|
||||
|
||||
|
||||
def all_scenarios(
|
||||
capabilities: Iterable[str] = CAPABILITIES,
|
||||
specs_dir: Path = SPECS_DIR,
|
||||
) -> set[Scenario]:
|
||||
return {
|
||||
(capability, scenario)
|
||||
for capability in capabilities
|
||||
for scenario in spec_scenarios(capability, specs_dir)
|
||||
}
|
||||
|
||||
|
||||
def coverage_problems(
|
||||
scenarios: set[Scenario],
|
||||
marks: Mapping[Scenario, list[str]],
|
||||
excluded: Mapping[Scenario, Exclusion],
|
||||
) -> list[str]:
|
||||
"""Расхождения между спеками, маркерами и исключениями."""
|
||||
problems = [
|
||||
f"no test and no exclusion: {capability} / {scenario}"
|
||||
for capability, scenario in sorted(
|
||||
scenarios - marks.keys() - excluded.keys(),
|
||||
)
|
||||
]
|
||||
problems += [
|
||||
f"marker points to unknown scenario {capability} / {scenario}: "
|
||||
+ ", ".join(nodeids)
|
||||
for (capability, scenario), nodeids in sorted(marks.items())
|
||||
if (capability, scenario) not in scenarios
|
||||
]
|
||||
problems += [
|
||||
f"exclusion points to unknown scenario: {capability} / {scenario}"
|
||||
for capability, scenario in sorted(excluded.keys() - scenarios)
|
||||
]
|
||||
problems += [
|
||||
f"scenario is both tested and excluded: {capability} / {scenario}"
|
||||
for capability, scenario in sorted(marks.keys() & excluded.keys())
|
||||
]
|
||||
return problems
|
||||
Reference in New Issue
Block a user