Skip to content


Browse files Browse the repository at this point in the history
added a test for memory layer fields being saved to XML
  • Loading branch information
SebDieBln committed Jan 1, 2016
1 parent dbc0f07 commit 836ea81
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion tests/src/python/
Expand Up @@ -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,
Expand Down Expand Up @@ -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(

# 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()

# 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(

if __name__ == '__main__':

0 comments on commit 836ea81

Please sign in to comment.