def test_rasterTableURI(self): def check_rasterTableURI(expected_dbname): tables = database.tables() raster_tables_count = 0 for tab in tables: if tab.type == Table.RasterType: raster_tables_count += 1 uri = tab.uri() m = re.search(' dbname=\'([^ ]*)\' ', uri) self.assertTrue(m) actual_dbname = m.group(1) self.assertEqual(actual_dbname, expected_dbname) # print(tab.type) # print(tab.quotedName()) # print(tab) # We need to make sure a database is created with at # least one raster table ! self.assertGreaterEqual(raster_tables_count, 1) obj = QObject() # needs to be kept alive obj.connectionName = lambda: 'fake' obj.providerName = lambda: 'postgres' # Test for empty URI # See https://github.com/qgis/QGIS/issues/24525 # and https://github.com/qgis/QGIS/issues/19005 expected_dbname = self.testdb os.environ['PGDATABASE'] = expected_dbname database = PGDatabase(obj, QgsDataSourceUri()) self.assertIsInstance(database, PGDatabase) uri = database.uri() self.assertEqual(uri.host(), '') self.assertEqual(uri.username(), '') self.assertEqual(uri.database(), expected_dbname) self.assertEqual(uri.service(), '') check_rasterTableURI(expected_dbname) # Test for service-only URI # See https://github.com/qgis/QGIS/issues/24526 os.environ['PGDATABASE'] = 'fake' database = PGDatabase(obj, QgsDataSourceUri('service=dbmanager')) self.assertIsInstance(database, PGDatabase) uri = database.uri() self.assertEqual(uri.host(), '') self.assertEqual(uri.username(), '') self.assertEqual(uri.database(), '') self.assertEqual(uri.service(), 'dbmanager') check_rasterTableURI(expected_dbname)
def test_rasterTableGdalURI(self): def check_rasterTableGdalURI(expected_dbname): tables = database.tables() raster_tables_count = 0 for tab in tables: if tab.type == Table.RasterType: raster_tables_count += 1 gdalUri = tab.gdalUri() m = re.search(' dbname=\'([^ ]*)\' ', gdalUri) self.assertTrue(m) actual_dbname = m.group(1) self.assertEqual(actual_dbname, expected_dbname) #print(tab.type) #print(tab.quotedName()) #print(tab) # We need to make sure a database is created with at # least one raster table ! self.assertEqual(raster_tables_count, 1) obj = QObject() # needs to be kept alive # Test for empty URI # See https://issues.qgis.org/issues/16625 # and https://issues.qgis.org/issues/10600 expected_dbname = self.testdb os.environ['PGDATABASE'] = expected_dbname database = PGDatabase(obj, QgsDataSourceUri()) self.assertIsInstance(database, PGDatabase) uri = database.uri() self.assertEqual(uri.host(), '') self.assertEqual(uri.username(), '') self.assertEqual(uri.database(), expected_dbname) self.assertEqual(uri.service(), '') check_rasterTableGdalURI(expected_dbname) # Test for service-only URI # See https://issues.qgis.org/issues/16626 os.environ['PGDATABASE'] = 'fake' database = PGDatabase(obj, QgsDataSourceUri('service=dbmanager')) self.assertIsInstance(database, PGDatabase) uri = database.uri() self.assertEqual(uri.host(), '') self.assertEqual(uri.username(), '') self.assertEqual(uri.database(), '') self.assertEqual(uri.service(), 'dbmanager') check_rasterTableGdalURI(expected_dbname)