diff options
| author | Danilo M. <danix@danix.xyz> | 2026-10-28 10:36:48 +0100 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-10-28 10:36:48 +0100 |
| commit | 6d03ca3093f0bf9d91f6d9f978c0a715b8060bab (patch) | |
| tree | 1b34fc5851187cf190a73ddeb5bb1ca0b76ecbc1 /tests/test_dbz.py | |
| parent | 6304f9b074452af3648266fe23c773f8fb478270 (diff) | |
| download | generat3D-6d03ca3093f0bf9d91f6d9f978c0a715b8060bab.tar.gz generat3D-6d03ca3093f0bf9d91f6d9f978c0a715b8060bab.zip | |
Build and atomically write .dbz documents
Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests/test_dbz.py')
| -rw-r--r-- | tests/test_dbz.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/tests/test_dbz.py b/tests/test_dbz.py new file mode 100644 index 0000000..7bcf69a --- /dev/null +++ b/tests/test_dbz.py @@ -0,0 +1,79 @@ +# generat3D - evaluate Daz DSON scenes and write .dbz files +# Copyright (C) 2026 Danilo M. <danix@danix.xyz> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import contextlib +import gzip +import io +import json +import math +import tempfile +import unittest +from pathlib import Path + +from generat3d.dbz import build, orientation_quat, write +from generat3d.library import Library +from generat3d.scene import load + +DATA = Path(__file__).parent / "data" + + +class DbzTest(unittest.TestCase): + def setUp(self): + with contextlib.redirect_stderr(io.StringIO()): + self.doc = build(load(DATA / "scene.duf", Library([DATA / "lib"])), [DATA / "lib"]) + + def test_header(self): + self.assertEqual(self.doc["application"], "export_to_blender") + self.assertEqual(self.doc["figures"][-1], {"name": "dummy", "num verts": 0}) + + def test_figure_entry(self): + fig = self.doc["figures"][0] + self.assertEqual((fig["name"], fig["id"], fig["label"], fig["class"]), + ("Fig", "Fig", "Test Figure", "DzFigure")) + self.assertEqual(fig["num verts"], 3) + self.assertEqual(fig["vertices"][1], [1.5, 2.0, 0.0]) + self.assertEqual(fig["material groups"], ["Skin"]) + self.assertEqual(fig["orientation"], [0.0, 0.0, 0.0, 1.0]) + self.assertNotIn("ws_transform", fig) + + def test_bones(self): + hip = self.doc["figures"][0]["bones"][0] + self.assertEqual(hip["name"], "hip") + self.assertEqual(hip["center_point"], [0, 7, 0]) + self.assertEqual(hip["origin"], [0, 7, 0]) + self.assertEqual(hip["rotation_order"], "YZX") + + def test_orientation_quat(self): + x, y, z, w = orientation_quat([90, 0, 0]) + self.assertAlmostEqual(x, math.sqrt(0.5)) + self.assertAlmostEqual(w, math.sqrt(0.5)) + self.assertAlmostEqual(y, 0) + self.assertAlmostEqual(z, 0) + + def test_write_is_gzip_json_and_atomic(self): + with tempfile.TemporaryDirectory() as d: + out = Path(d) / "s.dbz" + write(self.doc, out) + self.assertEqual(json.loads(gzip.decompress(out.read_bytes())), self.doc) + self.assertEqual([p.name for p in Path(d).iterdir()], ["s.dbz"]) + + def test_failed_write_leaves_nothing(self): + with tempfile.TemporaryDirectory() as d: + out = Path(d) / "s.dbz" + with self.assertRaises(TypeError): + write({"bad": object()}, out) + self.assertEqual(list(Path(d).iterdir()), []) |
