/
liquid-g
/
liquid-code
Обзор
Документация
Войти
/
liquid-g
/
liquid-code
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop-0.4
tests/unit/test_template.py
90 строк
3 KB
User
0.4.9 - переработка документации, рефакторинг кода, покрытие тестами 70%
06 июл 2026, 13:37
06 июл 2026, 13:37
79b2ebc
Код
Авторство
О чём код?
"""Unit-тесты для TemplateManager.""" import pytest import tempfile import os class TestTemplateManager: """Тесты для TemplateManager.""" def test_init_with_existing_dir(self): """Инициализация с существующей папкой.""" with tempfile.TemporaryDirectory() as tmpdir: # Создаем папку os.makedirs(tmpdir, exist_ok=True) from liquidcode.template import TemplateManager manager = TemplateManager(tmpdir) assert manager.template_dir == tmpdir assert manager.env is not None def test_init_with_non_existing_dir(self): """Инициализация с несуществующей папкой.""" with tempfile.TemporaryDirectory() as tmpdir: non_existing = os.path.join(tmpdir, "non_existing_dir") from liquidcode.template import TemplateManager manager = TemplateManager(non_existing) assert manager.template_dir == non_existing assert manager.env is not None assert os.path.exists(non_existing) def test_render_template(self): """Рендеринг шаблона.""" with tempfile.TemporaryDirectory() as tmpdir: # Создаем шаблон template_path = os.path.join(tmpdir, "test.html.j2") with open(template_path, "w") as f: f.write("<h1>{{ title }}</h1><p>{{ body }}</p>") from liquidcode.template import TemplateManager manager = TemplateManager(tmpdir) result = manager.render("test.html.j2", title="Test", body="Content") assert "<h1>Test</h1>" in result assert "<p>Content</p>" in result def test_render_template_with_loop(self): """Рендеринг шаблона с циклом.""" with tempfile.TemporaryDirectory() as tmpdir: # Создаем шаблон template_path = os.path.join(tmpdir, "list.html.j2") with open(template_path, "w") as f: f.write("{% for item in items %}{{ item }}{% endfor %}") from liquidcode.template import TemplateManager manager = TemplateManager(tmpdir) result = manager.render("list.html.j2", items=["a", "b", "c"]) assert "abc" in result def test_render_template_nested(self): """Рендеринг вложенного шаблона.""" with tempfile.TemporaryDirectory() as tmpdir: # Создаем вложенные папки subdir = os.path.join(tmpdir, "subdir") os.makedirs(subdir, exist_ok=True) # Создаем базовый шаблон base_path = os.path.join(tmpdir, "base.html.j2") with open(base_path, "w") as f: f.write("<html>{% block content %}{% endblock %}</html>") # Создаем дочерний шаблон child_path = os.path.join(subdir, "child.html.j2") with open(child_path, "w") as f: f.write("{% extends 'base.html.j2' %}{% block content %}<p>Child</p>{% endblock %}") from liquidcode.template import TemplateManager manager = TemplateManager(tmpdir) result = manager.render("subdir/child.html.j2") assert "<html><p>Child</p></html>" in result