def test_model_allow_empty(self):
        """Test model with empty entry"""
        model = QgsProviderConnectionModel('ogr')
        self.assertEqual(model.rowCount(), 0)
        model.setAllowEmptyConnection(True)
        self.assertEqual(model.rowCount(), 1)
        self.assertFalse(model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole))
        self.assertTrue(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleEmpty))
        md = QgsProviderRegistry.instance().providerMetadata('ogr')
        conn = md.createConnection(self.gpkg_path, {})
        md.saveConnection(conn, 'qgis_test1')

        model.setAllowEmptyConnection(False)
        model.setAllowEmptyConnection(False)

        self.assertEqual(model.rowCount(), 1)
        self.assertEqual(model.columnCount(), 1)
        self.assertEqual(model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole), 'qgis_test1')
        self.assertEqual(model.data(model.index(0, 0, QModelIndex()), Qt.ToolTipRole), self.gpkg_path)
        self.assertEqual(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleConnectionName), 'qgis_test1')
        self.assertEqual(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleUri), self.gpkg_path)
        self.assertEqual(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleConfiguration), {})
        self.assertFalse(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleEmpty))

        model.setAllowEmptyConnection(True)
        model.setAllowEmptyConnection(True)
        self.assertEqual(model.rowCount(), 2)
        self.assertFalse(model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole))
        self.assertFalse(model.data(model.index(0, 0, QModelIndex()), Qt.ToolTipRole))
        self.assertFalse(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleConnectionName))
        self.assertFalse(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleUri))
        self.assertFalse(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleConfiguration))
        self.assertTrue(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleEmpty))
        self.assertEqual(model.data(model.index(1, 0, QModelIndex()), Qt.DisplayRole), 'qgis_test1')
        self.assertEqual(model.data(model.index(1, 0, QModelIndex()), Qt.ToolTipRole), self.gpkg_path)
        self.assertEqual(model.data(model.index(1, 0, QModelIndex()), QgsProviderConnectionModel.RoleConnectionName), 'qgis_test1')
        self.assertEqual(model.data(model.index(1, 0, QModelIndex()), QgsProviderConnectionModel.RoleUri), self.gpkg_path)
        self.assertEqual(model.data(model.index(1, 0, QModelIndex()), QgsProviderConnectionModel.RoleConfiguration), {})
        self.assertFalse(model.data(model.index(1, 0, QModelIndex()), QgsProviderConnectionModel.RoleEmpty))

        md.saveConnection(conn, 'qgis_test1')
        self.assertEqual(model.rowCount(), 2)
        self.assertFalse(model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole))
        self.assertTrue(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleEmpty))
        self.assertEqual(model.data(model.index(1, 0, QModelIndex()), Qt.DisplayRole), 'qgis_test1')
        self.assertFalse(model.data(model.index(1, 0, QModelIndex()), QgsProviderConnectionModel.RoleEmpty))

        model.setAllowEmptyConnection(False)
        self.assertEqual(model.rowCount(), 1)
        self.assertEqual(model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole), 'qgis_test1')
        self.assertFalse(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleEmpty))
        model.setAllowEmptyConnection(True)

        conn2 = md.createConnection(self.gpkg_path2, {})
        md.saveConnection(conn2, 'qgis_test2')
        self.assertEqual(model.rowCount(), 3)
        self.assertFalse(model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole))
        self.assertTrue(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleEmpty))
        self.assertEqual(model.data(model.index(1, 0, QModelIndex()), Qt.DisplayRole), 'qgis_test1')
        self.assertEqual(model.data(model.index(1, 0, QModelIndex()), Qt.ToolTipRole), self.gpkg_path)
        self.assertEqual(model.data(model.index(1, 0, QModelIndex()), QgsProviderConnectionModel.RoleConnectionName), 'qgis_test1')
        self.assertEqual(model.data(model.index(1, 0, QModelIndex()), QgsProviderConnectionModel.RoleUri), self.gpkg_path)
        self.assertFalse(model.data(model.index(1, 0, QModelIndex()), QgsProviderConnectionModel.RoleEmpty))
        self.assertEqual(model.data(model.index(2, 0, QModelIndex()), Qt.DisplayRole), 'qgis_test2')
        self.assertEqual(model.data(model.index(2, 0, QModelIndex()), Qt.ToolTipRole), self.gpkg_path2)
        self.assertEqual(model.data(model.index(2, 0, QModelIndex()), QgsProviderConnectionModel.RoleConnectionName), 'qgis_test2')
        self.assertEqual(model.data(model.index(2, 0, QModelIndex()), QgsProviderConnectionModel.RoleUri), self.gpkg_path2)
        self.assertFalse(model.data(model.index(2, 0, QModelIndex()), QgsProviderConnectionModel.RoleEmpty))

        model.setAllowEmptyConnection(False)
        self.assertEqual(model.rowCount(), 2)
        self.assertEqual(model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole), 'qgis_test1')
        self.assertEqual(model.data(model.index(1, 0, QModelIndex()), Qt.DisplayRole), 'qgis_test2')
        model.setAllowEmptyConnection(True)

        md.deleteConnection('qgis_test1')
        self.assertEqual(model.rowCount(), 2)
        self.assertFalse(model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole))
        self.assertEqual(model.data(model.index(1, 0, QModelIndex()), Qt.DisplayRole), 'qgis_test2')

        model.setAllowEmptyConnection(False)
        self.assertEqual(model.rowCount(), 1)
        self.assertEqual(model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole), 'qgis_test2')
Exemplo n.º 2
0
    def test_model(self):
        """Test model functionality"""

        md = QgsProviderRegistry.instance().providerMetadata('ogr')
        conn = md.createConnection(self.gpkg_path, {})
        md.saveConnection(conn, 'qgis_test1')

        model = QgsProviderConnectionModel('ogr')
        self.assertEqual(model.rowCount(), 1)
        self.assertEqual(model.columnCount(), 1)
        self.assertEqual(
            model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole),
            'qgis_test1')
        self.assertEqual(
            model.data(model.index(0, 0, QModelIndex()), Qt.ToolTipRole),
            self.gpkg_path)
        self.assertEqual(
            model.data(model.index(0, 0, QModelIndex()),
                       QgsProviderConnectionModel.RoleConnectionName),
            'qgis_test1')
        self.assertEqual(
            model.data(model.index(0, 0, QModelIndex()),
                       QgsProviderConnectionModel.RoleUri), self.gpkg_path)
        self.assertEqual(
            model.data(model.index(0, 0, QModelIndex()),
                       QgsProviderConnectionModel.RoleConfiguration), {})

        md.saveConnection(conn, 'qgis_test1')
        self.assertEqual(model.rowCount(), 1)
        self.assertEqual(
            model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole),
            'qgis_test1')

        conn2 = md.createConnection(self.gpkg_path2, {})
        md.saveConnection(conn2, 'qgis_test2')
        self.assertEqual(model.rowCount(), 2)
        self.assertEqual(
            model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole),
            'qgis_test1')
        self.assertEqual(
            model.data(model.index(0, 0, QModelIndex()), Qt.ToolTipRole),
            self.gpkg_path)
        self.assertEqual(
            model.data(model.index(0, 0, QModelIndex()),
                       QgsProviderConnectionModel.RoleConnectionName),
            'qgis_test1')
        self.assertEqual(
            model.data(model.index(0, 0, QModelIndex()),
                       QgsProviderConnectionModel.RoleUri), self.gpkg_path)
        self.assertEqual(
            model.data(model.index(1, 0, QModelIndex()), Qt.DisplayRole),
            'qgis_test2')
        self.assertEqual(
            model.data(model.index(1, 0, QModelIndex()), Qt.ToolTipRole),
            self.gpkg_path2)
        self.assertEqual(
            model.data(model.index(1, 0, QModelIndex()),
                       QgsProviderConnectionModel.RoleConnectionName),
            'qgis_test2')
        self.assertEqual(
            model.data(model.index(1, 0, QModelIndex()),
                       QgsProviderConnectionModel.RoleUri), self.gpkg_path2)

        md.deleteConnection('qgis_test1')
        self.assertEqual(model.rowCount(), 1)
        self.assertEqual(
            model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole),
            'qgis_test2')

        md.deleteConnection('qgis_test2')