Esempio n. 1
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 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()
Esempio n. 3
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. 4
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. 5
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. 6
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()