-
Notifications
You must be signed in to change notification settings - Fork 13.6k
/
test_oracle.py
355 lines (308 loc) · 13.2 KB
/
test_oracle.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import json
import unittest
from datetime import datetime
from unittest import mock
import numpy
import pytest
from airflow.models import Connection
from airflow.providers.oracle.hooks.oracle import OracleHook
try:
import cx_Oracle
except ImportError:
cx_Oracle = None
@unittest.skipIf(cx_Oracle is None, 'cx_Oracle package not present')
class TestOracleHookConn(unittest.TestCase):
def setUp(self):
super().setUp()
self.connection = Connection(
login='login', password='password', host='host', schema='schema', port=1521
)
self.db_hook = OracleHook()
self.db_hook.get_connection = mock.Mock()
self.db_hook.get_connection.return_value = self.connection
@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_get_conn_host(self, mock_connect):
self.db_hook.get_conn()
assert mock_connect.call_count == 1
args, kwargs = mock_connect.call_args
assert args == ()
assert kwargs['user'] == 'login'
assert kwargs['password'] == 'password'
assert kwargs['dsn'] == 'host:1521/schema'
@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_get_conn_host_alternative_port(self, mock_connect):
self.connection.port = 1522
self.db_hook.get_conn()
assert mock_connect.call_count == 1
args, kwargs = mock_connect.call_args
assert args == ()
assert kwargs['user'] == 'login'
assert kwargs['password'] == 'password'
assert kwargs['dsn'] == 'host:1522/schema'
@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_get_conn_sid(self, mock_connect):
dsn_sid = {'dsn': 'ignored', 'sid': 'sid'}
self.connection.extra = json.dumps(dsn_sid)
self.db_hook.get_conn()
assert mock_connect.call_count == 1
args, kwargs = mock_connect.call_args
assert args == ()
assert kwargs['dsn'] == cx_Oracle.makedsn("host", self.connection.port, dsn_sid['sid'])
@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_get_conn_service_name(self, mock_connect):
dsn_service_name = {'dsn': 'ignored', 'service_name': 'service_name'}
self.connection.extra = json.dumps(dsn_service_name)
self.db_hook.get_conn()
assert mock_connect.call_count == 1
args, kwargs = mock_connect.call_args
assert args == ()
assert kwargs['dsn'] == cx_Oracle.makedsn(
"host", self.connection.port, service_name=dsn_service_name['service_name']
)
@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_get_conn_encoding_without_nencoding(self, mock_connect):
self.connection.extra = json.dumps({'encoding': 'UTF-8'})
self.db_hook.get_conn()
assert mock_connect.call_count == 1
args, kwargs = mock_connect.call_args
assert args == ()
assert kwargs['encoding'] == 'UTF-8'
assert kwargs['nencoding'] == 'UTF-8'
@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_get_conn_encoding_with_nencoding(self, mock_connect):
self.connection.extra = json.dumps({'encoding': 'UTF-8', 'nencoding': 'gb2312'})
self.db_hook.get_conn()
assert mock_connect.call_count == 1
args, kwargs = mock_connect.call_args
assert args == ()
assert kwargs['encoding'] == 'UTF-8'
assert kwargs['nencoding'] == 'gb2312'
@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_get_conn_nencoding(self, mock_connect):
self.connection.extra = json.dumps({'nencoding': 'UTF-8'})
self.db_hook.get_conn()
assert mock_connect.call_count == 1
args, kwargs = mock_connect.call_args
assert args == ()
assert 'encoding' not in kwargs
assert kwargs['nencoding'] == 'UTF-8'
@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_get_conn_mode(self, mock_connect):
mode = {
'sysdba': cx_Oracle.SYSDBA,
'sysasm': cx_Oracle.SYSASM,
'sysoper': cx_Oracle.SYSOPER,
'sysbkp': cx_Oracle.SYSBKP,
'sysdgd': cx_Oracle.SYSDGD,
'syskmt': cx_Oracle.SYSKMT,
}
first = True
for mod in mode:
self.connection.extra = json.dumps({'mode': mod})
self.db_hook.get_conn()
if first:
assert mock_connect.call_count == 1
first = False
args, kwargs = mock_connect.call_args
assert args == ()
assert kwargs['mode'] == mode.get(mod)
@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_get_conn_threaded(self, mock_connect):
self.connection.extra = json.dumps({'threaded': True})
self.db_hook.get_conn()
assert mock_connect.call_count == 1
args, kwargs = mock_connect.call_args
assert args == ()
assert kwargs['threaded'] is True
@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_get_conn_events(self, mock_connect):
self.connection.extra = json.dumps({'events': True})
self.db_hook.get_conn()
assert mock_connect.call_count == 1
args, kwargs = mock_connect.call_args
assert args == ()
assert kwargs['events'] is True
@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_get_conn_purity(self, mock_connect):
purity = {
'new': cx_Oracle.ATTR_PURITY_NEW,
'self': cx_Oracle.ATTR_PURITY_SELF,
'default': cx_Oracle.ATTR_PURITY_DEFAULT,
}
first = True
for pur in purity:
self.connection.extra = json.dumps({'purity': pur})
self.db_hook.get_conn()
if first:
assert mock_connect.call_count == 1
first = False
args, kwargs = mock_connect.call_args
assert args == ()
assert kwargs['purity'] == purity.get(pur)
@mock.patch('airflow.providers.oracle.hooks.oracle.cx_Oracle.connect')
def test_set_current_schema(self, mock_connect):
self.connection.schema = "schema_name"
self.connection.extra = json.dumps({'service_name': 'service_name'})
assert self.db_hook.get_conn().current_schema == self.connection.schema
@unittest.skipIf(cx_Oracle is None, 'cx_Oracle package not present')
class TestOracleHook(unittest.TestCase):
def setUp(self):
super().setUp()
self.cur = mock.MagicMock(rowcount=0)
self.conn = mock.MagicMock()
self.conn.cursor.return_value = self.cur
conn = self.conn
class UnitTestOracleHook(OracleHook):
conn_name_attr = 'test_conn_id'
def get_conn(self):
return conn
self.db_hook = UnitTestOracleHook()
def test_run_without_parameters(self):
sql = 'SQL'
self.db_hook.run(sql)
self.cur.execute.assert_called_once_with(sql)
assert self.conn.commit.called
def test_run_with_parameters(self):
sql = 'SQL'
param = ('p1', 'p2')
self.db_hook.run(sql, parameters=param)
self.cur.execute.assert_called_once_with(sql, param)
assert self.conn.commit.called
def test_insert_rows_with_fields(self):
rows = [
(
"'basestr_with_quote",
None,
numpy.NAN,
numpy.datetime64('2019-01-24T01:02:03'),
datetime(2019, 1, 24),
1,
10.24,
'str',
)
]
target_fields = [
'basestring',
'none',
'numpy_nan',
'numpy_datetime64',
'datetime',
'int',
'float',
'str',
]
self.db_hook.insert_rows('table', rows, target_fields)
self.cur.execute.assert_called_once_with(
"INSERT /*+ APPEND */ INTO table "
"(basestring, none, numpy_nan, numpy_datetime64, datetime, int, float, str) "
"VALUES ('''basestr_with_quote',NULL,NULL,'2019-01-24T01:02:03',"
"to_date('2019-01-24 00:00:00','YYYY-MM-DD HH24:MI:SS'),1,10.24,'str')"
)
def test_insert_rows_without_fields(self):
rows = [
(
"'basestr_with_quote",
None,
numpy.NAN,
numpy.datetime64('2019-01-24T01:02:03'),
datetime(2019, 1, 24),
1,
10.24,
'str',
)
]
self.db_hook.insert_rows('table', rows)
self.cur.execute.assert_called_once_with(
"INSERT /*+ APPEND */ INTO table "
" VALUES ('''basestr_with_quote',NULL,NULL,'2019-01-24T01:02:03',"
"to_date('2019-01-24 00:00:00','YYYY-MM-DD HH24:MI:SS'),1,10.24,'str')"
)
def test_bulk_insert_rows_with_fields(self):
rows = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
target_fields = ['col1', 'col2', 'col3']
self.db_hook.bulk_insert_rows('table', rows, target_fields)
self.cur.prepare.assert_called_once_with("insert into table (col1, col2, col3) values (:1, :2, :3)")
self.cur.executemany.assert_called_once_with(None, rows)
def test_bulk_insert_rows_with_commit_every(self):
rows = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
target_fields = ['col1', 'col2', 'col3']
self.db_hook.bulk_insert_rows('table', rows, target_fields, commit_every=2)
calls = [
mock.call("insert into table (col1, col2, col3) values (:1, :2, :3)"),
mock.call("insert into table (col1, col2, col3) values (:1, :2, :3)"),
]
self.cur.prepare.assert_has_calls(calls)
calls = [
mock.call(None, rows[:2]),
mock.call(None, rows[2:]),
]
self.cur.executemany.assert_has_calls(calls, any_order=True)
def test_bulk_insert_rows_without_fields(self):
rows = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
self.db_hook.bulk_insert_rows('table', rows)
self.cur.prepare.assert_called_once_with("insert into table values (:1, :2, :3)")
self.cur.executemany.assert_called_once_with(None, rows)
def test_bulk_insert_rows_no_rows(self):
rows = []
with pytest.raises(ValueError):
self.db_hook.bulk_insert_rows('table', rows)
def test_callproc_none(self):
parameters = None
class bindvar(int):
def getvalue(self):
return self
self.cur.bindvars = None
result = self.db_hook.callproc('proc', True, parameters)
assert self.cur.execute.mock_calls == [mock.call('BEGIN proc(); END;')]
assert result == parameters
def test_callproc_dict(self):
parameters = {"a": 1, "b": 2, "c": 3}
class bindvar(int):
def getvalue(self):
return self
self.cur.bindvars = {k: bindvar(v) for k, v in parameters.items()}
result = self.db_hook.callproc('proc', True, parameters)
assert self.cur.execute.mock_calls == [mock.call('BEGIN proc(:a,:b,:c); END;', parameters)]
assert result == parameters
def test_callproc_list(self):
parameters = [1, 2, 3]
class bindvar(int):
def getvalue(self):
return self
self.cur.bindvars = list(map(bindvar, parameters))
result = self.db_hook.callproc('proc', True, parameters)
assert self.cur.execute.mock_calls == [mock.call('BEGIN proc(:1,:2,:3); END;', parameters)]
assert result == parameters
def test_callproc_out_param(self):
parameters = [1, int, float, bool, str]
def bindvar(value):
m = mock.Mock()
m.getvalue.return_value = value
return m
self.cur.bindvars = [bindvar(p() if type(p) is type else p) for p in parameters]
result = self.db_hook.callproc('proc', True, parameters)
expected = [1, 0, 0.0, False, '']
assert self.cur.execute.mock_calls == [mock.call('BEGIN proc(:1,:2,:3,:4,:5); END;', expected)]
assert result == expected
def test_test_connection_use_dual_table(self):
status, message = self.db_hook.test_connection()
self.cur.execute.assert_called_once_with("select 1 from dual")
assert status is True
assert message == 'Connection successfully tested'