|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsVirtualLayerTask. |
| 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__ = 'Paul Blottiere' |
| 10 | +__date__ = '28/02/2018' |
| 11 | +__copyright__ = 'Copyright 2018, 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 | + QgsProject, |
| 20 | + QgsVectorLayer, |
| 21 | + QgsApplication, |
| 22 | + QgsVirtualLayerDefinition, |
| 23 | + QgsVirtualLayerTask |
| 24 | +) |
| 25 | +from qgis.PyQt.QtCore import QCoreApplication |
| 26 | +from qgis.testing import start_app, unittest |
| 27 | +from utilities import unitTestDataPath |
| 28 | + |
| 29 | +start_app() |
| 30 | + |
| 31 | + |
| 32 | +class TestQgsVirtualLayerTask(unittest.TestCase): |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + self.testDataDir = unitTestDataPath() |
| 36 | + self.success = False |
| 37 | + self.fail = False |
| 38 | + |
| 39 | + def onSuccess(self): |
| 40 | + self.success = True |
| 41 | + |
| 42 | + def onFail(self): |
| 43 | + self.fail = True |
| 44 | + |
| 45 | + def test(self): |
| 46 | + l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "françéà", "ogr", QgsVectorLayer.LayerOptions(False)) |
| 47 | + self.assertEqual(l1.isValid(), True) |
| 48 | + QgsProject.instance().addMapLayer(l1) |
| 49 | + |
| 50 | + df = QgsVirtualLayerDefinition() |
| 51 | + df.setQuery('select * from "françéà"') |
| 52 | + task = QgsVirtualLayerTask(df) |
| 53 | + |
| 54 | + ids = [f.id() for f in task.layer().getFeatures()] |
| 55 | + self.assertEqual(len(ids), 0) |
| 56 | + |
| 57 | + task.taskCompleted.connect(self.onSuccess) |
| 58 | + task.taskTerminated.connect(self.onFail) |
| 59 | + |
| 60 | + QgsApplication.taskManager().addTask(task) |
| 61 | + while not self.success and not self.fail: |
| 62 | + QCoreApplication.processEvents() |
| 63 | + |
| 64 | + self.assertTrue(self.success) |
| 65 | + self.assertFalse(self.fail) |
| 66 | + |
| 67 | + ids = [f.id() for f in task.layer().getFeatures()] |
| 68 | + self.assertEqual(len(ids), 4) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == '__main__': |
| 72 | + unittest.main() |
0 commit comments