15
15
import qgis # NOQA
16
16
17
17
import os
18
- from qgis .core import NULL
19
18
20
- from qgis .core import QgsVectorLayer , QgsFeatureRequest , QgsFeature
19
+ from qgis .core import (
20
+ QgsVectorLayer ,
21
+ QgsFeatureRequest ,
22
+ QgsFeature ,
23
+ QgsTransactionGroup ,
24
+ NULL
25
+ )
21
26
from PyQt .QtCore import QSettings , QDate , QTime , QDateTime , QVariant
22
27
from qgis .testing import start_app , unittest
23
28
from utilities import unitTestDataPath
@@ -37,10 +42,10 @@ def setUpClass(cls):
37
42
cls .dbconn = os .environ ['QGIS_PGTEST_DB' ]
38
43
# Create test layers
39
44
cls .vl = QgsVectorLayer (cls .dbconn + ' sslmode=disable key=\' pk\' srid=4326 type=POINT table="qgis_test"."someData" (geom) sql=' , 'test' , 'postgres' )
40
- assert ( cls .vl .isValid () )
45
+ assert cls .vl .isValid ()
41
46
cls .provider = cls .vl .dataProvider ()
42
47
cls .poly_vl = QgsVectorLayer (cls .dbconn + ' sslmode=disable key=\' pk\' srid=4326 type=POLYGON table="qgis_test"."some_poly_data" (geom) sql=' , 'test' , 'postgres' )
43
- assert ( cls .poly_vl .isValid () )
48
+ assert cls .poly_vl .isValid ()
44
49
cls .poly_provider = cls .poly_vl .dataProvider ()
45
50
46
51
@classmethod
@@ -55,13 +60,13 @@ def disableCompiler(self):
55
60
56
61
# HERE GO THE PROVIDER SPECIFIC TESTS
57
62
def testDefaultValue (self ):
58
- assert self .provider .defaultValue (0 ) == u'nextval(\' qgis_test."someData_pk_seq"\' ::regclass)'
59
- assert self .provider .defaultValue (1 ) == NULL
60
- assert self .provider .defaultValue (2 ) == '\' qgis\' ::text'
63
+ self .assertEqual ( self . provider .defaultValue (0 ), u'nextval(\' qgis_test."someData_pk_seq"\' ::regclass)' )
64
+ self .assertEqual ( self . provider .defaultValue (1 ), NULL )
65
+ self .assertEqual ( self . provider .defaultValue (2 ), '\' qgis\' ::text' )
61
66
62
67
def testDateTimeTypes (self ):
63
68
vl = QgsVectorLayer ('%s table="qgis_test"."date_times" sql=' % (self .dbconn ), "testdatetimes" , "postgres" )
64
- assert (vl .isValid ())
69
+ self . assertTrue (vl .isValid ())
65
70
66
71
fields = vl .dataProvider ().fields ()
67
72
self .assertEqual (fields .at (fields .indexFromName ('date_field' )).type (), QVariant .Date )
@@ -71,30 +76,28 @@ def testDateTimeTypes(self):
71
76
f = vl .getFeatures (QgsFeatureRequest ()).next ()
72
77
73
78
date_idx = vl .fieldNameIndex ('date_field' )
74
- assert isinstance (f .attributes ()[date_idx ], QDate )
79
+ self . assertTrue ( isinstance (f .attributes ()[date_idx ], QDate ) )
75
80
self .assertEqual (f .attributes ()[date_idx ], QDate (2004 , 3 , 4 ))
76
81
time_idx = vl .fieldNameIndex ('time_field' )
77
- assert isinstance (f .attributes ()[time_idx ], QTime )
82
+ self . assertTrue ( isinstance (f .attributes ()[time_idx ], QTime ) )
78
83
self .assertEqual (f .attributes ()[time_idx ], QTime (13 , 41 , 52 ))
79
84
datetime_idx = vl .fieldNameIndex ('datetime_field' )
80
- assert isinstance (f .attributes ()[datetime_idx ], QDateTime )
85
+ self . assertTrue ( isinstance (f .attributes ()[datetime_idx ], QDateTime ) )
81
86
self .assertEqual (f .attributes ()[datetime_idx ], QDateTime (QDate (2004 , 3 , 4 ), QTime (13 , 41 , 52 )))
82
87
83
88
def testQueryLayers (self ):
84
89
def test_query (dbconn , query , key ):
85
90
ql = QgsVectorLayer ('%s srid=4326 table="%s" (geom) key=\' %s\' sql=' % (dbconn , query .replace ('"' , '\\ "' ), key ), "testgeom" , "postgres" )
86
- print (query , key )
87
- assert (ql .isValid ())
91
+ self .assertTrue (ql .isValid (), '{} ({})' .format (query , key ))
88
92
89
93
test_query (self .dbconn , '(SELECT NULL::integer "Id1", NULL::integer "Id2", NULL::geometry(Point, 4326) geom LIMIT 0)' , '"Id1","Id2"' )
90
94
91
95
def testWkbTypes (self ):
92
96
def test_table (dbconn , table_name , wkt ):
93
97
vl = QgsVectorLayer ('%s srid=4326 table="qgis_test".%s (geom) sql=' % (dbconn , table_name ), "testgeom" , "postgres" )
94
- assert (vl .isValid ())
98
+ self . assertTrue (vl .isValid ())
95
99
for f in vl .getFeatures ():
96
- print (f .geometry ().exportToWkt (), wkt )
97
- assert f .geometry ().exportToWkt () == wkt
100
+ self .assertEqual (f .geometry ().exportToWkt (), wkt )
98
101
99
102
test_table (self .dbconn , 'p2d' , 'Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))' )
100
103
test_table (self .dbconn , 'p3d' , 'PolygonZ ((0 0 0, 1 0 0, 1 1 0, 0 1 0, 0 0 0))' )
@@ -187,5 +190,15 @@ def testPktMapInsert(self):
187
190
self .assertNotEqual (f [0 ]['obj_id' ], NULL , f [0 ].attributes ())
188
191
vl .deleteFeatures ([f [0 ].id ()])
189
192
193
+ def testNestedInsert (self ):
194
+ tg = QgsTransactionGroup ()
195
+ tg .addLayer (self .vl )
196
+ self .vl .startEditing ()
197
+ it = self .vl .getFeatures ()
198
+ f = next (it )
199
+ f ['pk' ] = NULL
200
+ self .vl .addFeature (f ) # Should not deadlock during an active iteration
201
+ f = next (it )
202
+
190
203
if __name__ == '__main__' :
191
204
unittest .main ()
0 commit comments