Coverage for gws-app/gws/plugin/model_field/related_feature_list/_test.py: 0%
68 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'})
9 u.pg.create('child', {'id': 'int primary key', 'cc': 'text', 'parent_k': 'int'})
11 cfg = '''
12 models+ {
13 uid "PARENT" type "postgres" tableName "parent"
14 fields+ { name "id" type "integer" }
15 fields+ { name "k" type "text" }
16 fields+ { name "pp" type "text" }
17 fields+ {
18 name "children"
19 type relatedFeatureList
20 fromColumn "k"
21 toModel "CHILD"
22 toColumn "parent_k"
23 }
24 }
26 models+ {
27 uid "PARENT_AUTO" type "postgres" tableName "parent_auto"
28 fields+ { name "id" type "integer" }
29 fields+ { name "k" type "text" }
30 fields+ { name "pp" type "text" }
31 fields+ {
32 name "children"
33 type relatedFeatureList
34 fromColumn "k"
35 toModel "CHILD"
36 toColumn "parent_k"
37 }
38 }
40 models+ {
41 uid "CHILD" type "postgres" tableName "child"
42 fields+ { name "id" type "integer" }
43 fields+ { name "cc" type "text" }
44 }
45 '''
47 yield u.gws_root(cfg)
50def test_find_depth(root: gws.Root):
51 mc = u.model_context(maxDepth=1)
53 u.pg.insert('parent', [
54 {'id': 1, 'k': 11, 'pp': 'p1'},
55 {'id': 2, 'k': 22, 'pp': 'p2'},
56 {'id': 3, 'k': 33, 'pp': 'p3'},
57 ])
58 u.pg.insert('child', [
59 {'id': 4, 'cc': 'a4', 'parent_k': 11},
60 {'id': 5, 'cc': 'a5', 'parent_k': 22},
61 {'id': 6, 'cc': 'a6', 'parent_k': 11},
62 {'id': 7, 'cc': 'a7', 'parent_k': 11},
63 {'id': 8, 'cc': 'a8', 'parent_k': 99},
64 ])
66 parent = u.cast(gws.Model, root.get('PARENT'))
67 fs = parent.get_features([1, 2, 3], mc)
69 assert [f.get('pp') for f in fs] == ['p1', 'p2', 'p3']
71 assert [c.get('cc') for c in fs[0].get('children')] == ['a4', 'a6', 'a7']
72 assert [c.get('cc') for c in fs[1].get('children')] == ['a5']
73 assert [c.get('cc') for c in fs[2].get('children')] == []
76def test_update(root: gws.Root):
77 mc = u.model_context(maxDepth=1)
79 u.pg.insert('parent', [
80 {'id': 1, 'k': 11, 'pp': 'p1'},
81 {'id': 2, 'k': 22, 'pp': 'p2'},
82 {'id': 3, 'k': 33, 'pp': 'p3'},
83 ])
84 u.pg.insert('child', [
85 {'id': 4, 'cc': 'a4', 'parent_k': 11},
86 {'id': 5, 'cc': 'a5', 'parent_k': 22},
87 {'id': 6, 'cc': 'a6', 'parent_k': 11},
88 {'id': 7, 'cc': 'a7', 'parent_k': 33},
89 ])
91 parent = u.cast(gws.Model, root.get('PARENT'))
92 child = u.cast(gws.Model, root.get('CHILD'))
94 f = u.feature(parent, id=1, children=[
95 u.feature(child, id=4),
96 u.feature(child, id=5),
97 ])
99 parent.update_feature(f, mc)
101 rows = u.pg.rows('SELECT id, parent_k FROM child ORDER BY id')
102 assert rows == [
103 (4, 11), (5, 11), (6, None), (7, 33)
104 ]
107def test_create(root: gws.Root):
108 mc = u.model_context(maxDepth=1)
110 u.pg.insert('parent', [
111 {'id': 1, 'k': 11, 'pp': 'p1'},
112 {'id': 2, 'k': 22, 'pp': 'p2'},
113 {'id': 3, 'k': 33, 'pp': 'p3'},
114 ])
115 u.pg.insert('child', [
116 {'id': 4, 'cc': 'a4', 'parent_k': 11},
117 {'id': 5, 'cc': 'a5', 'parent_k': 22},
118 {'id': 6, 'cc': 'a6', 'parent_k': 11},
119 {'id': 7, 'cc': 'a7', 'parent_k': 33},
120 ])
122 parent = u.cast(gws.Model, root.get('PARENT'))
123 child = u.cast(gws.Model, root.get('CHILD'))
125 f = u.feature(parent, id=999, k=99, children=[
126 u.feature(child, id=4),
127 u.feature(child, id=5),
128 u.feature(child, id=6),
129 ])
131 parent.create_feature(f, mc)
133 rows = u.pg.rows('SELECT id, parent_k FROM child ORDER BY id')
134 assert rows == [
135 (4, 99), (5, 99), (6, 99), (7, 33)
136 ]
139def test_create_auto(root: gws.Root):
140 mc = u.model_context(maxDepth=1)
142 u.pg.insert('child', [
143 {'id': 4, 'cc': 'a4', 'parent_k': 11},
144 {'id': 5, 'cc': 'a5', 'parent_k': 22},
145 {'id': 6, 'cc': 'a6', 'parent_k': 11},
146 {'id': 7, 'cc': 'a7', 'parent_k': 33},
147 ])
149 parent_auto = u.cast(gws.Model, root.get('PARENT_AUTO'))
150 child = u.cast(gws.Model, root.get('CHILD'))
152 f = u.feature(parent_auto, k=99, children=[
153 u.feature(child, id=4),
154 u.feature(child, id=5),
155 u.feature(child, id=6),
156 ])
158 parent_auto.create_feature(f, mc)
160 rows = u.pg.rows('SELECT id, parent_k FROM child ORDER BY id')
161 assert rows == [
162 (4, 99), (5, 99), (6, 99), (7, 33)
163 ]
166def test_delete(root: gws.Root):
167 mc = u.model_context(maxDepth=1)
169 u.pg.insert('parent', [
170 {'id': 1, 'k': 11, 'pp': 'p1'},
171 {'id': 2, 'k': 22, 'pp': 'p2'},
172 ])
173 u.pg.insert('child', [
174 {'id': 4, 'cc': 'a4', 'parent_k': 11},
175 {'id': 5, 'cc': 'a5', 'parent_k': 22},
176 {'id': 6, 'cc': 'a6', 'parent_k': 11},
177 {'id': 7, 'cc': 'a7', 'parent_k': 33},
178 ])
180 parent = u.cast(gws.Model, root.get('PARENT'))
182 f = u.feature(parent, id=1)
183 parent.delete_feature(f, mc)
185 rows = u.pg.rows('SELECT id, parent_k FROM child ORDER BY id')
186 assert rows == [
187 (4, None), (5, 22), (6, None), (7, 33),
188 ]
191def test_create_related(root: gws.Root):
192 mc = u.model_context(maxDepth=1)
194 u.pg.insert('parent', [
195 {'id': 1, 'k': 11, 'pp': 'p1'},
196 ])
197 u.pg.insert('child', [
198 {'id': 4, 'cc': 'a4', 'parent_k': 11},
199 {'id': 5, 'cc': 'a5', 'parent_k': 22},
200 ])
202 parent = u.cast(gws.Model, root.get('PARENT'))
203 child = u.cast(gws.Model, root.get('CHILD'))
205 child_f = u.feature(child, id=101)
206 child_f.createWithFeatures = [u.feature(parent, id=1)]
207 child.create_feature(child_f, mc)
209 rows = u.pg.rows('SELECT id, parent_k FROM child ORDER BY id')
210 assert rows == [
211 (4, 11), (5, 22), (101, 11)
212 ]