Skip to content

Commit 0287daf

Browse files
committedDec 9, 2015
Merge pull request #2555 from elpaso/bugfix-pg-inherits
Additional tests for tables with inherits
2 parents 50092eb + 94b5e60 commit 0287daf

File tree

2 files changed

+135
-1
lines changed

2 files changed

+135
-1
lines changed
 

‎tests/src/python/test_provider_postgres.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def enableCompiler(self):
5353
def disableCompiler(self):
5454
QSettings().setValue(u'/qgis/compileExpressions', False)
5555

56-
# HERE GO THE PROVIDER SPECIFIC TESTS
56+
# HERE GO THE PROVIDER SPECIFIC TESTS
5757
def testDefaultValue(self):
5858
assert self.provider.defaultValue(0) == u'nextval(\'qgis_test."someData_pk_seq"\'::regclass)'
5959
assert self.provider.defaultValue(1) == NULL
@@ -93,5 +93,38 @@ def test_table(dbconn, table_name, wkt):
9393
test_table(self.dbconn, 'mls2d', 'MultiLineString ((0 0, 1 1),(2 2, 3 3))')
9494
test_table(self.dbconn, 'mls3d', 'MultiLineStringZ ((0 0 0, 1 1 1),(2 2 2, 3 3 3))')
9595

96+
def testGetFeaturesUniqueId(self):
97+
"""
98+
Test tables with inheritance for unique ids
99+
"""
100+
def test_unique(features, num_features):
101+
featureids = []
102+
for f in features:
103+
self.assertFalse(f.id() in featureids)
104+
featureids.append(f.id())
105+
self.assertEqual(len(features), num_features)
106+
107+
vl = QgsVectorLayer('%s srid=4326 table="qgis_test".%s (geom) sql=' % (self.dbconn, 'someData'), "testgeom", "postgres")
108+
self.assertTrue(vl.isValid())
109+
# Test someData
110+
test_unique([f for f in vl.getFeatures()], 5)
111+
112+
# Test base_table_bad: layer is invalid
113+
vl = QgsVectorLayer('%s srid=4326 table="qgis_test".%s (geom) sql=' % (self.dbconn, 'base_table_bad'), "testgeom", "postgres")
114+
self.assertFalse(vl.isValid())
115+
# Test base_table_bad with use estimated metadata: layer is valid because the unique test is skipped
116+
vl = QgsVectorLayer('%s srid=4326 estimatedmetadata="true" table="qgis_test".%s (geom) sql=' % (self.dbconn, 'base_table_bad'), "testgeom", "postgres")
117+
self.assertTrue(vl.isValid())
118+
119+
# Test base_table_good: layer is valid
120+
vl = QgsVectorLayer('%s srid=4326 table="qgis_test".%s (geom) sql=' % (self.dbconn, 'base_table_good'), "testgeom", "postgres")
121+
self.assertTrue(vl.isValid())
122+
test_unique([f for f in vl.getFeatures()], 4)
123+
# Test base_table_good with use estimated metadata: layer is valid
124+
vl = QgsVectorLayer('%s srid=4326 estimatedmetadata="true" table="qgis_test".%s (geom) sql=' % (self.dbconn, 'base_table_good'), "testgeom", "postgres")
125+
self.assertTrue(vl.isValid())
126+
test_unique([f for f in vl.getFeatures()], 4)
127+
128+
96129
if __name__ == '__main__':
97130
unittest.main()

‎tests/testdata/provider/testdata.sql

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,104 @@ CREATE TABLE qgis_test.mls3d(
188188
);
189189

190190
INSERT INTO qgis_test.mls3d values (1, 'srid=4326;MultiLineString((0 0 0, 1 1 1),(2 2 2, 3 3 3))'::geometry);
191+
192+
193+
-----------------------------------------
194+
-- Test tables with INHERITS
195+
--
196+
-- This is bad design: the common fields
197+
-- are replicated in child tables and
198+
-- leads to duplicated ids in the parent
199+
-- table
200+
--
201+
202+
203+
CREATE TABLE qgis_test.base_table_bad
204+
(
205+
gid serial NOT NULL,
206+
geom geometry(Point,4326),
207+
code character varying,
208+
CONSTRAINT base_bad_pkey PRIMARY KEY (gid)
209+
)
210+
WITH (
211+
OIDS=FALSE
212+
);
213+
214+
CREATE TABLE qgis_test.child_table_bad
215+
(
216+
gid serial NOT NULL,
217+
geom geometry(Point,4326),
218+
code character varying,
219+
CONSTRAINT child_bad_pkey PRIMARY KEY (gid)
220+
)
221+
INHERITS ( qgis_test.base_table_bad)
222+
WITH (
223+
OIDS=FALSE
224+
);
225+
226+
227+
CREATE TABLE qgis_test.child_table2_bad
228+
(
229+
gid serial NOT NULL,
230+
geom geometry(Point,4326),
231+
code character varying,
232+
CONSTRAINT child2_bad_pkey PRIMARY KEY (gid)
233+
)
234+
INHERITS ( qgis_test.base_table_bad)
235+
WITH (
236+
OIDS=FALSE
237+
);
238+
239+
INSERT INTO qgis_test.child_table_bad (geom, code) VALUES ('srid=4326;Point(0 0)'::geometry, 'child 1');
240+
INSERT INTO qgis_test.child_table_bad (geom, code) VALUES ('srid=4326;Point(1 1)'::geometry, 'child 2');
241+
242+
243+
INSERT INTO qgis_test.child_table2_bad (geom, code) VALUES ('srid=4326;Point(-1 -1)'::geometry, 'child2 1');
244+
INSERT INTO qgis_test.child_table2_bad (geom, code) VALUES ('srid=4326;Point(-1 1)'::geometry, 'child2 2');
245+
246+
247+
248+
-----------------------------------------
249+
-- Test tables with INHERITS
250+
--
251+
-- This is good design: the common fields
252+
-- and the pk are only in the parent table
253+
-- no pk duplication
254+
255+
256+
CREATE TABLE qgis_test.base_table_good
257+
(
258+
gid serial NOT NULL,
259+
geom geometry(Point,4326),
260+
CONSTRAINT base_good_pkey PRIMARY KEY (gid)
261+
)
262+
WITH (
263+
OIDS=FALSE
264+
);
265+
266+
CREATE TABLE qgis_test.child_table_good
267+
(
268+
code1 character varying
269+
)
270+
INHERITS ( qgis_test.base_table_good)
271+
WITH (
272+
OIDS=FALSE
273+
);
274+
275+
276+
CREATE TABLE qgis_test.child_table2_good
277+
(
278+
code2 character varying
279+
)
280+
INHERITS ( qgis_test.base_table_good)
281+
WITH (
282+
OIDS=FALSE
283+
);
284+
285+
INSERT INTO qgis_test.child_table_good (geom, code1) VALUES ('srid=4326;Point(0 0)'::geometry, 'child 1');
286+
INSERT INTO qgis_test.child_table_good (geom, code1) VALUES ('srid=4326;Point(1 1)'::geometry, 'child 2');
287+
288+
289+
INSERT INTO qgis_test.child_table2_good (geom, code2) VALUES ('srid=4326;Point(-1 -1)'::geometry, 'child2 1');
290+
INSERT INTO qgis_test.child_table2_good (geom, code2) VALUES ('srid=4326;Point(-1 1)'::geometry, 'child2 2');
291+

0 commit comments

Comments
 (0)
Please sign in to comment.