Skip to content

Commit

Permalink
Merge pull request #2555 from elpaso/bugfix-pg-inherits
Browse files Browse the repository at this point in the history
Additional tests for tables with inherits
  • Loading branch information
elpaso committed Dec 9, 2015
2 parents 50092eb + 94b5e60 commit 0287daf
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 1 deletion.
35 changes: 34 additions & 1 deletion tests/src/python/test_provider_postgres.py
Expand Up @@ -53,7 +53,7 @@ def enableCompiler(self):
def disableCompiler(self):
QSettings().setValue(u'/qgis/compileExpressions', False)

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

def testGetFeaturesUniqueId(self):
"""
Test tables with inheritance for unique ids
"""
def test_unique(features, num_features):
featureids = []
for f in features:
self.assertFalse(f.id() in featureids)
featureids.append(f.id())
self.assertEqual(len(features), num_features)

vl = QgsVectorLayer('%s srid=4326 table="qgis_test".%s (geom) sql=' % (self.dbconn, 'someData'), "testgeom", "postgres")
self.assertTrue(vl.isValid())
# Test someData
test_unique([f for f in vl.getFeatures()], 5)

# Test base_table_bad: layer is invalid
vl = QgsVectorLayer('%s srid=4326 table="qgis_test".%s (geom) sql=' % (self.dbconn, 'base_table_bad'), "testgeom", "postgres")
self.assertFalse(vl.isValid())
# Test base_table_bad with use estimated metadata: layer is valid because the unique test is skipped
vl = QgsVectorLayer('%s srid=4326 estimatedmetadata="true" table="qgis_test".%s (geom) sql=' % (self.dbconn, 'base_table_bad'), "testgeom", "postgres")
self.assertTrue(vl.isValid())

# Test base_table_good: layer is valid
vl = QgsVectorLayer('%s srid=4326 table="qgis_test".%s (geom) sql=' % (self.dbconn, 'base_table_good'), "testgeom", "postgres")
self.assertTrue(vl.isValid())
test_unique([f for f in vl.getFeatures()], 4)
# Test base_table_good with use estimated metadata: layer is valid
vl = QgsVectorLayer('%s srid=4326 estimatedmetadata="true" table="qgis_test".%s (geom) sql=' % (self.dbconn, 'base_table_good'), "testgeom", "postgres")
self.assertTrue(vl.isValid())
test_unique([f for f in vl.getFeatures()], 4)


if __name__ == '__main__':
unittest.main()
101 changes: 101 additions & 0 deletions tests/testdata/provider/testdata.sql
Expand Up @@ -188,3 +188,104 @@ CREATE TABLE qgis_test.mls3d(
);

INSERT INTO qgis_test.mls3d values (1, 'srid=4326;MultiLineString((0 0 0, 1 1 1),(2 2 2, 3 3 3))'::geometry);


-----------------------------------------
-- Test tables with INHERITS
--
-- This is bad design: the common fields
-- are replicated in child tables and
-- leads to duplicated ids in the parent
-- table
--


CREATE TABLE qgis_test.base_table_bad
(
gid serial NOT NULL,
geom geometry(Point,4326),
code character varying,
CONSTRAINT base_bad_pkey PRIMARY KEY (gid)
)
WITH (
OIDS=FALSE
);

CREATE TABLE qgis_test.child_table_bad
(
gid serial NOT NULL,
geom geometry(Point,4326),
code character varying,
CONSTRAINT child_bad_pkey PRIMARY KEY (gid)
)
INHERITS ( qgis_test.base_table_bad)
WITH (
OIDS=FALSE
);


CREATE TABLE qgis_test.child_table2_bad
(
gid serial NOT NULL,
geom geometry(Point,4326),
code character varying,
CONSTRAINT child2_bad_pkey PRIMARY KEY (gid)
)
INHERITS ( qgis_test.base_table_bad)
WITH (
OIDS=FALSE
);

INSERT INTO qgis_test.child_table_bad (geom, code) VALUES ('srid=4326;Point(0 0)'::geometry, 'child 1');
INSERT INTO qgis_test.child_table_bad (geom, code) VALUES ('srid=4326;Point(1 1)'::geometry, 'child 2');


INSERT INTO qgis_test.child_table2_bad (geom, code) VALUES ('srid=4326;Point(-1 -1)'::geometry, 'child2 1');
INSERT INTO qgis_test.child_table2_bad (geom, code) VALUES ('srid=4326;Point(-1 1)'::geometry, 'child2 2');



-----------------------------------------
-- Test tables with INHERITS
--
-- This is good design: the common fields
-- and the pk are only in the parent table
-- no pk duplication


CREATE TABLE qgis_test.base_table_good
(
gid serial NOT NULL,
geom geometry(Point,4326),
CONSTRAINT base_good_pkey PRIMARY KEY (gid)
)
WITH (
OIDS=FALSE
);

CREATE TABLE qgis_test.child_table_good
(
code1 character varying
)
INHERITS ( qgis_test.base_table_good)
WITH (
OIDS=FALSE
);


CREATE TABLE qgis_test.child_table2_good
(
code2 character varying
)
INHERITS ( qgis_test.base_table_good)
WITH (
OIDS=FALSE
);

INSERT INTO qgis_test.child_table_good (geom, code1) VALUES ('srid=4326;Point(0 0)'::geometry, 'child 1');
INSERT INTO qgis_test.child_table_good (geom, code1) VALUES ('srid=4326;Point(1 1)'::geometry, 'child 2');


INSERT INTO qgis_test.child_table2_good (geom, code2) VALUES ('srid=4326;Point(-1 -1)'::geometry, 'child2 1');
INSERT INTO qgis_test.child_table2_good (geom, code2) VALUES ('srid=4326;Point(-1 1)'::geometry, 'child2 2');

0 comments on commit 0287daf

Please sign in to comment.