Skip to content

Commit 333cf42

Browse files
committed
Add unit tests
1 parent 140b40b commit 333cf42

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

tests/src/python/test_provider_postgres.py

+55
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,26 @@
2020

2121
from qgis.core import (
2222
QgsGeometry,
23+
QgsProject,
2324
QgsPoint,
2425
QgsVectorLayer,
2526
QgsVectorLayerImport,
2627
QgsFeatureRequest,
28+
QgsMapLayerRegistry,
2729
QgsFeature,
2830
QgsTransactionGroup,
2931
NULL
3032
)
33+
34+
from qgis.gui import (
35+
QgsEditorWidgetRegistry,
36+
QgsAttributeForm
37+
)
38+
3139
from qgis.PyQt.QtCore import QSettings, QDate, QTime, QDateTime, QVariant
40+
41+
from qgis.PyQt.QtWidgets import QLabel
42+
3243
from qgis.testing import start_app, unittest
3344
from utilities import unitTestDataPath
3445
from providertestbase import ProviderTestCase
@@ -54,6 +65,8 @@ def setUpClass(cls):
5465
cls.poly_provider = cls.poly_vl.dataProvider()
5566
cls.con = psycopg2.connect(cls.dbconn)
5667

68+
QgsEditorWidgetRegistry.initEditors()
69+
5770
@classmethod
5871
def tearDownClass(cls):
5972
"""Run after all tests"""
@@ -407,6 +420,48 @@ def testStyleDatabaseWithService(self):
407420
ids = styles[1]
408421
self.assertEqual(len(ids), 1)
409422

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+
410465

411466
if __name__ == '__main__':
412467
unittest.main()

tests/testdata/provider/testdata_pg.sql

+12-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ SET default_with_oids = false;
3030

3131
--
3232
-- TOC entry 171 (class 1259 OID 377761)
33-
-- Name: someData; Type: TABLE; Schema: qgis_test; Owner: postgres; Tablespace:
33+
-- Name: someData; Type: TABLE; Schema: qgis_test; Owner: postgres; Tablespace:
3434
--
3535

3636
CREATE TABLE qgis_test."someData" (
@@ -70,7 +70,7 @@ INSERT INTO qgis_test."some_poly_data" (pk, geom) VALUES
7070

7171
--
7272
-- TOC entry 3953 (class 2606 OID 377768)
73-
-- Name: someData_pkey; Type: CONSTRAINT; Schema: qgis_test; Owner: postgres; Tablespace:
73+
-- Name: someData_pkey; Type: CONSTRAINT; Schema: qgis_test; Owner: postgres; Tablespace:
7474
--
7575

7676
ALTER TABLE ONLY qgis_test."someData"
@@ -399,6 +399,16 @@ CREATE TABLE qgis_test.domains
399399
fld_numeric_domain qgis_test.numeric_domain
400400
);
401401

402+
CREATE TABLE qgis_test.check_constraints (
403+
id integer PRIMARY KEY,
404+
a integer,
405+
b integer, CHECK (a > b)
406+
);
407+
INSERT INTO qgis_test.check_constraints VALUES (
408+
1, -- id
409+
4, -- a
410+
3 -- b
411+
);
402412

403413
--------------------------------------
404414
-- Temporary table for testing renaming fields

0 commit comments

Comments
 (0)