|
36 | 36 | QgsSymbol,
|
37 | 37 | QgsSingleSymbolRenderer,
|
38 | 38 | QgsCoordinateReferenceSystem,
|
| 39 | + QgsVectorLayerCache, |
39 | 40 | QgsReadWriteContext,
|
40 | 41 | QgsProject,
|
41 | 42 | QgsUnitTypes,
|
|
54 | 55 | QgsTextFormat,
|
55 | 56 | QgsVectorLayerSelectedFeatureSource,
|
56 | 57 | NULL)
|
| 58 | +from qgis.gui import (QgsAttributeTableModel, |
| 59 | + QgsGui |
| 60 | + ) |
57 | 61 | from qgis.testing import start_app, unittest
|
58 | 62 | from featuresourcetestbase import FeatureSourceTestCase
|
59 | 63 | from utilities import unitTestDataPath
|
@@ -209,6 +213,7 @@ def getSource(cls):
|
209 | 213 | @classmethod
|
210 | 214 | def setUpClass(cls):
|
211 | 215 | """Run before all tests"""
|
| 216 | + QgsGui.editorWidgetRegistry().initEditors() |
212 | 217 | # Create test layer for FeatureSourceTestCase
|
213 | 218 | cls.source = cls.getSource()
|
214 | 219 |
|
@@ -1321,6 +1326,65 @@ def test_JoinStats(self):
|
1321 | 1326 | self.assertEqual(layer.maximumValue(3), 321)
|
1322 | 1327 | self.assertEqual(set(layer.uniqueValues(3)), set([111, 321]))
|
1323 | 1328 |
|
| 1329 | + def test_valid_join_when_opening_project(self): |
| 1330 | + join_field = "id" |
| 1331 | + fid = 4 |
| 1332 | + attr_idx = 4 |
| 1333 | + join_attr_idx = 1 |
| 1334 | + new_value = 33.0 |
| 1335 | + |
| 1336 | + # read project and get layers |
| 1337 | + myPath = os.path.join(unitTestDataPath(), 'joins.qgs') |
| 1338 | + rc = QgsProject.instance().read(myPath) |
| 1339 | + |
| 1340 | + layer = QgsProject.instance().mapLayersByName("polys_with_id")[0] |
| 1341 | + join_layer = QgsProject.instance().mapLayersByName("polys_overlapping_with_id")[0] |
| 1342 | + |
| 1343 | + # create an attribute table for the main_layer and the |
| 1344 | + # joined layer |
| 1345 | + cache = QgsVectorLayerCache(layer, 100) |
| 1346 | + am = QgsAttributeTableModel(cache) |
| 1347 | + am.loadLayer() |
| 1348 | + |
| 1349 | + join_cache = QgsVectorLayerCache(join_layer, 100) |
| 1350 | + join_am = QgsAttributeTableModel(join_cache) |
| 1351 | + join_am.loadLayer() |
| 1352 | + |
| 1353 | + # check feature value of a joined field from the attribute model |
| 1354 | + model_index = am.idToIndex(fid) |
| 1355 | + feature_model = am.feature(model_index) |
| 1356 | + |
| 1357 | + join_model_index = join_am.idToIndex(fid) |
| 1358 | + join_feature_model = join_am.feature(join_model_index) |
| 1359 | + |
| 1360 | + self.assertEqual(feature_model.attribute(attr_idx), join_feature_model.attribute(join_attr_idx)) |
| 1361 | + |
| 1362 | + # change attribute value for a feature of the joined layer |
| 1363 | + join_layer.startEditing() |
| 1364 | + join_layer.changeAttributeValue(fid, join_attr_idx, new_value) |
| 1365 | + join_layer.commitChanges() |
| 1366 | + |
| 1367 | + # check the feature previously modified |
| 1368 | + join_model_index = join_am.idToIndex(fid) |
| 1369 | + join_feature_model = join_am.feature(join_model_index) |
| 1370 | + self.assertEqual(join_feature_model.attribute(join_attr_idx), new_value) |
| 1371 | + |
| 1372 | + # recreate a new cache and model to simulate the opening of |
| 1373 | + # a new attribute table |
| 1374 | + cache = QgsVectorLayerCache(layer, 100) |
| 1375 | + am = QgsAttributeTableModel(cache) |
| 1376 | + am.loadLayer() |
| 1377 | + |
| 1378 | + # test that the model is up to date with the joined layer |
| 1379 | + model_index = am.idToIndex(fid) |
| 1380 | + feature_model = am.feature(model_index) |
| 1381 | + self.assertEqual(feature_model.attribute(attr_idx), new_value) |
| 1382 | + |
| 1383 | + # restore value |
| 1384 | + join_layer.startEditing() |
| 1385 | + join_layer.changeAttributeValue(fid, join_attr_idx, 7.0) |
| 1386 | + join_layer.commitChanges() |
| 1387 | + |
1324 | 1388 | def testUniqueValue(self):
|
1325 | 1389 | """ test retrieving unique values """
|
1326 | 1390 | layer = createLayerWithFivePoints()
|
|
0 commit comments