-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
stylestoragebase.py
157 lines (116 loc) · 4.81 KB
/
stylestoragebase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
""""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.PyQt.QtCore import QCoreApplication, QVariant
from qgis.PyQt.QtGui import QColor
from qgis.core import (
QgsAbstractDatabaseProviderConnection,
QgsCoordinateReferenceSystem,
QgsField,
QgsFields,
QgsProviderRegistry,
QgsSettings,
QgsVectorLayer,
QgsWkbTypes,
)
import unittest
from qgis.testing import start_app, QgisTestCase
class StyleStorageTestCaseBase(QgisTestCase):
@classmethod
def setUpClass(cls):
"""Run before all tests"""
super().setUpClass()
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 schemaName(self):
"""Providers may override (Oracle?)"""
return 'test_styles_schema'
def tableName(self):
"""Providers may override (Oracle?)"""
return 'test_styles_table'
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.Capability.CreateSchema
and capabilities & QgsAbstractDatabaseProviderConnection.Capability.Schemas
and capabilities & QgsAbstractDatabaseProviderConnection.Capability.DropSchema):
schema = self.schemaName()
# Start clean
if schema in conn.schemas():
conn.dropSchema(schema, True)
# Create
conn.createSchema(schema)
schemas = conn.schemas()
self.assertIn(schema, schemas)
elif (capabilities & QgsAbstractDatabaseProviderConnection.Capability.Schemas):
schema = self.schemaName()
try:
conn.dropVectorTable(schema, self.tableName())
except Exception:
pass
try:
conn.createSchema(schema)
except Exception:
pass
schemas = conn.schemas()
self.assertIn(schema, schemas)
fields = QgsFields()
fields.append(QgsField("string_t", QVariant.String))
options = {}
crs = QgsCoordinateReferenceSystem.fromEpsgId(4326)
typ = QgsWkbTypes.Type.Point
# Create table
conn.createVectorTable(schema, self.tableName(), fields, typ, crs, True, options)
uri = self.layerUri(conn, schema, self.tableName())
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({'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({'style2', 'style3', 'style1'}.issubset(set(names)))