/
test_provider_mssql.py
102 lines (85 loc) · 3.33 KB
/
test_provider_mssql.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
# -*- coding: utf-8 -*-
"""QGIS Unit tests for the MS SQL provider.
.. 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__ = 'Nyall Dawson'
__date__ = '2015-12-07'
__copyright__ = 'Copyright 2015, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import qgis
import os
import sys
from qgis.core import NULL
from qgis.core import (
QgsVectorLayer,
QgsFeatureRequest,
QgsFeature,
QgsProviderRegistry
)
from PyQt4.QtCore import (
QSettings,
QDate,
QTime,
QDateTime,
QVariant
)
from utilities import unitTestDataPath
from qgis.testing import (
start_app,
unittest
)
from providertestbase import ProviderTestCase
start_app()
TEST_DATA_DIR=unitTestDataPath()
class TestPyQgsMssqlProvider(unittest.TestCase, ProviderTestCase):
@classmethod
def setUpClass(cls):
"""Run before all tests"""
cls.dbconn=u"dbname='gis' host=localhost\sqlexpress"
if 'QGIS_MSSQLTEST_DB' in os.environ:
cls.dbconn=os.environ['QGIS_MSSQLTEST_DB']
# Create test layers
cls.vl=QgsVectorLayer(
cls.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POINT table="qgis_test"."someData" (geom) sql=', 'test', 'mssql')
assert(cls.vl.isValid())
cls.provider=cls.vl.dataProvider()
cls.poly_vl=QgsVectorLayer(
cls.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POLYGON table="qgis_test"."some_poly_data" (geom) sql=', 'test', 'mssql')
assert(cls.poly_vl.isValid())
cls.poly_provider=cls.poly_vl.dataProvider()
@classmethod
def tearDownClass(cls):
"""Run after all tests"""
def enableCompiler(self):
QSettings().setValue(u'/qgis/compileExpressions', True)
def disableCompiler(self):
QSettings().setValue(u'/qgis/compileExpressions', False)
# HERE GO THE PROVIDER SPECIFIC TESTS
def testDateTimeTypes(self):
vl=QgsVectorLayer('%s table="qgis_test"."date_times" sql=' %
(self.dbconn), "testdatetimes", "mssql")
assert(vl.isValid())
fields=vl.dataProvider().fields()
self.assertEqual(fields.at(fields.indexFromName(
'date_field')).type(), QVariant.Date)
self.assertEqual(fields.at(fields.indexFromName(
'time_field')).type(), QVariant.Time)
self.assertEqual(fields.at(fields.indexFromName(
'datetime_field')).type(), QVariant.DateTime)
f=vl.getFeatures(QgsFeatureRequest()).next()
date_idx=vl.fieldNameIndex('date_field')
assert isinstance(f.attributes()[date_idx], QDate)
self.assertEqual(f.attributes()[date_idx], QDate(2004, 3, 4))
time_idx=vl.fieldNameIndex('time_field')
assert isinstance(f.attributes()[time_idx], QTime)
self.assertEqual(f.attributes()[time_idx], QTime(13, 41, 52))
datetime_idx=vl.fieldNameIndex('datetime_field')
assert isinstance(f.attributes()[datetime_idx], QDateTime)
self.assertEqual(f.attributes()[datetime_idx], QDateTime(
QDate(2004, 3, 4), QTime(13, 41, 52)))
if __name__ == '__main__':
unittest.main()