Skip to content

Commit

Permalink
Mechanism to test features on PostgreSQL 12+
Browse files Browse the repository at this point in the history
  • Loading branch information
espinafre authored and nyalldawson committed Jun 2, 2020
1 parent fef79a0 commit 3e9da5d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/src/python/test_provider_postgres.py
Expand Up @@ -504,6 +504,33 @@ def testPktIntInsert(self):
self.assertNotEqual(f[0]['pk'], NULL, f[0].attributes())
vl.deleteFeatures([f[0].id()])

def testGeneratedFields(self):
cur = self.con.cursor()
cur.execute("SHOW server_version_num")
pgversion = int(cur.fetchone()[0])
if pgversion < 120000:
print("DEBUGUE: postgresql < 12. Skip.")
return

vl = QgsVectorLayer('{} table="qgis_test"."{}" (geom) srid=4326 type=POLYGON key="id" sql='.format(self.dbconn, "test_gen_col"), "test_gen_col", "postgres")
self.assertTrue(vl.isValid())

f = QgsFeature(vl.fields())
f.setGeometry(QgsGeometry.fromWkt('Polygon ((-67 -2, -67 0, -68 0, -70 -1, -67 -2))'))
self.assertTrue(vl.startEditing())
self.assertTrue(vl.addFeatures([f]))
self.assertTrue(vl.commitChanges())

# reading back to see if we saved the centroid correctly.
vl2 = QgsVectorLayer('{} table="qgis_test"."{}" (cent) srid=4326 type=POINT key="id" sql='.format(self.dbconn, "test_gen_col"), "test_gen_col", "postgres")
# centroid must be 0101000020E6100000310CC3300C0351C03DCFF33CCFF3ECBF , or Point (-68.047619047619051 -0.90476190476190477)
f2 = next(vl2.getFeatures(QgsGetFeatureRequest()))
generated_geometry = f2.geometry().asWkt()
expected_geometry = 'Point (-68.047619047619051 -0.90476190476190477)'
print("DEBUGUE: versão 12 antes assert.")
assert compareWkt(generated_geometry, expected_geometry), "Geometry mismatch! Expected:\n{}\nGot:\n{}\n".format(expected_geometry, generated_geometry)
print("DEBUGUE: versão 12 após assert.")

def testNonPkBigintField(self):
"""Test if we can correctly insert, read and change attributes(fields) of type bigint and which are not PKs."""
vl = QgsVectorLayer(
Expand Down
12 changes: 12 additions & 0 deletions tests/testdata/provider/testdata_pg.sh
Expand Up @@ -17,13 +17,25 @@ SCRIPTS="
tests/testdata/provider/testdata_pg_bigint_pk.sql
"

SCRIPTS12="
tests/testdata/provider/testdata_pg_12_generated.sql
"

dropdb --if-exists $DB
createdb $DB -E UTF8 -T template0 || exit 1
for f in ${SCRIPTS}; do
echo "Restoring $f"
psql -q --echo-errors -c "SET client_min_messages TO WARNING;" -f $f $DB -v ON_ERROR_STOP=1 || exit 1
done

PGSERVERVERSION=$(psql -XtA -c 'SHOW server_version_num' $DB)
if test $PGSERVERVERSION -gt 120000; then
for f in ${SCRIPTS12}; do
echo "Restoring $f"
psql -q --echo-errors -c "SET client_min_messages TO WARNING;" -f $f $DB -v ON_ERROR_STOP=1 || exit 1
done
fi

# Test existence of qgis_test service, and recommend how to set it up
# otherwise
TESTDB=$(psql -XtA 'service=qgis_test' -c "select current_database()")
Expand Down
8 changes: 8 additions & 0 deletions tests/testdata/provider/testdata_pg_12_generated.sql
@@ -0,0 +1,8 @@
-- valid only for PostgreSQL 12 and newer, because we are testing features
-- available only on this version.

CREATE TABLE qgis_test.test_gen_col (
id SERIAL PRIMARY KEY,
geom GEOMETRY('Polygon', 4326) NOT NULL,
cent GEOMETRY('Point') GENERATED ALWAYS AS ( st_centroid(geom) ) STORED
);

0 comments on commit 3e9da5d

Please sign in to comment.