Skip to content

Commit

Permalink
Merge pull request #32857 from strk/consistent-pg-test-connstring
Browse files Browse the repository at this point in the history
Use service=qgis_test for referencing test database
  • Loading branch information
strk committed Nov 14, 2019
2 parents cbe6150 + 20114fc commit bd01f66
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 26 deletions.
3 changes: 1 addition & 2 deletions tests/README.md
Expand Up @@ -57,9 +57,8 @@ To test the postgres provider you will need to have a database available to
which the postgres provider can connect. The server will need to have PostGIS
support enabled.

By default the test uses one of the following connection string:
By default the tests use the following connection string:

dbname=qgis_test
service=qgis_test

If these do not match your setup you can set the environment variable
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgsvectorlayerjoinbuffer.cpp
Expand Up @@ -116,7 +116,7 @@ void TestVectorLayerJoinBuffer::initTestCase()
QString dbConn = getenv( "QGIS_PGTEST_DB" );
if ( dbConn.isEmpty() )
{
dbConn = "dbname='qgis_test'";
dbConn = "service=qgis_test";
}
QgsVectorLayer *vlA_PG = new QgsVectorLayer( QString( "%1 sslmode=disable key='id_a' table=\"qgis_test\".\"table_a\" sql=" ).arg( dbConn ), "A_PG", "postgres" );
QgsVectorLayer *vlB_PG = new QgsVectorLayer( QString( "%1 sslmode=disable key='id_b' table=\"qgis_test\".\"table_b\" sql=" ).arg( dbConn ), "B_PG", "postgres" );
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_db_manager_postgis.py
Expand Up @@ -171,7 +171,7 @@ def setUpServer(cls):

@classmethod
def setUpProvider(cls, authId):
cls.dbconn = 'dbname=\'qgis_test\''
cls.dbconn = 'service=qgis_test'
if 'QGIS_PGTEST_DB' in os.environ:
cls.dbconn = os.environ['QGIS_PGTEST_DB']
uri = QgsDataSourceUri()
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_processing_importintopostgis.py
Expand Up @@ -88,7 +88,7 @@ def test_import(self):
# Check that data have been imported correctly
exported = QgsVectorLayer(unitTestDataPath() + '/points.shp', 'exported')
self.assertTrue(exported.isValid())
imported = QgsVectorLayer("service='qgis_test' dbname=\'qgis_test\' table=\"CamelCaseSchema\".\"%s\" (geom)" % table_name, 'imported', 'postgres')
imported = QgsVectorLayer("service='qgis_test' table=\"CamelCaseSchema\".\"%s\" (geom)" % table_name, 'imported', 'postgres')
self.assertTrue(imported.isValid())
imported_fields = [f.name() for f in imported.fields()]
for f in exported.fields():
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_project_storage_postgres.py
Expand Up @@ -70,7 +70,7 @@ class TestPyQgsProjectStoragePostgres(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""Run before all tests"""
cls.dbconn = 'dbname=\'qgis_test\''
cls.dbconn = 'service=qgis_test'
if 'QGIS_PGTEST_DB' in os.environ:
cls.dbconn = os.environ['QGIS_PGTEST_DB']
cls.ds_uri = QgsDataSourceUri(cls.dbconn)
Expand Down
15 changes: 5 additions & 10 deletions tests/src/python/test_provider_postgres.py
Expand Up @@ -61,7 +61,7 @@ class TestPyQgsPostgresProvider(unittest.TestCase, ProviderTestCase):
@classmethod
def setUpClass(cls):
"""Run before all tests"""
cls.dbconn = 'dbname=\'qgis_test\''
cls.dbconn = 'service=qgis_test'
if 'QGIS_PGTEST_DB' in os.environ:
cls.dbconn = os.environ['QGIS_PGTEST_DB']
# Create test layers
Expand Down Expand Up @@ -1295,13 +1295,8 @@ def receive(self, msg):
def testStyleDatabaseWithService(self):
"""Test saving style in DB using a service file.
To run this test, you first need to create a service with:
[qgis_test]
host=localhost
port=5432
dbname=qgis_test
user=USERNAME
password=PASSWORD
To run this test, you first need to setup the test
database with tests/testdata/provider/testdata_pg.sh
"""
myconn = 'service=\'qgis_test\''
if 'QGIS_PGTEST_DB' in os.environ:
Expand Down Expand Up @@ -1379,7 +1374,7 @@ def testMassivePaste(self):
def testFilterOnCustomBbox(self):
extent = QgsRectangle(-68, 70, -67, 80)
request = QgsFeatureRequest().setFilterRect(extent)
dbconn = 'dbname=\'qgis_test\''
dbconn = 'service=qgis_test'
uri = '%s srid=4326 key="pk" sslmode=disable table="qgis_test"."some_poly_data_shift_bbox" (geom)' % (dbconn)

def _test(vl, ids):
Expand Down Expand Up @@ -1421,7 +1416,7 @@ class TestPyQgsPostgresProviderCompoundKey(unittest.TestCase, ProviderTestCase):
@classmethod
def setUpClass(cls):
"""Run before all tests"""
cls.dbconn = 'dbname=\'qgis_test\''
cls.dbconn = 'service=qgis_test'
if 'QGIS_PGTEST_DB' in os.environ:
cls.dbconn = os.environ['QGIS_PGTEST_DB']
# Create test layers
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_qgspostgrestransaction.py
Expand Up @@ -34,7 +34,7 @@ def setUpClass(cls):
Setup the involved layers and relations for a n:m relation
:return:
"""
cls.dbconn = 'dbname=\'qgis_test\''
cls.dbconn = 'service=qgis_test'
if 'QGIS_PGTEST_DB' in os.environ:
cls.dbconn = os.environ['QGIS_PGTEST_DB']
# Create test layer
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_qgsproviderconnection_postgres.py
Expand Up @@ -39,7 +39,7 @@ class TestPyQgsProviderConnectionPostgres(unittest.TestCase, TestPyQgsProviderCo
def setUpClass(cls):
"""Run before all tests"""
TestPyQgsProviderConnectionBase.setUpClass()
cls.postgres_conn = 'dbname=\'qgis_test\''
cls.postgres_conn = "service='qgis_test'"
if 'QGIS_PGTEST_DB' in os.environ:
cls.postgres_conn = os.environ['QGIS_PGTEST_DB']
# Create test layers
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_qgsvectorfilewriter.py
Expand Up @@ -120,7 +120,7 @@ def testWriteWithLongLongField(self):
def testWriteWithBoolField(self):

# init connection string
dbconn = 'dbname=\'qgis_test\''
dbconn = 'service=qgis_test'
if 'QGIS_PGTEST_DB' in os.environ:
dbconn = os.environ['QGIS_PGTEST_DB']

Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_qgsvectorlayerutils.py
Expand Up @@ -307,7 +307,7 @@ def testCreateFeature(self):
"addfeat", "memory")

# init connection string
dbconn = 'dbname=\'qgis_test\''
dbconn = 'service=qgis_test'
if 'QGIS_PGTEST_DB' in os.environ:
dbconn = os.environ['QGIS_PGTEST_DB']

Expand Down
30 changes: 26 additions & 4 deletions tests/testdata/provider/testdata_pg.sh
@@ -1,7 +1,5 @@
#!/bin/sh

set -e

DB=${DB:-qgis_test}

SCRIPTS="
Expand All @@ -16,7 +14,31 @@ SCRIPTS="
"

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

# Test existence of qgis_test service, and recommend how to set it up
# otherwise
TESTDB=$(psql -XtA 'service=qgis_test' -c "select current_database()")
if test "${TESTDB}" != "${DB}"; then
exec >&2
if test -n "${TESTDB}"; then
echo "WARNING: [qgis_test] service section points to db '${TESTDB}'" \
"but we populated db '${DB}' instead"
else
echo "ERROR: [qgis_test] service not found in ~/.pg_service.conf"
echo "HINT: create a section like the following:"
cat <<EOF
[qgis_test]
host=localhost
port=5432
dbname=${DB}
user=USERNAME
password=PASSWORD
EOF
fi
else
echo "Database ${DB} populated and ready for use with 'service=qgis_test'"
fi
4 changes: 2 additions & 2 deletions tests/testdata/qgis_server/test_project_postgres_types.qgs
Expand Up @@ -19,7 +19,7 @@
</projectCrs>
<layer-tree-group>
<customproperties/>
<layer-tree-layer expanded="1" checked="Qt::Checked" source="dbname='qgis_test' port=5432 user='postgres' sslmode=disable key='pk' table=&quot;qgis_test&quot;.&quot;json&quot; sql=" providerKey="postgres" id="json_b2af056d_ba24_4fb7_805b_5a7720e242f3" name="json">
<layer-tree-layer expanded="1" checked="Qt::Checked" source="service='qgis_test' key='pk' table=&quot;qgis_test&quot;.&quot;json&quot; sql=" providerKey="postgres" id="json_b2af056d_ba24_4fb7_805b_5a7720e242f3" name="json">
<customproperties/>
</layer-tree-layer>
<custom-order enabled="0"/>
Expand Down Expand Up @@ -63,7 +63,7 @@
<projectlayers>
<maplayer maxScale="0" autoRefreshEnabled="0" hasScaleBasedVisibilityFlag="0" type="vector" refreshOnNotifyMessage="" autoRefreshTime="0" geometry="No geometry" styleCategories="AllStyleCategories" refreshOnNotifyEnabled="0" minScale="1e+8" readOnly="0">
<id>json_b2af056d_ba24_4fb7_805b_5a7720e242f3</id>
<datasource>dbname='qgis_test' port=5432 sslmode=disable key='pk' table="qgis_test"."json" sql=</datasource>
<datasource>service='qgis_test' key='pk' table="qgis_test"."json" sql=</datasource>
<keywordList>
<value></value>
</keywordList>
Expand Down

0 comments on commit bd01f66

Please sign in to comment.