Coverage for gws-app/gws/core/_test/tree_test.py: 100%
111 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 23:09 +0200
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 23:09 +0200
1from typing import Optional, cast
3import gws
4import gws.spec.runtime
5import gws.test.util as u
6from gws.base.auth.user import User
9# @formatter:off
11class H1(gws.Node): pass
12class H2(gws.Node): pass
14class A1(gws.Node): pass
15class A2(gws.Node): pass
17class X(gws.Node):
18 def configure(self):
19 u.log.write('X')
21class Y(X):
22 def configure(self):
23 u.log.write('Y')
25class Z(Y):
26 def configure(self):
27 u.log.write('Z')
29# @formatter:on
32@u.fixture(scope='function')
33def root():
34 r = u.gws_root('', defaults=False)
36 r.specs.register_object(gws.ext.object.helper, 'H1', H1)
37 r.specs.register_object(gws.ext.object.helper, 'H2', H2)
39 r.specs.register_object(gws.ext.object.action, 'A1', A1)
40 r.specs.register_object(gws.ext.object.action, 'A2', A2)
42 r.specs.register_object(gws.ext.object.helper, 'X', X)
43 r.specs.register_object(gws.ext.object.helper, 'Y', Y)
44 r.specs.register_object(gws.ext.object.helper, 'Z', Z)
46 yield r
49def test_create(root: gws.Root):
50 a1 = root.create(gws.ext.object.action, type='A1')
51 a2 = root.create(gws.ext.object.action, type='A2')
53 assert not root.configErrors
54 assert root.find_all(gws.ext.object.action) == [a1, a2]
57def test_create_shared(root: gws.Root):
58 a1 = root.create_shared(gws.ext.object.action, type='A1', uid='x')
59 a2 = root.create_shared(A1, uid='x')
61 assert not root.configErrors
62 assert a1 == a2
65def test_create_child(root: gws.Root):
66 a = root.create(A1)
67 b1 = a.create_child(H1)
68 b2 = a.create_child(H2)
70 assert not root.configErrors
71 assert a.children == [b1, b2]
74def test_create_child_if_configured(root: gws.Root):
75 a = root.create(A1)
76 b1 = a.create_child_if_configured(H1, gws.Config(foo=123))
77 b2 = a.create_child_if_configured(H1)
79 assert not root.configErrors
80 assert a.children == [b1]
81 assert b2 is None
84def test_auto_super_configure(root: gws.Root):
85 u.log.reset()
86 root.create(Z)
87 assert u.log.get() == ['X', 'Y', 'Z']
90def test_is_a(root: gws.Root):
91 a = root.create(A1)
93 assert a.is_a(gws.ext.object.action)
94 assert a.is_a('gws.ext.object.action')
95 assert a.is_a('gws.ext.object.action.A1')
96 assert a.is_a(A1)
98 assert a.is_a(gws.ext.object.helper) is False
99 assert a.is_a('gws.ext.object.helper') is False
102def test_find_all(root: gws.Root):
103 a = root.create(A1)
104 b = root.create(A1)
106 a1 = a.create_child(H1)
107 a2 = a.create_child(H2)
108 a3 = a.create_child(A2)
110 b1 = b.create_child(H1)
111 b2 = b.create_child(H2)
113 assert a.find_all(gws.ext.object.helper) == [a1, a2]
114 assert a.find_all(gws.ext.object.action) == [a3]
115 assert a.find_all(H1) == [a1]
116 assert a.find_all(gws.ext.object.model) == []
119def test_find_first(root: gws.Root):
120 a = root.create(A1)
122 a1 = a.create_child(H1)
123 a2 = a.create_child(H2)
124 a3 = a.create_child(A2)
126 assert a.find_first(gws.ext.object.helper) == a1
127 assert a.find_first(gws.ext.object.action) == a3
128 assert a.find_first(gws.ext.object.model) is None
130 assert root.find_first(gws.ext.object.action) == a
133def test_find_closest(root: gws.Root):
134 a = root.create(A1)
136 a1 = a.create_child(H1)
137 a2 = a1.create_child(H2)
138 a3 = a2.create_child(A2)
139 a4 = a3.create_child(H2)
141 assert a4.find_closest(gws.ext.object.action) == a3
142 assert a4.find_closest(gws.ext.object.helper) == a2
143 assert a4.find_closest(H1) == a1
146def test_find_ancestors(root: gws.Root):
147 a = root.create(A1)
149 a1 = a.create_child(H1)
150 a2 = a1.create_child(H2)
151 a3 = a2.create_child(A2)
152 a4 = a3.create_child(H2)
154 assert a4.find_ancestors(gws.ext.object.action) == [a3, a]
155 assert a4.find_ancestors() == [a3, a2, a1, a]
158def test_find_descendants(root: gws.Root):
159 a = root.create(A1)
161 a1 = a.create_child(H1)
162 a2 = a1.create_child(H2)
163 b1 = a.create_child(A2)
164 b2 = b1.create_child(H2)
165 b3 = b1.create_child(A2)
167 assert a.find_descendants(gws.ext.object.action) == [b1, b3]
168 assert a.find_descendants() == [a1, a2, b1, b2, b3]