Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Apr 6, 2018
1 parent 140b40b commit 333cf42
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
55 changes: 55 additions & 0 deletions tests/src/python/test_provider_postgres.py
Expand Up @@ -20,15 +20,26 @@

from qgis.core import (
QgsGeometry,
QgsProject,
QgsPoint,
QgsVectorLayer,
QgsVectorLayerImport,
QgsFeatureRequest,
QgsMapLayerRegistry,
QgsFeature,
QgsTransactionGroup,
NULL
)

from qgis.gui import (
QgsEditorWidgetRegistry,
QgsAttributeForm
)

from qgis.PyQt.QtCore import QSettings, QDate, QTime, QDateTime, QVariant

from qgis.PyQt.QtWidgets import QLabel

from qgis.testing import start_app, unittest
from utilities import unitTestDataPath
from providertestbase import ProviderTestCase
Expand All @@ -54,6 +65,8 @@ def setUpClass(cls):
cls.poly_provider = cls.poly_vl.dataProvider()
cls.con = psycopg2.connect(cls.dbconn)

QgsEditorWidgetRegistry.initEditors()

@classmethod
def tearDownClass(cls):
"""Run after all tests"""
Expand Down Expand Up @@ -407,6 +420,48 @@ def testStyleDatabaseWithService(self):
ids = styles[1]
self.assertEqual(len(ids), 1)

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.instance()
p.setAutoTransaction(True)

QgsMapLayerRegistry.instance().addMapLayers([vl])

# get feature
f = next(vl.getFeatures(QgsFeatureRequest(1))) # fid=1
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)
form.setFeature(f)
self.assertTrue(form.editable())
for w in form.findChildren(QLabel):
if w.buddy():
spinBox = w.buddy()
if w.text() == 'a':
spinBox.setValue('1')
if w.text() == 'b':
spinBox.setValue('0')

# save
form.save()

# check new values
f = next(vl.getFeatures(QgsFeatureRequest(1))) # fid=1
self.assertEqual(f.attributes(), [1, 1, 0])


if __name__ == '__main__':
unittest.main()
14 changes: 12 additions & 2 deletions tests/testdata/provider/testdata_pg.sql
Expand Up @@ -30,7 +30,7 @@ SET default_with_oids = false;

--
-- TOC entry 171 (class 1259 OID 377761)
-- Name: someData; Type: TABLE; Schema: qgis_test; Owner: postgres; Tablespace:
-- Name: someData; Type: TABLE; Schema: qgis_test; Owner: postgres; Tablespace:
--

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

--
-- TOC entry 3953 (class 2606 OID 377768)
-- Name: someData_pkey; Type: CONSTRAINT; Schema: qgis_test; Owner: postgres; Tablespace:
-- Name: someData_pkey; Type: CONSTRAINT; Schema: qgis_test; Owner: postgres; Tablespace:
--

ALTER TABLE ONLY qgis_test."someData"
Expand Down Expand Up @@ -399,6 +399,16 @@ CREATE TABLE qgis_test.domains
fld_numeric_domain qgis_test.numeric_domain
);

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
);

--------------------------------------
-- Temporary table for testing renaming fields
Expand Down

0 comments on commit 333cf42

Please sign in to comment.