Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added unit test for vector file writer as it is currently segfaulting…
… in master
  • Loading branch information
timlinux committed Nov 9, 2012
1 parent b965254 commit 3564eae
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -19,3 +19,4 @@ ADD_PYTHON_TEST(PyQgsPoint test_qgspoint.py)
ADD_PYTHON_TEST(PyQgsAtlasComposition test_qgsatlascomposition.py)
ADD_PYTHON_TEST(PyQgsComposerLabel test_qgscomposerlabel.py)
#ADD_PYTHON_TEST(PyQgsPalLabeling test_qgspallabeling.py)
ADD_PYTHON_TEST(PyQgsVectorFileWriter test_qgsvectorfilewriter.py)
84 changes: 84 additions & 0 deletions tests/src/python/test_qgsvectorfilewriter.py
@@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsVectorFileWriter.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Tim Sutton'
__date__ = '20/08/2012'
__copyright__ = 'Copyright 2012, The Quantum GIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os

from PyQt4.QtCore import QVariant, QDir, QString, QStringList

from qgis.core import (QgsVectorLayer,
QgsFeature,
QgsField,
QgsGeometry,
QgsPoint,
QgsVectorFileWriter,
QgsCoordinateReferenceSystem)

from utilities import (unitTestDataPath,
getQgisTestApp,
TestCase,
unittest,
#expectedFailure
)
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()


class TestQgsVectorLayer(TestCase):

mMemoryLayer = None

def setUp(self):
self.mMemoryLayer = QgsVectorLayer("Point", "test", "memory")
myProvider = self.mMemoryLayer.dataProvider()

myProvider.addAttributes([
QgsField("name", QVariant.String),
QgsField("age", QVariant.Int),
QgsField("size", QVariant.Double)])

ft = QgsFeature()
ft.setGeometry(QgsGeometry.fromPoint(QgsPoint(10,10)))
ft.setAttributeMap({0 : QVariant("Johny"),
1 : QVariant(20),
2 : QVariant(0.3)})
myProvider.addFeatures([ft])


def testWrite(self):
"""Check we can write a vector file."""

myFileName = os.path.join(str(QDir.tempPath()), 'writetest.shp')
print myFileName
# Explicitly giving all options, not really needed but nice for clarity
myErrorMessage = QString()
myOptions = QStringList()
myLayerOptions = QStringList()
mySelectedOnlyFlag = False
mySkipAttributesFlag = False
myGeoCrs = QgsCoordinateReferenceSystem()
myGeoCrs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
myResult = QgsVectorFileWriter.writeAsVectorFormat(
self.mMemoryLayer,
myFileName,
'utf-8',
myGeoCrs,
"ESRI Shapefile",
mySelectedOnlyFlag,
myErrorMessage,
myOptions,
myLayerOptions,
mySkipAttributesFlag)
assert myResult==True

if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions tests/src/python/utilities.py
Expand Up @@ -159,3 +159,4 @@ def setCanvasCrs(theEpsgId, theOtfpFlag=False):
# Reproject all layers to WGS84 geographic CRS
CANVAS.mapRenderer().setDestinationCrs(myCrs)


0 comments on commit 3564eae

Please sign in to comment.