def testListLayer(self): connection_name = 'testListLayer' plugin = createDbPlugin('gpkg') uri = QgsDataSourceUri() uri.setDatabase(self.test_gpkg) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin('gpkg', connection_name) connection.connect() db = connection.database() self.assertIsNotNone(db) tables = db.tables() self.assertEqual(len(tables), 1) table = tables[0] self.assertEqual(table.name, 'testLayer') info = table.info() expected_html = """<div class="section"><h2>General info</h2><div><table><tr><td>Relation type: </td><td>Table </td></tr><tr><td>Rows: </td><td>1 </td></tr></table></div></div><div class="section"><h2>GeoPackage</h2><div><table><tr><td>Column: </td><td>geom </td></tr><tr><td>Geometry: </td><td>LINESTRING </td></tr><tr><td>Dimension: </td><td>XY </td></tr><tr><td>Spatial ref: </td><td>Undefined (-1) </td></tr><tr><td>Extent: </td><td>1.00000, 2.00000 - 3.00000, 4.00000 </td></tr></table><p><warning> No spatial index defined (<a href="action:spatialindex/create">create it</a>)</p></div></div><div class="section"><h2>Fields</h2><div><table class="header"><tr><th># </th><th>Name </th><th>Type </th><th>Null </th><th>Default </th></tr><tr><td>0 </td><td class="underline">fid </td><td>INTEGER </td><td>Y </td><td> </td></tr><tr><td>1 </td><td>geom </td><td>LINESTRING </td><td>Y </td><td> </td></tr><tr><td>2 </td><td>text_field </td><td>TEXT </td><td>Y </td><td> </td></tr></table></div></div>""" # GDAL 2.2.0 expected_html_2 = """<div class="section"><h2>General info</h2><div><table><tr><td>Relation type: </td><td>Table </td></tr><tr><td>Rows: </td><td>1 </td></tr></table></div></div><div class="section"><h2>GeoPackage</h2><div><table><tr><td>Column: </td><td>geom </td></tr><tr><td>Geometry: </td><td>LINESTRING </td></tr><tr><td>Dimension: </td><td>XY </td></tr><tr><td>Spatial ref: </td><td>Undefined (-1) </td></tr><tr><td>Extent: </td><td>1.00000, 2.00000 - 3.00000, 4.00000 </td></tr></table><p><warning> No spatial index defined (<a href="action:spatialindex/create">create it</a>)</p></div></div><div class="section"><h2>Fields</h2><div><table class="header"><tr><th># </th><th>Name </th><th>Type </th><th>Null </th><th>Default </th></tr><tr><td>0 </td><td class="underline">fid </td><td>INTEGER </td><td>N </td><td> </td></tr><tr><td>1 </td><td>geom </td><td>LINESTRING </td><td>Y </td><td> </td></tr><tr><td>2 </td><td>text_field </td><td>TEXT </td><td>Y </td><td> </td></tr></table></div></div><div class="section"><h2>Triggers</h2><div><table class="header"><tr><th>Name </th><th>Function </th></tr><tr><td>trigger_insert_feature_count_testLayer (<a href="action:trigger/trigger_insert_feature_count_testLayer/delete">delete</a>) </td><td>CREATE TRIGGER "trigger_insert_feature_count_testLayer" AFTER INSERT ON "testLayer" BEGIN UPDATE gpkg_ogr_contents SET feature_count = feature_count + 1 WHERE table_name = 'testLayer'; END </td></tr><tr><td>trigger_delete_feature_count_testLayer (<a href="action:trigger/trigger_delete_feature_count_testLayer/delete">delete</a>) </td><td>CREATE TRIGGER "trigger_delete_feature_count_testLayer" AFTER DELETE ON "testLayer" BEGIN UPDATE gpkg_ogr_contents SET feature_count = feature_count - 1 WHERE table_name = 'testLayer'; END </td></tr></table></div></div>""" # GDAL 2.3.0 expected_html_3 = """<div class="section"><h2>General info</h2><div><table><tr><td>Relation type: </td><td>Table </td></tr><tr><td>Rows: </td><td>1 </td></tr></table></div></div><div class="section"><h2>GeoPackage</h2><div><table><tr><td>Column: </td><td>geom </td></tr><tr><td>Geometry: </td><td>LINESTRING </td></tr><tr><td>Dimension: </td><td>XY </td></tr><tr><td>Spatial ref: </td><td>Undefined (-1) </td></tr><tr><td>Extent: </td><td>1.00000, 2.00000 - 3.00000, 4.00000 </td></tr></table><p><warning> No spatial index defined (<a href="action:spatialindex/create">create it</a>)</p></div></div><div class="section"><h2>Fields</h2><div><table class="header"><tr><th># </th><th>Name </th><th>Type </th><th>Null </th><th>Default </th></tr><tr><td>0 </td><td class="underline">fid </td><td>INTEGER </td><td>N </td><td> </td></tr><tr><td>1 </td><td>geom </td><td>LINESTRING </td><td>Y </td><td> </td></tr><tr><td>2 </td><td>text_field </td><td>TEXT </td><td>Y </td><td> </td></tr></table></div></div><div class="section"><h2>Triggers</h2><div><table class="header"><tr><th>Name </th><th>Function </th></tr><tr><td>trigger_insert_feature_count_testLayer (<a href="action:trigger/trigger_insert_feature_count_testLayer/delete">delete</a>) </td><td>CREATE TRIGGER "trigger_insert_feature_count_testLayer" AFTER INSERT ON "testLayer" BEGIN UPDATE gpkg_ogr_contents SET feature_count = feature_count + 1 WHERE lower(table_name) = lower('testLayer'); END </td></tr><tr><td>trigger_delete_feature_count_testLayer (<a href="action:trigger/trigger_delete_feature_count_testLayer/delete">delete</a>) </td><td>CREATE TRIGGER "trigger_delete_feature_count_testLayer" AFTER DELETE ON "testLayer" BEGIN UPDATE gpkg_ogr_contents SET feature_count = feature_count - 1 WHERE lower(table_name) = lower('testLayer'); END </td></tr></table></div></div>""" self.assertIn(info.toHtml(), [expected_html, expected_html_2, expected_html_3]) connection.remove()
def testNonSpatial(self): connection_name = 'testNonSpatial' plugin = createDbPlugin('gpkg') uri = QgsDataSourceUri() test_gpkg = os.path.join(self.basetestpath, 'testNonSpatial.gpkg') ds = ogr.GetDriverByName('GPKG').CreateDataSource(test_gpkg) lyr = ds.CreateLayer('testNonSpatial', geom_type=ogr.wkbNone) lyr.CreateField(ogr.FieldDefn('text_field', ogr.OFTString)) f = ogr.Feature(lyr.GetLayerDefn()) f['text_field'] = 'foo' lyr.CreateFeature(f) f = None ds = None uri.setDatabase(test_gpkg) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin('gpkg', connection_name) connection.connect() db = connection.database() self.assertIsNotNone(db) tables = db.tables() self.assertEqual(len(tables), 1) table = tables[0] self.assertEqual(table.name, 'testNonSpatial') info = table.info() expected_html = """<div class="section"><h2>General info</h2><div><table><tr><td>Relation type: </td><td>Table </td></tr><tr><td>Rows: </td><td>1 </td></tr></table></div></div><div class="section"><h2>Fields</h2><div><table class="header"><tr><th># </th><th>Name </th><th>Type </th><th>Null </th><th>Default </th></tr><tr><td>0 </td><td class="underline">fid </td><td>INTEGER </td><td>Y </td><td> </td></tr><tr><td>1 </td><td>text_field </td><td>TEXT </td><td>Y </td><td> </td></tr></table></div></div>""" self.assertEqual(info.toHtml(), expected_html, info.toHtml()) connection.remove()
def testTableDataModel(self): connection_name = 'testTableDataModel' plugin = createDbPlugin('spatialite') uri = QgsDataSourceUri() uri.setDatabase(self.test_spatialite) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin('spatialite', connection_name) connection.connect() db = connection.database() self.assertIsNotNone(db) tables = db.tables() self.assertEqual(len(tables), 1) table = tables[0] self.assertEqual(table.name, 'testlayer') model = table.tableDataModel(None) self.assertEqual(model.rowCount(), 1) self.assertEqual(model.getData(0, 0), 1) # fid wkb = model.getData(0, 1) geometry = ogr.CreateGeometryFromWkb(wkb) self.assertEqual(geometry.ExportToWkt(), 'LINESTRING (1 2,3 4)') self.assertEqual(model.getData(0, 2), 'foo') connection.remove()
def testExecuteRegExp(self): """This test checks for REGEXP syntax support, which is enabled in Qgis.utils' spatialite_connection()""" connection_name = 'testListLayer' plugin = createDbPlugin('spatialite') uri = QgsDataSourceUri() uri.setDatabase(self.test_spatialite) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin('spatialite', connection_name) connection.connect() db = connection.database() db.connector._execute(None, 'SELECT \'ABC\' REGEXP \'[CBA]\'')
def testCreateRenameDeleteFields(self): if not self.supportsAlterFieldDefn: return connection_name = 'testCreateRenameDeleteFields' plugin = createDbPlugin('spatialite') uri = QgsDataSourceUri() test_spatialite_new = os.path.join(self.basetestpath, 'testCreateRenameDeleteFields.spatialite') shutil.copy(self.test_spatialite, test_spatialite_new) uri.setDatabase(test_spatialite_new) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin('spatialite', connection_name) connection.connect() db = connection.database() self.assertIsNotNone(db) tables = db.tables() self.assertEqual(len(tables), 1) table = tables[0] field_before_count = len(table.fields()) field = TableField(table) field.name = 'real_field' field.dataType = 'DOUBLE' self.assertTrue(table.addField(field)) self.assertEqual(len(table.fields()), field_before_count + 1) # not supported in spatialite # self.assertTrue(field.update('real_field2', new_type_str='TEXT (30)', new_not_null=True, new_default_str='foo')) field = table.fields()[field_before_count] self.assertEqual(field.name, 'real_field') self.assertEqual(field.dataType, 'DOUBLE') # self.assertEqual(field.notNull, 1) # self.assertEqual(field.default, "'foo'") # self.assertTrue(table.deleteField(field)) # self.assertEqual(len(table.fields()), field_before_count) connection.remove()
def testRemoveTemporaryCerts(self): """ Check that no temporary cert remain after connection with db_manager postgis plugin """ def cleanTempPki(): pkies = glob.glob(os.path.join(tempfile.gettempdir(), 'tmp*_{*}.pem')) for fn in pkies: f = QFile(fn) f.setPermissions(QFile.WriteOwner) f.remove() # remove any temppki in temprorary path to check that no # other pki remain after connection cleanTempPki() # connect self.setUpProvider(self.auth_config.id()) plugin = createDbPlugin('postgis') connections = plugin.connections() self.assertEqual(len(connections), 1) # test connection postgisConnPlugin = connections[0] self.assertTrue(postgisConnPlugin.connect()) # do test no certs remained pkies = glob.glob(os.path.join(tempfile.gettempdir(), 'tmp*_{*}.pem')) self.assertEqual(len(pkies), 0)
def testCreateRenameDeleteFields(self): if not self.supportsAlterFieldDefn: return connection_name = "testCreateRenameDeleteFields" plugin = createDbPlugin("gpkg") uri = QgsDataSourceUri() test_gpkg_new = os.path.join(self.basetestpath, "testCreateRenameDeleteFields.gpkg") shutil.copy(self.test_gpkg, test_gpkg_new) uri.setDatabase(test_gpkg_new) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin("gpkg", connection_name) connection.connect() db = connection.database() self.assertIsNotNone(db) tables = db.tables() self.assertEqual(len(tables), 1) table = tables[0] field_before_count = len(table.fields()) field = TableField(table) field.name = "real_field" field.dataType = "DOUBLE" self.assertTrue(table.addField(field)) self.assertEqual(len(table.fields()), field_before_count + 1) self.assertTrue(field.update("real_field2", new_type_str="TEXT (30)", new_not_null=True, new_default_str="foo")) field = table.fields()[field_before_count] self.assertEqual(field.name, "real_field2") self.assertEqual(field.dataType, "TEXT(30)") self.assertEqual(field.notNull, 1) self.assertEqual(field.default, "'foo'") self.assertTrue(table.deleteField(field)) self.assertEqual(len(table.fields()), field_before_count) connection.remove()
def testRaster(self): if int(gdal.VersionInfo("VERSION_NUM")) < GDAL_COMPUTE_VERSION(2, 0, 2): return connection_name = "testRaster" plugin = createDbPlugin("gpkg") uri = QgsDataSourceUri() test_gpkg_new = os.path.join(self.basetestpath, "testRaster.gpkg") shutil.copy(self.test_gpkg, test_gpkg_new) mem_ds = gdal.GetDriverByName("MEM").Create("", 20, 20) mem_ds.SetGeoTransform([2, 0.01, 0, 49, 0, -0.01]) sr = osr.SpatialReference() sr.ImportFromEPSG(4326) mem_ds.SetProjection(sr.ExportToWkt()) mem_ds.GetRasterBand(1).Fill(255) gdal.GetDriverByName("GPKG").CreateCopy( test_gpkg_new, mem_ds, options=["APPEND_SUBDATASET=YES", "RASTER_TABLE=raster_table"] ) mem_ds = None uri.setDatabase(test_gpkg_new) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin("gpkg", connection_name) connection.connect() db = connection.database() self.assertIsNotNone(db) tables = db.tables() self.assertEqual(len(tables), 2) table = None for i in range(2): if tables[i].name == "raster_table": table = tables[i] break self.assertIsNotNone(table) info = table.info() expected_html = """<div class="section"><h2>General info</h2><div><table><tr><td>Relation type: </td><td>Table </td></tr><tr><td>Rows: </td><td>Unknown (<a href="action:rows/count">find out</a>) </td></tr></table></div></div><div class="section"><h2>GeoPackage</h2><div><table><tr><td>Column: </td><td> </td></tr><tr><td>Geometry: </td><td>RASTER </td></tr><tr><td>Spatial ref: </td><td>WGS 84 geodetic (4326) </td></tr><tr><td>Extent: </td><td>2.00000, 48.80000 - 2.20000, 49.00000 </td></tr></table></div></div><div class="section"><h2>Fields</h2><div><table class="header"><tr><th># </th><th>Name </th><th>Type </th><th>Null </th><th>Default </th></tr><tr><td>0 </td><td class="underline">id </td><td>INTEGER </td><td>Y </td><td> </td></tr><tr><td>1 </td><td>zoom_level </td><td>INTEGER </td><td>N </td><td> </td></tr><tr><td>2 </td><td>tile_column </td><td>INTEGER </td><td>N </td><td> </td></tr><tr><td>3 </td><td>tile_row </td><td>INTEGER </td><td>N </td><td> </td></tr><tr><td>4 </td><td>tile_data </td><td>BLOB </td><td>N </td><td> </td></tr></table></div></div><div class="section"><h2>Indexes</h2><div><table class="header"><tr><th>Name </th><th>Column(s) </th></tr><tr><td>sqlite_autoindex_raster_table_1 </td><td>zoom_level<br>tile_column<br>tile_row </td></tr></table></div></div>""" self.assertEqual(info.toHtml(), expected_html, info.toHtml()) connection.remove()
def testTwoRaster(self): if int(gdal.VersionInfo("VERSION_NUM")) < GDAL_COMPUTE_VERSION(2, 0, 2): return connection_name = "testTwoRaster" plugin = createDbPlugin("gpkg") uri = QgsDataSourceUri() test_gpkg_new = os.path.join(self.basetestpath, "testTwoRaster.gpkg") shutil.copy(self.test_gpkg, test_gpkg_new) mem_ds = gdal.GetDriverByName("MEM").Create("", 20, 20) mem_ds.SetGeoTransform([2, 0.01, 0, 49, 0, -0.01]) sr = osr.SpatialReference() sr.ImportFromEPSG(4326) mem_ds.SetProjection(sr.ExportToWkt()) mem_ds.GetRasterBand(1).Fill(255) for i in range(2): gdal.GetDriverByName("GPKG").CreateCopy( test_gpkg_new, mem_ds, options=["APPEND_SUBDATASET=YES", "RASTER_TABLE=raster_table%d" % (i + 1)] ) mem_ds = None uri.setDatabase(test_gpkg_new) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin("gpkg", connection_name) connection.connect() db = connection.database() self.assertIsNotNone(db) tables = db.tables() self.assertEqual(len(tables), 3) table = None for i in range(2): if tables[i].name.startswith("raster_table"): table = tables[i] info = table.info() info.toHtml() connection.remove()
def testNonSpatial(self): connection_name = 'testNonSpatial' plugin = createDbPlugin('gpkg') uri = QgsDataSourceUri() test_gpkg = os.path.join(self.basetestpath, 'testNonSpatial.gpkg') ds = ogr.GetDriverByName('GPKG').CreateDataSource(test_gpkg) lyr = ds.CreateLayer('testNonSpatial', geom_type=ogr.wkbNone) lyr.CreateField(ogr.FieldDefn('text_field', ogr.OFTString)) f = ogr.Feature(lyr.GetLayerDefn()) f['text_field'] = 'foo' lyr.CreateFeature(f) f = None ds = None uri.setDatabase(test_gpkg) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin('gpkg', connection_name) connection.connect() db = connection.database() self.assertIsNotNone(db) tables = db.tables() self.assertEqual(len(tables), 1) table = tables[0] self.assertEqual(table.name, 'testNonSpatial') info = table.info() expected_html = """<div class="section"><h2>General info</h2><div><table><tr><td>Relation type: </td><td>Table </td></tr><tr><td>Rows: </td><td>1 </td></tr></table></div></div><div class="section"><h2>Fields</h2><div><table class="header"><tr><th># </th><th>Name </th><th>Type </th><th>Null </th><th>Default </th></tr><tr><td>0 </td><td class="underline">fid </td><td>INTEGER </td><td>Y </td><td> </td></tr><tr><td>1 </td><td>text_field </td><td>TEXT </td><td>Y </td><td> </td></tr></table></div></div>""" # GDAL 2.2.0 expected_html_2 = """<div class="section"><h2>General info</h2><div><table><tr><td>Relation type: </td><td>Table </td></tr><tr><td>Rows: </td><td>1 </td></tr></table></div></div><div class="section"><h2>Fields</h2><div><table class="header"><tr><th># </th><th>Name </th><th>Type </th><th>Null </th><th>Default </th></tr><tr><td>0 </td><td class="underline">fid </td><td>INTEGER </td><td>N </td><td> </td></tr><tr><td>1 </td><td>text_field </td><td>TEXT </td><td>Y </td><td> </td></tr></table></div></div><div class="section"><h2>Triggers</h2><div><table class="header"><tr><th>Name </th><th>Function </th></tr><tr><td>trigger_insert_feature_count_testNonSpatial (<a href="action:trigger/trigger_insert_feature_count_testNonSpatial/delete">delete</a>) </td><td>CREATE TRIGGER "trigger_insert_feature_count_testNonSpatial" AFTER INSERT ON "testNonSpatial" BEGIN UPDATE gpkg_ogr_contents SET feature_count = feature_count + 1 WHERE table_name = 'testNonSpatial'; END </td></tr><tr><td>trigger_delete_feature_count_testNonSpatial (<a href="action:trigger/trigger_delete_feature_count_testNonSpatial/delete">delete</a>) </td><td>CREATE TRIGGER "trigger_delete_feature_count_testNonSpatial" AFTER DELETE ON "testNonSpatial" BEGIN UPDATE gpkg_ogr_contents SET feature_count = feature_count - 1 WHERE table_name = 'testNonSpatial'; END </td></tr></table></div></div>""" # GDAL 2.3.0 expected_html_3 = """<div class="section"><h2>General info</h2><div><table><tr><td>Relation type: </td><td>Table </td></tr><tr><td>Rows: </td><td>1 </td></tr></table></div></div><div class="section"><h2>Fields</h2><div><table class="header"><tr><th># </th><th>Name </th><th>Type </th><th>Null </th><th>Default </th></tr><tr><td>0 </td><td class="underline">fid </td><td>INTEGER </td><td>N </td><td> </td></tr><tr><td>1 </td><td>text_field </td><td>TEXT </td><td>Y </td><td> </td></tr></table></div></div><div class="section"><h2>Triggers</h2><div><table class="header"><tr><th>Name </th><th>Function </th></tr><tr><td>trigger_insert_feature_count_testNonSpatial (<a href="action:trigger/trigger_insert_feature_count_testNonSpatial/delete">delete</a>) </td><td>CREATE TRIGGER "trigger_insert_feature_count_testNonSpatial" AFTER INSERT ON "testNonSpatial" BEGIN UPDATE gpkg_ogr_contents SET feature_count = feature_count + 1 WHERE lower(table_name) = lower('testNonSpatial'); END </td></tr><tr><td>trigger_delete_feature_count_testNonSpatial (<a href="action:trigger/trigger_delete_feature_count_testNonSpatial/delete">delete</a>) </td><td>CREATE TRIGGER "trigger_delete_feature_count_testNonSpatial" AFTER DELETE ON "testNonSpatial" BEGIN UPDATE gpkg_ogr_contents SET feature_count = feature_count - 1 WHERE lower(table_name) = lower('testNonSpatial'); END </td></tr></table></div></div>""" self.assertIn(info.toHtml(), [expected_html, expected_html_2, expected_html_3], info.toHtml()) connection.remove()
def testListLayer(self): connection_name = 'testListLayer' plugin = createDbPlugin('gpkg') uri = QgsDataSourceUri() uri.setDatabase(self.test_gpkg) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin('gpkg', connection_name) connection.connect() db = connection.database() self.assertIsNotNone(db) tables = db.tables() self.assertEqual(len(tables), 1) table = tables[0] self.assertEqual(table.name, 'testLayer') info = table.info() expected_html = """<div class="section"><h2>General info</h2><div><table><tr><td>Relation type: </td><td>Table </td></tr><tr><td>Rows: </td><td>1 </td></tr></table></div></div><div class="section"><h2>GeoPackage</h2><div><table><tr><td>Column: </td><td>geom </td></tr><tr><td>Geometry: </td><td>LINESTRING </td></tr><tr><td>Dimension: </td><td>XY </td></tr><tr><td>Spatial ref: </td><td>Undefined (-1) </td></tr><tr><td>Extent: </td><td>1.00000, 2.00000 - 3.00000, 4.00000 </td></tr></table><p><warning> No spatial index defined (<a href="action:spatialindex/create">create it</a>)</p></div></div><div class="section"><h2>Fields</h2><div><table class="header"><tr><th># </th><th>Name </th><th>Type </th><th>Null </th><th>Default </th></tr><tr><td>0 </td><td class="underline">fid </td><td>INTEGER </td><td>Y </td><td> </td></tr><tr><td>1 </td><td>geom </td><td>LINESTRING </td><td>Y </td><td> </td></tr><tr><td>2 </td><td>text_field </td><td>TEXT </td><td>Y </td><td> </td></tr></table></div></div>""" self.assertEqual(info.toHtml(), expected_html, info.toHtml()) connection.remove()
def testAllGeometryTypes(self): connection_name = 'testAllGeometryTypes' plugin = createDbPlugin('gpkg') uri = QgsDataSourceUri() test_gpkg = os.path.join(self.basetestpath, 'testAllGeometryTypes.gpkg') ds = ogr.GetDriverByName('GPKG').CreateDataSource(test_gpkg) ds.CreateLayer('testPoint', geom_type=ogr.wkbPoint) ds.CreateLayer('testLineString', geom_type=ogr.wkbLineString) ds.CreateLayer('testPolygon', geom_type=ogr.wkbPolygon) ds.CreateLayer('testMultiPoint', geom_type=ogr.wkbMultiPoint) ds.CreateLayer('testMultiLineString', geom_type=ogr.wkbMultiLineString) ds.CreateLayer('testMultiPolygon', geom_type=ogr.wkbMultiPolygon) ds.CreateLayer('testGeometryCollection', geom_type=ogr.wkbGeometryCollection) if int(gdal.VersionInfo('VERSION_NUM')) >= GDAL_COMPUTE_VERSION(2, 0, 0): ds.CreateLayer('testCircularString', geom_type=ogr.wkbCircularString) ds.CreateLayer('testCompoundCurve', geom_type=ogr.wkbCompoundCurve) ds.CreateLayer('testCurvePolygon', geom_type=ogr.wkbCurvePolygon) ds.CreateLayer('testMultiCurve', geom_type=ogr.wkbMultiCurve) ds.CreateLayer('testMultiSurface', geom_type=ogr.wkbMultiSurface) ds = None uri.setDatabase(test_gpkg) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin('gpkg', connection_name) connection.connect() db = connection.database() self.assertIsNotNone(db) tables = db.tables() for i in range(len(tables)): table = tables[i] info = table.info() connection.remove()
def testConnect(self): # create a PKI postgis connection # that will be listed in postgis connection of db_manager self.setUpProvider(self.auth_config.id()) plugin = createDbPlugin('postgis') connections = plugin.connections() self.assertEqual(len(connections), 1) # test connection postgisConnPlugin = connections[0] self.assertTrue(postgisConnPlugin.connect()) # test removing connection self.assertTrue(postgisConnPlugin.remove()) self.assertEqual(len(plugin.connections()), 0) # test without connection params => fail connection = createDbPlugin('postgis', 'conn name') connection_succeeded = False try: connection.connect() connection_succeeded = True except: pass self.assertFalse(connection_succeeded, 'exception should have been raised')
def testAllGeometryTypes(self): connection_name = 'testAllGeometryTypes' plugin = createDbPlugin('spatialite') uri = QgsDataSourceUri() test_spatialite = os.path.join(self.basetestpath, 'testAllGeometryTypes.spatialite') ds = ogr.GetDriverByName('SQLite').CreateDataSource(test_spatialite) ds.CreateLayer('testPoint', geom_type=ogr.wkbPoint) ds.CreateLayer('testLineString', geom_type=ogr.wkbLineString) ds.CreateLayer('testPolygon', geom_type=ogr.wkbPolygon) ds.CreateLayer('testMultiPoint', geom_type=ogr.wkbMultiPoint) ds.CreateLayer('testMultiLineString', geom_type=ogr.wkbMultiLineString) ds.CreateLayer('testMultiPolygon', geom_type=ogr.wkbMultiPolygon) ds.CreateLayer('testGeometryCollection', geom_type=ogr.wkbGeometryCollection) ds.CreateLayer('testCircularString', geom_type=ogr.wkbCircularString) ds.CreateLayer('testCompoundCurve', geom_type=ogr.wkbCompoundCurve) ds.CreateLayer('testCurvePolygon', geom_type=ogr.wkbCurvePolygon) ds.CreateLayer('testMultiCurve', geom_type=ogr.wkbMultiCurve) ds.CreateLayer('testMultiSurface', geom_type=ogr.wkbMultiSurface) ds = None uri.setDatabase(test_spatialite) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin('spatialite', connection_name) connection.connect() db = connection.database() self.assertIsNotNone(db) # tables = db.tables() # for i in range(len(tables)): # table = tables[i] # info = table.info() connection.remove()
def testConnect(self): connection_name = 'testConnect' plugin = createDbPlugin('gpkg') uri = QgsDataSourceUri() uri.setDatabase(self.test_gpkg) self.assertTrue(plugin.addConnection(connection_name, uri)) connections = plugin.connections() self.assertEqual(len(connections), 1) connection = createDbPlugin('gpkg', connection_name + '_does_not_exist') connection_succeeded = False try: connection.connect() connection_succeeded = True except: pass self.assertFalse(connection_succeeded, 'exception should have been raised') connection = connections[0] connection.connect() connection.reconnect() connection.remove() self.assertEqual(len(plugin.connections()), 0) connection = createDbPlugin('gpkg', connection_name) connection_succeeded = False try: connection.connect() connection_succeeded = True except: pass self.assertFalse(connection_succeeded, 'exception should have been raised')
def testTableDataModel(self): connection_name = 'testTableDataModel' plugin = createDbPlugin('gpkg') uri = QgsDataSourceUri() uri.setDatabase(self.test_gpkg) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin('gpkg', connection_name) connection.connect() db = connection.database() self.assertIsNotNone(db) tables = db.tables() self.assertEqual(len(tables), 1) table = tables[0] self.assertEqual(table.name, 'testLayer') model = table.tableDataModel(None) self.assertEqual(model.rowCount(), 1) self.assertEqual(model.getData(0, 0), 1) # fid self.assertEqual(model.getData(0, 1), 'LINESTRING (1 2,3 4)') self.assertEqual(model.getData(0, 2), 'foo') connection.remove()
def testCreateRenameDeleteTable(self): connection_name = "testCreateRenameDeleteTable" plugin = createDbPlugin("gpkg") uri = QgsDataSourceUri() test_gpkg_new = os.path.join(self.basetestpath, "testCreateRenameDeleteTable.gpkg") shutil.copy(self.test_gpkg, test_gpkg_new) uri.setDatabase(test_gpkg_new) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin("gpkg", connection_name) connection.connect() db = connection.database() self.assertIsNotNone(db) tables = db.tables() self.assertEqual(len(tables), 1) table = tables[0] self.assertTrue(table.rename("newName")) self.assertEqual(table.name, "newName") connection.reconnect() db = connection.database() tables = db.tables() self.assertEqual(len(tables), 1) table = tables[0] self.assertEqual(table.name, "newName") fields = [] geom = ["geometry", "POINT", 4326, 3] field1 = TableField(table) field1.name = "fid" field1.dataType = "INTEGER" field1.notNull = True field1.primaryKey = True field2 = TableField(table) field2.name = "str_field" field2.dataType = "TEXT" field2.modifier = 20 fields = [field1, field2] self.assertTrue(db.createVectorTable("newName2", fields, geom)) tables = db.tables() self.assertEqual(len(tables), 2) new_table = tables[1] self.assertEqual(new_table.name, "newName2") fields = new_table.fields() self.assertEqual(len(fields), 3) self.assertFalse(new_table.hasSpatialIndex()) self.assertTrue(new_table.createSpatialIndex()) self.assertTrue(new_table.hasSpatialIndex()) self.assertTrue(new_table.delete()) tables = db.tables() self.assertEqual(len(tables), 1) connection.remove()
def testCreateRenameDeleteTable(self): connection_name = 'testCreateRenameDeleteTable' plugin = createDbPlugin('gpkg') uri = QgsDataSourceUri() test_gpkg_new = os.path.join(self.basetestpath, 'testCreateRenameDeleteTable.gpkg') shutil.copy(self.test_gpkg, test_gpkg_new) uri.setDatabase(test_gpkg_new) self.assertTrue(plugin.addConnection(connection_name, uri)) connection = createDbPlugin('gpkg', connection_name) connection.connect() db = connection.database() self.assertIsNotNone(db) tables = db.tables() self.assertEqual(len(tables), 1) table = tables[0] self.assertTrue(table.rename('newName')) self.assertEqual(table.name, 'newName') connection.reconnect() db = connection.database() tables = db.tables() self.assertEqual(len(tables), 1) table = tables[0] self.assertEqual(table.name, 'newName') fields = [] geom = ['geometry', 'POINT', 4326, 3] field1 = TableField(table) field1.name = 'fid' field1.dataType = 'INTEGER' field1.notNull = True field1.primaryKey = True field2 = TableField(table) field2.name = 'str_field' field2.dataType = 'TEXT' field2.modifier = 20 fields = [field1, field2] self.assertTrue(db.createVectorTable('newName2', fields, geom)) tables = db.tables() self.assertEqual(len(tables), 2) new_table = tables[1] self.assertEqual(new_table.name, 'newName2') fields = new_table.fields() self.assertEqual(len(fields), 3) self.assertFalse(new_table.hasSpatialIndex()) self.assertTrue(new_table.createSpatialIndex()) self.assertTrue(new_table.hasSpatialIndex()) self.assertTrue(new_table.delete()) tables = db.tables() self.assertEqual(len(tables), 1) connection.remove()
def testCreateDbPlugin(self): plugin = createDbPlugin('gpkg') self.assertIsNotNone(plugin)
def testCreateDbPlugin(self): plugin = createDbPlugin('spatialite') self.assertIsNotNone(plugin)
def testCreateDbPlugin(self): plugin = createDbPlugin('postgis') self.assertIsNotNone(plugin)