Skip to content

Commit

Permalink
Add unit test for f947ffb
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 18, 2019
1 parent f742044 commit 786929b
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/src/python/test_qgsvectorlayer.py
Expand Up @@ -358,6 +358,41 @@ def testSetDataSource(self):
self.assertNotEqual(layer.renderer(), r)
self.assertEqual(layer.renderer().symbol().type(), QgsSymbol.Line)

def testSetDataSourceInvalidToValid(self):
"""
Test that changing an invalid layer path to valid maintains the renderer
"""
layer = createLayerWithOnePoint()
layer.setCrs(QgsCoordinateReferenceSystem("epsg:3111"))
r = QgsSingleSymbolRenderer(QgsSymbol.defaultSymbol(QgsWkbTypes.PointGeometry))
layer.setRenderer(r)
self.assertEqual(layer.renderer().symbol().type(), QgsSymbol.Marker)

# change to invalid path
options = QgsDataProvider.ProviderOptions()
layer.setDataSource('nothing', 'new name', 'ogr', options)

self.assertFalse(layer.isValid())
# these properties should be kept intact!
self.assertEqual(layer.name(), 'new name')
self.assertEqual(layer.wkbType(), QgsWkbTypes.Point)
self.assertEqual(layer.crs().authid(), 'EPSG:3111')
# should have kept the same renderer!
self.assertEqual(layer.renderer(), r)

# set to a valid path
points_path = os.path.join(unitTestDataPath(), 'points.shp')
layer.setDataSource(points_path, 'new name2', 'ogr', options)

self.assertTrue(layer.isValid())
self.assertEqual(layer.name(), 'new name2')
self.assertEqual(layer.wkbType(), QgsWkbTypes.Point)
self.assertEqual(layer.crs().authid(), 'EPSG:4326')
self.assertIn(points_path, layer.dataProvider().dataSourceUri())

# should STILL have kept renderer!
self.assertEqual(layer.renderer(), r)

def test_layer_crs(self):
"""
Test that spatial layers have CRS, and non-spatial don't
Expand Down

0 comments on commit 786929b

Please sign in to comment.