Coverage for gws-app/gws/plugin/model_field/related_multi_feature_list/_test.py: 0%
101 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
1import gws
2import gws.test.util as u
5@u.fixture(scope='module')
6def root():
7 u.pg.create('parent', {'id': 'int primary key', 'k': 'int', 'pp': 'text'})
8 u.pg.create('parent_auto', {'id': 'int primary key generated always as identity', 'k': 'int', 'pp': 'text'})
10 u.pg.create('a', {'id': 'int primary key', 'cc': 'text', 'parent_k': 'int'})
11 u.pg.create('b', {'id': 'int primary key', 'cc': 'text', 'parent_k': 'int'})
12 u.pg.create('c', {'id': 'int primary key', 'cc': 'text', 'parent_k': 'int'})
14 cfg = '''
15 models+ {
16 uid "PARENT" type "postgres" tableName "parent"
17 fields+ { name "id" type "integer" }
18 fields+ { name "k" type "text" }
19 fields+ { name "pp" type "text" }
20 fields+ {
21 name "children"
22 type relatedMultiFeatureList
23 fromColumn "k"
24 related+ { toModel "A" toColumn "parent_k" }
25 related+ { toModel "B" toColumn "parent_k" }
26 related+ { toModel "C" toColumn "parent_k" }
27 }
28 }
30 models+ {
31 uid "PARENT_AUTO" type "postgres" tableName "parent_auto"
32 fields+ { name "id" type "integer" }
33 fields+ { name "k" type "text" }
34 fields+ { name "pp" type "text" }
35 fields+ {
36 name "children"
37 type relatedMultiFeatureList
38 fromColumn "k"
39 related+ { toModel "A" toColumn "parent_k" }
40 related+ { toModel "B" toColumn "parent_k" }
41 related+ { toModel "C" toColumn "parent_k" }
42 }
43 }
45 models+ { uid "A" type "postgres" tableName "a" fields+ { name "id" type "integer" } fields+ { name "cc" type "text" } }
46 models+ { uid "B" type "postgres" tableName "b" fields+ { name "id" type "integer" } fields+ { name "cc" type "text" } }
47 models+ { uid "C" type "postgres" tableName "c" fields+ { name "id" type "integer" } fields+ { name "cc" type "text" } }
48 '''
50 yield u.gws_root(cfg)
53def test_find_no_depth(root: gws.Root):
54 mc = u.model_context()
56 u.pg.insert('parent', [
57 {'id': 1, 'k': 11, 'pp': 'p1'},
58 {'id': 2, 'k': 22, 'pp': 'p2'},
59 ])
61 parent = u.cast(gws.Model, root.get('PARENT'))
62 fs = parent.get_features([1, 2], mc)
64 assert set(f.get('pp') for f in fs) == {'p1', 'p2'}
65 assert fs[0].get('children') is None
66 assert fs[1].get('children') is None
69def test_find_depth(root: gws.Root):
70 mc = u.model_context(maxDepth=1)
72 u.pg.insert('parent', [
73 {'id': 1, 'k': 11, 'pp': 'p1'},
74 {'id': 2, 'k': 22, 'pp': 'p2'},
75 {'id': 3, 'k': 33, 'pp': 'p3'},
76 ])
77 u.pg.insert('a', [
78 {'id': 1, 'cc': 'a1', 'parent_k': 11},
79 {'id': 2, 'cc': 'a2', 'parent_k': 22},
80 {'id': 3, 'cc': 'a3', 'parent_k': 99},
81 ])
82 u.pg.insert('b', [
83 {'id': 1, 'cc': 'b1', 'parent_k': 11},
84 {'id': 2, 'cc': 'b2', 'parent_k': 11},
85 ])
86 u.pg.insert('c', [
87 {'id': 1, 'cc': 'c1', 'parent_k': 22},
88 {'id': 2, 'cc': 'c2', 'parent_k': 99},
89 ])
91 parent = u.cast(gws.Model, root.get('PARENT'))
92 fs = parent.get_features([1, 2, 3], mc)
94 assert [f.get('pp') for f in fs] == ['p1', 'p2', 'p3']
96 assert [c.get('cc') for c in fs[0].get('children')] == ['a1', 'b1', 'b2']
97 assert [c.get('cc') for c in fs[1].get('children')] == ['a2', 'c1']
98 assert [c.get('cc') for c in fs[2].get('children')] == []
101_SELECT_ALL = '''
102 SELECT p.t, p.id, p.parent_k FROM (
103 (SELECT 'a' AS t, id, parent_k FROM a)
104 UNION
105 (SELECT 'b' AS t, id, parent_k FROM b)
106 UNION
107 (SELECT 'c' AS t, id, parent_k FROM c)
108 ) AS p
109 ORDER BY p.t, p.id, p.parent_k
110'''
113def test_update(root: gws.Root):
114 mc = u.model_context(maxDepth=1)
116 u.pg.insert('parent', [
117 {'id': 1, 'k': 11, 'pp': 'p1'},
118 {'id': 2, 'k': 22, 'pp': 'p2'},
119 {'id': 3, 'k': 33, 'pp': 'p3'},
120 ])
121 u.pg.insert('a', [
122 {'id': 1, 'cc': 'a1', 'parent_k': 11},
123 {'id': 2, 'cc': 'a2', 'parent_k': 11},
124 {'id': 3, 'cc': 'a3', 'parent_k': 99},
125 ])
126 u.pg.insert('b', [
127 {'id': 3, 'cc': 'b3', 'parent_k': 11},
128 {'id': 4, 'cc': 'b4', 'parent_k': 11},
129 ])
130 u.pg.insert('c', [
131 {'id': 5, 'cc': 'c5', 'parent_k': 11},
132 {'id': 6, 'cc': 'c6', 'parent_k': 11},
133 ])
135 parent = u.cast(gws.Model, root.get('PARENT'))
136 a = u.cast(gws.Model, root.get('A'))
137 b = u.cast(gws.Model, root.get('B'))
138 c = u.cast(gws.Model, root.get('C'))
140 f = u.feature(parent, id=1, children=[
141 u.feature(a, id=1),
142 u.feature(a, id=3),
143 u.feature(b, id=4),
144 ])
146 parent.update_feature(f, mc)
148 assert u.pg.rows(_SELECT_ALL) == [
149 ('a', 1, 11),
150 ('a', 2, None),
151 ('a', 3, 11),
152 ('b', 3, None),
153 ('b', 4, 11),
154 ('c', 5, None),
155 ('c', 6, None),
156 ]
159def test_create(root: gws.Root):
160 mc = u.model_context(maxDepth=1)
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 u.pg.insert('a', [
168 {'id': 1, 'cc': 'a1', 'parent_k': 11},
169 {'id': 2, 'cc': 'a2', 'parent_k': 11},
170 {'id': 3, 'cc': 'a2', 'parent_k': 11},
171 ])
172 u.pg.insert('b', [
173 {'id': 3, 'cc': 'b3', 'parent_k': 11},
174 {'id': 4, 'cc': 'b4', 'parent_k': 11},
175 ])
176 u.pg.insert('c', [
177 {'id': 5, 'cc': 'c5', 'parent_k': 11},
178 {'id': 6, 'cc': 'c6', 'parent_k': 11},
179 ])
181 parent = u.cast(gws.Model, root.get('PARENT'))
182 a = u.cast(gws.Model, root.get('A'))
183 b = u.cast(gws.Model, root.get('B'))
184 c = u.cast(gws.Model, root.get('C'))
186 f = u.feature(parent, id=999, k=99, children=[
187 u.feature(a, id=1),
188 u.feature(a, id=3),
189 u.feature(b, id=4),
190 ])
192 parent.create_feature(f, mc)
194 assert u.pg.rows(_SELECT_ALL) == [
195 ('a', 1, 99),
196 ('a', 2, 11),
197 ('a', 3, 99),
198 ('b', 3, 11),
199 ('b', 4, 99),
200 ('c', 5, 11),
201 ('c', 6, 11),
202 ]
205def test_create_auto(root: gws.Root):
206 mc = u.model_context(maxDepth=1)
208 u.pg.insert('parent', [
209 {'id': 1, 'k': 11, 'pp': 'p1'},
210 {'id': 2, 'k': 22, 'pp': 'p2'},
211 {'id': 3, 'k': 33, 'pp': 'p3'},
212 ])
213 u.pg.insert('a', [
214 {'id': 1, 'cc': 'a1', 'parent_k': 11},
215 {'id': 2, 'cc': 'a2', 'parent_k': 11},
216 {'id': 3, 'cc': 'a2', 'parent_k': 11},
217 ])
218 u.pg.insert('b', [
219 {'id': 3, 'cc': 'b3', 'parent_k': 11},
220 {'id': 4, 'cc': 'b4', 'parent_k': 11},
221 ])
222 u.pg.insert('c', [
223 {'id': 5, 'cc': 'c5', 'parent_k': 11},
224 {'id': 6, 'cc': 'c6', 'parent_k': 11},
225 ])
227 parent_auto = u.cast(gws.Model, root.get('PARENT_AUTO'))
228 a = u.cast(gws.Model, root.get('A'))
229 b = u.cast(gws.Model, root.get('B'))
230 c = u.cast(gws.Model, root.get('C'))
232 f = u.feature(parent_auto, k=99, children=[
233 u.feature(a, id=1),
234 u.feature(a, id=3),
235 u.feature(b, id=4),
236 ])
238 parent_auto.create_feature(f, mc)
240 assert u.pg.rows(_SELECT_ALL) == [
241 ('a', 1, 99),
242 ('a', 2, 11),
243 ('a', 3, 99),
244 ('b', 3, 11),
245 ('b', 4, 99),
246 ('c', 5, 11),
247 ('c', 6, 11),
248 ]
251def test_delete(root: gws.Root):
252 mc = u.model_context(maxDepth=1)
254 u.pg.insert('parent', [
255 {'id': 1, 'k': 11, 'pp': 'p1'},
256 {'id': 2, 'k': 22, 'pp': 'p2'},
257 ])
258 u.pg.insert('a', [
259 {'id': 1, 'cc': 'a1', 'parent_k': 11},
260 {'id': 2, 'cc': 'a2', 'parent_k': 11},
261 {'id': 3, 'cc': 'a2', 'parent_k': 22},
262 ])
263 u.pg.insert('b', [
264 {'id': 3, 'cc': 'b3', 'parent_k': 11},
265 {'id': 4, 'cc': 'b4', 'parent_k': 22},
266 ])
267 u.pg.insert('c', [
268 {'id': 5, 'cc': 'c5', 'parent_k': 22},
269 {'id': 6, 'cc': 'c6', 'parent_k': 11},
270 ])
272 parent = u.cast(gws.Model, root.get('PARENT'))
274 f = u.feature(parent, id=1)
275 parent.delete_feature(f, mc)
277 assert u.pg.rows(_SELECT_ALL) == [
278 ('a', 1, None),
279 ('a', 2, None),
280 ('a', 3, 22),
281 ('b', 3, None),
282 ('b', 4, 22),
283 ('c', 5, 22),
284 ('c', 6, None),
285 ]
288def test_create_related(root: gws.Root):
289 mc = u.model_context(maxDepth=1)
291 u.pg.insert('parent', [
292 {'id': 1, 'k': 11, 'pp': 'p1'},
293 ])
294 u.pg.insert('a', [
295 {'id': 1, 'cc': 'a1', 'parent_k': 11},
296 ])
297 u.pg.insert('b', [
298 {'id': 3, 'cc': 'b3', 'parent_k': 22},
299 ])
300 u.pg.insert('c', [
301 {'id': 5, 'cc': 'c5', 'parent_k': 33},
302 ])
304 parent = u.cast(gws.Model, root.get('PARENT'))
305 a = u.cast(gws.Model, root.get('A'))
306 b = u.cast(gws.Model, root.get('B'))
307 c = u.cast(gws.Model, root.get('C'))
309 a_f = u.feature(a, id=101)
310 a_f.createWithFeatures = [u.feature(parent, id=1)]
311 a.create_feature(a_f, mc)
313 b_f = u.feature(b, id=201)
314 b_f.createWithFeatures = [u.feature(parent, id=1)]
315 b.create_feature(b_f, mc)
317 c_f = u.feature(c, id=202)
318 c_f.createWithFeatures = [u.feature(parent, id=1)]
319 c.create_feature(c_f, mc)
321 assert u.pg.rows(_SELECT_ALL) == [
322 ('a', 1, 11),
323 ('a', 101, 11),
324 ('b', 3, 22),
325 ('b', 201, 11),
326 ('c', 5, 33),
327 ('c', 202, 11),
328 ]