Skip to content

Commit

Permalink
Added tests with use estimated metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Dec 8, 2015
1 parent 614db66 commit 96789f3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 19 deletions.
32 changes: 25 additions & 7 deletions 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 @@ -94,18 +94,36 @@ def test_table(dbconn, table_name, wkt):
test_table(self.dbconn, 'mls3d', 'MultiLineStringZ ((0 0 0, 1 1 1),(2 2 2, 3 3 3))')

def testGetFeaturesUniqueId(self):
def test_unique(dbconn, table_name, num_features):
vl = QgsVectorLayer('%s srid=4326 table="qgis_test".%s (geom) sql=' % (dbconn, table_name), "testgeom", "postgres")
assert(vl.isValid())
features = [f for f in vl.getFeatures()]
"""
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)

test_unique(self.dbconn, 'someData', 5)
test_unique(self.dbconn, 'base_table', 4)
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_bad 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__':
Expand Down
81 changes: 69 additions & 12 deletions tests/testdata/provider/testdata.sql
Expand Up @@ -190,45 +190,102 @@ 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);


CREATE TABLE qgis_test.base_table
-----------------------------------------
-- 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_pkey PRIMARY KEY (gid)
CONSTRAINT base_bad_pkey PRIMARY KEY (gid)
)
WITH (
OIDS=FALSE
);

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


CREATE TABLE qgis_test.child_table2
CREATE TABLE qgis_test.child_table2_bad
(
gid serial NOT NULL,
geom geometry(Point,4326),
code character varying,
CONSTRAINT child2_pkey PRIMARY KEY (gid)
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)
INHERITS ( qgis_test.base_table_good)
WITH (
OIDS=FALSE
);

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

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

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

0 comments on commit 96789f3

Please sign in to comment.