Skip to content
Permalink
Browse files

Add unit test for QgsVirtualLayerTask

  • Loading branch information
pblottiere committed Mar 2, 2018
1 parent 7b18fa8 commit b8a009c456bc3048491749dd24ef1f959fa87185
Showing with 73 additions and 0 deletions.
  1. +1 −0 tests/src/python/CMakeLists.txt
  2. +72 −0 tests/src/python/test_qgsvirtuallayertask.py
@@ -184,6 +184,7 @@ ADD_PYTHON_TEST(PyQgsVectorLayerEditBuffer test_qgsvectorlayereditbuffer.py)
ADD_PYTHON_TEST(PyQgsVectorLayerUtils test_qgsvectorlayerutils.py)
ADD_PYTHON_TEST(PyQgsZonalStatistics test_qgszonalstatistics.py)
ADD_PYTHON_TEST(PyQgsVirtualLayerProvider test_provider_virtual.py)
ADD_PYTHON_TEST(PyQgsVirtualLayerTask test_qgsvirtuallayertask.py)
ADD_PYTHON_TEST(PyQgsVirtualLayerDefinition test_qgsvirtuallayerdefinition.py)
ADD_PYTHON_TEST(PyQgsLayerDefinition test_qgslayerdefinition.py)
ADD_PYTHON_TEST(PyQgsWFSProvider test_provider_wfs.py)
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsVirtualLayerTask.
.. 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__ = 'Paul Blottiere'
__date__ = '28/02/2018'
__copyright__ = 'Copyright 2018, 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 (
QgsProject,
QgsVectorLayer,
QgsApplication,
QgsVirtualLayerDefinition,
QgsVirtualLayerTask
)
from qgis.PyQt.QtCore import QCoreApplication
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath

start_app()


class TestQgsVirtualLayerTask(unittest.TestCase):

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

def onSuccess(self):
self.success = True

def onFail(self):
self.fail = True

def test(self):
l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "françéà", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1)

df = QgsVirtualLayerDefinition()
df.setQuery('select * from "françéà"')
task = QgsVirtualLayerTask(df)

ids = [f.id() for f in task.layer().getFeatures()]
self.assertEqual(len(ids), 0)

task.taskCompleted.connect(self.onSuccess)
task.taskTerminated.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)

ids = [f.id() for f in task.layer().getFeatures()]
self.assertEqual(len(ids), 4)


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

0 comments on commit b8a009c

Please sign in to comment.
You can’t perform that action at this time.