"Fossies" - the Fresh Open Source Software Archive

Member "QGIS-final-3_14_16/tests/src/python/test_qgsproviderconnection_ogr_gpkg.py" (11 Sep 2020, 5245 Bytes) of package /linux/misc/QGIS-final-3_14_16.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_qgsproviderconnection_ogr_gpkg.py": 3_14_15_vs_3_14_16.

    1 # -*- coding: utf-8 -*-
    2 """QGIS Unit tests for OGR GeoPackage QgsAbastractProviderConnection API.
    3 
    4 .. note:: This program is free software; you can redistribute it and/or modify
    5 it under the terms of the GNU General Public License as published by
    6 the Free Software Foundation; either version 2 of the License, or
    7 (at your option) any later version.
    8 
    9 """
   10 __author__ = 'Alessandro Pasotti'
   11 __date__ = '10/08/2019'
   12 __copyright__ = 'Copyright 2019, The QGIS Project'
   13 # This will get replaced with a git SHA1 when you do a git archive
   14 __revision__ = 'a235a149f0864a22dc99fed5f6267e2b586b8e57'
   15 
   16 import os
   17 import shutil
   18 from test_qgsproviderconnection_base import TestPyQgsProviderConnectionBase
   19 from qgis.core import (
   20     QgsWkbTypes,
   21     QgsAbstractDatabaseProviderConnection,
   22     QgsProviderConnectionException,
   23     QgsVectorLayer,
   24     QgsRasterLayer,
   25     QgsProviderRegistry,
   26     QgsFields,
   27     QgsCoordinateReferenceSystem,
   28 )
   29 from qgis.testing import unittest
   30 from utilities import unitTestDataPath
   31 
   32 TEST_DATA_DIR = unitTestDataPath()
   33 
   34 
   35 class TestPyQgsProviderConnectionGpkg(unittest.TestCase, TestPyQgsProviderConnectionBase):
   36 
   37     # Provider test cases must define the string URI for the test
   38     uri = ''
   39     # Provider test cases must define the provider name (e.g. "postgres" or "ogr")
   40     providerKey = 'ogr'
   41 
   42     @classmethod
   43     def setUpClass(cls):
   44         """Run before all tests"""
   45         TestPyQgsProviderConnectionBase.setUpClass()
   46         gpkg_original_path = '{}/qgis_server/test_project_wms_grouped_layers.gpkg'.format(TEST_DATA_DIR)
   47         cls.gpkg_path = '{}/qgis_server/test_project_wms_grouped_layers_test.gpkg'.format(TEST_DATA_DIR)
   48         shutil.copy(gpkg_original_path, cls.gpkg_path)
   49         vl = QgsVectorLayer('{}|layername=cdb_lines'.format(cls.gpkg_path), 'test', 'ogr')
   50         assert vl.isValid()
   51         cls.uri = cls.gpkg_path
   52 
   53     @classmethod
   54     def tearDownClass(cls):
   55         """Run after all tests"""
   56         os.unlink(cls.gpkg_path)
   57 
   58     def test_gpkg_connections_from_uri(self):
   59         """Create a connection from a layer uri and retrieve it"""
   60 
   61         md = QgsProviderRegistry.instance().providerMetadata('ogr')
   62         vl = QgsVectorLayer('{}|layername=cdb_lines'.format(self.gpkg_path), 'test', 'ogr')
   63         conn = md.createConnection(vl.dataProvider().uri().uri(), {})
   64         self.assertEqual(conn.uri(), self.gpkg_path)
   65 
   66     def test_gpkg_table_uri(self):
   67         """Create a connection from a layer uri and create a table URI"""
   68 
   69         md = QgsProviderRegistry.instance().providerMetadata('ogr')
   70         conn = md.createConnection(self.uri, {})
   71         self.assertEqual(conn.tableUri('', 'cdb_lines'), '{}|layername=cdb_lines'.format(self.gpkg_path))
   72         vl = QgsVectorLayer(conn.tableUri('', 'cdb_lines'), 'lines', 'ogr')
   73         self.assertTrue(vl.isValid())
   74 
   75         # Test table(), throws if not found
   76         table_info = conn.table('', 'osm')
   77         table_info = conn.table('', 'cdb_lines')
   78 
   79         self.assertEqual(conn.tableUri('', 'osm'), "GPKG:%s:osm" % self.uri)
   80         rl = QgsRasterLayer(conn.tableUri('', 'osm'), 'r', 'gdal')
   81         self.assertTrue(rl.isValid())
   82 
   83     def test_gpkg_connections(self):
   84         """Create some connections and retrieve them"""
   85 
   86         md = QgsProviderRegistry.instance().providerMetadata('ogr')
   87 
   88         conn = md.createConnection(self.uri, {})
   89         md.saveConnection(conn, 'qgis_test1')
   90 
   91         # Retrieve capabilities
   92         capabilities = conn.capabilities()
   93         self.assertTrue(bool(capabilities & QgsAbstractDatabaseProviderConnection.Tables))
   94         self.assertFalse(bool(capabilities & QgsAbstractDatabaseProviderConnection.Schemas))
   95         self.assertTrue(bool(capabilities & QgsAbstractDatabaseProviderConnection.CreateVectorTable))
   96         self.assertTrue(bool(capabilities & QgsAbstractDatabaseProviderConnection.DropVectorTable))
   97         self.assertTrue(bool(capabilities & QgsAbstractDatabaseProviderConnection.RenameVectorTable))
   98         self.assertFalse(bool(capabilities & QgsAbstractDatabaseProviderConnection.RenameRasterTable))
   99 
  100         crs = QgsCoordinateReferenceSystem.fromEpsgId(3857)
  101         typ = QgsWkbTypes.LineString
  102         conn.createVectorTable('', 'myNewAspatialTable', QgsFields(), QgsWkbTypes.NoGeometry, crs, True, {})
  103         conn.createVectorTable('', 'myNewTable', QgsFields(), typ, crs, True, {})
  104 
  105         # Check filters and special cases
  106         table_names = self._table_names(conn.tables('', QgsAbstractDatabaseProviderConnection.Raster))
  107         self.assertTrue('osm' in table_names)
  108         self.assertFalse('myNewTable' in table_names)
  109         self.assertFalse('myNewAspatialTable' in table_names)
  110 
  111         table_names = self._table_names(conn.tables('', QgsAbstractDatabaseProviderConnection.View))
  112         self.assertFalse('osm' in table_names)
  113         self.assertFalse('myNewTable' in table_names)
  114         self.assertFalse('myNewAspatialTable' in table_names)
  115 
  116         table_names = self._table_names(conn.tables('', QgsAbstractDatabaseProviderConnection.Aspatial))
  117         self.assertFalse('osm' in table_names)
  118         self.assertFalse('myNewTable' in table_names)
  119         self.assertTrue('myNewAspatialTable' in table_names)
  120 
  121 
  122 if __name__ == '__main__':
  123     unittest.main()