Skip to content

Commit e98e16f

Browse files
committedApr 7, 2016
Unit test for 4ec97c3
1 parent 4ec97c3 commit e98e16f

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed
 

‎tests/src/python/test_provider_postgres.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import os
1818
from qgis.core import NULL
1919

20-
from qgis.core import QgsVectorLayer, QgsFeatureRequest
20+
from qgis.core import QgsVectorLayer, QgsFeatureRequest, QgsFeature
2121
from PyQt.QtCore import QSettings, QDate, QTime, QDateTime, QVariant
2222
from qgis.testing import start_app, unittest
2323
from utilities import unitTestDataPath
@@ -165,6 +165,16 @@ def test_query_attribute(dbconn, query, att, val, fidval):
165165
test_query_attribute(self.dbconn, '(SELECT -1::int8 i, NULL::geometry(Point) g)', 'i', -1, 1)
166166
test_query_attribute(self.dbconn, '(SELECT -65535::int8 i, NULL::geometry(Point) g)', 'i', -65535, 1)
167167

168+
def testPktMapInsert(self):
169+
vl = QgsVectorLayer('{} table="qgis_test"."{}" key="pk" sql='.format(self.dbconn, 'books_view'), "books_view", "postgres")
170+
self.assertTrue(vl.isValid())
171+
f = QgsFeature(vl.fields())
172+
f.setAttribute(0, NULL)
173+
f.setAttribute(1, 'Das Drama des begabten Kindes')
174+
r, f = vl.dataProvider().addFeatures([f])
175+
self.assertTrue(r)
176+
self.assertIsNotNone(f[0]['pk'])
177+
vl.deleteFeatures([f[0].id()])
168178

169179
if __name__ == '__main__':
170180
unittest.main()

‎tests/testdata/provider/testdata_pg.sql

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,30 @@ INSERT INTO qgis_test.child_table_good (geom, code1) VALUES ('srid=4326;Point(1
309309
INSERT INTO qgis_test.child_table2_good (geom, code2) VALUES ('srid=4326;Point(-1 -1)'::geometry, 'child2 1');
310310
INSERT INTO qgis_test.child_table2_good (geom, code2) VALUES ('srid=4326;Point(-1 1)'::geometry, 'child2 2');
311311

312+
--------------------------------------
313+
-- A writable view
314+
--
315+
CREATE OR REPLACE VIEW qgis_test.books_view
316+
AS
317+
SELECT *
318+
FROM qgis_test.books;
319+
320+
CREATE OR REPLACE FUNCTION qgis_test.books_view_insert()
321+
RETURNS trigger AS
322+
$BODY$
323+
BEGIN
324+
INSERT INTO qgis_test.books (
325+
"name"
326+
)
327+
VALUES (
328+
NEW.name
329+
)
330+
RETURNING pk INTO NEW.pk;
331+
332+
RETURN NEW;
333+
END; $BODY$
334+
LANGUAGE plpgsql VOLATILE
335+
COST 100;
336+
337+
CREATE TRIGGER books_view_ON_INSERT INSTEAD OF INSERT ON qgis_test.books_view
338+
FOR EACH ROW EXECUTE PROCEDURE qgis_test.books_view_insert();

0 commit comments

Comments
 (0)
Please sign in to comment.