Skip to content

Commit 385655f

Browse files
committedOct 25, 2017
Add some tests
1 parent 32ff78b commit 385655f

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed
 

‎tests/src/python/test_provider_postgres.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
QgsReadWriteContext,
3434
QgsRectangle,
3535
QgsDefaultValue,
36-
QgsDataSourceUri
36+
QgsDataSourceUri,
37+
QgsProject
3738
)
3839
from qgis.gui import QgsGui
3940
from qgis.PyQt.QtCore import QDate, QTime, QDateTime, QVariant, QDir, QObject
@@ -312,6 +313,54 @@ def testNestedInsert(self):
312313
self.vl.addFeature(f) # Should not deadlock during an active iteration
313314
f = next(it)
314315

316+
def testTransactionNotDirty(self):
317+
# create a vector ayer based on postgres
318+
vl = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POLYGON table="qgis_test"."some_poly_data" (geom) sql=', 'test', 'postgres')
319+
self.assertTrue(vl.isValid())
320+
321+
# prepare a project with transactions enabled
322+
p = QgsProject()
323+
p.setAutoTransaction(True)
324+
p.addMapLayers([vl])
325+
vl.startEditing()
326+
327+
# check that the feature used for testing is ok
328+
ft0 = vl.getFeatures('pk=1')
329+
f = QgsFeature()
330+
self.assertTrue(ft0.nextFeature(f))
331+
332+
# update the data within the transaction
333+
tr = vl.dataProvider().transaction()
334+
sql = "update qgis_test.some_poly_data set pk=33 where pk=1"
335+
self.assertTrue(tr.executeSql(sql, True)[0])
336+
337+
# check that the pk of the feature has been changed
338+
ft = vl.getFeatures('pk=1')
339+
self.assertFalse(ft.nextFeature(f))
340+
341+
ft = vl.getFeatures('pk=33')
342+
self.assertTrue(ft.nextFeature(f))
343+
344+
# underlying data has been modified but the layer is not tagged as
345+
# modified
346+
self.assertTrue(vl.isModified())
347+
348+
# undo sql query
349+
vl.undoStack().undo()
350+
351+
# check that the original feature with pk is back
352+
ft0 = vl.getFeatures('pk=1')
353+
self.assertTrue(ft0.nextFeature(f))
354+
355+
# redo
356+
vl.undoStack().redo()
357+
358+
# check that the pk of the feature has been changed
359+
ft1 = vl.getFeatures('pk=1')
360+
self.assertFalse(ft1.nextFeature(f))
361+
362+
p.setAutoTransaction(False)
363+
315364
def testDomainTypes(self):
316365
"""Test that domain types are correctly mapped"""
317366

0 commit comments

Comments
 (0)
Please sign in to comment.