diff options
| author | Danilo M. <danix@danix.xyz> | 2026-10-28 10:25:46 +0100 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-10-28 10:25:46 +0100 |
| commit | fc51e791dfbc502900d4dbf93928e5ff1633a33f (patch) | |
| tree | 0d2d1d1757755a019f6a0e320d1e83afae0cfc28 /tests/test_library.py | |
| parent | 34d6ca51955fc2a10de326de5d2ac8f81cf660bb (diff) | |
| download | generat3D-fc51e791dfbc502900d4dbf93928e5ff1633a33f.tar.gz generat3D-fc51e791dfbc502900d4dbf93928e5ff1633a33f.zip | |
Add DSON URL parsing and library lookup
Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests/test_library.py')
| -rw-r--r-- | tests/test_library.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/tests/test_library.py b/tests/test_library.py new file mode 100644 index 0000000..84fa3cc --- /dev/null +++ b/tests/test_library.py @@ -0,0 +1,91 @@ +# 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 gzip +import json +import tempfile +import unittest +from pathlib import Path + +from generat3d import Error +from generat3d.library import Library, parse_url, read_dson + + +class ParseUrlTest(unittest.TestCase): + def test_full(self): + self.assertEqual( + parse_url("lShldrBend:/data/Daz%203D/G9.dsf#lShldrBend?center_point/x"), + ("lShldrBend", "/data/Daz 3D/G9.dsf", "lShldrBend", "center_point/x")) + + def test_path_and_id(self): + self.assertEqual(parse_url("/data/a%20b.dsf#m1"), ("", "/data/a b.dsf", "m1", "")) + + def test_local_ref(self): + self.assertEqual(parse_url("#hip"), ("", "", "hip", "")) + + def test_node_with_local_ref(self): + self.assertEqual(parse_url("Fig:#Tall?value"), ("Fig", "", "Tall", "value")) + + +class LibraryTest(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + base = Path(self.tmp.name) + self.r1, self.r2 = base / "r1", base / "r2" + (self.r1 / "data/Vendor/Fig/Morphs").mkdir(parents=True) + (self.r2 / "data/vendor/fig").mkdir(parents=True) + (self.r1 / "data/Vendor/Fig/Morphs/A.dsf").write_text('{"a": 1}') + (self.r2 / "data/vendor/fig/B.dsf").write_bytes(gzip.compress(b'{"b": 2}')) + (self.r2 / "data/vendor/fig/Bad.dsf").write_text("{nope") + self.lib = Library([self.r1, self.r2]) + + def tearDown(self): + self.tmp.cleanup() + + def test_exact_match(self): + self.assertEqual(self.lib.find("/data/Vendor/Fig/Morphs/A.dsf"), + self.r1 / "data/Vendor/Fig/Morphs/A.dsf") + + def test_case_insensitive_second_root(self): + self.assertEqual(self.lib.find("/data/Vendor/Fig/B.dsf"), + self.r2 / "data/vendor/fig/B.dsf") + + def test_missing(self): + self.assertIsNone(self.lib.find("/data/Nope.dsf")) + + def test_load_gzip_and_cache(self): + doc = self.lib.load("/data/Vendor/Fig/B.dsf") + self.assertEqual(doc, {"b": 2}) + self.assertIs(self.lib.load("/data/vendor/fig/b.dsf"), doc) + + def test_load_missing_is_fatal(self): + with self.assertRaisesRegex(Error, "not found in library roots: /data/Nope.dsf"): + self.lib.load("/data/Nope.dsf") + + def test_broken_json_names_file(self): + with self.assertRaisesRegex(Error, "Bad.dsf"): + self.lib.load("/data/Vendor/Fig/Bad.dsf") + + def test_walk_merges_roots(self): + self.assertEqual(sorted(self.lib.walk("/data/Vendor/Fig")), + ["/data/Vendor/Fig/Morphs/A.dsf", + "/data/vendor/fig/B.dsf", "/data/vendor/fig/Bad.dsf"]) + + def test_read_dson_plain(self): + p = Path(self.tmp.name) / "x.duf" + p.write_text(json.dumps({"x": 1})) + self.assertEqual(read_dson(p), {"x": 1}) |
