Skip to content
Permalink
Browse files

added a test for memory layer fields being saved to XML

  • Loading branch information
SebDieBln committed Dec 31, 2015
1 parent dbc0f07 commit 836ea81b772f592bfe187c90f6e58631e137a080
Showing with 34 additions and 1 deletion.
  1. +34 −1 tests/src/python/test_provider_memory.py
@@ -17,7 +17,7 @@
import shutil
import glob

from qgis.core import QGis, QgsField, QgsPoint, QgsVectorLayer, QgsFeatureRequest, QgsFeature, QgsProviderRegistry, \
from qgis.core import QGis, QgsField, QgsPoint, QgsMapLayer, QgsVectorLayer, QgsFeatureRequest, QgsFeature, QgsProviderRegistry, \
QgsGeometry, NULL
from PyQt4.QtCore import QSettings
from utilities import (unitTestDataPath,
@@ -171,6 +171,39 @@ def testFromUri(self):
myProvider = myMemoryLayer.dataProvider()
assert myProvider is not None

def testSaveFields(self):
# Create a new memory layer with no fields
myMemoryLayer = QgsVectorLayer(
('Point?crs=epsg:4326&index=yes'),
'test',
'memory')

# Add some fields to the layer
myFields = [QgsField('TestInt', QVariant.Int, 'integer', 2, 0),
QgsField('TestDbl', QVariant.Double, 'double', 8, 6),
QgsField('TestString', QVariant.String, 'string', 50, 0)]
assert myMemoryLayer.startEditing()
for f in myFields:
assert myMemoryLayer.addAttribute(f)
assert myMemoryLayer.commitChanges()
myMemoryLayer.updateFields()

# Export the layer to a layer-definition-XML
qlr = QgsMapLayer.asLayerDefinition([myMemoryLayer])
assert qlr is not None

# Import the layer from the layer-definition-XML
layers = QgsMapLayer.fromLayerDefinition(qlr)
assert layers is not None
myImportedLayer = layers[0]
assert myImportedLayer is not None

# Check for the presence of the fields
importedFields = myImportedLayer.fields()
assert importedFields is not None
for f in myFields:
assert f == importedFields.field(f.name())


if __name__ == '__main__':
unittest.main()

0 comments on commit 836ea81

Please sign in to comment.
You can’t perform that action at this time.