Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Jan 29, 2018
1 parent c1fac42 commit aed6608
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
43 changes: 42 additions & 1 deletion tests/src/python/test_provider_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
QgsWkbTypes,
QgsGeometry
)
from qgis.gui import QgsGui
from qgis.gui import QgsGui, QgsAttributeForm
from qgis.PyQt.QtCore import QDate, QTime, QDateTime, QVariant, QDir, QObject
from qgis.PyQt.QtWidgets import QLabel
from qgis.testing import start_app, unittest
from qgis.PyQt.QtXml import QDomDocument
from utilities import unitTestDataPath
Expand Down Expand Up @@ -427,6 +428,46 @@ def testTransactionDirty(self):
ft1 = vl.getFeatures('pk=1')
self.assertFalse(ft1.nextFeature(f))

def testTransactionConstrains(self):
# create a vector layer based on postgres
vl = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'id\' table="qgis_test"."check_constraints" sql=', 'test', 'postgres')
self.assertTrue(vl.isValid())

# prepare a project with transactions enabled
p = QgsProject()
p.setAutoTransaction(True)
p.addMapLayers([vl])

# get feature
f = QgsFeature()
self.assertTrue(vl.getFeatures('id=1').nextFeature(f))
self.assertEqual(f.attributes(), [1, 4, 3])

# start edition
vl.startEditing()

# update attribute form with a failing constraints
# coming from the database if attributes are updated
# one at a time.
# Current feature: a = 4 / b = 3
# Update feature: a = 1 / b = 0
# If updated one at a time, '(a = 1) < (b = 3)' => FAIL!
form = QgsAttributeForm(vl, f)
for w in form.findChildren(QLabel):
if w.buddy():
spinBox = w.buddy()
if w.text() == 'a':
spinBox.setValue(1)
elif w.text() == 'b':
spinBox.setValue(0)

# save
form.save()

# check new values
self.assertTrue(vl.getFeatures('id=1').nextFeature(f))
self.assertEqual(f.attributes(), [1, 1, 0])

def testTransactionTuple(self):
# create a vector layer based on postgres
vl = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POLYGON table="qgis_test"."some_poly_data" (geom) sql=', 'test', 'postgres')
Expand Down
11 changes: 11 additions & 0 deletions tests/testdata/provider/testdata_pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,14 @@ CREATE UNIQUE INDEX constraints_uniq
ON qgis_test.constraints
USING btree
(name COLLATE pg_catalog."default"); -- unique index

CREATE TABLE qgis_test.check_constraints (
id integer PRIMARY KEY,
a integer,
b integer, CHECK (a > b)
);
INSERT INTO qgis_test.check_constraints VALUES (
1, -- id
4, -- a
3 -- b
)

0 comments on commit aed6608

Please sign in to comment.