Salome HOME
Fix unit tests for the Folder feature
[modules/shaper.git] / src / ModelAPI / Test / TestFolder_Update.py
1 ## Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 ##
3 ## This library is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU Lesser General Public
5 ## License as published by the Free Software Foundation; either
6 ## version 2.1 of the License, or (at your option) any later version.
7 ##
8 ## This library is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 ## Lesser General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU Lesser General Public
14 ## License along with this library; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 ##
17 ## See http:##www.salome-platform.org/ or
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 #=========================================================================
22 # Initialization of the test
23 #=========================================================================
24 from ModelAPI import *
25
26 __updated__ = "2017-11-23"
27
28 aSession = ModelAPI_Session.get()
29
30
31 def newPoint(theDocument, theX, theY, theZ):
32     aSession.startOperation()
33     aPoint = theDocument.addFeature("Point")
34     aPointData = aPoint.data()
35     assert(aPointData is not None)
36     aPointData.real("x").setValue(theX)
37     aPointData.real("y").setValue(theY)
38     aPointData.real("z").setValue(theZ)
39     aPointData.string("creation_method").setValue("by_xyz")
40     aSession.finishOperation()
41     return aPoint
42
43
44 #=========================================================================
45 # Test 1. Add a point into a folder above
46 #=========================================================================
47 aSession.startOperation()
48 aPart = aSession.moduleDocument().addFeature("Part")
49 aSession.finishOperation()
50
51 # add point and a folder before it
52 aPartDoc = aSession.activeDocument()
53 aPoint1 = newPoint(aPartDoc, 0., 0., 0.)
54
55 aSession.startOperation()
56 aFolder1 = aPartDoc.addFolder(aPoint1)
57 aSession.finishOperation()
58
59 NB_FEATURES_FULL = 2
60 NB_FEATURES_OUT  = 2
61
62 assert(aPartDoc.size("Folders") == 1), "Wrong number of folders: {}".format(aPartDoc.size("Folders"))
63 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}".format(aPartDoc.size("Features"))
64 FOLDER_NAME_EXPECTED = "Folder_1"
65 assert(aFolder1.name() == FOLDER_NAME_EXPECTED), "Actual name '{}', expected '{}'".format(aFolder1.name(), FOLDER_NAME_EXPECTED)
66
67 toFolder = FeatureList()
68 toFolder.append(aPoint1)
69
70 # move point to the folder
71 aSession.startOperation()
72 aFolder = aPartDoc.findFolderAbove(toFolder)
73 assert(aFolder is not None)
74 isAdded = aPartDoc.moveToFolder(toFolder, aFolder)
75 aSession.finishOperation()
76 assert(isAdded)
77 NB_FEATURES_OUT -= 1
78 # full number of features
79 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
80 # number of features outside the folder
81 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
82
83 # check the index of the point in the folder
84 aFound = aPartDoc.findContainingFolder(aPoint1)
85 assert(aFound[0].data().isEqual(aFolder1.data()))
86 assert(aFound[1] == 0)
87
88 #=========================================================================
89 # Test 2. Add a point, check it is added to a folder, then move it out
90 #=========================================================================
91 aPoint2 = newPoint(aPartDoc, 10., 0., 0.)
92
93 NB_FEATURES_FULL += 1
94 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
95 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
96
97 fromFolder = FeatureList()
98 fromFolder.append(aPoint2)
99
100 aSession.startOperation()
101 isMovedOut = aPartDoc.removeFromFolder(fromFolder)
102 aSession.finishOperation()
103 assert(isMovedOut)
104
105 NB_FEATURES_OUT += 1
106 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
107 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
108
109 #=========================================================================
110 # Test 3. Add a point into a folder below
111 #=========================================================================
112 aPoint3 = newPoint(aPartDoc, 10., 10., 0.)
113
114 NB_FEATURES_FULL += 1
115 NB_FEATURES_OUT += 1
116
117 # add a folder
118 aSession.startOperation()
119 aFolder2 = aPartDoc.addFolder(aPoint3)
120 aSession.finishOperation()
121
122 NB_FEATURES_FULL += 1
123 NB_FEATURES_OUT += 1
124
125 assert(aPartDoc.size("Folders") == 2), "Wrong number of folders: {}".format(aPartDoc.size("Folders"))
126 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
127 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
128
129 toFolder = FeatureList()
130 toFolder.append(aPoint2)
131
132 # move point to the folder
133 aSession.startOperation()
134 aFolder = aPartDoc.findFolderBelow(toFolder)
135 assert(aFolder is not None)
136 isAdded = aPartDoc.moveToFolder(toFolder, aFolder)
137 aSession.finishOperation()
138 assert(isAdded)
139
140 NB_FEATURES_OUT -= 1
141
142 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
143 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
144
145 # check the index of the point in the folder
146 aFound = aPartDoc.findContainingFolder(aPoint2)
147 assert(aFound[0].data().isEqual(aFolder2.data()))
148 assert(aFound[1] == 0)
149 aFound = aPartDoc.findContainingFolder(aPoint3)
150 assert(aFound == -1)
151
152 #=========================================================================
153 # Test 4. Add several points into a folder
154 #=========================================================================
155 aPoint4 = newPoint(aPartDoc, 0., 10., 0.)
156
157 NB_FEATURES_FULL += 1
158 NB_FEATURES_OUT += 1
159
160 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
161 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
162
163 # index without respect to foldering
164 assert(aPartDoc.index(aPoint4) == 5), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4))
165 # index according to folders
166 assert(aPartDoc.index(aPoint4, True) == 3), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4, True))
167
168 # add a folder
169 aSession.startOperation()
170 aFolder3 = aPartDoc.addFolder(aPoint3)
171 aSession.finishOperation()
172
173 NB_FEATURES_FULL += 1
174 NB_FEATURES_OUT += 1
175
176 assert(aPartDoc.size("Folders") == 3), "Wrong number of folders: {}".format(aPartDoc.size("Folders"))
177 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
178 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
179
180 # index without respect to foldering
181 assert(aPartDoc.index(aFolder3) == 4), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3))
182 assert(aPartDoc.index(aPoint4) == 6), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4))
183 # index according to folders
184 assert(aPartDoc.index(aFolder3, True) == 2), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
185 assert(aPartDoc.index(aPoint4, True) == 4), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4, True))
186
187 toFolder = FeatureList()
188 toFolder.append(aPoint3)
189 toFolder.append(aPoint4)
190
191 # move point to the folder
192 aSession.startOperation()
193 aFolder = aPartDoc.findFolderAbove(toFolder)
194 assert(aFolder is not None)
195 isAdded = aPartDoc.moveToFolder(toFolder, aFolder)
196 aSession.finishOperation()
197 assert(isAdded)
198
199 NB_FEATURES_OUT -= 2
200
201 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
202 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
203
204 # index without respect to foldering
205 assert(aPartDoc.index(aFolder3) == 4), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3))
206 assert(aPartDoc.index(aPoint3) == 5), "Wrong index of the point: {}".format(aPartDoc.index(aPoint3))
207 assert(aPartDoc.index(aPoint4) == 6), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4))
208 # index according to folders
209 assert(aPartDoc.index(aFolder3, True) == 2), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
210 assert(aPartDoc.index(aPoint3, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint3, True))
211 assert(aPartDoc.index(aPoint4, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4, True))
212
213 # check the index of the point in the folder
214 aFound = aPartDoc.findContainingFolder(aPoint3)
215 assert(aFound[0].data().isEqual(aFolder3.data()))
216 assert(aFound[1] == 0)
217 aFound = aPartDoc.findContainingFolder(aPoint4)
218 assert(aFound[0].data().isEqual(aFolder3.data()))
219 assert(aFound[1] == 1)
220
221 aPoint5 = newPoint(aPartDoc, 0., 0., 10.)
222 fromFolder = FeatureList()
223 fromFolder.append(aPoint5)
224
225 aSession.startOperation()
226 isMovedOut = aPartDoc.removeFromFolder(fromFolder)
227 aSession.finishOperation()
228 assert(isMovedOut)
229
230 # add more points to the folder to move them out
231 aPoint6 = newPoint(aPartDoc, 10., 0., 10.)
232 aPoint7 = newPoint(aPartDoc, 10., 10., 10.)
233 aPoint8 = newPoint(aPartDoc, 0., 10., 10.)
234
235 toFolder = FeatureList()
236 toFolder.append(aPoint5)
237 toFolder.append(aPoint6)
238 toFolder.append(aPoint7)
239 toFolder.append(aPoint8)
240
241 # move point to the folder
242 aSession.startOperation()
243 aFolder = aPartDoc.findFolderAbove(toFolder)
244 assert(aFolder is not None)
245 isAdded = aPartDoc.moveToFolder(toFolder, aFolder)
246 aSession.finishOperation()
247 assert(isAdded)
248
249 NB_FEATURES_FULL += 4
250 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
251 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
252
253 assert(aPartDoc.index(aPoint5, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint5, True))
254 assert(aPartDoc.index(aPoint6, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint6, True))
255 assert(aPartDoc.index(aPoint7, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint7, True))
256 assert(aPartDoc.index(aPoint8, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint8, True))
257
258 assert(aFolder3.reference("first_feature").value() is not None)
259 assert(aFolder3.reference("last_feature").value() is not None)
260
261 #=========================================================================
262 # Test 5. Remove a point from a folder before it
263 #=========================================================================
264 fromFolder = FeatureList()
265 fromFolder.append(aPoint3)
266
267 aSession.startOperation()
268 isMovedOut = aPartDoc.removeFromFolder(fromFolder)
269 aSession.finishOperation()
270 assert(isMovedOut)
271
272 NB_FEATURES_OUT += 1
273 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
274 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
275
276 assert(aPartDoc.index(aPoint3, True) == 2), "Wrong index of the point: {}".format(aPartDoc.index(aPoint3, True))
277 assert(aPartDoc.index(aFolder3, True) == 3), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
278
279 assert(aFolder3.reference("first_feature").value() is not None)
280 assert(aFolder3.reference("last_feature").value() is not None)
281
282 #=========================================================================
283 # Test 6. Remove a point from a folder after it
284 #=========================================================================
285 fromFolder = FeatureList()
286 fromFolder.append(aPoint8)
287
288 aSession.startOperation()
289 isMovedOut = aPartDoc.removeFromFolder(fromFolder)
290 aSession.finishOperation()
291 assert(isMovedOut)
292
293 NB_FEATURES_OUT += 1
294 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
295 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
296
297 assert(aPartDoc.index(aPoint8, True) == 4), "Wrong index of the point: {}".format(aPartDoc.index(aPoint8, True))
298 assert(aPartDoc.index(aFolder3, True) == 3), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
299
300 assert(aFolder3.reference("first_feature").value() is not None)
301 assert(aFolder3.reference("last_feature").value() is not None)
302
303 #=========================================================================
304 # Test 7. Try to remove several points which are not start nor end in a folder
305 #=========================================================================
306 fromFolder = FeatureList()
307 fromFolder.append(aPoint5)
308 fromFolder.append(aPoint6)
309
310 aSession.startOperation()
311 isMovedOut = aPartDoc.removeFromFolder(fromFolder)
312 aSession.finishOperation()
313 assert(isMovedOut is False)
314
315 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
316 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
317
318 assert(aPartDoc.index(aFolder3, True) == 3), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
319 assert(aPartDoc.index(aPoint5, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint5, True))
320 assert(aPartDoc.index(aPoint6, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint6, True))
321 assert(aPartDoc.index(aPoint8, True) == 4), "Wrong index of the point: {}".format(aPartDoc.index(aPoint8, True))
322
323 assert(aFolder3.reference("first_feature").value() is not None)
324 assert(aFolder3.reference("last_feature").value() is not None)
325
326 #=========================================================================
327 # Test 8. Remove several points from a folder after it
328 #=========================================================================
329 fromFolder = FeatureList()
330 fromFolder.append(aPoint6)
331 fromFolder.append(aPoint7)
332
333 aSession.startOperation()
334 isMovedOut = aPartDoc.removeFromFolder(fromFolder)
335 aSession.finishOperation()
336 assert(isMovedOut)
337
338 NB_FEATURES_OUT += 2
339 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
340 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
341
342 assert(aPartDoc.index(aFolder3, True) == 3), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
343 assert(aPartDoc.index(aPoint6, True) == 4), "Wrong index of the point: {}".format(aPartDoc.index(aPoint6, True))
344 assert(aPartDoc.index(aPoint7, True) == 5), "Wrong index of the point: {}".format(aPartDoc.index(aPoint7, True))
345 assert(aPartDoc.index(aPoint8, True) == 6), "Wrong index of the point: {}".format(aPartDoc.index(aPoint8, True))
346
347 assert(aFolder3.reference("first_feature").value() is not None)
348 assert(aFolder3.reference("last_feature").value() is not None)
349
350 #=========================================================================
351 # Test 9. Remove all remaining points from a folder after it
352 #=========================================================================
353 fromFolder = FeatureList()
354 fromFolder.append(aPoint4)
355 fromFolder.append(aPoint5)
356
357 aSession.startOperation()
358 isMovedOut = aPartDoc.removeFromFolder(fromFolder, False)
359 aSession.finishOperation()
360 assert(isMovedOut)
361
362 NB_FEATURES_OUT += 2
363 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
364 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
365
366 assert(aPartDoc.index(aFolder3, True) == 3), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
367 assert(aPartDoc.index(aPoint4, True) == 4), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4, True))
368 assert(aPartDoc.index(aPoint5, True) == 5), "Wrong index of the point: {}".format(aPartDoc.index(aPoint5, True))
369 assert(aPartDoc.index(aPoint6, True) == 6), "Wrong index of the point: {}".format(aPartDoc.index(aPoint6, True))
370 assert(aPartDoc.index(aPoint7, True) == 7), "Wrong index of the point: {}".format(aPartDoc.index(aPoint7, True))
371 assert(aPartDoc.index(aPoint8, True) == 8), "Wrong index of the point: {}".format(aPartDoc.index(aPoint8, True))
372
373 # folder is empty
374 assert(aFolder3.reference("first_feature").value() is None)
375 assert(aFolder3.reference("last_feature").value() is None)
376 # remove empty folder
377 aSession.startOperation()
378 aPartDoc.removeFolder(aFolder3)
379 aSession.finishOperation()
380
381 from salome.shaper import model
382 assert(model.checkPythonDump())