20
20
21
21
from qgis .core import (
22
22
QgsGeometry ,
23
+ QgsProject ,
23
24
QgsPoint ,
24
25
QgsVectorLayer ,
25
26
QgsVectorLayerImport ,
26
27
QgsFeatureRequest ,
28
+ QgsMapLayerRegistry ,
27
29
QgsFeature ,
28
30
QgsTransactionGroup ,
29
31
NULL
30
32
)
33
+
34
+ from qgis .gui import (
35
+ QgsEditorWidgetRegistry ,
36
+ QgsAttributeForm
37
+ )
38
+
31
39
from qgis .PyQt .QtCore import QSettings , QDate , QTime , QDateTime , QVariant
40
+
41
+ from qgis .PyQt .QtWidgets import QLabel
42
+
32
43
from qgis .testing import start_app , unittest
33
44
from utilities import unitTestDataPath
34
45
from providertestbase import ProviderTestCase
@@ -54,6 +65,8 @@ def setUpClass(cls):
54
65
cls .poly_provider = cls .poly_vl .dataProvider ()
55
66
cls .con = psycopg2 .connect (cls .dbconn )
56
67
68
+ QgsEditorWidgetRegistry .initEditors ()
69
+
57
70
@classmethod
58
71
def tearDownClass (cls ):
59
72
"""Run after all tests"""
@@ -407,6 +420,48 @@ def testStyleDatabaseWithService(self):
407
420
ids = styles [1 ]
408
421
self .assertEqual (len (ids ), 1 )
409
422
423
+ def testTransactionConstrains (self ):
424
+ # create a vector layer based on postgres
425
+ vl = QgsVectorLayer (self .dbconn + ' sslmode=disable key=\' id\' table="qgis_test"."check_constraints" sql=' , 'test' , 'postgres' )
426
+ self .assertTrue (vl .isValid ())
427
+
428
+ # prepare a project with transactions enabled
429
+ p = QgsProject .instance ()
430
+ p .setAutoTransaction (True )
431
+
432
+ QgsMapLayerRegistry .instance ().addMapLayers ([vl ])
433
+
434
+ # get feature
435
+ f = next (vl .getFeatures (QgsFeatureRequest (1 ))) # fid=1
436
+ self .assertEqual (f .attributes (), [1 , 4 , 3 ])
437
+
438
+ # start edition
439
+ vl .startEditing ()
440
+
441
+ # update attribute form with a failing constraints
442
+ # coming from the database if attributes are updated
443
+ # one at a time.
444
+ # Current feature: a = 4 / b = 3
445
+ # Update feature: a = 1 / b = 0
446
+ # If updated one at a time, '(a = 1) < (b = 3)' => FAIL!
447
+ form = QgsAttributeForm (vl )
448
+ form .setFeature (f )
449
+ self .assertTrue (form .editable ())
450
+ for w in form .findChildren (QLabel ):
451
+ if w .buddy ():
452
+ spinBox = w .buddy ()
453
+ if w .text () == 'a' :
454
+ spinBox .setValue ('1' )
455
+ if w .text () == 'b' :
456
+ spinBox .setValue ('0' )
457
+
458
+ # save
459
+ form .save ()
460
+
461
+ # check new values
462
+ f = next (vl .getFeatures (QgsFeatureRequest (1 ))) # fid=1
463
+ self .assertEqual (f .attributes (), [1 , 1 , 0 ])
464
+
410
465
411
466
if __name__ == '__main__' :
412
467
unittest .main ()
0 commit comments