aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_library.py
blob: 7e06ca48f0780ac2d7aa659dd4173fdc1eba6079 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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})


class ReadDsonCrashTest(unittest.TestCase):
    def read(self, data):
        with tempfile.TemporaryDirectory() as d:
            p = Path(d) / "x.dsf"
            p.write_bytes(data)
            read_dson(p)

    def test_corrupt_gzip_is_error(self):
        with self.assertRaises(Error):
            self.read(b"\x1f\x8b\x08\x00" + b"\xff" * 20)

    def test_deep_nesting_is_error(self):
        with self.assertRaises(Error):
            self.read(b"[" * 200000)