Skip to content

Commit 1f852ff

Browse files
committedOct 25, 2018
[mssql] Run provider layer edits test suite
1 parent 37a6856 commit 1f852ff

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed
 

‎tests/src/python/test_provider_mssql.py

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
QgsCoordinateReferenceSystem)
3131

3232
from qgis.PyQt.QtCore import QDate, QTime, QDateTime, QVariant
33+
from qgis.PyQt.QtSql import QSqlDatabase, QSqlQuery
3334
from utilities import unitTestDataPath
3435
from qgis.testing import start_app, unittest
3536
from providertestbase import ProviderTestCase
@@ -50,16 +51,73 @@ def setUpClass(cls):
5051
# Create test layers
5152
cls.vl = QgsVectorLayer(
5253
cls.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POINT table="qgis_test"."someData" (geom) sql=', 'test', 'mssql')
53-
assert cls.vl.isValid(), cls.vl.dataProvider().error()
54+
assert cls.vl.isValid(), cls.vl.dataProvider().error().message()
5455
cls.source = cls.vl.dataProvider()
5556
cls.poly_vl = QgsVectorLayer(
5657
cls.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POLYGON table="qgis_test"."some_poly_data" (geom) sql=', 'test', 'mssql')
57-
assert cls.poly_vl.isValid(), cls.poly_vl.dataProvider().error()
58+
assert cls.poly_vl.isValid(), cls.poly_vl.dataProvider().error().message()
5859
cls.poly_provider = cls.poly_vl.dataProvider()
5960

61+
cls.conn = QSqlDatabase.addDatabase('QODBC')
62+
cls.conn.setDatabaseName('testsqlserver')
63+
if 'QGIS_MSSQLTEST_DB' in os.environ:
64+
cls.conn.setDatabaseName('QGIS_MSSQLTEST_DB')
65+
cls.conn.setUserName('SA')
66+
cls.conn.setPassword('<YourStrong!Passw0rd>')
67+
assert cls.conn.open(), cls.conn.lastError().text()
68+
6069
@classmethod
6170
def tearDownClass(cls):
6271
"""Run after all tests"""
72+
pass
73+
74+
def setUp(self):
75+
for t in ['new_table', 'new_table_multipoint', 'new_table_multipolygon']:
76+
self.execSQLCommand('DROP TABLE IF EXISTS qgis_test.[{}]'.format(t))
77+
78+
def execSQLCommand(self, sql):
79+
self.assertTrue(self.conn)
80+
query = QSqlQuery(self.conn)
81+
self.assertTrue(query.exec_(sql), sql + ': ' + query.lastError().text())
82+
query.finish()
83+
84+
def getSource(self):
85+
# create temporary table for edit tests
86+
self.execSQLCommand('DROP TABLE IF EXISTS qgis_test.edit_data')
87+
self.execSQLCommand(
88+
"""CREATE TABLE qgis_test.edit_data (pk INTEGER PRIMARY KEY,cnt integer, name nvarchar(max), name2 nvarchar(max), num_char nvarchar(max), geom geometry)""")
89+
90+
vl = QgsVectorLayer(
91+
self.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POINT table="qgis_test"."edit_data" (geom) sql=',
92+
'test', 'mssql')
93+
94+
self.assertTrue(vl.isValid(), vl.dataProvider().error().message())
95+
96+
f1 = QgsFeature()
97+
f1.setAttributes([5, -200, NULL, 'NuLl', '5'])
98+
f1.setGeometry(QgsGeometry.fromWkt('Point (-71.123 78.23)'))
99+
100+
f2 = QgsFeature()
101+
f2.setAttributes([3, 300, 'Pear', 'PEaR', '3'])
102+
103+
f3 = QgsFeature()
104+
f3.setAttributes([1, 100, 'Orange', 'oranGe', '1'])
105+
f3.setGeometry(QgsGeometry.fromWkt('Point (-70.332 66.33)'))
106+
107+
f4 = QgsFeature()
108+
f4.setAttributes([2, 200, 'Apple', 'Apple', '2'])
109+
f4.setGeometry(QgsGeometry.fromWkt('Point (-68.2 70.8)'))
110+
111+
f5 = QgsFeature()
112+
f5.setAttributes([4, 400, 'Honey', 'Honey', '4'])
113+
f5.setGeometry(QgsGeometry.fromWkt('Point (-65.32 78.3)'))
114+
115+
self.assertTrue(vl.dataProvider().addFeatures([f1, f2, f3, f4, f5]))
116+
117+
return vl
118+
119+
def getEditableLayer(self):
120+
return self.getSource()
63121

64122
def enableCompiler(self):
65123
QgsSettings().setValue('/qgis/compileExpressions', True)

0 commit comments

Comments
 (0)
Please sign in to comment.