|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + plugin_test.py |
| 6 | + --------------------- |
| 7 | + Date : May 2017 |
| 8 | + Copyright : (C) 2017, Sandro Santilli |
| 9 | + Email : strk at kbt dot io |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Sandro Santilli' |
| 21 | +__date__ = 'May 2017' |
| 22 | +__copyright__ = '(C) 2017, Sandro Santilli' |
| 23 | +# This will get replaced with a git SHA1 when you do a git archive |
| 24 | +__revision__ = '$Format:%H$' |
| 25 | + |
| 26 | +import os |
| 27 | +import re |
| 28 | +import qgis |
| 29 | +from qgis.testing import start_app, unittest |
| 30 | +from qgis.core import QgsDataSourceURI |
| 31 | +from qgis.utils import iface |
| 32 | +from qgis.PyQt.QtCore import QObject |
| 33 | + |
| 34 | +start_app() |
| 35 | + |
| 36 | +from db_manager.db_plugins.postgis.plugin import PostGisDBPlugin, PGRasterTable |
| 37 | +from db_manager.db_plugins.postgis.plugin import PGDatabase |
| 38 | +from db_manager.db_plugins.plugin import Table |
| 39 | +from db_manager.db_plugins.postgis.connector import PostGisDBConnector |
| 40 | + |
| 41 | + |
| 42 | +class TestDBManagerPostgisPlugin(unittest.TestCase): |
| 43 | + |
| 44 | + @classmethod |
| 45 | + def setUpClass(self): |
| 46 | + self.old_pgdatabase_env = os.environ.get('PGDATABASE') |
| 47 | + self.testdb = os.environ.get('QGIS_PGTEST_DB') or 'qgis_test' |
| 48 | + os.environ['PGDATABASE'] = self.testdb |
| 49 | + |
| 50 | + # Create temporary service file |
| 51 | + self.old_pgservicefile_env = os.environ.get('PGSERVICEFILE') |
| 52 | + self.tmpservicefile = '/tmp/qgis-test-{}-pg_service.conf'.format(os.getpid()) |
| 53 | + os.environ['PGSERVICEFILE'] = self.tmpservicefile |
| 54 | + |
| 55 | + f = open(self.tmpservicefile, "w") |
| 56 | + f.write("[dbmanager]\ndbname={}\n".format(self.testdb)) |
| 57 | + # TODO: add more things if PGSERVICEFILE was already set ? |
| 58 | + f.close() |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def tearDownClass(self): |
| 62 | + # Restore previous env variables if needed |
| 63 | + if self.old_pgdatabase_env: |
| 64 | + os.environ['PGDATABASE'] = self.old_pgdatabase_env |
| 65 | + if self.old_pgservicefile_env: |
| 66 | + os.environ['PGSERVICEFILE'] = self.old_pgservicefile_env |
| 67 | + # Remove temporary service file |
| 68 | + os.unlink(self.tmpservicefile) |
| 69 | + |
| 70 | + # See https://issues.qgis.org/issues/16625 |
| 71 | + |
| 72 | + def test_rasterTableGdalURI(self): |
| 73 | + |
| 74 | + def check_rasterTableGdalURI(expected_dbname): |
| 75 | + tables = database.tables() |
| 76 | + raster_tables_count = 0 |
| 77 | + for tab in tables: |
| 78 | + if tab.type == Table.RasterType: |
| 79 | + raster_tables_count += 1 |
| 80 | + gdalUri = tab.gdalUri() |
| 81 | + m = re.search(' dbname=([^ ]*) ', gdalUri) |
| 82 | + self.assertTrue(m) |
| 83 | + actual_dbname = m.group(1) |
| 84 | + self.assertEqual(actual_dbname, expected_dbname) |
| 85 | + #print(tab.type) |
| 86 | + #print(tab.quotedName()) |
| 87 | + #print(tab) |
| 88 | + |
| 89 | + # We need to make sure a database is created with at |
| 90 | + # least one raster table ! |
| 91 | + self.assertEqual(raster_tables_count, 1) |
| 92 | + |
| 93 | + obj = QObject() # needs to be kept alive |
| 94 | + |
| 95 | + # Test for empty URI |
| 96 | + # See https://issues.qgis.org/issues/16625 |
| 97 | + # and https://issues.qgis.org/issues/10600 |
| 98 | + |
| 99 | + expected_dbname = self.testdb |
| 100 | + os.environ['PGDATABASE'] = expected_dbname |
| 101 | + |
| 102 | + database = PGDatabase(obj, QgsDataSourceURI()) |
| 103 | + self.assertIsInstance(database, PGDatabase) |
| 104 | + |
| 105 | + uri = database.uri() |
| 106 | + self.assertEqual(uri.host(), '') |
| 107 | + self.assertEqual(uri.username(), '') |
| 108 | + self.assertEqual(uri.database(), expected_dbname) |
| 109 | + self.assertEqual(uri.service(), '') |
| 110 | + |
| 111 | + check_rasterTableGdalURI(expected_dbname) |
| 112 | + |
| 113 | + # Test for service-only URI |
| 114 | + # See https://issues.qgis.org/issues/16626 |
| 115 | + |
| 116 | + os.environ['PGDATABASE'] = 'fake' |
| 117 | + database = PGDatabase(obj, QgsDataSourceURI('service=dbmanager')) |
| 118 | + self.assertIsInstance(database, PGDatabase) |
| 119 | + |
| 120 | + uri = database.uri() |
| 121 | + self.assertEqual(uri.host(), '') |
| 122 | + self.assertEqual(uri.username(), '') |
| 123 | + self.assertEqual(uri.database(), '') |
| 124 | + self.assertEqual(uri.service(), 'dbmanager') |
| 125 | + |
| 126 | + check_rasterTableGdalURI(expected_dbname) |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == '__main__': |
| 130 | + unittest.main() |
0 commit comments