Skip to content

Commit 4b3d401

Browse files
committed
Add unit tests for QgsVectorFileWriterTask
1 parent 15f8cfb commit 4b3d401

File tree

3 files changed

+129
-2
lines changed

3 files changed

+129
-2
lines changed

src/core/qgsvectorfilewritertask.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ QgsVectorFileWriterTask::QgsVectorFileWriterTask( QgsVectorLayer* layer, const Q
2929
mOwnedFeedback.reset( new QgsFeedback() );
3030
mOptions.feedback = mOwnedFeedback.get();
3131
}
32-
setDependentLayers( QStringList() << mLayer->id() );
32+
if ( mLayer )
33+
setDependentLayers( QStringList() << mLayer->id() );
3334
}
3435

3536
void QgsVectorFileWriterTask::cancel()
@@ -43,7 +44,6 @@ bool QgsVectorFileWriterTask::run()
4344
if ( !mLayer )
4445
return false;
4546

46-
4747
mError = QgsVectorFileWriter::writeAsVectorFormat(
4848
mLayer, mDestFileName, mOptions, &mNewFilename, &mErrorMessage );
4949
return mError == QgsVectorFileWriter::NoError;

tests/src/python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ ADD_PYTHON_TEST(PyQgsTreeWidgetItem test_qgstreewidgetitem.py)
125125
ADD_PYTHON_TEST(PyQgsUnitTypes test_qgsunittypes.py)
126126
ADD_PYTHON_TEST(PyQgsVectorColorRamp test_qgsvectorcolorramp.py)
127127
ADD_PYTHON_TEST(PyQgsVectorFileWriter test_qgsvectorfilewriter.py)
128+
ADD_PYTHON_TEST(PyQgsVectorFileWriterTask test_qgsvectorfilewritertask.py)
128129
ADD_PYTHON_TEST(PyQgsVectorLayer test_qgsvectorlayer.py)
129130
ADD_PYTHON_TEST(PyQgsVectorLayerEditBuffer test_qgsvectorlayereditbuffer.py)
130131
ADD_PYTHON_TEST(PyQgsVectorLayerUtils test_qgsvectorlayerutils.py)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsVectorFileWriterTask.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Nyall Dawson'
10+
__date__ = '12/02/2017'
11+
__copyright__ = 'Copyright 2017, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis # NOQA
16+
import os
17+
18+
from qgis.core import (
19+
QgsTask,
20+
QgsTaskManager,
21+
QgsApplication,
22+
QgsVectorLayer,
23+
QgsFeature,
24+
QgsGeometry,
25+
QgsPoint,
26+
QgsVectorFileWriter,
27+
QgsVectorFileWriterTask
28+
29+
)
30+
from qgis.PyQt.QtCore import (QCoreApplication,
31+
QDir)
32+
33+
from qgis.testing import start_app, unittest
34+
35+
start_app()
36+
37+
38+
def create_temp_filename(base_file):
39+
return os.path.join(str(QDir.tempPath()), base_file)
40+
41+
42+
class TestQgsVectorFileWriterTask(unittest.TestCase):
43+
44+
def setUp(self):
45+
self.success = False
46+
self.fail = False
47+
48+
def onSuccess(self):
49+
self.success = True
50+
51+
def onFail(self):
52+
self.fail = True
53+
54+
def createLayer(self):
55+
layer = QgsVectorLayer(
56+
('Point?crs=epsg:4326&field=name:string(20)&'
57+
'field=age:integer&field=size:double&index=yes'),
58+
'test',
59+
'memory')
60+
61+
self.assertIsNotNone(layer, 'Provider not initialized')
62+
provider = layer.dataProvider()
63+
self.assertIsNotNone(provider)
64+
65+
ft = QgsFeature()
66+
ft.setGeometry(QgsGeometry.fromPoint(QgsPoint(10, 10)))
67+
ft.setAttributes(['Johny', 20, 0.3])
68+
provider.addFeatures([ft])
69+
return layer
70+
71+
def testSuccess(self):
72+
"""test successfully writing a layer"""
73+
self.layer = self.createLayer()
74+
options = QgsVectorFileWriter.SaveVectorOptions()
75+
tmp = create_temp_filename('successlayer.shp')
76+
task = QgsVectorFileWriterTask(self.layer, tmp, options)
77+
78+
task.writeComplete.connect(self.onSuccess)
79+
task.errorOccurred.connect(self.onFail)
80+
81+
QgsApplication.taskManager().addTask(task)
82+
while not self.success and not self.fail:
83+
QCoreApplication.processEvents()
84+
85+
self.assertTrue(self.success)
86+
self.assertFalse(self.fail)
87+
88+
def testLayerRemovalBeforeRun(self):
89+
"""test behavior when layer is removed before task begins"""
90+
self.layer = self.createLayer()
91+
options = QgsVectorFileWriter.SaveVectorOptions()
92+
tmp = create_temp_filename('fail.shp')
93+
task = QgsVectorFileWriterTask(self.layer, tmp, options)
94+
95+
task.writeComplete.connect(self.onSuccess)
96+
task.errorOccurred.connect(self.onFail)
97+
98+
# remove layer
99+
self.layer = None
100+
101+
QgsApplication.taskManager().addTask(task)
102+
while not self.success and not self.fail:
103+
QCoreApplication.processEvents()
104+
105+
self.assertFalse(self.success)
106+
self.assertTrue(self.fail)
107+
108+
def testNoLayer(self):
109+
"""test that failure (and not crash) occurs when no layer set"""
110+
111+
options = QgsVectorFileWriter.SaveVectorOptions()
112+
tmp = create_temp_filename('fail.shp')
113+
task = QgsVectorFileWriterTask(None, tmp, options)
114+
task.writeComplete.connect(self.onSuccess)
115+
task.errorOccurred.connect(self.onFail)
116+
117+
QgsApplication.taskManager().addTask(task)
118+
while not self.success and not self.fail:
119+
QCoreApplication.processEvents()
120+
121+
self.assertFalse(self.success)
122+
self.assertTrue(self.fail)
123+
124+
125+
if __name__ == '__main__':
126+
unittest.main()

0 commit comments

Comments
 (0)