Ejemplo n.º 1
0
 def testModelSpecificSchema(self):
     conn = QgsProviderRegistry.instance().providerMetadata(
         'postgres').createConnection(self.uri, {})
     self.assertTrue(conn)
     model = QgsDatabaseTableModel(conn, 'qgis_test')
     self.assertGreaterEqual(model.rowCount(), 3)
     old_count = model.rowCount()
     self.assertEqual(model.columnCount(), 1)
     tables = [
         model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
         for r in range(model.rowCount())
     ]
     self.assertIn('someData', tables)
     self.assertIn('some_poly_data', tables)
     self.assertNotIn('attributes', tables)
     self.assertEqual(
         model.data(model.index(tables.index('someData'), 0, QModelIndex()),
                    QgsDatabaseTableModel.RoleTableName), 'someData')
     self.assertEqual(
         model.data(model.index(tables.index('someData'), 0, QModelIndex()),
                    QgsDatabaseTableModel.RoleSchema), 'qgis_test')
     self.assertEqual(
         model.data(model.index(tables.index('someData'), 0, QModelIndex()),
                    QgsDatabaseTableModel.RoleComment), 'QGIS Test Table')
     self.assertEqual(
         model.data(model.index(tables.index('someData'), 0, QModelIndex()),
                    QgsDatabaseTableModel.RoleCrs),
         QgsCoordinateReferenceSystem('EPSG:4326'))
     self.assertEqual(
         model.data(model.index(tables.index('someData'), 0, QModelIndex()),
                    QgsDatabaseTableModel.RoleCustomInfo), {})
     self.assertEqual(
         model.data(model.index(tables.index('someData'), 0, QModelIndex()),
                    QgsDatabaseTableModel.RoleTableFlags), 4)
     self.assertEqual(
         model.data(model.index(tables.index('someData'), 0, QModelIndex()),
                    QgsDatabaseTableModel.RoleWkbType), QgsWkbTypes.Point)
     self.assertEqual(
         model.data(
             model.index(tables.index('some_poly_data'), 0, QModelIndex()),
             QgsDatabaseTableModel.RoleWkbType), QgsWkbTypes.Polygon)
     self.assertIsNone(
         model.data(model.index(model.rowCount(), 0, QModelIndex()),
                    Qt.DisplayRole))
    def testModel(self):
        conn = QgsProviderRegistry.instance().providerMetadata(
            'postgres').createConnection(self.uri, {})
        self.assertTrue(conn)
        model = QgsDatabaseTableModel(conn)
        self.assertGreaterEqual(model.rowCount(), 3)
        old_count = model.rowCount()
        self.assertEqual(model.columnCount(), 1)
        tables = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('qgis_test.someData', tables)
        self.assertIn('qgis_test.some_poly_data', tables)
        self.assertIn('information_schema.attributes', tables)
        self.assertEqual(
            model.data(
                model.index(tables.index('qgis_test.someData'), 0,
                            QModelIndex()),
                QgsDatabaseTableModel.RoleTableName), 'someData')
        self.assertEqual(
            model.data(
                model.index(tables.index('qgis_test.someData'), 0,
                            QModelIndex()), QgsDatabaseTableModel.RoleSchema),
            'qgis_test')
        self.assertEqual(
            model.data(
                model.index(tables.index('qgis_test.someData'), 0,
                            QModelIndex()), QgsDatabaseTableModel.RoleComment),
            'QGIS Test Table')
        self.assertEqual(
            model.data(
                model.index(tables.index('qgis_test.someData'), 0,
                            QModelIndex()), QgsDatabaseTableModel.RoleCrs),
            QgsCoordinateReferenceSystem('EPSG:4326'))
        self.assertEqual(
            model.data(
                model.index(tables.index('qgis_test.someData'), 0,
                            QModelIndex()),
                QgsDatabaseTableModel.RoleCustomInfo), {})
        self.assertEqual(
            model.data(
                model.index(tables.index('qgis_test.someData'), 0,
                            QModelIndex()),
                QgsDatabaseTableModel.RoleTableFlags), 4)
        self.assertEqual(
            model.data(
                model.index(tables.index('qgis_test.someData'), 0,
                            QModelIndex()), QgsDatabaseTableModel.RoleWkbType),
            QgsWkbTypes.Point)
        self.assertEqual(
            model.data(
                model.index(tables.index('qgis_test.some_poly_data'), 0,
                            QModelIndex()), QgsDatabaseTableModel.RoleWkbType),
            QgsWkbTypes.Polygon)
        self.assertIsNone(
            model.data(model.index(model.rowCount(), 0, QModelIndex()),
                       Qt.DisplayRole))

        model.refresh()
        self.assertEqual(model.rowCount(), old_count)

        fields = QgsFields()
        fields.append(QgsField('test', QVariant.String))
        conn.createVectorTable('qgis_test', 'myNewTable', fields,
                               QgsWkbTypes.Point,
                               QgsCoordinateReferenceSystem('EPSG:3857'),
                               False, {})
        self.assertEqual(model.rowCount(), old_count)
        model.refresh()
        self.assertEqual(model.rowCount(), old_count + 1)
        tables = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('qgis_test.someData', tables)
        self.assertIn('qgis_test.some_poly_data', tables)
        self.assertIn('information_schema.attributes', tables)
        self.assertIn('qgis_test.myNewTable', tables)

        conn.createVectorTable('qgis_test', 'myNewTable2', fields,
                               QgsWkbTypes.Point,
                               QgsCoordinateReferenceSystem('EPSG:3857'),
                               False, {})
        conn.createVectorTable('qgis_test', 'myNewTable3', fields,
                               QgsWkbTypes.Point,
                               QgsCoordinateReferenceSystem('EPSG:3857'),
                               False, {})
        model.refresh()
        self.assertEqual(model.rowCount(), old_count + 3)
        tables = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('qgis_test.someData', tables)
        self.assertIn('qgis_test.some_poly_data', tables)
        self.assertIn('information_schema.attributes', tables)
        self.assertIn('qgis_test.myNewTable', tables)
        self.assertIn('qgis_test.myNewTable2', tables)
        self.assertIn('qgis_test.myNewTable3', tables)

        conn.createVectorTable('qgis_test', 'myNewTable4', fields,
                               QgsWkbTypes.Point,
                               QgsCoordinateReferenceSystem('EPSG:3857'),
                               False, {})
        conn.dropVectorTable('qgis_test', 'myNewTable2')
        conn.dropVectorTable('qgis_test', 'myNewTable')
        model.refresh()
        self.assertEqual(model.rowCount(), old_count + 2)
        tables = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('qgis_test.someData', tables)
        self.assertIn('qgis_test.some_poly_data', tables)
        self.assertIn('information_schema.attributes', tables)
        self.assertNotIn('qgis_test.myNewTable', tables)
        self.assertNotIn('qgis_test.myNewTable2', tables)
        self.assertIn('qgis_test.myNewTable3', tables)
        self.assertIn('qgis_test.myNewTable4', tables)

        conn.dropVectorTable('qgis_test', 'myNewTable3')
        conn.dropVectorTable('qgis_test', 'myNewTable4')
        model.refresh()
        self.assertEqual(model.rowCount(), old_count)
        tables = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('qgis_test.someData', tables)
        self.assertIn('qgis_test.some_poly_data', tables)
        self.assertIn('information_schema.attributes', tables)
        self.assertNotIn('qgis_test.myNewTable', tables)
        self.assertNotIn('qgis_test.myNewTable2', tables)
        self.assertNotIn('qgis_test.myNewTable3', tables)
        self.assertNotIn('qgis_test.myNewTable4', tables)