-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
test_qgsmaplayerstore.py
577 lines (457 loc) · 20.2 KB
/
test_qgsmaplayerstore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsMapLayerStore.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Nyall Dawson'
__date__ = '2017-05'
__copyright__ = 'Copyright 2017, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
from qgis.core import (
QgsMapLayerStore,
QgsVectorLayer,
QgsMapLayer,
QgsDataProvider,
QgsProject,
QgsReadWriteContext,
)
from qgis.testing import start_app, unittest
from qgis.PyQt.QtCore import QT_VERSION_STR
from qgis.PyQt import sip
from qgis.PyQt.QtTest import QSignalSpy
from qgis.PyQt.QtXml import QDomDocument, QDomNode
from time import sleep
from utilities import unitTestDataPath
start_app()
TEST_DATA_DIR = unitTestDataPath()
def createLayer(name):
return QgsVectorLayer("Point?field=x:string", name, "memory")
class TestQgsMapLayerStore(unittest.TestCase):
def setUp(self):
pass
def test_addMapLayer(self):
""" test adding individual map layers to store"""
store = QgsMapLayerStore()
l1 = createLayer('test')
self.assertEqual(store.addMapLayer(l1), l1)
self.assertEqual(len(store.mapLayersByName('test')), 1)
self.assertEqual(store.count(), 1)
self.assertEqual(len(store), 1)
# adding a second layer should leave existing layers intact
l2 = createLayer('test2')
self.assertEqual(store.addMapLayer(l2), l2)
self.assertEqual(len(store.mapLayersByName('test')), 1)
self.assertEqual(len(store.mapLayersByName('test2')), 1)
self.assertEqual(store.count(), 2)
self.assertEqual(len(store), 2)
def test_addMapLayerAlreadyAdded(self):
""" test that already added layers can't be readded to store """
store = QgsMapLayerStore()
l1 = createLayer('test')
store.addMapLayer(l1)
self.assertEqual(len(store.mapLayersByName('test')), 1)
self.assertEqual(store.count(), 1)
self.assertEqual(store.addMapLayer(l1), None)
self.assertEqual(len(store.mapLayersByName('test')), 1)
self.assertEqual(store.count(), 1)
self.assertEqual(len(store), 1)
def test_addMapLayerInvalid(self):
""" test that invalid map layers can't be added to store """
store = QgsMapLayerStore()
vl = QgsVectorLayer("Point?field=x:string", 'test', "xxx")
self.assertEqual(store.addMapLayer(vl), vl)
self.assertEqual(len(store.mapLayersByName('test')), 1)
self.assertEqual(store.count(), 1)
self.assertEqual(store.validCount(), 0)
def test_addMapLayerSignals(self):
""" test that signals are correctly emitted when adding map layer"""
store = QgsMapLayerStore()
layer_was_added_spy = QSignalSpy(store.layerWasAdded)
layers_added_spy = QSignalSpy(store.layersAdded)
l1 = createLayer('test')
store.addMapLayer(l1)
# can't seem to actually test the data which was emitted, so best we can do is test
# the signal count
self.assertEqual(len(layer_was_added_spy), 1)
self.assertEqual(len(layers_added_spy), 1)
store.addMapLayer(createLayer('test2'))
self.assertEqual(len(layer_was_added_spy), 2)
self.assertEqual(len(layers_added_spy), 2)
# try readding a layer already in the store
store.addMapLayer(l1)
# should be no extra signals emitted
self.assertEqual(len(layer_was_added_spy), 2)
self.assertEqual(len(layers_added_spy), 2)
def test_addMapLayers(self):
""" test adding multiple map layers to store """
store = QgsMapLayerStore()
l1 = createLayer('test')
l2 = createLayer('test2')
self.assertEqual(set(store.addMapLayers([l1, l2])), {l1, l2})
self.assertEqual(len(store.mapLayersByName('test')), 1)
self.assertEqual(len(store.mapLayersByName('test2')), 1)
self.assertEqual(store.count(), 2)
# adding more layers should leave existing layers intact
l3 = createLayer('test3')
l4 = createLayer('test4')
self.assertEqual(set(store.addMapLayers([l3, l4])), {l3, l4})
self.assertEqual(len(store.mapLayersByName('test')), 1)
self.assertEqual(len(store.mapLayersByName('test2')), 1)
self.assertEqual(len(store.mapLayersByName('test3')), 1)
self.assertEqual(len(store.mapLayersByName('test4')), 1)
self.assertEqual(store.count(), 4)
store.removeAllMapLayers()
def test_addMapLayersInvalid(self):
""" test that invalid map layers can be added to store """
store = QgsMapLayerStore()
vl = QgsVectorLayer("Point?field=x:string", 'test', "xxx")
self.assertEqual(store.addMapLayers([vl]), [vl])
self.assertEqual(len(store.mapLayersByName('test')), 1)
self.assertEqual(store.count(), 1)
self.assertEqual(store.validCount(), 0)
def test_addMapLayersAlreadyAdded(self):
""" test that already added layers can't be readded to store """
store = QgsMapLayerStore()
l1 = createLayer('test')
self.assertEqual(store.addMapLayers([l1]), [l1])
self.assertEqual(len(store.mapLayersByName('test')), 1)
self.assertEqual(store.count(), 1)
self.assertEqual(store.addMapLayers([l1]), [])
self.assertEqual(len(store.mapLayersByName('test')), 1)
self.assertEqual(store.count(), 1)
def test_addMapLayersSignals(self):
""" test that signals are correctly emitted when adding map layers"""
store = QgsMapLayerStore()
layer_was_added_spy = QSignalSpy(store.layerWasAdded)
layers_added_spy = QSignalSpy(store.layersAdded)
l1 = createLayer('test')
l2 = createLayer('test2')
store.addMapLayers([l1, l2])
# can't seem to actually test the data which was emitted, so best we can do is test
# the signal count
self.assertEqual(len(layer_was_added_spy), 2)
self.assertEqual(len(layers_added_spy), 1)
store.addMapLayers([createLayer('test3'), createLayer('test4')])
self.assertEqual(len(layer_was_added_spy), 4)
self.assertEqual(len(layers_added_spy), 2)
# try readding a layer already in the store
store.addMapLayers([l1, l2])
# should be no extra signals emitted
self.assertEqual(len(layer_was_added_spy), 4)
self.assertEqual(len(layers_added_spy), 2)
def test_mapLayerById(self):
""" test retrieving map layer by ID """
store = QgsMapLayerStore()
# test no crash with empty store
self.assertEqual(store.mapLayer('bad'), None)
self.assertEqual(store.mapLayer(None), None)
l1 = createLayer('test')
l2 = createLayer('test2')
store.addMapLayers([l1, l2])
self.assertEqual(store.mapLayer('bad'), None)
self.assertEqual(store.mapLayer(None), None)
self.assertEqual(store.mapLayer(l1.id()), l1)
self.assertEqual(store.mapLayer(l2.id()), l2)
def test_mapLayersByName(self):
""" test retrieving map layer by name """
store = QgsMapLayerStore()
# test no crash with empty store
self.assertEqual(store.mapLayersByName('bad'), [])
self.assertEqual(store.mapLayersByName(None), [])
l1 = createLayer('test')
l2 = createLayer('test2')
store.addMapLayers([l1, l2])
self.assertEqual(store.mapLayersByName('bad'), [])
self.assertEqual(store.mapLayersByName(None), [])
self.assertEqual(store.mapLayersByName('test'), [l1])
self.assertEqual(store.mapLayersByName('test2'), [l2])
#duplicate name
# little bit of a hack - we don't want a duplicate ID and since IDs are currently based on time we wait a bit here
sleep(0.1)
l3 = createLayer('test')
store.addMapLayer(l3)
self.assertEqual(set(store.mapLayersByName('test')), {l1, l3})
def test_mapLayers(self):
""" test retrieving map layers list """
store = QgsMapLayerStore()
# test no crash with empty store
self.assertEqual(store.mapLayers(), {})
l1 = createLayer('test')
l2 = createLayer('test2')
store.addMapLayers([l1, l2])
self.assertEqual(store.mapLayers(), {l1.id(): l1, l2.id(): l2})
def test_removeMapLayersById(self):
""" test removing map layers by ID """
store = QgsMapLayerStore()
# test no crash with empty store
store.removeMapLayersById(['bad'])
store.removeMapLayersById([None])
l1 = createLayer('test')
l2 = createLayer('test2')
l3 = createLayer('test3')
store.addMapLayers([l1, l2, l3])
self.assertEqual(store.count(), 3)
#remove bad layers
store.removeMapLayersById(['bad'])
self.assertEqual(store.count(), 3)
store.removeMapLayersById([None])
self.assertEqual(store.count(), 3)
# remove valid layers
l1_id = l1.id()
store.removeMapLayersById([l1_id])
self.assertEqual(store.count(), 2)
# double remove
store.removeMapLayersById([l1_id])
self.assertEqual(store.count(), 2)
# test that layer has been deleted
self.assertTrue(sip.isdeleted(l1))
# remove multiple
store.removeMapLayersById([l2.id(), l3.id()])
self.assertEqual(store.count(), 0)
self.assertTrue(sip.isdeleted(l2))
# try removing a layer not in the store
l4 = createLayer('test4')
store.removeMapLayersById([l4.id()])
self.assertFalse(sip.isdeleted(l4))
def test_removeMapLayersByLayer(self):
""" test removing map layers by layer"""
store = QgsMapLayerStore()
# test no crash with empty store
store.removeMapLayers([None])
l1 = createLayer('test')
l2 = createLayer('test2')
l3 = createLayer('test3')
store.addMapLayers([l1, l2, l3])
self.assertEqual(store.count(), 3)
#remove bad layers
store.removeMapLayers([None])
self.assertEqual(store.count(), 3)
# remove valid layers
store.removeMapLayers([l1])
self.assertEqual(store.count(), 2)
# test that layer has been deleted
self.assertTrue(sip.isdeleted(l1))
# remove multiple
store.removeMapLayers([l2, l3])
self.assertEqual(store.count(), 0)
self.assertTrue(sip.isdeleted(l2))
self.assertTrue(sip.isdeleted(l3))
def test_removeMapLayerById(self):
""" test removing a map layer by ID """
store = QgsMapLayerStore()
# test no crash with empty store
store.removeMapLayer('bad')
store.removeMapLayer(None)
l1 = createLayer('test')
l2 = createLayer('test2')
store.addMapLayers([l1, l2])
self.assertEqual(store.count(), 2)
#remove bad layers
store.removeMapLayer('bad')
self.assertEqual(store.count(), 2)
store.removeMapLayer(None)
self.assertEqual(store.count(), 2)
# remove valid layers
l1_id = l1.id()
store.removeMapLayer(l1_id)
self.assertEqual(store.count(), 1)
# double remove
store.removeMapLayer(l1_id)
self.assertEqual(store.count(), 1)
# test that layer has been deleted
self.assertTrue(sip.isdeleted(l1))
# remove second layer
store.removeMapLayer(l2.id())
self.assertEqual(store.count(), 0)
self.assertTrue(sip.isdeleted(l2))
# try removing a layer not in the store
l3 = createLayer('test3')
store.removeMapLayer(l3.id())
self.assertFalse(sip.isdeleted(l3))
def test_removeMapLayerByLayer(self):
""" test removing a map layer by layer """
store = QgsMapLayerStore()
# test no crash with empty store
store.removeMapLayer('bad')
store.removeMapLayer(None)
l1 = createLayer('test')
l2 = createLayer('test2')
store.addMapLayers([l1, l2])
self.assertEqual(store.count(), 2)
#remove bad layers
store.removeMapLayer(None)
self.assertEqual(store.count(), 2)
l3 = createLayer('test3')
store.removeMapLayer(l3)
self.assertEqual(store.count(), 2)
# remove valid layers
store.removeMapLayer(l1)
self.assertEqual(store.count(), 1)
# test that layer has been deleted
self.assertTrue(sip.isdeleted(l1))
# remove second layer
store.removeMapLayer(l2)
self.assertEqual(store.count(), 0)
self.assertTrue(sip.isdeleted(l2))
# try removing a layer not in the store
l3 = createLayer('test3')
store.removeMapLayer(l3)
self.assertFalse(sip.isdeleted(l3))
def test_removeAllMapLayers(self):
""" test removing all map layers from store """
store = QgsMapLayerStore()
l1 = createLayer('test')
l2 = createLayer('test2')
store.addMapLayers([l1, l2])
self.assertEqual(store.count(), 2)
store.removeAllMapLayers()
self.assertEqual(store.count(), 0)
self.assertEqual(store.mapLayersByName('test'), [])
self.assertEqual(store.mapLayersByName('test2'), [])
def test_addRemoveLayersSignals(self):
""" test that signals are correctly emitted when removing map layers"""
store = QgsMapLayerStore()
layers_will_be_removed_spy = QSignalSpy(store.layersWillBeRemoved)
layer_will_be_removed_spy_str = QSignalSpy(store.layerWillBeRemoved[str])
layer_will_be_removed_spy_layer = QSignalSpy(store.layerWillBeRemoved[QgsMapLayer])
layers_removed_spy = QSignalSpy(store.layersRemoved)
layer_removed_spy = QSignalSpy(store.layerRemoved)
remove_all_spy = QSignalSpy(store.allLayersRemoved)
l1 = createLayer('l1')
l2 = createLayer('l2')
l3 = createLayer('l3')
l4 = createLayer('l4')
store.addMapLayers([l1, l2, l3, l4])
# remove 1 layer
store.removeMapLayer(l1)
# can't seem to actually test the data which was emitted, so best we can do is test
# the signal count
self.assertEqual(len(layers_will_be_removed_spy), 1)
self.assertEqual(len(layer_will_be_removed_spy_str), 1)
self.assertEqual(len(layer_will_be_removed_spy_layer), 1)
self.assertEqual(len(layers_removed_spy), 1)
self.assertEqual(len(layer_removed_spy), 1)
self.assertEqual(len(remove_all_spy), 0)
self.assertEqual(store.count(), 3)
# remove 2 layers at once
store.removeMapLayersById([l2.id(), l3.id()])
self.assertEqual(len(layers_will_be_removed_spy), 2)
self.assertEqual(len(layer_will_be_removed_spy_str), 3)
self.assertEqual(len(layer_will_be_removed_spy_layer), 3)
self.assertEqual(len(layers_removed_spy), 2)
self.assertEqual(len(layer_removed_spy), 3)
self.assertEqual(len(remove_all_spy), 0)
self.assertEqual(store.count(), 1)
# remove all
store.removeAllMapLayers()
self.assertEqual(len(layers_will_be_removed_spy), 3)
self.assertEqual(len(layer_will_be_removed_spy_str), 4)
self.assertEqual(len(layer_will_be_removed_spy_layer), 4)
self.assertEqual(len(layers_removed_spy), 3)
self.assertEqual(len(layer_removed_spy), 4)
self.assertEqual(len(remove_all_spy), 1)
#remove some layers which aren't in the store
store.removeMapLayersById(['asdasd'])
self.assertEqual(len(layers_will_be_removed_spy), 3)
self.assertEqual(len(layer_will_be_removed_spy_str), 4)
self.assertEqual(len(layer_will_be_removed_spy_layer), 4)
self.assertEqual(len(layers_removed_spy), 3)
self.assertEqual(len(layer_removed_spy), 4)
self.assertEqual(len(remove_all_spy), 1)
l5 = createLayer('test5')
store.removeMapLayer(l5)
self.assertEqual(len(layers_will_be_removed_spy), 3)
self.assertEqual(len(layer_will_be_removed_spy_str), 4)
self.assertEqual(len(layer_will_be_removed_spy_layer), 4)
self.assertEqual(len(layers_removed_spy), 3)
self.assertEqual(len(layer_removed_spy), 4)
self.assertEqual(len(remove_all_spy), 1)
def test_RemoveLayerShouldNotSegFault(self):
store = QgsMapLayerStore()
# Should not segfault
store.removeMapLayersById(['not_exists'])
store.removeMapLayer('not_exists2')
# check also that the removal of an unexistent layer does not insert a null layer
for k, layer in list(store.mapLayers().items()):
assert(layer is not None)
def testTakeLayer(self):
# test taking ownership of a layer from the store
l1 = createLayer('l1')
l2 = createLayer('l2')
store = QgsMapLayerStore()
# add one layer to store
store.addMapLayer(l1)
self.assertEqual(store.mapLayers(), {l1.id(): l1})
self.assertEqual(l1.parent(), store)
# try taking some layers which don't exist in store
self.assertFalse(store.takeMapLayer(None))
self.assertFalse(store.takeMapLayer(l2))
# but l2 should still exist..
self.assertTrue(l2.isValid())
# take layer from store
self.assertEqual(store.takeMapLayer(l1), l1)
self.assertFalse(store.mapLayers()) # no layers left
# but l1 should still exist
self.assertTrue(l1.isValid())
# layer should have no parent now
self.assertFalse(l1.parent())
# destroy store
store = None
self.assertTrue(l1.isValid())
def testTransferLayers(self):
# test transferring all layers from another store
store1 = QgsMapLayerStore()
store2 = QgsMapLayerStore()
# empty stores
store1.transferLayersFromStore(store2)
# silly behavior checks
store1.transferLayersFromStore(None)
store1.transferLayersFromStore(store1)
l1 = createLayer('l1')
l2 = createLayer('l2')
store1.addMapLayer(l1)
store1.addMapLayer(l2)
l3 = createLayer('l3')
store2.addMapLayer(l3)
store2.transferLayersFromStore(store1)
self.assertFalse(store1.mapLayers()) # no layers left
self.assertEqual(len(store2.mapLayers()), 3)
self.assertEqual(store2.mapLayers(), {l1.id(): l1, l2.id(): l2, l3.id(): l3})
store1.transferLayersFromStore(store2)
self.assertFalse(store2.mapLayers()) # no layers left
self.assertEqual(len(store1.mapLayers()), 3)
self.assertEqual(store1.mapLayers(), {l1.id(): l1, l2.id(): l2, l3.id(): l3})
def testLayerDataSourceReset(self):
"""When adding a layer with the same id to the store make sure
the data source is also updated in case the layer validity has
changed from False to True"""
p = QgsProject()
store = p.layerStore()
vl1 = createLayer('valid')
vl2 = QgsVectorLayer('/not_a_valid_path.shp', 'invalid', 'ogr')
self.assertTrue(vl1.isValid())
self.assertFalse(vl2.isValid())
store.addMapLayers([vl1, vl2])
self.assertEqual(store.validCount(), 1)
self.assertEqual(len(store.mapLayers()), 2)
# Re-add the bad layer
store.addMapLayers([vl2])
self.assertEqual(store.validCount(), 1)
self.assertEqual(len(store.mapLayers()), 2)
doc = QDomDocument()
doc.setContent('<maplayer><provider encoding="UTF-8">ogr</provider><layername>fixed</layername><id>%s</id></maplayer>' % vl2.id())
layer_node = QDomNode(doc.firstChild())
self.assertTrue(vl2.writeXml(layer_node, doc, QgsReadWriteContext()))
datasource_node = doc.createElement("datasource")
datasource_node.appendChild(doc.createTextNode(os.path.join(TEST_DATA_DIR, 'points.shp')))
layer_node.appendChild(datasource_node)
p.readLayer(layer_node)
self.assertEqual(store.validCount(), 2)
self.assertEqual(len(store.mapLayers()), 2)
self.assertEqual(store.mapLayers()[vl2.id()].name(), 'fixed')
if __name__ == '__main__':
unittest.main()