DispelEvilBot/tests/test_seed_templates.py

37 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Тесты сида шаблонов и контекста предпросмотра (без БД)."""
from __future__ import annotations
import re
from pathlib import Path
from admin.preview import SAMPLE_CONTEXT
from common.texts import TEMPLATES
from common.texts.render import SUPPORTED_TAGS, validate
SEED_FILE = Path(__file__).resolve().parents[1] / "sql" / "V15__seed_message_templates.sql"
_ROW_RE = re.compile(r"\('([^']+)', \$tpl\$(.*?)\$tpl\$", re.DOTALL)
_TAG_RE = re.compile(r"\{([a-z_][a-z0-9_]*)\}")
def test_seed_matches_code_templates() -> None:
"""Миграция-сид не должна расходиться с реестром TEMPLATES."""
seeded = dict(_ROW_RE.findall(SEED_FILE.read_text(encoding="utf-8")))
assert seeded == TEMPLATES
def test_templates_have_known_tags_only() -> None:
for key, text in TEMPLATES.items():
assert validate(text) == [], f"неизвестные теги в {key}"
def test_preview_context_covers_all_tags() -> None:
"""В предпросмотре у каждого тега должно быть тестовое значение."""
assert set(SAMPLE_CONTEXT) == set(SUPPORTED_TAGS)
used: set[str] = set()
for text in TEMPLATES.values():
used.update(_TAG_RE.findall(text))
assert used <= set(SAMPLE_CONTEXT)