@@ -23,6 +23,7 @@ class Testing(unittest.TestCase):
23
23
24
24
_data = None
25
25
_dsn = None
26
+ _pyodbc = None
26
27
27
28
def __init__ (self , test_data , dsn = None ):
28
29
super ().__init__ ()
@@ -32,9 +33,10 @@ def __init__(self, test_data, dsn=None):
32
33
33
34
# only import pyODBC if running tests (vs. for instance only loading test data in ES)
34
35
import pyodbc
36
+ self ._pyodbc = pyodbc
35
37
36
38
def _reconstitute_csv (self , index_name ):
37
- with pyodbc .connect (self ._dsn ) as cnxn :
39
+ with self . _pyodbc .connect (self ._dsn ) as cnxn :
38
40
cnxn .autocommit = True
39
41
csv = u""
40
42
cols = self ._data .csv_attributes (index_name )[1 ]
@@ -69,7 +71,7 @@ def _as_csv(self, index_name):
69
71
def _count_all (self , index_name ):
70
72
print ("Counting records in index '%s.'" % index_name )
71
73
cnt = 0
72
- with pyodbc .connect (self ._dsn ) as cnxn :
74
+ with self . _pyodbc .connect (self ._dsn ) as cnxn :
73
75
cnxn .autocommit = True
74
76
with cnxn .execute ("select 1 from %s" % index_name ) as curs :
75
77
while curs .fetchone ():
@@ -81,7 +83,7 @@ def _count_all(self, index_name):
81
83
82
84
def _clear_cursor (self , index_name ):
83
85
conn_str = self ._dsn + ";MaxFetchSize=5"
84
- with pyodbc .connect (conn_str ) as cnxn :
86
+ with self . _pyodbc .connect (conn_str ) as cnxn :
85
87
cnxn .autocommit = True
86
88
with cnxn .execute ("select 1 from %s limit 10" % index_name ) as curs :
87
89
for i in range (3 ): # must be lower than MaxFetchSize, so no next page be requested
@@ -93,7 +95,7 @@ def _clear_cursor(self, index_name):
93
95
94
96
def _select_columns (self , index_name , columns ):
95
97
print ("Selecting columns '%s' from index '%s'." % (columns , index_name ))
96
- with pyodbc .connect (self ._dsn ) as cnxn :
98
+ with self . _pyodbc .connect (self ._dsn ) as cnxn :
97
99
cnxn .autocommit = True
98
100
stmt = "select %s from %s" % (columns , index_name )
99
101
with cnxn .execute (stmt ) as curs :
@@ -103,14 +105,14 @@ def _select_columns(self, index_name, columns):
103
105
print ("Selected %s rows from %s." % (cnt , index_name ))
104
106
105
107
def _check_info (self , attr , expected ):
106
- with pyodbc .connect (self ._dsn ) as cnxn :
108
+ with self . _pyodbc .connect (self ._dsn ) as cnxn :
107
109
cnxn .autocommit = True
108
110
value = cnxn .getinfo (attr )
109
111
self .assertEqual (value , expected )
110
112
111
113
# tables(table=None, catalog=None, schema=None, tableType=None)
112
114
def _catalog_tables (self , no_table_type_as = "" ):
113
- with pyodbc .connect (self ._dsn ) as cnxn :
115
+ with self . _pyodbc .connect (self ._dsn ) as cnxn :
114
116
cnxn .autocommit = True
115
117
curs = cnxn .cursor ()
116
118
@@ -140,7 +142,7 @@ def _catalog_tables(self, no_table_type_as=""):
140
142
# use_surrogate: pyodbc seems to not reliably null-terminate the catalog and/or table name string,
141
143
# despite indicating so.
142
144
def _catalog_columns (self , use_catalog = False , use_surrogate = True ):
143
- with pyodbc .connect (self ._dsn ) as cnxn :
145
+ with self . _pyodbc .connect (self ._dsn ) as cnxn :
144
146
cnxn .autocommit = True
145
147
curs = cnxn .cursor ()
146
148
if not use_surrogate :
@@ -248,7 +250,7 @@ def _type_to_instance(self, data_type, data_val):
248
250
249
251
def _proto_tests (self ):
250
252
tests = self ._data .proto_tests ()
251
- with pyodbc .connect (self ._dsn ) as cnxn :
253
+ with self . _pyodbc .connect (self ._dsn ) as cnxn :
252
254
cnxn .autocommit = True
253
255
self ._install_output_converters (cnxn )
254
256
try :
@@ -279,8 +281,8 @@ def _proto_tests(self):
279
281
cnxn .clear_output_converters ()
280
282
281
283
def perform (self ):
282
- self ._check_info (pyodbc .SQL_USER_NAME , UID )
283
- self ._check_info (pyodbc .SQL_DATABASE_NAME , CATALOG )
284
+ self ._check_info (self . _pyodbc .SQL_USER_NAME , UID )
285
+ self ._check_info (self . _pyodbc .SQL_DATABASE_NAME , CATALOG )
284
286
285
287
# simulate catalog querying as apps do in ES/GH#40775 do
286
288
self ._catalog_tables (no_table_type_as = "" )
0 commit comments