add tests

This commit is contained in:
Pavel Sobolev
2025-11-13 21:30:45 +03:00
parent b506f55060
commit 80911bd538
11 changed files with 363 additions and 7 deletions

View File

@@ -1,3 +0,0 @@
"""Тесты приложения `cv`."""
# Create your tests here.

1
cv/tests/__init__.py Normal file
View File

@@ -0,0 +1 @@
"""Тестовый пакет приложения `cv`."""

View File

@@ -0,0 +1,74 @@
"""Тесты для `DocxRenderer` и сохранения DOCX в буфер."""
import logging
from io import BytesIO
import pytest
from cv.services.dowload.docx import DocxRenderer
from resume.utils.logging import configure_root_logger
logger = logging.getLogger(__name__)
configure_root_logger()
@pytest.fixture
def profile() -> object:
"""Минимальный фейковый профиль.
Returns:
object: Объект с обязательными атрибутами и менеджерами all().
"""
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_docx_render_unit(monkeypatch: pytest.MonkeyPatch, profile: object) -> None:
"""Юнит: замокаем serialize и save, проверим байты и единичный вызов.
Args:
monkeypatch (pytest.MonkeyPatch): Инструмент подмены атрибутов.
profile (object): Фейковый профиль.
"""
logger.info("Юнит-тест DOCX: проверяем проводку до save()")
fake_serialized = {
"full_name": "Jane Doe",
"role": "Dev",
"summary": "Summary",
"location": "Earth",
"languages": ["EN"],
"contacts": {"email": "jane@example.com", "phone": "", "telegram": ""},
"experience": [],
"skills_map": [],
}
expected_docx = b"PK\x03\x04FAKE-DOCX" # в юните можно вернуть фиктивные байты
def fake_serialize(_self: object, _p: object) -> dict:
"""Детерминированная сериализация."""
return fake_serialized
calls = {"save": 0}
def fake_save(self: object, stream: BytesIO) -> None: # noqa: D417
"""Подмена docx.document.Document.save — пишет ожидаемые байты.
Args:
stream (BytesIO): Целевой буфер.
"""
calls["save"] += 1
stream.write(expected_docx)
monkeypatch.setattr("cv.services.dowload.docx.ProfileSerializer.serialize", fake_serialize)
monkeypatch.setattr("docx.document.Document.save", fake_save, raising=True)
out = DocxRenderer().render(profile)
assert isinstance(out, BytesIO)
assert out.getvalue() == expected_docx
assert calls["save"] == 1

View File

@@ -0,0 +1,93 @@
"""Тесты для `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))

68
cv/tests/test_views.py Normal file
View File

@@ -0,0 +1,68 @@
"""Интеграционные тесты Django views приложения `cv`."""
import logging
from io import BytesIO
import pytest
from django.test import Client
from django.urls import reverse
from cv.models import Profile
from resume.utils.logging import configure_root_logger
logger = logging.getLogger(__name__)
configure_root_logger()
@pytest.mark.django_db
def test_profile_view_context(client: Client) -> None:
"""Контекст содержит profile при наличии записи.
Args:
client (Client): Django тестовый клиент.
"""
logger.info("Создаём профиль и проверяем контекст главной страницы")
Profile.objects.create(
full_name="John Tester", role="QA", gender="male", summary="", location="", languages=[]
)
resp = client.get("/")
assert resp.status_code == 200
assert "profile" in resp.context
@pytest.mark.django_db
def test_download_pdf_ok(client: Client, monkeypatch: pytest.MonkeyPatch) -> None:
"""Выдача PDF c корректными заголовками.
Args:
client (Client): Django тестовый клиент.
monkeypatch (pytest.MonkeyPatch): Подмена рендера PDF.
"""
logger.info("Проверяем скачивание PDF с реальным роутом")
Profile.objects.create(
full_name="John Tester", role="QA", gender="male", summary="", location="", languages=[]
)
monkeypatch.setattr("cv.views.PdfRenderer.render", lambda _self, _p: BytesIO(b"%PDF"))
resp = client.get(reverse("cv:resume-pdf"))
assert resp.status_code == 200
assert resp["Content-Type"] == "application/pdf"
assert resp["Cache-Control"] == "no-store"
assert "resume_John_Tester.pdf" in resp["Content-Disposition"]
@pytest.mark.django_db
def test_download_docx_ok(client: Client) -> None:
"""Smoke: скачивание DOCX с корректными заголовками."""
logger.info("Проверяем скачивание DOCX")
Profile.objects.create(
full_name="John Tester", role="QA", gender="male", summary="", location="", languages=[]
)
resp = client.get(reverse("cv:resume-docx"))
assert resp.status_code == 200
assert (
resp["Content-Type"]
== "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
assert resp["Cache-Control"] == "no-store"
assert "attachment; filename=" in resp["Content-Disposition"]
assert "resume_John_Tester.docx" in resp["Content-Disposition"]