54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
"""Тесты рендера шаблонов (без БД и сети)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from common.texts.render import (
|
|
TemplateError,
|
|
format_tops,
|
|
format_user_list,
|
|
format_usernames,
|
|
render,
|
|
validate,
|
|
)
|
|
|
|
|
|
def test_render_substitutes_and_escapes():
|
|
template = "Привет, {username}!"
|
|
result = render(template, {"username": "<b>bad</b>"})
|
|
assert result == "Привет, <b>bad</b>!"
|
|
|
|
|
|
def test_render_empty_value():
|
|
assert render("Ник: {nick}", {"nick": None}) == "Ник: "
|
|
|
|
|
|
def test_render_unknown_tag_raises():
|
|
with pytest.raises(TemplateError):
|
|
render("Значение {unknown}", {})
|
|
|
|
|
|
def test_render_keeps_literal_braces_untouched():
|
|
# Одиночная фигурная скобка не считается тегом.
|
|
assert render("текст { не тег", {}) == "текст { не тег"
|
|
|
|
|
|
def test_validate_reports_unknown_tags():
|
|
assert validate("Норм {username}, плохо {nope} {bad2}") == ["bad2", "nope"]
|
|
|
|
|
|
def test_format_usernames_escapes_and_joins():
|
|
rows = [{"username": "a"}, {"username": None}, {"username": "b&c"}]
|
|
assert format_usernames(rows) == "@a @b&c"
|
|
|
|
|
|
def test_format_user_list_numbering():
|
|
rows = [{"username": "u1", "nick": "Nick1"}, {"username": None, "nick": None}]
|
|
assert format_user_list(rows) == "1: @u1 - Nick1\n2: — - "
|
|
|
|
|
|
def test_format_tops():
|
|
rows = [{"nick": "N1", "count": 3}, {"nick": None, "count": 1}]
|
|
assert format_tops(rows) == "1) N1 - 3\n2) — - 1"
|