30
30
QgsCoordinateReferenceSystem )
31
31
32
32
from qgis .PyQt .QtCore import QDate , QTime , QDateTime , QVariant
33
+ from qgis .PyQt .QtSql import QSqlDatabase , QSqlQuery
33
34
from utilities import unitTestDataPath
34
35
from qgis .testing import start_app , unittest
35
36
from providertestbase import ProviderTestCase
@@ -50,16 +51,73 @@ def setUpClass(cls):
50
51
# Create test layers
51
52
cls .vl = QgsVectorLayer (
52
53
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 ()
54
55
cls .source = cls .vl .dataProvider ()
55
56
cls .poly_vl = QgsVectorLayer (
56
57
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 ()
58
59
cls .poly_provider = cls .poly_vl .dataProvider ()
59
60
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
+
60
69
@classmethod
61
70
def tearDownClass (cls ):
62
71
"""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 ()
63
121
64
122
def enableCompiler (self ):
65
123
QgsSettings ().setValue ('/qgis/compileExpressions' , True )
0 commit comments