Navigation Menu

Skip to content

Commit

Permalink
Add unit tests for QgsVectorFileWriterTask
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 12, 2017
1 parent 15f8cfb commit 4b3d401
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/core/qgsvectorfilewritertask.cpp
Expand Up @@ -29,7 +29,8 @@ QgsVectorFileWriterTask::QgsVectorFileWriterTask( QgsVectorLayer* layer, const Q
mOwnedFeedback.reset( new QgsFeedback() );
mOptions.feedback = mOwnedFeedback.get();
}
setDependentLayers( QStringList() << mLayer->id() );
if ( mLayer )
setDependentLayers( QStringList() << mLayer->id() );
}

void QgsVectorFileWriterTask::cancel()
Expand All @@ -43,7 +44,6 @@ bool QgsVectorFileWriterTask::run()
if ( !mLayer )
return false;


mError = QgsVectorFileWriter::writeAsVectorFormat(
mLayer, mDestFileName, mOptions, &mNewFilename, &mErrorMessage );
return mError == QgsVectorFileWriter::NoError;
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -125,6 +125,7 @@ ADD_PYTHON_TEST(PyQgsTreeWidgetItem test_qgstreewidgetitem.py)
ADD_PYTHON_TEST(PyQgsUnitTypes test_qgsunittypes.py)
ADD_PYTHON_TEST(PyQgsVectorColorRamp test_qgsvectorcolorramp.py)
ADD_PYTHON_TEST(PyQgsVectorFileWriter test_qgsvectorfilewriter.py)
ADD_PYTHON_TEST(PyQgsVectorFileWriterTask test_qgsvectorfilewritertask.py)
ADD_PYTHON_TEST(PyQgsVectorLayer test_qgsvectorlayer.py)
ADD_PYTHON_TEST(PyQgsVectorLayerEditBuffer test_qgsvectorlayereditbuffer.py)
ADD_PYTHON_TEST(PyQgsVectorLayerUtils test_qgsvectorlayerutils.py)
Expand Down
126 changes: 126 additions & 0 deletions tests/src/python/test_qgsvectorfilewritertask.py
@@ -0,0 +1,126 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsVectorFileWriterTask.
.. 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__ = 'Nyall Dawson'
__date__ = '12/02/2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA
import os

from qgis.core import (
QgsTask,
QgsTaskManager,
QgsApplication,
QgsVectorLayer,
QgsFeature,
QgsGeometry,
QgsPoint,
QgsVectorFileWriter,
QgsVectorFileWriterTask

)
from qgis.PyQt.QtCore import (QCoreApplication,
QDir)

from qgis.testing import start_app, unittest

start_app()


def create_temp_filename(base_file):
return os.path.join(str(QDir.tempPath()), base_file)


class TestQgsVectorFileWriterTask(unittest.TestCase):

def setUp(self):
self.success = False
self.fail = False

def onSuccess(self):
self.success = True

def onFail(self):
self.fail = True

def createLayer(self):
layer = QgsVectorLayer(
('Point?crs=epsg:4326&field=name:string(20)&'
'field=age:integer&field=size:double&index=yes'),
'test',
'memory')

self.assertIsNotNone(layer, 'Provider not initialized')
provider = layer.dataProvider()
self.assertIsNotNone(provider)

ft = QgsFeature()
ft.setGeometry(QgsGeometry.fromPoint(QgsPoint(10, 10)))
ft.setAttributes(['Johny', 20, 0.3])
provider.addFeatures([ft])
return layer

def testSuccess(self):
"""test successfully writing a layer"""
self.layer = self.createLayer()
options = QgsVectorFileWriter.SaveVectorOptions()
tmp = create_temp_filename('successlayer.shp')
task = QgsVectorFileWriterTask(self.layer, tmp, options)

task.writeComplete.connect(self.onSuccess)
task.errorOccurred.connect(self.onFail)

QgsApplication.taskManager().addTask(task)
while not self.success and not self.fail:
QCoreApplication.processEvents()

self.assertTrue(self.success)
self.assertFalse(self.fail)

def testLayerRemovalBeforeRun(self):
"""test behavior when layer is removed before task begins"""
self.layer = self.createLayer()
options = QgsVectorFileWriter.SaveVectorOptions()
tmp = create_temp_filename('fail.shp')
task = QgsVectorFileWriterTask(self.layer, tmp, options)

task.writeComplete.connect(self.onSuccess)
task.errorOccurred.connect(self.onFail)

# remove layer
self.layer = None

QgsApplication.taskManager().addTask(task)
while not self.success and not self.fail:
QCoreApplication.processEvents()

self.assertFalse(self.success)
self.assertTrue(self.fail)

def testNoLayer(self):
"""test that failure (and not crash) occurs when no layer set"""

options = QgsVectorFileWriter.SaveVectorOptions()
tmp = create_temp_filename('fail.shp')
task = QgsVectorFileWriterTask(None, tmp, options)
task.writeComplete.connect(self.onSuccess)
task.errorOccurred.connect(self.onFail)

QgsApplication.taskManager().addTask(task)
while not self.success and not self.fail:
QCoreApplication.processEvents()

self.assertFalse(self.success)
self.assertTrue(self.fail)


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

0 comments on commit 4b3d401

Please sign in to comment.