Skip to content

Commit 96789f3

Browse files
committed
Added tests with use estimated metadata
1 parent 614db66 commit 96789f3

File tree

2 files changed

+94
-19
lines changed

2 files changed

+94
-19
lines changed

tests/src/python/test_provider_postgres.py

+25-7
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
@@ -94,18 +94,36 @@ def test_table(dbconn, table_name, wkt):
9494
test_table(self.dbconn, 'mls3d', 'MultiLineStringZ ((0 0 0, 1 1 1),(2 2 2, 3 3 3))')
9595

9696
def testGetFeaturesUniqueId(self):
97-
def test_unique(dbconn, table_name, num_features):
98-
vl = QgsVectorLayer('%s srid=4326 table="qgis_test".%s (geom) sql=' % (dbconn, table_name), "testgeom", "postgres")
99-
assert(vl.isValid())
100-
features = [f for f in vl.getFeatures()]
97+
"""
98+
Test tables with inheritance for unique ids
99+
"""
100+
def test_unique(features, num_features):
101101
featureids = []
102102
for f in features:
103103
self.assertFalse(f.id() in featureids)
104104
featureids.append(f.id())
105105
self.assertEqual(len(features), num_features)
106106

107-
test_unique(self.dbconn, 'someData', 5)
108-
test_unique(self.dbconn, 'base_table', 4)
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_bad 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)
109127

110128

111129
if __name__ == '__main__':

tests/testdata/provider/testdata.sql

+69-12
Original file line numberDiff line numberDiff line change
@@ -190,45 +190,102 @@ CREATE TABLE qgis_test.mls3d(
190190
INSERT INTO qgis_test.mls3d values (1, 'srid=4326;MultiLineString((0 0 0, 1 1 1),(2 2 2, 3 3 3))'::geometry);
191191

192192

193-
CREATE TABLE qgis_test.base_table
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
194204
(
195205
gid serial NOT NULL,
196206
geom geometry(Point,4326),
197207
code character varying,
198-
CONSTRAINT base_pkey PRIMARY KEY (gid)
208+
CONSTRAINT base_bad_pkey PRIMARY KEY (gid)
199209
)
200210
WITH (
201211
OIDS=FALSE
202212
);
203213

204-
CREATE TABLE qgis_test.child_table
214+
CREATE TABLE qgis_test.child_table_bad
205215
(
206216
gid serial NOT NULL,
207217
geom geometry(Point,4326),
208218
code character varying,
209-
CONSTRAINT child_pkey PRIMARY KEY (gid)
219+
CONSTRAINT child_bad_pkey PRIMARY KEY (gid)
210220
)
211-
INHERITS ( qgis_test.base_table)
221+
INHERITS ( qgis_test.base_table_bad)
212222
WITH (
213223
OIDS=FALSE
214224
);
215225

216226

217-
CREATE TABLE qgis_test.child_table2
227+
CREATE TABLE qgis_test.child_table2_bad
218228
(
219229
gid serial NOT NULL,
220230
geom geometry(Point,4326),
221231
code character varying,
222-
CONSTRAINT child2_pkey PRIMARY KEY (gid)
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
223269
)
224-
INHERITS ( qgis_test.base_table)
270+
INHERITS ( qgis_test.base_table_good)
225271
WITH (
226272
OIDS=FALSE
227273
);
228274

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

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

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

0 commit comments

Comments
 (0)