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 22:59 +0200

1from typing import Optional, cast 

2 

3import gws 

4import gws.spec.runtime 

5import gws.test.util as u 

6from gws.base.auth.user import User 

7 

8 

9# @formatter:off 

10 

11class H1(gws.Node): pass 

12class H2(gws.Node): pass 

13 

14class A1(gws.Node): pass 

15class A2(gws.Node): pass 

16 

17class X(gws.Node): 

18 def configure(self): 

19 u.log.write('X') 

20 

21class Y(X): 

22 def configure(self): 

23 u.log.write('Y') 

24 

25class Z(Y): 

26 def configure(self): 

27 u.log.write('Z') 

28 

29# @formatter:on 

30 

31 

32@u.fixture(scope='function') 

33def root(): 

34 r = u.gws_root('', defaults=False) 

35 

36 r.specs.register_object(gws.ext.object.helper, 'H1', H1) 

37 r.specs.register_object(gws.ext.object.helper, 'H2', H2) 

38 

39 r.specs.register_object(gws.ext.object.action, 'A1', A1) 

40 r.specs.register_object(gws.ext.object.action, 'A2', A2) 

41 

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) 

45 

46 yield r 

47 

48 

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') 

52 

53 assert not root.configErrors 

54 assert root.find_all(gws.ext.object.action) == [a1, a2] 

55 

56 

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') 

60 

61 assert not root.configErrors 

62 assert a1 == a2 

63 

64 

65def test_create_child(root: gws.Root): 

66 a = root.create(A1) 

67 b1 = a.create_child(H1) 

68 b2 = a.create_child(H2) 

69 

70 assert not root.configErrors 

71 assert a.children == [b1, b2] 

72 

73 

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) 

78 

79 assert not root.configErrors 

80 assert a.children == [b1] 

81 assert b2 is None 

82 

83 

84def test_auto_super_configure(root: gws.Root): 

85 u.log.reset() 

86 root.create(Z) 

87 assert u.log.get() == ['X', 'Y', 'Z'] 

88 

89 

90def test_is_a(root: gws.Root): 

91 a = root.create(A1) 

92 

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) 

97 

98 assert a.is_a(gws.ext.object.helper) is False 

99 assert a.is_a('gws.ext.object.helper') is False 

100 

101 

102def test_find_all(root: gws.Root): 

103 a = root.create(A1) 

104 b = root.create(A1) 

105 

106 a1 = a.create_child(H1) 

107 a2 = a.create_child(H2) 

108 a3 = a.create_child(A2) 

109 

110 b1 = b.create_child(H1) 

111 b2 = b.create_child(H2) 

112 

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) == [] 

117 

118 

119def test_find_first(root: gws.Root): 

120 a = root.create(A1) 

121 

122 a1 = a.create_child(H1) 

123 a2 = a.create_child(H2) 

124 a3 = a.create_child(A2) 

125 

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 

129 

130 assert root.find_first(gws.ext.object.action) == a 

131 

132 

133def test_find_closest(root: gws.Root): 

134 a = root.create(A1) 

135 

136 a1 = a.create_child(H1) 

137 a2 = a1.create_child(H2) 

138 a3 = a2.create_child(A2) 

139 a4 = a3.create_child(H2) 

140 

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 

144 

145 

146def test_find_ancestors(root: gws.Root): 

147 a = root.create(A1) 

148 

149 a1 = a.create_child(H1) 

150 a2 = a1.create_child(H2) 

151 a3 = a2.create_child(A2) 

152 a4 = a3.create_child(H2) 

153 

154 assert a4.find_ancestors(gws.ext.object.action) == [a3, a] 

155 assert a4.find_ancestors() == [a3, a2, a1, a] 

156 

157 

158def test_find_descendants(root: gws.Root): 

159 a = root.create(A1) 

160 

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) 

166 

167 assert a.find_descendants(gws.ext.object.action) == [b1, b3] 

168 assert a.find_descendants() == [a1, a2, b1, b2, b3]