Esempio n. 1
0
    def testModel(self):
        conn = QgsProviderRegistry.instance().providerMetadata(
            'postgres').createConnection(self.uri, {})
        self.assertTrue(conn)
        model = QgsDatabaseSchemaModel(conn)
        self.assertGreaterEqual(model.rowCount(), 3)
        old_count = model.rowCount()
        self.assertEqual(model.columnCount(), 1)
        schemas = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('public', schemas)
        self.assertIn('CamelCaseSchema', schemas)
        self.assertIn('qgis_test', schemas)
        self.assertEqual(
            model.data(
                model.index(schemas.index('qgis_test'), 0, QModelIndex()),
                Qt.ToolTipRole), 'qgis_test')
        self.assertIsNone(
            model.data(model.index(model.rowCount(), 0, QModelIndex()),
                       Qt.DisplayRole))

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

        conn.createSchema('myNewSchema')
        self.assertEqual(model.rowCount(), old_count)
        model.refresh()
        self.assertEqual(model.rowCount(), old_count + 1)
        schemas = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('public', schemas)
        self.assertIn('CamelCaseSchema', schemas)
        self.assertIn('qgis_test', schemas)
        self.assertIn('myNewSchema', schemas)

        conn.createSchema('myNewSchema2')
        conn.createSchema('myNewSchema3')
        model.refresh()
        self.assertEqual(model.rowCount(), old_count + 3)
        schemas = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('public', schemas)
        self.assertIn('CamelCaseSchema', schemas)
        self.assertIn('qgis_test', schemas)
        self.assertIn('myNewSchema', schemas)
        self.assertIn('myNewSchema2', schemas)
        self.assertIn('myNewSchema3', schemas)

        conn.createSchema('myNewSchema4')
        conn.dropSchema('myNewSchema2')
        conn.dropSchema('myNewSchema')
        model.refresh()
        self.assertEqual(model.rowCount(), old_count + 2)
        schemas = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('public', schemas)
        self.assertIn('CamelCaseSchema', schemas)
        self.assertIn('qgis_test', schemas)
        self.assertNotIn('myNewSchema', schemas)
        self.assertNotIn('myNewSchema2', schemas)
        self.assertIn('myNewSchema3', schemas)
        self.assertIn('myNewSchema4', schemas)

        conn.dropSchema('myNewSchema3')
        conn.dropSchema('myNewSchema4')
        model.refresh()
        self.assertEqual(model.rowCount(), old_count)
        schemas = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('public', schemas)
        self.assertIn('CamelCaseSchema', schemas)
        self.assertIn('qgis_test', schemas)
        self.assertNotIn('myNewSchema3', schemas)
        self.assertNotIn('myNewSchema4', schemas)
Esempio n. 2
0
    def test_model_allow_empty(self):
        """Test model with empty entry"""
        conn = QgsProviderRegistry.instance().providerMetadata(
            'postgres').createConnection(self.uri, {})
        self.assertTrue(conn)
        model = QgsDatabaseSchemaModel(conn)
        self.assertGreaterEqual(model.rowCount(), 3)
        old_count = model.rowCount()
        model.setAllowEmptySchema(True)
        self.assertEqual(model.rowCount(), old_count + 1)

        schemas = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('public', schemas)
        self.assertIn('CamelCaseSchema', schemas)
        self.assertIn('qgis_test', schemas)
        self.assertFalse(
            model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole))
        self.assertTrue(
            model.data(model.index(0, 0, QModelIndex()),
                       QgsDatabaseSchemaModel.RoleEmpty))
        self.assertFalse(
            model.data(
                model.index(schemas.index('qgis_test'), 0, QModelIndex()),
                QgsDatabaseSchemaModel.RoleEmpty))
        self.assertIsNone(
            model.data(model.index(model.rowCount(), 0, QModelIndex()),
                       Qt.DisplayRole))

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

        conn.createSchema('myNewSchema')
        self.assertEqual(model.rowCount(), old_count + 1)
        model.refresh()
        self.assertEqual(model.rowCount(), old_count + 2)
        schemas = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('public', schemas)
        self.assertIn('CamelCaseSchema', schemas)
        self.assertIn('qgis_test', schemas)
        self.assertIn('myNewSchema', schemas)
        self.assertFalse(
            model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole))
        self.assertTrue(
            model.data(model.index(0, 0, QModelIndex()),
                       QgsDatabaseSchemaModel.RoleEmpty))
        self.assertFalse(
            model.data(
                model.index(schemas.index('qgis_test'), 0, QModelIndex()),
                QgsDatabaseSchemaModel.RoleEmpty))

        model.setAllowEmptySchema(False)
        self.assertEqual(model.rowCount(), old_count + 1)
        self.assertTrue(
            model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole))
        self.assertFalse(
            model.data(model.index(0, 0, QModelIndex()),
                       QgsDatabaseSchemaModel.RoleEmpty))
        model.setAllowEmptySchema(True)
        self.assertEqual(model.rowCount(), old_count + 2)
        self.assertFalse(
            model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole))
        self.assertTrue(
            model.data(model.index(0, 0, QModelIndex()),
                       QgsDatabaseSchemaModel.RoleEmpty))
        self.assertFalse(
            model.data(
                model.index(schemas.index('qgis_test'), 0, QModelIndex()),
                QgsDatabaseSchemaModel.RoleEmpty))

        conn.createSchema('myNewSchema2')
        conn.createSchema('myNewSchema3')
        model.refresh()
        self.assertEqual(model.rowCount(), old_count + 4)
        schemas = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('public', schemas)
        self.assertIn('CamelCaseSchema', schemas)
        self.assertIn('qgis_test', schemas)
        self.assertIn('myNewSchema', schemas)
        self.assertIn('myNewSchema2', schemas)
        self.assertIn('myNewSchema3', schemas)
        self.assertFalse(
            model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole))
        self.assertTrue(
            model.data(model.index(0, 0, QModelIndex()),
                       QgsDatabaseSchemaModel.RoleEmpty))
        self.assertFalse(
            model.data(
                model.index(schemas.index('qgis_test'), 0, QModelIndex()),
                QgsDatabaseSchemaModel.RoleEmpty))

        conn.createSchema('myNewSchema4')
        conn.dropSchema('myNewSchema2')
        conn.dropSchema('myNewSchema')
        model.refresh()
        self.assertEqual(model.rowCount(), old_count + 3)
        schemas = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('public', schemas)
        self.assertIn('CamelCaseSchema', schemas)
        self.assertIn('qgis_test', schemas)
        self.assertNotIn('myNewSchema', schemas)
        self.assertNotIn('myNewSchema2', schemas)
        self.assertIn('myNewSchema3', schemas)
        self.assertIn('myNewSchema4', schemas)
        self.assertFalse(
            model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole))
        self.assertTrue(
            model.data(model.index(0, 0, QModelIndex()),
                       QgsDatabaseSchemaModel.RoleEmpty))
        self.assertFalse(
            model.data(
                model.index(schemas.index('qgis_test'), 0, QModelIndex()),
                QgsDatabaseSchemaModel.RoleEmpty))

        conn.dropSchema('myNewSchema3')
        conn.dropSchema('myNewSchema4')
        model.refresh()
        self.assertEqual(model.rowCount(), old_count + 1)
        schemas = [
            model.data(model.index(r, 0, QModelIndex()), Qt.DisplayRole)
            for r in range(model.rowCount())
        ]
        self.assertIn('public', schemas)
        self.assertIn('CamelCaseSchema', schemas)
        self.assertIn('qgis_test', schemas)
        self.assertNotIn('myNewSchema3', schemas)
        self.assertNotIn('myNewSchema4', schemas)
        self.assertFalse(
            model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole))
        self.assertTrue(
            model.data(model.index(0, 0, QModelIndex()),
                       QgsDatabaseSchemaModel.RoleEmpty))
        self.assertFalse(
            model.data(
                model.index(schemas.index('qgis_test'), 0, QModelIndex()),
                QgsDatabaseSchemaModel.RoleEmpty))

        model.setAllowEmptySchema(False)
        self.assertEqual(model.rowCount(), old_count)
        self.assertTrue(
            model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole))
        self.assertFalse(
            model.data(model.index(0, 0, QModelIndex()),
                       QgsDatabaseSchemaModel.RoleEmpty))