Spaces:
Running
Running
Sahil Kumar
Claude Opus 5
deploy: split the tree into backend/frontend/database and centralise hosts
2e740d5 Download tests/conftest.py from sahil-12kumar/IL_CMS_Tools: direct link, hf CLI and curl.
- Browser
- Download file 3.43 kB
-
https://huggingface.co/spaces/sahil-12kumar/IL_CMS_Tools/resolve/main/tests/conftest.py
- Command line
-
hf download hf://spaces/sahil-12kumar/IL_CMS_Tools/tests/conftest.py
-
curl -L -o conftest.py https://huggingface.co/spaces/sahil-12kumar/IL_CMS_Tools/resolve/main/tests/conftest.py
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' | |
| 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>') | |
| 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() | |
| 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 | |
| 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 | |
| def _reset_stylesheet_cache(app_mod): | |
| """get_omml2mml() memoises into a module global; keep tests independent.""" | |
| yield | |
| app_mod._omml2mml = None | |