Files
resume/cv/tests/test_pdf_renderer.py
Pavel Sobolev 80911bd538 add tests
2025-11-13 21:30:45 +03:00

94 lines
3.0 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.
"""Тесты для `PdfRenderer` и его HTML-вывода."""
import logging
from typing import Any, cast
import pytest
from cv.models import Profile
from cv.services.dowload.pdf import PdfRenderer
from resume.utils.logging import configure_root_logger
logger = logging.getLogger(__name__)
configure_root_logger()
@pytest.fixture
def profile() -> object:
"""Минимальный fake-профиль для тестов.
Returns:
object: Объект с обязательными атрибутами.
"""
class P:
def __init__(self) -> None:
self.full_name = "Jane Doe"
self.experience = type("Q", (), {"all": lambda self: []})()
self.skills_map = type("Q", (), {"all": lambda self: []})()
return P()
def test_render_success(monkeypatch: pytest.MonkeyPatch, profile: object) -> None:
"""Юнит: валидируем HTML и что write_pdf вызван ровно один раз."""
logger.info("Проверяем успешный рендер PDF (валидация HTML)")
fake_serialized = {
"full_name": "Jane Doe",
"role": "Dev",
"summary": "Summary",
"location": "Earth",
"languages": ["EN"],
"contacts": {"email": "jane@example.com", "phone": "", "telegram": ""},
"experience": [],
"skills_map": [],
}
def fake_serialize(_self: object, _p: object) -> dict:
return fake_serialized
captured: dict[str, Any] = {"html": "", "calls": 0}
class RecordingHTML:
def __init__(self, string: str) -> None:
captured["html"] = string
def write_pdf(self) -> bytes:
captured["calls"] += 1
return b"%PDF-FAKE"
monkeypatch.setattr("cv.services.dowload.pdf.ProfileSerializer.serialize", fake_serialize)
monkeypatch.setattr("cv.services.dowload.pdf.HTML", RecordingHTML)
out = PdfRenderer().render(cast(Profile, profile))
assert captured["calls"] == 1
assert isinstance(out.getvalue(), bytes)
html: str = captured["html"]
assert "<!DOCTYPE html>" in html
assert "<title>Jane Doe — Резюме (PDF)</title>" in html
assert "<h1>Jane Doe</h1>" in html
assert "<h2>Контакты</h2>" in html
assert "<h2>Опыт работы</h2>" in html
assert "<h2>Навыки</h2>" in html
assert "@page { size: A4;" in html
def test_render_missing_full_name_raises(monkeypatch: pytest.MonkeyPatch, profile: object) -> None:
"""Должен бросать ValueError при пустом full_name.
Args:
monkeypatch (pytest.MonkeyPatch): Инструмент подмены атрибутов.
profile (object): Фейковый профиль.
"""
logger.info("Проверяем ошибку при отсутствии full_name")
monkeypatch.setattr(
"cv.services.dowload.pdf.ProfileSerializer.serialize",
lambda _self, _p: {"full_name": ""},
)
with pytest.raises(ValueError):
PdfRenderer().render(cast(Profile, profile))