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