Skip to content

Commit 6a85309

Browse files
espinafrenyalldawson
authored andcommittedJun 17, 2020
Adjusting tests to deal with floating point madness
PostgreSQL 11 and 12 handle floating point values differently; the test originally passed on PostgreSQL 12, but not on 11 (Travis). Now, with floating point data truncated to only 6 digits, they run on both versions of the DBMS. See https://www.postgresql.org/docs/12/release-12.html
1 parent c521f39 commit 6a85309

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed
 

‎tests/src/python/test_provider_postgres.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -781,14 +781,15 @@ def testPktComposite(self):
781781

782782
fields = vl.fields()
783783

784-
f = next(vl.getFeatures(QgsFeatureRequest().setFilterExpression('pk3 = 3.1415927')))
784+
# Only 6 decimals for PostgreSQL 11.
785+
f = next(vl.getFeatures(QgsFeatureRequest().setFilterExpression('pk3 = 3.14159')))
785786
# first of all: we must be able to fetch a valid feature
786787
self.assertTrue(f.isValid())
787788
self.assertEqual(f['pk1'], 1)
788789
self.assertEqual(f['pk2'], 2)
789790

790-
# round() needed to make sure Python older than 3.8 do the right thing.
791-
self.assertEqual(round(f['pk3'], 5), round(3.1415927, 5))
791+
# Only 6 decimals for PostgreSQL 11.
792+
self.assertEqual(f['pk3'], 3.14159)
792793
self.assertEqual(f['value'], 'test 2')
793794

794795
# can we edit a field?
@@ -799,12 +800,12 @@ def testPktComposite(self):
799800
# Did we get it right? Let's create a new QgsVectorLayer and try to read back our changes:
800801
vl2 = QgsVectorLayer('{} sslmode=disable srid=4326 table="qgis_test"."tb_test_compound_pk" (geom) key=\'"pk1","pk2","pk3"\' '.format(self.dbconn), "test_compound2", "postgres")
801802
self.assertTrue(vl2.isValid())
802-
f2 = next(vl.getFeatures(QgsFeatureRequest().setFilterExpression('pk3 = 3.1415927')))
803+
f2 = next(vl.getFeatures(QgsFeatureRequest().setFilterExpression('pk3 = 3.14159')))
803804
self.assertTrue(f2.isValid())
804805

805806
# just making sure we have the correct feature
806-
# round() needed to make sure Python older than 3.8 do the right thing.
807-
self.assertEqual(round(f2['pk3'], 5), round(3.1415927, 5))
807+
# Only 6 decimals for PostgreSQL 11.
808+
self.assertEqual(f2['pk3'], 3.14159)
808809

809810
# Then, making sure we really did change our value.
810811
self.assertEqual(f2['value'], 'Edited Test 2')
@@ -824,9 +825,7 @@ def testPktComposite(self):
824825
f4 = next(vl2.getFeatures(QgsFeatureRequest().setFilterExpression('pk2 = -9223372036854775800')))
825826

826827
self.assertTrue(f4.isValid())
827-
# round() needed to make sure Python older than 3.8 do the right thing.
828-
expected_attrs = [4, -9223372036854775800, round(7.29154, 5), 'other test']
829-
gotten_attrs = [f4['pk1'], f4['pk2'], round(f4['pk3'], 5), f4['value']]
828+
expected_attrs = [4, -9223372036854775800, 7.29154, 'other test']
830829
self.assertEqual(f4.attributes(), expected_attrs)
831830

832831
# Finally, let's delete one of the features.
@@ -854,11 +853,13 @@ def testPktFloat(self):
854853
vl = QgsVectorLayer('{} sslmode=disable srid=4326 key="pk" table="qgis_test"."tb_test_float_pk" (geom)'.format(self.dbconn), "test_floatpk", "postgres")
855854
self.assertTrue(vl.isValid())
856855
fields = vl.fields()
857-
f = next(vl.getFeatures(QgsFeatureRequest().setFilterExpression('pk = 3.141592741')))
856+
# floating point madness: PostgreSQL 11 only outputs 6 decimal digits for real type by default
857+
# Thus, QGIS only stores 6 decimal digits in its attributes. For this reason,
858+
# we must insert only 3.14159 instead of the more accurate 3.141592741 in the test table.
859+
f = next(vl.getFeatures(QgsFeatureRequest().setFilterExpression('pk = 3.14159')))
858860
self.assertTrue(f.isValid())
859861

860-
# round() needed to make sure Python older than 3.8 do the right thing.
861-
self.assertEqual(round(f['pk'], 5), round(3.1415927, 5))
862+
self.assertEqual(f['pk'], 3.14159)
862863
self.assertEqual(f['value'], 'first test')
863864

864865
# editing
@@ -869,7 +870,7 @@ def testPktFloat(self):
869870
# checking out if we really wrote to the table
870871
vl2 = QgsVectorLayer('{} sslmode=disable srid=4326 key="pk" table="qgis_test"."tb_test_float_pk" (geom)'.format(self.dbconn), "test_floatpk2", "postgres")
871872
self.assertTrue(vl2.isValid())
872-
f2 = next(vl2.getFeatures(QgsFeatureRequest().setFilterExpression('pk = 3.141592741')))
873+
f2 = next(vl2.getFeatures(QgsFeatureRequest().setFilterExpression('pk = 3.14159')))
873874

874875
self.assertEqual(f2['value'], 'first check')
875876

@@ -888,14 +889,15 @@ def testPktFloat(self):
888889
self.assertEqual(f4['value'], 'newly inserted')
889890

890891
# Checking deletion
891-
f5 = next(vl2.getFeatures(QgsFeatureRequest().setFilterExpression('pk = 2.7182817')))
892+
# same as above: no more than 6 digits of Euler's number in the testdata for PostgreSQL 11.
893+
f5 = next(vl2.getFeatures(QgsFeatureRequest().setFilterExpression('pk = 2.71828')))
892894
self.assertTrue(f5.isValid())
893895
vl2.startEditing()
894896
vl2.deleteFeatures([f5.id()])
895897
self.assertTrue(vl2.commitChanges())
896898

897899
# did we really delete?
898-
f_iterator = vl.getFeatures(QgsFeatureRequest().setFilterExpression('pk = 2.7182817'))
900+
f_iterator = vl.getFeatures(QgsFeatureRequest().setFilterExpression('pk = 2.71828'))
899901
got_feature = True
900902

901903
try:

‎tests/testdata/provider/testdata_pg_bigint_pk.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ CREATE TABLE qgis_test.tb_test_compound_pk
116116

117117
INSERT INTO qgis_test.tb_test_compound_pk (pk1, pk2, pk3, value, geom) VALUES
118118
(1, 1, 1.0, 'test 1', ST_SetSRID(ST_Point(-47.930, -15.818), 4326)),
119-
(1, 2, 3.141592741, 'test 2', ST_SetSRID(ST_Point(-47.887, -15.864), 4326)),
119+
(1, 2, 3.14159, 'test 2', ST_SetSRID(ST_Point(-47.887, -15.864), 4326)),
120120
(2, 2, 2.718281828, 'test 3', ST_SetSRID(ST_Point(-47.902, -15.763), 4326)),
121121
(2, 2, 1.0, 'test 4', ST_SetSRID(ST_Point(-47.952, -15.781), 4326));
122122

@@ -128,5 +128,5 @@ CREATE TABLE qgis_test.tb_test_float_pk
128128
);
129129

130130
INSERT INTO qgis_test.tb_test_float_pk (pk, value, geom) VALUES
131-
(3.141592741, 'first test', ST_SetSRID(ST_Point(-47.887, -15.864), 4326)),
132-
(2.718281828, 'second test', ST_SetSRID(ST_Point(-47.902, -15.763), 4326));
131+
(3.14159, 'first test', ST_SetSRID(ST_Point(-47.887, -15.864), 4326)),
132+
(2.71828, 'second test', ST_SetSRID(ST_Point(-47.902, -15.763), 4326));

0 commit comments

Comments
 (0)
Please sign in to comment.