Skip to content

Commit

Permalink
Add test case for the layer store when re-adding a fixed layer
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Mar 25, 2019
1 parent 6560473 commit e75308e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
4 changes: 2 additions & 2 deletions python/core/auto_generated/qgsmaplayerstore.sip.in
Expand Up @@ -122,8 +122,8 @@ The layersAdded() and layerWasAdded() signals will always be emitted.

.. note::

if layer with the same id is already in the store it is not added again,
but if the validity of the layer has changed from false to true, the
If a layer with the same id is already in the store it is not added again,
but if the validity of the layer has changed from ``False`` to ``True``, the
layer data source is updated to the new one.


Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsmaplayerstore.cpp
Expand Up @@ -74,8 +74,8 @@ QList<QgsMapLayer *> QgsMapLayerStore::addMapLayers( const QList<QgsMapLayer *>
QgsDebugMsg( QStringLiteral( "Cannot add null layers" ) );
continue;
}
// If the layer is already in the store but its validity has changed reset data source
if ( mMapLayers.contains( myLayer->id() ) && ! mMapLayers[myLayer->id()]->isValid() && myLayer->isValid() && myLayer->dataProvider() )
// If the layer is already in the store but its validity has flipped to TRUE reset data source
if ( mMapLayers.contains( myLayer->id() ) && ! mMapLayers[myLayer->id()]->isValid() && myLayer->isValid() && myLayer->dataProvider() )
{
mMapLayers[myLayer->id()]->setDataSource( myLayer->dataProvider()->dataSourceUri(), myLayer->name(), myLayer->providerType(), QgsDataProvider::ProviderOptions() );
}
Expand Down
45 changes: 44 additions & 1 deletion tests/src/python/test_qgsmaplayerstore.py
Expand Up @@ -12,14 +12,25 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from qgis.core import QgsMapLayerStore, QgsVectorLayer, QgsMapLayer
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):
Expand Down Expand Up @@ -529,6 +540,38 @@ def testTransferLayers(self):
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()

0 comments on commit e75308e

Please sign in to comment.