IL_CMS_Tools / tests /conftest.py
Sahil Kumar
Claude Opus 5
deploy: split the tree into backend/frontend/database and centralise hosts
2e740d5
Raw History Blame Contribute Delete
3.43 kB
"""Shared fixtures: build .docx files in a temp dir rather than committing binaries,
so the OMML each test exercises is visible in the test that needs it.
"""
import io
import sys
from pathlib import Path
import pytest
from lxml import etree
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / 'backend'))
M_NS = 'http://schemas.openxmlformats.org/officeDocument/2006/math'
@pytest.fixture(scope='session')
def app_mod():
import app
return app
def mrun(text):
"""An OMML text run."""
return f'<m:r><m:t>{text}</m:t></m:r>'
def omath(inner):
"""Wrap OMML fragments in the <m:oMath> element Word puts in a paragraph."""
return etree.fromstring(
f'<m:oMath xmlns:m="{M_NS}">{inner}</m:oMath>')
# ── OMML fragments, written the way Word itself writes them ──────────────────
# A square root: Word marks the (empty) degree hidden rather than omitting it,
# which is what makes the stylesheet emit <msqrt> instead of an empty <mroot>.
SQRT_2GH = (f'<m:rad><m:radPr><m:degHide m:val="1"/></m:radPr><m:deg/>'
f'<m:e>{mrun("2gh")}</m:e></m:rad>')
B_SQUARED = f'<m:sSup><m:e>{mrun("b")}</m:e><m:sup>{mrun("2")}</m:sup></m:sSup>'
H2O = f'<m:sSub><m:e>{mrun("H")}</m:e><m:sub>{mrun("2")}</m:sub></m:sSub>{mrun("O")}'
HALF = f'<m:f><m:num>{mrun("1")}</m:num><m:den>{mrun("2")}</m:den></m:f>'
# Word's "boxed formula" and its diagonal-strike (cancel) variant.
BOXED = (f'<m:borderBox><m:borderBoxPr><m:hideTop m:val="0"/></m:borderBoxPr>'
f'<m:e>{mrun("E=mc")}</m:e></m:borderBox>')
CANCELLED = (f'<m:borderBox><m:borderBoxPr><m:strikeBLTR m:val="1"/></m:borderBoxPr>'
f'<m:e>{mrun("x")}</m:e></m:borderBox>')
@pytest.fixture
def png_bytes():
"""A small but genuine PNG."""
from PIL import Image
im = Image.new('RGB', (120, 60), 'white')
buf = io.BytesIO()
im.save(buf, 'PNG')
return buf.getvalue()
@pytest.fixture
def make_docx(tmp_path):
"""build([...]) -> path to a .docx. Each item is either a string (a plain
paragraph) or a list of parts: strings, ('math', omml_str) or ('img', bytes).
"""
from docx import Document
from docx.shared import Inches
counter = {'n': 0}
def build(items):
doc = Document()
for item in items:
if isinstance(item, str):
doc.add_paragraph(item)
continue
p = doc.add_paragraph()
for part in item:
if isinstance(part, str):
p.add_run(part)
elif part[0] == 'math':
p._p.append(omath(part[1]))
elif part[0] == 'img':
p.add_run().add_picture(io.BytesIO(part[1]), width=Inches(1.5))
counter['n'] += 1
out = tmp_path / f'q{counter["n"]}.docx'
doc.save(str(out))
return str(out)
return build
@pytest.fixture
def no_omml_stylesheet(app_mod, monkeypatch):
"""Simulate the Docker image: no Office, so OMML2MML.XSL is nowhere to be found."""
monkeypatch.setattr(app_mod, 'OMML2MML_PATHS', [r'C:\nonexistent\OMML2MML.XSL'])
monkeypatch.setattr(app_mod, '_omml2mml', None)
yield
app_mod._omml2mml = None
@pytest.fixture(autouse=True)
def _reset_stylesheet_cache(app_mod):
"""get_omml2mml() memoises into a module global; keep tests independent."""
yield
app_mod._omml2mml = None