75 lines
2.5 KiB
Python
75 lines
2.5 KiB
Python
"""Тесты для `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
|