Coverage for gws-app/gws/plugin/model_field/related_feature/_test.py: 0%

85 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +0200

1import gws 

2import gws.test.util as u 

3 

4 

5@u.fixture(scope='module') 

6def root(): 

7 u.pg.create('parent', {'id': 'int primary key', 'k': 'int', 'pp': 'text'}) 

8 u.pg.create('child', {'id': 'int primary key', 'cc': 'text', 'parent_k': 'int'}) 

9 u.pg.create('child_auto', {'id': 'int primary key generated always as identity', 'cc': 'text', 'parent_k': 'int'}) 

10 

11 cfg = ''' 

12 models+ {  

13 uid "PARENT" type "postgres" tableName "parent" 

14 fields+ { name "id" type "integer" } 

15 fields+ { name "k" type "integer" } 

16 fields+ { name "pp" type "text" } 

17 } 

18  

19 models+ {  

20 uid "CHILD" type "postgres" tableName "child" 

21 fields+ { name "id" type "integer" } 

22 fields+ { name "cc" type "text" } 

23 fields+ {  

24 name "parent"  

25 type relatedFeature  

26 fromColumn "parent_k"  

27 toModel "PARENT"  

28 toColumn "k"  

29 } 

30 } 

31 

32 models+ {  

33 uid "CHILD_AUTO" type "postgres" tableName "child_auto" 

34 fields+ { name "id" type "integer" } 

35 fields+ { name "cc" type "text" } 

36 fields+ {  

37 name "parent"  

38 type relatedFeature  

39 fromColumn "parent_k"  

40 toModel "PARENT"  

41 toColumn "k"  

42 } 

43 } 

44 ''' 

45 

46 yield u.gws_root(cfg) 

47 

48 

49def test_find_no_depth(root: gws.Root): 

50 mc = u.model_context(maxDepth=0) 

51 

52 u.pg.insert('parent', [ 

53 {'id': 1, 'k': 11, 'pp': 'p1'}, 

54 {'id': 2, 'k': 22, 'pp': 'p2'}, 

55 ]) 

56 u.pg.insert('child', [ 

57 {'id': 1, 'cc': 'c1', 'parent_k': 1}, 

58 {'id': 2, 'cc': 'c2', 'parent_k': 1}, 

59 {'id': 3, 'cc': 'c3', 'parent_k': 1}, 

60 ]) 

61 

62 child = u.cast(gws.Model, root.get('CHILD')) 

63 fs = child.get_features([2, 3], mc) 

64 

65 assert set(f.get('cc') for f in fs) == {'c2', 'c3'} 

66 assert fs[0].get('parent') is None 

67 assert fs[1].get('parent') is None 

68 

69 

70def test_find_depth(root: gws.Root): 

71 mc = u.model_context(maxDepth=1) 

72 

73 u.pg.insert('parent', [ 

74 {'id': 1, 'k': 11, 'pp': 'p1'}, 

75 {'id': 2, 'k': 22, 'pp': 'p2'}, 

76 ]) 

77 u.pg.insert('child', [ 

78 {'id': 1, 'cc': 'c1', 'parent_k': 11}, 

79 {'id': 2, 'cc': 'c2', 'parent_k': 11}, 

80 {'id': 3, 'cc': 'c3', 'parent_k': 22}, 

81 {'id': 4, 'cc': 'c4', 'parent_k': 99}, 

82 ]) 

83 

84 child = u.cast(gws.Model, root.get('CHILD')) 

85 fs = child.get_features([1, 2, 3, 4], mc) 

86 

87 assert set(f.get('cc') for f in fs) == {'c1', 'c2', 'c3', 'c4'} 

88 assert fs[0].get('parent').get('pp') == 'p1' 

89 assert fs[1].get('parent').get('pp') == 'p1' 

90 assert fs[2].get('parent').get('pp') == 'p2' 

91 assert fs[3].get('parent') is None 

92 

93 

94def test_update(root: gws.Root): 

95 mc = u.model_context(maxDepth=1) 

96 

97 u.pg.insert('parent', [ 

98 {'id': 1, 'k': 11, 'pp': 'p1'}, 

99 {'id': 2, 'k': 22, 'pp': 'p2'}, 

100 {'id': 3, 'k': 33, 'pp': 'p3'}, 

101 ]) 

102 u.pg.insert('child', [ 

103 {'id': 1, 'cc': 'c1', 'parent_k': 11}, 

104 {'id': 2, 'cc': 'c2', 'parent_k': 11}, 

105 {'id': 3, 'cc': 'c3', 'parent_k': 11}, 

106 {'id': 4, 'cc': 'c4', 'parent_k': 11}, 

107 ]) 

108 

109 parent = u.cast(gws.Model, root.get('PARENT')) 

110 child = u.cast(gws.Model, root.get('CHILD')) 

111 

112 f = u.feature(child, id=2, parent=u.feature(parent, id=3)) 

113 child.update_feature(f, mc) 

114 

115 rows = u.pg.rows('SELECT id, parent_k FROM child ORDER BY id') 

116 assert rows == [(1, 11), (2, 33), (3, 11), (4, 11)] 

117 

118 f = u.feature(child, id=3, parent=u.feature(parent, id=99)) 

119 child.update_feature(f, mc) 

120 

121 rows = u.pg.rows('SELECT id, parent_k FROM child ORDER BY id') 

122 assert rows == [(1, 11), (2, 33), (3, None), (4, 11)] 

123 

124 f = u.feature(child, id=4) 

125 child.update_feature(f, mc) 

126 

127 rows = u.pg.rows('SELECT id, parent_k FROM child ORDER BY id') 

128 assert rows == [(1, 11), (2, 33), (3, None), (4, 11)] 

129 

130 

131def test_create(root: gws.Root): 

132 mc = u.model_context(maxDepth=1) 

133 

134 u.pg.insert('parent', [ 

135 {'id': 1, 'k': 11, 'pp': 'p1'}, 

136 {'id': 2, 'k': 22, 'pp': 'p2'}, 

137 {'id': 3, 'k': 33, 'pp': 'p3'}, 

138 ]) 

139 u.pg.insert('child', [ 

140 {'id': 1, 'cc': 'c1', 'parent_k': 11}, 

141 ]) 

142 

143 parent = u.cast(gws.Model, root.get('PARENT')) 

144 child = u.cast(gws.Model, root.get('CHILD')) 

145 

146 f = u.feature(child, id=101, parent=u.feature(parent, id=1)) 

147 child.create_feature(f, mc) 

148 

149 rows = u.pg.rows('SELECT id, parent_k FROM child ORDER BY id') 

150 assert rows == [(1, 11), (101, 11)] 

151 

152 f = u.feature(child, id=102) 

153 child.create_feature(f, mc) 

154 

155 rows = u.pg.rows('SELECT id, parent_k FROM child ORDER BY id') 

156 assert rows == [(1, 11), (101, 11), (102, None)] 

157 

158 

159def test_create_auto(root: gws.Root): 

160 mc = u.model_context(maxDepth=1) 

161 

162 u.pg.insert('parent', [ 

163 {'id': 1, 'k': 11, 'pp': 'p1'}, 

164 {'id': 2, 'k': 22, 'pp': 'p2'}, 

165 {'id': 3, 'k': 33, 'pp': 'p3'}, 

166 ]) 

167 

168 parent = u.cast(gws.Model, root.get('PARENT')) 

169 child_auto = u.cast(gws.Model, root.get('CHILD_AUTO')) 

170 

171 f = u.feature(child_auto, parent=u.feature(parent, id=1)) 

172 child_auto.create_feature(f, mc) 

173 

174 rows = u.pg.rows('SELECT id, parent_k FROM child_auto ORDER BY id') 

175 assert rows == [(1, 11)] 

176 

177 f = u.feature(child_auto) 

178 child_auto.create_feature(f, mc) 

179 

180 rows = u.pg.rows('SELECT id, parent_k FROM child_auto ORDER BY id') 

181 assert rows == [(1, 11), (2, None)] 

182 

183 

184def test_create_related(root: gws.Root): 

185 mc = u.model_context(maxDepth=1) 

186 

187 u.pg.insert('parent', [ 

188 {'id': 1, 'k': 11, 'pp': 'p1'}, 

189 ]) 

190 u.pg.insert('child', [ 

191 {'id': 1, 'cc': 'c1', 'parent_k': 11}, 

192 {'id': 2, 'cc': 'c2', 'parent_k': 22}, 

193 ]) 

194 

195 parent = u.cast(gws.Model, root.get('PARENT')) 

196 child = u.cast(gws.Model, root.get('CHILD')) 

197 

198 parent_f = u.feature(parent, id=9, k=99) 

199 parent_f.createWithFeatures = [u.feature(child, id=2)] 

200 parent.create_feature(parent_f, mc) 

201 

202 rows = u.pg.rows('SELECT id, parent_k FROM child ORDER BY id') 

203 assert rows == [(1, 11), (2, 99)]