Skip to content
Permalink
Browse files

Add test

  • Loading branch information
pblottiere committed Jan 29, 2018
1 parent c1fac42 commit aed6608095a0a437e07a6d770efabf8712f55579
Showing with 53 additions and 1 deletion.
  1. +42 −1 tests/src/python/test_provider_postgres.py
  2. +11 −0 tests/testdata/provider/testdata_pg.sql
@@ -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
@@ -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')
@@ -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.
You can’t perform that action at this time.