63
63
from qgis .testing import start_app , unittest
64
64
from featuresourcetestbase import FeatureSourceTestCase
65
65
from utilities import unitTestDataPath
66
+
66
67
start_app ()
67
68
68
69
@@ -353,6 +354,7 @@ def checkBefore():
353
354
354
355
checkAfter ()
355
356
self .assertEqual (layer .dataProvider ().featureCount (), 2 )
357
+
356
358
# DELETE FEATURE
357
359
358
360
def test_DeleteFeature (self ):
@@ -738,12 +740,12 @@ def testUpdateFeature(self):
738
740
739
741
# change geometry and attributes
740
742
f = features [0 ]
741
- f .setAttributes (['new' ,321 ])
743
+ f .setAttributes (['new' , 321 ])
742
744
f .setGeometry (QgsGeometry .fromPointXY (QgsPointXY (- 200 , - 200 )))
743
745
self .assertTrue (layer .updateFeature (f ))
744
746
745
747
new_feature = next (layer .getFeatures (QgsFeatureRequest (f .id ())))
746
- self .assertEqual (new_feature .attributes (), ['new' ,321 ])
748
+ self .assertEqual (new_feature .attributes (), ['new' , 321 ])
747
749
self .assertEqual (new_feature .geometry ().asPoint (), QgsPointXY (- 200 , - 200 ))
748
750
749
751
# add feature with no geometry
@@ -774,7 +776,8 @@ def testUpdateFeature(self):
774
776
def test_AddAttribute (self ):
775
777
layer = createLayerWithOnePoint ()
776
778
fld1 = QgsField ("fld1" , QVariant .Int , "integer" )
777
- #fld2 = QgsField("fld2", QVariant.Int, "integer")
779
+
780
+ # fld2 = QgsField("fld2", QVariant.Int, "integer")
778
781
779
782
def checkBefore ():
780
783
# check fields
@@ -1197,7 +1200,7 @@ def checkFieldNames(names):
1197
1200
checkFieldNames (['fldtxt' , 'fldint' ])
1198
1201
self .assertTrue (layer .renameAttribute (1 , 'fldint2' ))
1199
1202
checkFieldNames (['fldtxt' , 'fldint2' ])
1200
- #add an attribute
1203
+ # add an attribute
1201
1204
self .assertTrue (layer .addAttribute (QgsField ("flddouble" , QVariant .Double , "double" )))
1202
1205
checkFieldNames (['fldtxt' , 'fldint2' , 'flddouble' ])
1203
1206
# rename it
@@ -1260,15 +1263,15 @@ def checkFieldNames(names):
1260
1263
self .assertTrue (layer .renameAttribute (2 , 'flddouble2' ))
1261
1264
checkFieldNames (['fldtxt2' , 'fldint' , 'flddouble2' ])
1262
1265
1263
- #delete an attribute
1266
+ # delete an attribute
1264
1267
self .assertTrue (layer .deleteAttribute (0 ))
1265
1268
checkFieldNames (['fldint' , 'flddouble2' ])
1266
1269
# rename remaining
1267
1270
self .assertTrue (layer .renameAttribute (0 , 'fldint2' ))
1268
1271
checkFieldNames (['fldint2' , 'flddouble2' ])
1269
1272
self .assertTrue (layer .renameAttribute (1 , 'flddouble3' ))
1270
1273
checkFieldNames (['fldint2' , 'flddouble3' ])
1271
- #delete an attribute
1274
+ # delete an attribute
1272
1275
self .assertTrue (layer .deleteAttribute (0 ))
1273
1276
checkFieldNames (['flddouble3' ])
1274
1277
self .assertTrue (layer .renameAttribute (0 , 'flddouble4' ))
@@ -1289,16 +1292,16 @@ def checkFieldNames(names):
1289
1292
layer .undoStack ().undo ()
1290
1293
checkFieldNames (['fldtxt' , 'fldint' , 'flddouble' ])
1291
1294
1292
- #layer.undoStack().redo()
1293
- #checkFieldNames(['fldtxt2', 'fldint'])
1294
- #layer.undoStack().redo()
1295
- #checkFieldNames(['fldint'])
1295
+ # layer.undoStack().redo()
1296
+ # checkFieldNames(['fldtxt2', 'fldint'])
1297
+ # layer.undoStack().redo()
1298
+ # checkFieldNames(['fldint'])
1296
1299
1297
1300
def test_RenameExpressionField (self ):
1298
1301
layer = createLayerWithOnePoint ()
1299
1302
exp_field_idx = layer .addExpressionField ('1+1' , QgsField ('math_is_hard' , QVariant .Int ))
1300
1303
1301
- #rename and check
1304
+ # rename and check
1302
1305
self .assertTrue (layer .renameAttribute (exp_field_idx , 'renamed' ))
1303
1306
self .assertEqual (layer .fields ()[exp_field_idx ].name (), 'renamed' )
1304
1307
f = next (layer .getFeatures ())
@@ -1559,7 +1562,8 @@ def testUniqueStringsMatching(self):
1559
1562
f1_id = next (layer .getFeatures ()).id ()
1560
1563
self .assertTrue (layer .changeAttributeValue (f1_id , 0 , 'coconut' ))
1561
1564
# note - this isn't 100% accurate, since orange no longer exists - but it avoids looping through all features
1562
- self .assertEqual (set (layer .uniqueStringsMatching (0 , 'n' )), set (['orange' , 'BanaNa' , 'waterMelon' , 'pineapple' , 'coconut' ]))
1565
+ self .assertEqual (set (layer .uniqueStringsMatching (0 , 'n' )),
1566
+ set (['orange' , 'BanaNa' , 'waterMelon' , 'pineapple' , 'coconut' ]))
1563
1567
1564
1568
def testMinValue (self ):
1565
1569
""" test retrieving minimum values """
@@ -1638,7 +1642,7 @@ def test_InvalidOperations(self):
1638
1642
self .assertFalse (layer .deleteFeature (- 333 ))
1639
1643
# we do not check for existence of the feature id if it's
1640
1644
# not newly added feature
1641
- #self.assertFalse(layer.deleteFeature(333))
1645
+ # self.assertFalse(layer.deleteFeature(333))
1642
1646
1643
1647
# CHANGE GEOMETRY
1644
1648
@@ -1712,7 +1716,7 @@ def test_ExpressionField(self):
1712
1716
self .assertEqual (fet ['test2' ], NULL )
1713
1717
1714
1718
def test_ExpressionFieldEllipsoidLengthCalculation (self ):
1715
- #create a temporary layer
1719
+ # create a temporary layer
1716
1720
temp_layer = QgsVectorLayer ("LineString?crs=epsg:3111&field=pk:int" , "vl" , "memory" )
1717
1721
self .assertTrue (temp_layer .isValid ())
1718
1722
f1 = QgsFeature (temp_layer .dataProvider ().fields (), 1 )
@@ -1740,12 +1744,14 @@ def test_ExpressionFieldEllipsoidLengthCalculation(self):
1740
1744
self .assertAlmostEqual (f ['length' ], expected , 3 )
1741
1745
1742
1746
def test_ExpressionFieldEllipsoidAreaCalculation (self ):
1743
- #create a temporary layer
1747
+ # create a temporary layer
1744
1748
temp_layer = QgsVectorLayer ("Polygon?crs=epsg:3111&field=pk:int" , "vl" , "memory" )
1745
1749
self .assertTrue (temp_layer .isValid ())
1746
1750
f1 = QgsFeature (temp_layer .dataProvider ().fields (), 1 )
1747
1751
f1 .setAttribute ("pk" , 1 )
1748
- f1 .setGeometry (QgsGeometry .fromPolygonXY ([[QgsPointXY (2484588 , 2425722 ), QgsPointXY (2482767 , 2398853 ), QgsPointXY (2520109 , 2397715 ), QgsPointXY (2520792 , 2425494 ), QgsPointXY (2484588 , 2425722 )]]))
1752
+ f1 .setGeometry (QgsGeometry .fromPolygonXY ([[QgsPointXY (2484588 , 2425722 ), QgsPointXY (2482767 , 2398853 ),
1753
+ QgsPointXY (2520109 , 2397715 ), QgsPointXY (2520792 , 2425494 ),
1754
+ QgsPointXY (2484588 , 2425722 )]]))
1749
1755
temp_layer .dataProvider ().addFeatures ([f1 ])
1750
1756
1751
1757
# set project CRS and ellipsoid
@@ -1774,11 +1780,11 @@ def test_ExpressionFilter(self):
1774
1780
1775
1781
features = layer .getFeatures (QgsFeatureRequest ().setFilterExpression ('"test" = 6' ))
1776
1782
1777
- assert (len (list (features )) == 0 )
1783
+ assert (len (list (features )) == 0 )
1778
1784
1779
1785
features = layer .getFeatures (QgsFeatureRequest ().setFilterExpression ('"test" = 5' ))
1780
1786
1781
- assert (len (list (features )) == 1 )
1787
+ assert (len (list (features )) == 1 )
1782
1788
1783
1789
def testSelectByIds (self ):
1784
1790
""" Test selecting by ID"""
@@ -2198,14 +2204,16 @@ def testGetSetConstraints(self):
2198
2204
layer .setFieldConstraint (1 , QgsFieldConstraints .ConstraintNotNull )
2199
2205
layer .setFieldConstraint (1 , QgsFieldConstraints .ConstraintUnique )
2200
2206
self .assertEqual (layer .fieldConstraints (0 ), QgsFieldConstraints .ConstraintNotNull )
2201
- self .assertEqual (layer .fieldConstraints (1 ), QgsFieldConstraints .ConstraintNotNull | QgsFieldConstraints .ConstraintUnique )
2207
+ self .assertEqual (layer .fieldConstraints (1 ),
2208
+ QgsFieldConstraints .ConstraintNotNull | QgsFieldConstraints .ConstraintUnique )
2202
2209
self .assertFalse (layer .fieldConstraints (2 ))
2203
2210
self .assertEqual (layer .fields ().at (0 ).constraints ().constraints (), QgsFieldConstraints .ConstraintNotNull )
2204
2211
self .assertEqual (layer .fields ().at (0 ).constraints ().constraintOrigin (QgsFieldConstraints .ConstraintNotNull ),
2205
2212
QgsFieldConstraints .ConstraintOriginLayer )
2206
2213
self .assertEqual (layer .fields ().at (0 ).constraints ().constraintStrength (QgsFieldConstraints .ConstraintNotNull ),
2207
2214
QgsFieldConstraints .ConstraintStrengthHard )
2208
- self .assertEqual (layer .fields ().at (1 ).constraints ().constraints (), QgsFieldConstraints .ConstraintNotNull | QgsFieldConstraints .ConstraintUnique )
2215
+ self .assertEqual (layer .fields ().at (1 ).constraints ().constraints (),
2216
+ QgsFieldConstraints .ConstraintNotNull | QgsFieldConstraints .ConstraintUnique )
2209
2217
self .assertEqual (layer .fields ().at (1 ).constraints ().constraintOrigin (QgsFieldConstraints .ConstraintNotNull ),
2210
2218
QgsFieldConstraints .ConstraintOriginLayer )
2211
2219
self .assertEqual (layer .fields ().at (1 ).constraints ().constraintOrigin (QgsFieldConstraints .ConstraintUnique ),
@@ -2257,13 +2265,15 @@ def testSaveRestoreConstraints(self):
2257
2265
layer3 = createLayerWithOnePoint ()
2258
2266
self .assertTrue (layer3 .readXml (elem , QgsReadWriteContext ()))
2259
2267
self .assertEqual (layer3 .fieldConstraints (0 ), QgsFieldConstraints .ConstraintNotNull )
2260
- self .assertEqual (layer3 .fieldConstraints (1 ), QgsFieldConstraints .ConstraintNotNull | QgsFieldConstraints .ConstraintUnique )
2268
+ self .assertEqual (layer3 .fieldConstraints (1 ),
2269
+ QgsFieldConstraints .ConstraintNotNull | QgsFieldConstraints .ConstraintUnique )
2261
2270
self .assertEqual (layer3 .fields ().at (0 ).constraints ().constraints (), QgsFieldConstraints .ConstraintNotNull )
2262
2271
self .assertEqual (layer3 .fields ().at (0 ).constraints ().constraintOrigin (QgsFieldConstraints .ConstraintNotNull ),
2263
2272
QgsFieldConstraints .ConstraintOriginLayer )
2264
2273
self .assertEqual (layer .fields ().at (0 ).constraints ().constraintStrength (QgsFieldConstraints .ConstraintNotNull ),
2265
2274
QgsFieldConstraints .ConstraintStrengthHard )
2266
- self .assertEqual (layer3 .fields ().at (1 ).constraints ().constraints (), QgsFieldConstraints .ConstraintNotNull | QgsFieldConstraints .ConstraintUnique )
2275
+ self .assertEqual (layer3 .fields ().at (1 ).constraints ().constraints (),
2276
+ QgsFieldConstraints .ConstraintNotNull | QgsFieldConstraints .ConstraintUnique )
2267
2277
self .assertEqual (layer3 .fields ().at (1 ).constraints ().constraintOrigin (QgsFieldConstraints .ConstraintNotNull ),
2268
2278
QgsFieldConstraints .ConstraintOriginLayer )
2269
2279
self .assertEqual (layer3 .fields ().at (1 ).constraints ().constraintOrigin (QgsFieldConstraints .ConstraintUnique ),
@@ -2605,7 +2615,8 @@ def testFeatureRequestWithReprojectionAndVirtualFields(self):
2605
2615
self .assertAlmostEqual (virtual_values [4 ], - 65.32 , 2 )
2606
2616
2607
2617
# repeat, with reprojection on request
2608
- request = QgsFeatureRequest ().setDestinationCrs (QgsCoordinateReferenceSystem ('epsg:3785' ), QgsProject .instance ().transformContext ())
2618
+ request = QgsFeatureRequest ().setDestinationCrs (QgsCoordinateReferenceSystem ('epsg:3785' ),
2619
+ QgsProject .instance ().transformContext ())
2609
2620
features = [f for f in layer .getFeatures (request )]
2610
2621
# virtual field value should not change, even though geometry has
2611
2622
self .assertAlmostEqual (features [0 ]['virtual' ], - 71.123 , 2 )
0 commit comments