Skip to content

Commit ef6df0f

Browse files
committedJul 5, 2018
Add feature source test for QgsVectorLayerSelectedFeatureSource
1 parent 2430529 commit ef6df0f

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
 

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ ADD_PYTHON_TEST(PyQgsVectorFileWriterTask test_qgsvectorfilewritertask.py)
195195
ADD_PYTHON_TEST(PyQgsVectorLayer test_qgsvectorlayer.py)
196196
ADD_PYTHON_TEST(PyQgsVectorLayerCache test_qgsvectorlayercache.py)
197197
ADD_PYTHON_TEST(PyQgsVectorLayerEditBuffer test_qgsvectorlayereditbuffer.py)
198+
ADD_PYTHON_TEST(PyQgsVectorLayerSelectedFeatureSource test_qgsvectorlayerselectedfeaturesource.py)
198199
ADD_PYTHON_TEST(PyQgsVectorLayerUtils test_qgsvectorlayerutils.py)
199200
ADD_PYTHON_TEST(PyQgsZonalStatistics test_qgszonalstatistics.py)
200201
ADD_PYTHON_TEST(PyQgsVirtualLayerProvider test_provider_virtual.py)
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsVectorLayerSelectedFeatureSource
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__ = '2018-07-05'
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+
16+
from qgis.core import (
17+
QgsVectorLayer,
18+
QgsFeatureRequest,
19+
QgsFeature,
20+
QgsGeometry,
21+
NULL,
22+
QgsVectorLayerSelectedFeatureSource
23+
)
24+
25+
from qgis.testing import (
26+
start_app,
27+
unittest
28+
)
29+
30+
from utilities import (
31+
unitTestDataPath
32+
)
33+
34+
from featuresourcetestbase import FeatureSourceTestCase
35+
36+
start_app()
37+
TEST_DATA_DIR = unitTestDataPath()
38+
39+
40+
class TestPyQgsVectorLayerSelectedFeatureSource(unittest.TestCase, FeatureSourceTestCase):
41+
42+
@classmethod
43+
def createLayer(cls):
44+
vl = QgsVectorLayer(
45+
'Point?crs=epsg:4326&field=pk:integer&field=cnt:integer&field=name:string(0)&field=name2:string(0)&field=num_char:string&key=pk',
46+
'test', 'memory')
47+
assert (vl.isValid())
48+
49+
f1 = QgsFeature()
50+
f1.setAttributes([5, -200, NULL, 'NuLl', '5'])
51+
f1.setGeometry(QgsGeometry.fromWkt('Point (-71.123 78.23)'))
52+
53+
f2 = QgsFeature()
54+
f2.setAttributes([3, 300, 'Pear', 'PEaR', '3'])
55+
56+
f3 = QgsFeature()
57+
f3.setAttributes([1, 100, 'Orange', 'oranGe', '1'])
58+
f3.setGeometry(QgsGeometry.fromWkt('Point (-70.332 66.33)'))
59+
60+
f4 = QgsFeature()
61+
f4.setAttributes([2, 200, 'Apple', 'Apple', '2'])
62+
f4.setGeometry(QgsGeometry.fromWkt('Point (-68.2 70.8)'))
63+
64+
f5 = QgsFeature()
65+
f5.setAttributes([4, 400, 'Honey', 'Honey', '4'])
66+
f5.setGeometry(QgsGeometry.fromWkt('Point (-65.32 78.3)'))
67+
68+
f6 = QgsFeature()
69+
f6.setAttributes([6, -200, NULL, 'NuLl', '5'])
70+
f6.setGeometry(QgsGeometry.fromWkt('Point (-71.123 78.23)'))
71+
72+
f7 = QgsFeature()
73+
f7.setAttributes([7, 300, 'Pear', 'PEaR', '3'])
74+
75+
f8 = QgsFeature()
76+
f8.setAttributes([8, 100, 'Orange', 'oranGe', '1'])
77+
f8.setGeometry(QgsGeometry.fromWkt('Point (-70.332 66.33)'))
78+
79+
f9 = QgsFeature()
80+
f9.setAttributes([9, 200, 'Apple', 'Apple', '2'])
81+
f9.setGeometry(QgsGeometry.fromWkt('Point (-68.2 70.8)'))
82+
83+
f10 = QgsFeature()
84+
f10.setAttributes([10, 400, 'Honey', 'Honey', '4'])
85+
f10.setGeometry(QgsGeometry.fromWkt('Point (-65.32 78.3)'))
86+
87+
vl.dataProvider().addFeatures([f1, f2, f3, f4, f5, f6, f7, f8, f9, f10])
88+
return vl
89+
90+
@classmethod
91+
def setUpClass(cls):
92+
"""Run before all tests"""
93+
# Create test layer
94+
cls.vl = cls.createLayer()
95+
assert (cls.vl.isValid())
96+
97+
ids = [f.id() for f in cls.vl.getFeatures(QgsFeatureRequest().setFilterExpression('pk in (1,2,3,4,5)'))]
98+
assert len(ids) == 5
99+
100+
cls.vl.selectByIds(ids)
101+
cls.source = QgsVectorLayerSelectedFeatureSource(cls.vl)
102+
103+
@classmethod
104+
def tearDownClass(cls):
105+
"""Run after all tests"""
106+
107+
def testGetFeaturesSubsetAttributes2(self):
108+
""" Override and skip this test for memory provider, as it's actually more efficient for the memory provider to return
109+
its features as direct copies (due to implicit sharing of QgsFeature)
110+
"""
111+
pass
112+
113+
def testGetFeaturesNoGeometry(self):
114+
""" Override and skip this test for memory provider, as it's actually more efficient for the memory provider to return
115+
its features as direct copies (due to implicit sharing of QgsFeature)
116+
"""
117+
pass
118+
119+
120+
if __name__ == '__main__':
121+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.