Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
412 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# coding=utf-8 | ||
""""Base test for provider style DB storage | ||
.. note:: This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
""" | ||
|
||
__author__ = 'elpaso@itopen.it' | ||
__date__ = '2022-11-07' | ||
__copyright__ = 'Copyright 2022, ItOpen' | ||
|
||
from qgis.testing import start_app, unittest | ||
from qgis.PyQt.QtCore import QCoreApplication, QVariant | ||
from qgis.PyQt.QtGui import QColor | ||
|
||
from qgis.core import ( | ||
QgsProviderRegistry, | ||
QgsSettings, | ||
QgsAbstractDatabaseProviderConnection, | ||
QgsField, | ||
QgsFields, | ||
QgsCoordinateReferenceSystem, | ||
QgsWkbTypes, | ||
QgsVectorLayer, | ||
) | ||
|
||
import time | ||
|
||
|
||
class StyleStorageTestCaseBase(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
"""Run before all tests""" | ||
|
||
QCoreApplication.setOrganizationName("QGIS_Test") | ||
QCoreApplication.setOrganizationDomain("%s.com" % __name__) | ||
QCoreApplication.setApplicationName(__name__) | ||
QgsSettings().clear() | ||
start_app() | ||
|
||
|
||
class StyleStorageTestBase(): | ||
|
||
def layerUri(self, conn, schema_name, table_name): | ||
"""Providers may override if they need more complex URI generation than | ||
what tableUri() offers""" | ||
|
||
return conn.tableUri(schema_name, table_name) | ||
|
||
def testMultipleStyles(self): | ||
|
||
md = QgsProviderRegistry.instance().providerMetadata(self.providerKey) | ||
|
||
conn = md.createConnection(self.uri, {}) | ||
md.saveConnection(conn, 'qgis_test1') | ||
|
||
schema = None | ||
capabilities = conn.capabilities() | ||
|
||
if (capabilities & QgsAbstractDatabaseProviderConnection.CreateSchema | ||
and capabilities & QgsAbstractDatabaseProviderConnection.Schemas | ||
and capabilities & QgsAbstractDatabaseProviderConnection.DropSchema): | ||
|
||
schema = 'testStyles' | ||
# Start clean | ||
if schema in conn.schemas(): | ||
conn.dropSchema(schema, True) | ||
|
||
# Create | ||
conn.createSchema(schema) | ||
schemas = conn.schemas() | ||
self.assertTrue(schema in schemas) | ||
|
||
fields = QgsFields() | ||
fields.append(QgsField("string_t", QVariant.String)) | ||
options = {} | ||
crs = QgsCoordinateReferenceSystem.fromEpsgId(4326) | ||
typ = QgsWkbTypes.Point | ||
|
||
# Create table | ||
conn.createVectorTable(schema, 'test_styles', fields, typ, crs, True, options) | ||
uri = self.layerUri(conn, schema, 'test_styles') | ||
|
||
vl = QgsVectorLayer(uri, 'vl', self.providerKey) | ||
self.assertTrue(vl.isValid()) | ||
renderer = vl.renderer() | ||
symbol = renderer.symbol().clone() | ||
symbol.setColor(QColor('#ff0000')) | ||
renderer.setSymbol(symbol) | ||
|
||
vl.saveStyleToDatabase('style1', 'style1', False, None) | ||
|
||
symbol = renderer.symbol().clone() | ||
symbol.setColor(QColor('#00ff00')) | ||
renderer.setSymbol(symbol) | ||
|
||
vl.saveStyleToDatabase('style2', 'style2', True, None) | ||
|
||
symbol = renderer.symbol().clone() | ||
symbol.setColor(QColor('#0000ff')) | ||
renderer.setSymbol(symbol) | ||
|
||
vl.saveStyleToDatabase('style3', 'style3', False, None) | ||
num, ids, names, desc, err = vl.listStylesInDatabase() | ||
|
||
self.assertTrue(set(['style2', 'style3', 'style1']).issubset(set(names))) | ||
|
||
del vl | ||
vl = QgsVectorLayer(uri, 'vl', self.providerKey) | ||
self.assertTrue(vl.isValid()) | ||
renderer = vl.renderer() | ||
symbol = renderer.symbol() | ||
self.assertEqual(symbol.color().name(), '#00ff00') | ||
|
||
mgr = vl.styleManager() | ||
self.assertEqual(mgr.styles(), ['style2']) | ||
|
||
del vl | ||
options = QgsVectorLayer.LayerOptions() | ||
options.loadAllStoredStyles = True | ||
vl = QgsVectorLayer(uri, 'vl', self.providerKey, options) | ||
self.assertTrue(vl.isValid()) | ||
renderer = vl.renderer() | ||
symbol = renderer.symbol() | ||
self.assertEqual(symbol.color().name(), '#00ff00') | ||
|
||
mgr = vl.styleManager() | ||
self.assertTrue(set(['style2', 'style3', 'style1']).issubset(set(names))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# coding=utf-8 | ||
""""Style storage tests for GPKG | ||
.. note:: This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
""" | ||
|
||
__author__ = 'elpaso@itopen.it' | ||
__date__ = '2022-11-07' | ||
__copyright__ = 'Copyright 2022, ItOpen' | ||
|
||
import os | ||
from stylestoragebase import StyleStorageTestBase | ||
from qgis.PyQt.QtCore import QTemporaryDir | ||
from qgis.core import ( | ||
QgsProviderRegistry, | ||
) | ||
from qgis.testing import unittest | ||
|
||
|
||
class StyleStorageTest(unittest.TestCase, StyleStorageTestBase): | ||
|
||
# Provider test cases must define the provider name (e.g. "postgres" or "ogr") | ||
providerKey = 'ogr' | ||
|
||
def setUp(self): | ||
|
||
super().setUp() | ||
self.temp_dir = QTemporaryDir() | ||
self.temp_path = self.temp_dir.path() | ||
md = QgsProviderRegistry.instance().providerMetadata(self.providerKey) | ||
self.test_uri = os.path.join(self.temp_path, 'test.gpkg') | ||
self.assertTrue(md.createDatabase(self.test_uri)[0]) | ||
self.uri = self.test_uri | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# coding=utf-8 | ||
""""Style storage tests for MSSQL | ||
.. note:: This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
""" | ||
|
||
__author__ = 'elpaso@itopen.it' | ||
__date__ = '2022-11-07' | ||
__copyright__ = 'Copyright 2022, ItOpen' | ||
|
||
import os | ||
from stylestoragebase import StyleStorageTestBase, StyleStorageTestCaseBase | ||
from qgis.PyQt.QtCore import QTemporaryDir | ||
from qgis.core import ( | ||
QgsDataSourceUri | ||
) | ||
from qgis.testing import unittest | ||
|
||
|
||
class StyleStorageTest(StyleStorageTestCaseBase, StyleStorageTestBase): | ||
|
||
# Provider test cases must define the string URI for the test | ||
uri = '' | ||
# Provider test cases must define the provider name (e.g. "postgres" or "ogr") | ||
providerKey = 'mssql' | ||
|
||
def setUp(self): | ||
|
||
super().setUp() | ||
|
||
dbconn = "service='testsqlserver' user=sa password='<YourStrong!Passw0rd>' " | ||
if 'QGIS_MSSQLTEST_DB' in os.environ: | ||
dbconn = os.environ['QGIS_MSSQLTEST_DB'] | ||
|
||
self.uri = dbconn | ||
|
||
def layerUri(self, conn, schema_name, table_name): | ||
"""Providers may override if they need more complex URI generation than | ||
what tableUri() offers""" | ||
|
||
uri = QgsDataSourceUri(conn.tableUri(schema_name, table_name)) | ||
uri.setGeometryColumn('geom') | ||
uri.setParam('srid', '4326') | ||
uri.setParam('type', 'POINT') | ||
return uri.uri() | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Oops, something went wrong.