Skip to content
Permalink
Browse files

Mechanism to test features on PostgreSQL 12+

  • Loading branch information
espinafre authored and nyalldawson committed May 1, 2020
1 parent fef79a0 commit 3e9da5d28d56adb4b4e6190bab3b42b04f3a9844
@@ -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(
@@ -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()")
@@ -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.
You can’t perform that action at this time.