@@ -781,14 +781,15 @@ def testPktComposite(self):
781
781
782
782
fields = vl .fields ()
783
783
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' )))
785
786
# first of all: we must be able to fetch a valid feature
786
787
self .assertTrue (f .isValid ())
787
788
self .assertEqual (f ['pk1' ], 1 )
788
789
self .assertEqual (f ['pk2' ], 2 )
789
790
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 )
792
793
self .assertEqual (f ['value' ], 'test 2' )
793
794
794
795
# can we edit a field?
@@ -799,12 +800,12 @@ def testPktComposite(self):
799
800
# Did we get it right? Let's create a new QgsVectorLayer and try to read back our changes:
800
801
vl2 = QgsVectorLayer ('{} sslmode=disable srid=4326 table="qgis_test"."tb_test_compound_pk" (geom) key=\' "pk1","pk2","pk3"\' ' .format (self .dbconn ), "test_compound2" , "postgres" )
801
802
self .assertTrue (vl2 .isValid ())
802
- f2 = next (vl .getFeatures (QgsFeatureRequest ().setFilterExpression ('pk3 = 3.1415927 ' )))
803
+ f2 = next (vl .getFeatures (QgsFeatureRequest ().setFilterExpression ('pk3 = 3.14159 ' )))
803
804
self .assertTrue (f2 .isValid ())
804
805
805
806
# 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 )
808
809
809
810
# Then, making sure we really did change our value.
810
811
self .assertEqual (f2 ['value' ], 'Edited Test 2' )
@@ -824,9 +825,7 @@ def testPktComposite(self):
824
825
f4 = next (vl2 .getFeatures (QgsFeatureRequest ().setFilterExpression ('pk2 = -9223372036854775800' )))
825
826
826
827
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' ]
830
829
self .assertEqual (f4 .attributes (), expected_attrs )
831
830
832
831
# Finally, let's delete one of the features.
@@ -854,11 +853,13 @@ def testPktFloat(self):
854
853
vl = QgsVectorLayer ('{} sslmode=disable srid=4326 key="pk" table="qgis_test"."tb_test_float_pk" (geom)' .format (self .dbconn ), "test_floatpk" , "postgres" )
855
854
self .assertTrue (vl .isValid ())
856
855
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' )))
858
860
self .assertTrue (f .isValid ())
859
861
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 )
862
863
self .assertEqual (f ['value' ], 'first test' )
863
864
864
865
# editing
@@ -869,7 +870,7 @@ def testPktFloat(self):
869
870
# checking out if we really wrote to the table
870
871
vl2 = QgsVectorLayer ('{} sslmode=disable srid=4326 key="pk" table="qgis_test"."tb_test_float_pk" (geom)' .format (self .dbconn ), "test_floatpk2" , "postgres" )
871
872
self .assertTrue (vl2 .isValid ())
872
- f2 = next (vl2 .getFeatures (QgsFeatureRequest ().setFilterExpression ('pk = 3.141592741 ' )))
873
+ f2 = next (vl2 .getFeatures (QgsFeatureRequest ().setFilterExpression ('pk = 3.14159 ' )))
873
874
874
875
self .assertEqual (f2 ['value' ], 'first check' )
875
876
@@ -888,14 +889,15 @@ def testPktFloat(self):
888
889
self .assertEqual (f4 ['value' ], 'newly inserted' )
889
890
890
891
# 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' )))
892
894
self .assertTrue (f5 .isValid ())
893
895
vl2 .startEditing ()
894
896
vl2 .deleteFeatures ([f5 .id ()])
895
897
self .assertTrue (vl2 .commitChanges ())
896
898
897
899
# did we really delete?
898
- f_iterator = vl .getFeatures (QgsFeatureRequest ().setFilterExpression ('pk = 2.7182817 ' ))
900
+ f_iterator = vl .getFeatures (QgsFeatureRequest ().setFilterExpression ('pk = 2.71828 ' ))
899
901
got_feature = True
900
902
901
903
try :
0 commit comments