/
liquid-g
/
liquid-code
Обзор
Документация
Войти
/
liquid-g
/
liquid-code
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
tests/test_template.py
143 строки
5 KB
User
ci: настроен Black и flake8 для автоматического форматирования кода
04 июл 2026, 08:14
04 июл 2026, 08:14
4778ed6
Код
Авторство
О чём код?
"""Тесты для TemplateManager.""" import os import tempfile import shutil import pytest from liquidcode.template import TemplateManager class TestTemplateManagerInit: """Тесты инициализации TemplateManager.""" def test_init_default_directory(self, tmp_path): """Инициализация с директорией по умолчанию.""" os.chdir(tmp_path) manager = TemplateManager() assert manager.template_dir == "templates" assert os.path.exists("templates") def test_init_custom_directory(self, tmp_path): """Инициализация с пользовательской директорией.""" custom_dir = tmp_path / "custom_templates" manager = TemplateManager(str(custom_dir)) assert manager.template_dir == str(custom_dir) assert os.path.exists(custom_dir) def test_init_creates_directory(self, tmp_path): """TemplateManager должен создавать директорию, если её нет.""" new_dir = tmp_path / "new_templates" assert not new_dir.exists() manager = TemplateManager(str(new_dir)) assert new_dir.exists() assert manager.env is not None def test_init_with_existing_directory(self, tmp_path): """Инициализация с существующей директорией.""" existing_dir = tmp_path / "existing_templates" existing_dir.mkdir() manager = TemplateManager(str(existing_dir)) assert manager.template_dir == str(existing_dir) assert manager.env is not None class TestTemplateManagerRender: """Тесты рендеринга шаблонов.""" def test_render_simple_template(self, tmp_path): """Рендеринг простого шаблона.""" template_dir = tmp_path / "templates" template_dir.mkdir() template_file = template_dir / "simple.html.j2" template_file.write_text("<h1>{{ title }}</h1>") manager = TemplateManager(str(template_dir)) result = manager.render("simple.html.j2", title="Test Title") assert "<h1>Test Title</h1>" in result def test_render_template_with_loop(self, tmp_path): """Рендеринг шаблона с циклом.""" template_dir = tmp_path / "templates" template_dir.mkdir() template_file = template_dir / "list.html.j2" template_file.write_text( "<ul>{% for item in items %}<li>{{ item }}</li>{% endfor %}</ul>" ) manager = TemplateManager(str(template_dir)) result = manager.render("list.html.j2", items=["a", "b", "c"]) assert "<ul>" in result assert "<li>a</li>" in result assert "<li>b</li>" in result assert "<li>c</li>" in result def test_render_template_not_found(self, tmp_path): """Рендеринг несуществующего шаблона.""" template_dir = tmp_path / "templates" template_dir.mkdir() manager = TemplateManager(str(template_dir)) with pytest.raises(Exception): # jinja2.exceptions.TemplateNotFound manager.render("nonexistent.html.j2") def test_render_empty_template(self, tmp_path): """Рендеринг пустого шаблона.""" template_dir = tmp_path / "templates" template_dir.mkdir() template_file = template_dir / "empty.html.j2" template_file.write_text("") manager = TemplateManager(str(template_dir)) result = manager.render("empty.html.j2") assert result == "" def test_render_with_special_characters(self, tmp_path): """Рендеринг шаблона со специальными символами.""" template_dir = tmp_path / "templates" template_dir.mkdir() template_file = template_dir / "special.html.j2" template_file.write_text("<div>{{ message }}</div>") manager = TemplateManager(str(template_dir)) result = manager.render("special.html.j2", message='Hello <World> & "Test"') # Jinja2 автоматически экранирует HTML assert "Hello <World>" in result assert "&" in result class TestTemplateManagerInitEnvironment: """Тесты инициализации окружения Jinja2.""" def test_init_environment_autoescape(self, tmp_path): """Autoescape должен быть включен.""" template_dir = tmp_path / "templates" template_dir.mkdir() manager = TemplateManager(str(template_dir)) assert manager.env is not None assert manager.env.autoescape is True def test_init_environment_filesystem_loader(self, tmp_path): """Должен быть использован FileSystemLoader.""" template_dir = tmp_path / "templates" template_dir.mkdir() manager = TemplateManager(str(template_dir)) assert manager.env is not None assert manager.env.loader is not None