Skip to content

Commit c6ff640

Browse files
committedApr 10, 2021
Added unit tests for Jenks classification
1 parent ed4e97d commit c6ff640

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed
 

‎tests/src/python/test_qgsclassificationmethod.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
__copyright__ = 'Copyright 2019, The QGIS Project'
1212

1313
import qgis # NOQA
14+
import random
1415

1516
from qgis.PyQt.QtCore import QLocale
1617
from qgis.testing import unittest, start_app
17-
from qgis.core import QgsClassificationMethod, QgsClassificationLogarithmic, QgsFeature, QgsVectorLayer, QgsPointXY, \
18+
from qgis.core import QgsClassificationMethod, QgsClassificationLogarithmic, QgsClassificationJenks, QgsFeature, QgsVectorLayer, QgsPointXY, \
1819
QgsGeometry
1920

2021
start_app()
@@ -85,6 +86,34 @@ def testQgsClassificationLogarithmic_FilterZeroNeg(self):
8586
self.assertEqual(r[0].label(), '-2 - 10^0')
8687
self.assertEqual(QgsClassificationMethod.rangesToBreaks(r), [1.0, 10.0, 100.0, 1000.0, 10000.0])
8788

89+
def testQgsClassificationJenksSimple(self):
90+
# This is a simple Natural Breaks Jenks test checking if simple calculation can be done
91+
# when number of values is below 3000 (value hardcoded in qgsclassificationjenks.h)
92+
# And if returned values are consistent (the same as at the time of creation of this test)
93+
values = [-33, -41, -43, 16, 29, 9, -35, 57, 26, -30]
94+
vl = createMemoryLayer(values)
95+
m = QgsClassificationJenks()
96+
97+
r = m.classes(vl, 'value', 4)
98+
self.assertEqual(len(r), 4)
99+
self.assertEqual(QgsClassificationMethod.rangesToBreaks(r),
100+
[-30.0, 16.0, 29.0, 57.0])
101+
102+
def testQgsClassificationJenksHighNumber(self):
103+
# This test checks if Jenkis classification does not crash when number of
104+
# values in a set is higher than 3000 (value hardcoded in qgsclassificationjenks.h)
105+
# And if returned values are consistent (the same as at the time of creation of this test)
106+
random.seed(42*42);
107+
values = [random.randint(-1000, 1000) for _ in range(5000)]
108+
vl = createMemoryLayer(values)
109+
m = QgsClassificationJenks()
110+
111+
r = m.classes(vl, 'value', 4)
112+
print('result high=',QgsClassificationMethod.rangesToBreaks(r))
113+
self.assertEqual(len(r), 4)
114+
self.assertEqual(QgsClassificationMethod.rangesToBreaks(r),
115+
[-506.0, -4.0, 499.0, 1000.0])
116+
88117

89118
if __name__ == "__main__":
90119
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.