Files
technical-visualization-haness/tests/test_renderers.py
T

49 lines
1.8 KiB
Python

from __future__ import annotations
import json
import tempfile
import unittest
import xml.etree.ElementTree as ET
from pathlib import Path
from techviz.layout import build_layout
from techviz.renderers import render_formats
from techviz.spec import load_spec
ROOT = Path(__file__).resolve().parents[1]
class RendererTests(unittest.TestCase):
def test_all_builtin_outputs_are_parseable(self) -> None:
spec = load_spec(ROOT / "examples/work/payment/spec.json")
layout = build_layout(spec)
with tempfile.TemporaryDirectory() as tmp:
outputs = render_formats(
spec,
layout,
tmp,
["svg", "mermaid", "d2", "dot", "drawio", "excalidraw", "a11y"],
)
by_suffix = {path.suffix: path for path in outputs}
ET.parse(by_suffix[".svg"])
ET.parse(by_suffix[".drawio"])
excalidraw = json.loads(by_suffix[".excalidraw"].read_text(encoding="utf-8"))
self.assertEqual(excalidraw["type"], "excalidraw")
self.assertGreater(len(excalidraw["elements"]), 0)
self.assertIn("flowchart LR", by_suffix[".mmd"].read_text(encoding="utf-8"))
self.assertIn("direction: right", by_suffix[".d2"].read_text(encoding="utf-8"))
self.assertIn("digraph techviz", by_suffix[".dot"].read_text(encoding="utf-8"))
alt_file = next(path for path in outputs if path.name.endswith(".alt.md"))
self.assertIn("## Long description", alt_file.read_text(encoding="utf-8"))
def test_layout_is_deterministic(self) -> None:
spec = load_spec(ROOT / "examples/work/payment/spec.json")
first = build_layout(spec)
second = build_layout(spec)
self.assertEqual(first, second)
if __name__ == "__main__":
unittest.main()