Esempio n. 1
0
    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:&nbsp;</td><td>Table&nbsp;</td></tr><tr><td>Rows:&nbsp;</td><td>1&nbsp;</td></tr></table></div></div><div class="section"><h2>GeoPackage</h2><div><table><tr><td>Column:&nbsp;</td><td>geom&nbsp;</td></tr><tr><td>Geometry:&nbsp;</td><td>LINESTRING&nbsp;</td></tr><tr><td>Dimension:&nbsp;</td><td>XY&nbsp;</td></tr><tr><td>Spatial ref:&nbsp;</td><td>Undefined (-1)&nbsp;</td></tr><tr><td>Extent:&nbsp;</td><td>1.00000, 2.00000 - 3.00000, 4.00000&nbsp;</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>#&nbsp;</th><th>Name&nbsp;</th><th>Type&nbsp;</th><th>Null&nbsp;</th><th>Default&nbsp;</th></tr><tr><td>0&nbsp;</td><td class="underline">fid&nbsp;</td><td>INTEGER&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</td></tr><tr><td>1&nbsp;</td><td>geom&nbsp;</td><td>LINESTRING&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</td></tr><tr><td>2&nbsp;</td><td>text_field&nbsp;</td><td>TEXT&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</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:&nbsp;</td><td>Table&nbsp;</td></tr><tr><td>Rows:&nbsp;</td><td>1&nbsp;</td></tr></table></div></div><div class="section"><h2>GeoPackage</h2><div><table><tr><td>Column:&nbsp;</td><td>geom&nbsp;</td></tr><tr><td>Geometry:&nbsp;</td><td>LINESTRING&nbsp;</td></tr><tr><td>Dimension:&nbsp;</td><td>XY&nbsp;</td></tr><tr><td>Spatial ref:&nbsp;</td><td>Undefined (-1)&nbsp;</td></tr><tr><td>Extent:&nbsp;</td><td>1.00000, 2.00000 - 3.00000, 4.00000&nbsp;</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>#&nbsp;</th><th>Name&nbsp;</th><th>Type&nbsp;</th><th>Null&nbsp;</th><th>Default&nbsp;</th></tr><tr><td>0&nbsp;</td><td class="underline">fid&nbsp;</td><td>INTEGER&nbsp;</td><td>N&nbsp;</td><td>&nbsp;</td></tr><tr><td>1&nbsp;</td><td>geom&nbsp;</td><td>LINESTRING&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</td></tr><tr><td>2&nbsp;</td><td>text_field&nbsp;</td><td>TEXT&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</td></tr></table></div></div><div class="section"><h2>Triggers</h2><div><table class="header"><tr><th>Name&nbsp;</th><th>Function&nbsp;</th></tr><tr><td>trigger_insert_feature_count_testLayer (<a href="action:trigger/trigger_insert_feature_count_testLayer/delete">delete</a>)&nbsp;</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&nbsp;</td></tr><tr><td>trigger_delete_feature_count_testLayer (<a href="action:trigger/trigger_delete_feature_count_testLayer/delete">delete</a>)&nbsp;</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&nbsp;</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:&nbsp;</td><td>Table&nbsp;</td></tr><tr><td>Rows:&nbsp;</td><td>1&nbsp;</td></tr></table></div></div><div class="section"><h2>GeoPackage</h2><div><table><tr><td>Column:&nbsp;</td><td>geom&nbsp;</td></tr><tr><td>Geometry:&nbsp;</td><td>LINESTRING&nbsp;</td></tr><tr><td>Dimension:&nbsp;</td><td>XY&nbsp;</td></tr><tr><td>Spatial ref:&nbsp;</td><td>Undefined (-1)&nbsp;</td></tr><tr><td>Extent:&nbsp;</td><td>1.00000, 2.00000 - 3.00000, 4.00000&nbsp;</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>#&nbsp;</th><th>Name&nbsp;</th><th>Type&nbsp;</th><th>Null&nbsp;</th><th>Default&nbsp;</th></tr><tr><td>0&nbsp;</td><td class="underline">fid&nbsp;</td><td>INTEGER&nbsp;</td><td>N&nbsp;</td><td>&nbsp;</td></tr><tr><td>1&nbsp;</td><td>geom&nbsp;</td><td>LINESTRING&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</td></tr><tr><td>2&nbsp;</td><td>text_field&nbsp;</td><td>TEXT&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</td></tr></table></div></div><div class="section"><h2>Triggers</h2><div><table class="header"><tr><th>Name&nbsp;</th><th>Function&nbsp;</th></tr><tr><td>trigger_insert_feature_count_testLayer (<a href="action:trigger/trigger_insert_feature_count_testLayer/delete">delete</a>)&nbsp;</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&nbsp;</td></tr><tr><td>trigger_delete_feature_count_testLayer (<a href="action:trigger/trigger_delete_feature_count_testLayer/delete">delete</a>)&nbsp;</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&nbsp;</td></tr></table></div></div>"""

        self.assertIn(info.toHtml(), [expected_html, expected_html_2, expected_html_3])

        connection.remove()
Esempio n. 2
0
    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:&nbsp;</td><td>Table&nbsp;</td></tr><tr><td>Rows:&nbsp;</td><td>1&nbsp;</td></tr></table></div></div><div class="section"><h2>Fields</h2><div><table class="header"><tr><th>#&nbsp;</th><th>Name&nbsp;</th><th>Type&nbsp;</th><th>Null&nbsp;</th><th>Default&nbsp;</th></tr><tr><td>0&nbsp;</td><td class="underline">fid&nbsp;</td><td>INTEGER&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</td></tr><tr><td>1&nbsp;</td><td>text_field&nbsp;</td><td>TEXT&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</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)
Esempio n. 7
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()
Esempio n. 8
0
    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:&nbsp;</td><td>Table&nbsp;</td></tr><tr><td>Rows:&nbsp;</td><td>Unknown (<a href="action:rows/count">find out</a>)&nbsp;</td></tr></table></div></div><div class="section"><h2>GeoPackage</h2><div><table><tr><td>Column:&nbsp;</td><td>&nbsp;</td></tr><tr><td>Geometry:&nbsp;</td><td>RASTER&nbsp;</td></tr><tr><td>Spatial ref:&nbsp;</td><td>WGS 84 geodetic (4326)&nbsp;</td></tr><tr><td>Extent:&nbsp;</td><td>2.00000, 48.80000 - 2.20000, 49.00000&nbsp;</td></tr></table></div></div><div class="section"><h2>Fields</h2><div><table class="header"><tr><th>#&nbsp;</th><th>Name&nbsp;</th><th>Type&nbsp;</th><th>Null&nbsp;</th><th>Default&nbsp;</th></tr><tr><td>0&nbsp;</td><td class="underline">id&nbsp;</td><td>INTEGER&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</td></tr><tr><td>1&nbsp;</td><td>zoom_level&nbsp;</td><td>INTEGER&nbsp;</td><td>N&nbsp;</td><td>&nbsp;</td></tr><tr><td>2&nbsp;</td><td>tile_column&nbsp;</td><td>INTEGER&nbsp;</td><td>N&nbsp;</td><td>&nbsp;</td></tr><tr><td>3&nbsp;</td><td>tile_row&nbsp;</td><td>INTEGER&nbsp;</td><td>N&nbsp;</td><td>&nbsp;</td></tr><tr><td>4&nbsp;</td><td>tile_data&nbsp;</td><td>BLOB&nbsp;</td><td>N&nbsp;</td><td>&nbsp;</td></tr></table></div></div><div class="section"><h2>Indexes</h2><div><table class="header"><tr><th>Name&nbsp;</th><th>Column(s)&nbsp;</th></tr><tr><td>sqlite_autoindex_raster_table_1&nbsp;</td><td>zoom_level<br>tile_column<br>tile_row&nbsp;</td></tr></table></div></div>"""
        self.assertEqual(info.toHtml(), expected_html, info.toHtml())

        connection.remove()
Esempio n. 9
0
    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()
Esempio n. 10
0
    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:&nbsp;</td><td>Table&nbsp;</td></tr><tr><td>Rows:&nbsp;</td><td>1&nbsp;</td></tr></table></div></div><div class="section"><h2>Fields</h2><div><table class="header"><tr><th>#&nbsp;</th><th>Name&nbsp;</th><th>Type&nbsp;</th><th>Null&nbsp;</th><th>Default&nbsp;</th></tr><tr><td>0&nbsp;</td><td class="underline">fid&nbsp;</td><td>INTEGER&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</td></tr><tr><td>1&nbsp;</td><td>text_field&nbsp;</td><td>TEXT&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</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:&nbsp;</td><td>Table&nbsp;</td></tr><tr><td>Rows:&nbsp;</td><td>1&nbsp;</td></tr></table></div></div><div class="section"><h2>Fields</h2><div><table class="header"><tr><th>#&nbsp;</th><th>Name&nbsp;</th><th>Type&nbsp;</th><th>Null&nbsp;</th><th>Default&nbsp;</th></tr><tr><td>0&nbsp;</td><td class="underline">fid&nbsp;</td><td>INTEGER&nbsp;</td><td>N&nbsp;</td><td>&nbsp;</td></tr><tr><td>1&nbsp;</td><td>text_field&nbsp;</td><td>TEXT&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</td></tr></table></div></div><div class="section"><h2>Triggers</h2><div><table class="header"><tr><th>Name&nbsp;</th><th>Function&nbsp;</th></tr><tr><td>trigger_insert_feature_count_testNonSpatial (<a href="action:trigger/trigger_insert_feature_count_testNonSpatial/delete">delete</a>)&nbsp;</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&nbsp;</td></tr><tr><td>trigger_delete_feature_count_testNonSpatial (<a href="action:trigger/trigger_delete_feature_count_testNonSpatial/delete">delete</a>)&nbsp;</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&nbsp;</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:&nbsp;</td><td>Table&nbsp;</td></tr><tr><td>Rows:&nbsp;</td><td>1&nbsp;</td></tr></table></div></div><div class="section"><h2>Fields</h2><div><table class="header"><tr><th>#&nbsp;</th><th>Name&nbsp;</th><th>Type&nbsp;</th><th>Null&nbsp;</th><th>Default&nbsp;</th></tr><tr><td>0&nbsp;</td><td class="underline">fid&nbsp;</td><td>INTEGER&nbsp;</td><td>N&nbsp;</td><td>&nbsp;</td></tr><tr><td>1&nbsp;</td><td>text_field&nbsp;</td><td>TEXT&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</td></tr></table></div></div><div class="section"><h2>Triggers</h2><div><table class="header"><tr><th>Name&nbsp;</th><th>Function&nbsp;</th></tr><tr><td>trigger_insert_feature_count_testNonSpatial (<a href="action:trigger/trigger_insert_feature_count_testNonSpatial/delete">delete</a>)&nbsp;</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&nbsp;</td></tr><tr><td>trigger_delete_feature_count_testNonSpatial (<a href="action:trigger/trigger_delete_feature_count_testNonSpatial/delete">delete</a>)&nbsp;</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&nbsp;</td></tr></table></div></div>"""
        self.assertIn(info.toHtml(), [expected_html, expected_html_2, expected_html_3], info.toHtml())

        connection.remove()
Esempio n. 11
0
    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:&nbsp;</td><td>Table&nbsp;</td></tr><tr><td>Rows:&nbsp;</td><td>1&nbsp;</td></tr></table></div></div><div class="section"><h2>GeoPackage</h2><div><table><tr><td>Column:&nbsp;</td><td>geom&nbsp;</td></tr><tr><td>Geometry:&nbsp;</td><td>LINESTRING&nbsp;</td></tr><tr><td>Dimension:&nbsp;</td><td>XY&nbsp;</td></tr><tr><td>Spatial ref:&nbsp;</td><td>Undefined (-1)&nbsp;</td></tr><tr><td>Extent:&nbsp;</td><td>1.00000, 2.00000 - 3.00000, 4.00000&nbsp;</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>#&nbsp;</th><th>Name&nbsp;</th><th>Type&nbsp;</th><th>Null&nbsp;</th><th>Default&nbsp;</th></tr><tr><td>0&nbsp;</td><td class="underline">fid&nbsp;</td><td>INTEGER&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</td></tr><tr><td>1&nbsp;</td><td>geom&nbsp;</td><td>LINESTRING&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</td></tr><tr><td>2&nbsp;</td><td>text_field&nbsp;</td><td>TEXT&nbsp;</td><td>Y&nbsp;</td><td>&nbsp;</td></tr></table></div></div>"""
        self.assertEqual(info.toHtml(), expected_html, info.toHtml())

        connection.remove()
Esempio n. 12
0
    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()
Esempio n. 15
0
    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')
Esempio n. 16
0
    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()
Esempio n. 17
0
    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()
Esempio n. 18
0
    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()
Esempio n. 19
0
 def testCreateDbPlugin(self):
     plugin = createDbPlugin('gpkg')
     self.assertIsNotNone(plugin)
    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()
Esempio n. 21
0
 def testCreateDbPlugin(self):
     plugin = createDbPlugin('gpkg')
     self.assertIsNotNone(plugin)
Esempio n. 22
0
 def testCreateDbPlugin(self):
     plugin = createDbPlugin('spatialite')
     self.assertIsNotNone(plugin)
 def testCreateDbPlugin(self):
     plugin = createDbPlugin('postgis')
     self.assertIsNotNone(plugin)
 def testCreateDbPlugin(self):
     plugin = createDbPlugin('spatialite')
     self.assertIsNotNone(plugin)
Esempio n. 25
0
 def testCreateDbPlugin(self):
     plugin = createDbPlugin('postgis')
     self.assertIsNotNone(plugin)