/
test_cursor.py
455 lines (363 loc) · 16.5 KB
/
test_cursor.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# Copyright 2020 Google LLC All rights reserved.
#
# Licensed 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.
"""Cursor() class unit tests."""
import mock
import sys
import unittest
class TestCursor(unittest.TestCase):
INSTANCE = "test-instance"
DATABASE = "test-database"
def _get_target_class(self):
from google.cloud.spanner_dbapi import Cursor
return Cursor
def _make_one(self, *args, **kwargs):
return self._get_target_class()(*args, **kwargs)
def _make_connection(self, *args, **kwargs):
from google.cloud.spanner_dbapi import Connection
return Connection(*args, **kwargs)
def test_property_connection(self):
connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
self.assertEqual(cursor.connection, connection)
def test_property_description(self):
from google.cloud.spanner_dbapi._helpers import ColumnInfo
connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
self.assertIsNone(cursor.description)
cursor._result_set = res_set = mock.MagicMock()
res_set.metadata.row_type.fields = [mock.MagicMock()]
self.assertIsNotNone(cursor.description)
self.assertIsInstance(cursor.description[0], ColumnInfo)
def test_property_rowcount(self):
from google.cloud.spanner_dbapi.cursor import _UNSET_COUNT
connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
self.assertEqual(cursor.rowcount, _UNSET_COUNT)
def test_callproc(self):
from google.cloud.spanner_dbapi.exceptions import InterfaceError
connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
cursor._is_closed = True
with self.assertRaises(InterfaceError):
cursor.callproc(procname=None)
def test_close(self):
from google.cloud.spanner_dbapi import connect, InterfaceError
with mock.patch(
"google.cloud.spanner_v1.instance.Instance.exists", return_value=True
):
with mock.patch(
"google.cloud.spanner_v1.database.Database.exists", return_value=True
):
connection = connect(self.INSTANCE, self.DATABASE)
cursor = connection.cursor()
self.assertFalse(cursor.is_closed)
cursor.close()
self.assertTrue(cursor.is_closed)
with self.assertRaises(InterfaceError):
cursor.execute("SELECT * FROM database")
def test_do_execute_update(self):
from google.cloud.spanner_dbapi.cursor import _UNSET_COUNT
connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
transaction = mock.MagicMock()
def run_helper(ret_value):
transaction.execute_update.return_value = ret_value
res = cursor._do_execute_update(
transaction=transaction, sql="SELECT * WHERE true", params={},
)
return res
expected = "good"
self.assertEqual(run_helper(expected), expected)
self.assertEqual(cursor._row_count, _UNSET_COUNT)
expected = 1234
self.assertEqual(run_helper(expected), expected)
self.assertEqual(cursor._row_count, expected)
def test_execute_programming_error(self):
from google.cloud.spanner_dbapi.exceptions import ProgrammingError
connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
cursor.connection = None
with self.assertRaises(ProgrammingError):
cursor.execute(sql="")
def test_execute_attribute_error(self):
connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
with self.assertRaises(AttributeError):
cursor.execute(sql="")
def test_execute_autocommit_off(self):
from google.cloud.spanner_dbapi.utils import PeekIterator
connection = self._make_connection(self.INSTANCE, mock.MagicMock())
cursor = self._make_one(connection)
cursor.connection._autocommit = False
cursor.connection.transaction_checkout = mock.MagicMock(autospec=True)
cursor.execute("sql")
self.assertIsInstance(cursor._result_set, mock.MagicMock)
self.assertIsInstance(cursor._itr, PeekIterator)
def test_execute_statement(self):
from google.cloud.spanner_dbapi import parse_utils
connection = self._make_connection(self.INSTANCE, mock.MagicMock())
cursor = self._make_one(connection)
with mock.patch(
"google.cloud.spanner_dbapi.parse_utils.classify_stmt",
return_value=parse_utils.STMT_DDL,
) as mock_classify_stmt:
sql = "sql"
cursor.execute(sql=sql)
mock_classify_stmt.assert_called_once_with(sql)
self.assertEqual(cursor.connection._ddl_statements, [sql])
with mock.patch(
"google.cloud.spanner_dbapi.parse_utils.classify_stmt",
return_value=parse_utils.STMT_NON_UPDATING,
):
with mock.patch(
"google.cloud.spanner_dbapi.cursor.Cursor._handle_DQL",
return_value=parse_utils.STMT_NON_UPDATING,
) as mock_handle_ddl:
connection.autocommit = True
sql = "sql"
cursor.execute(sql=sql)
mock_handle_ddl.assert_called_once_with(sql, None)
with mock.patch(
"google.cloud.spanner_dbapi.parse_utils.classify_stmt",
return_value=parse_utils.STMT_INSERT,
):
with mock.patch(
"google.cloud.spanner_dbapi._helpers.handle_insert",
return_value=parse_utils.STMT_INSERT,
) as mock_handle_insert:
sql = "sql"
cursor.execute(sql=sql)
mock_handle_insert.assert_called_once_with(connection, sql, None)
with mock.patch(
"google.cloud.spanner_dbapi.parse_utils.classify_stmt",
return_value="other_statement",
):
cursor.connection._database = mock_db = mock.MagicMock()
mock_db.run_in_transaction = mock_run_in = mock.MagicMock()
sql = "sql"
cursor.execute(sql=sql)
mock_run_in.assert_called_once_with(cursor._do_execute_update, sql, None)
def test_execute_integrity_error(self):
from google.api_core import exceptions
from google.cloud.spanner_dbapi.exceptions import IntegrityError
connection = self._make_connection(self.INSTANCE, mock.MagicMock())
cursor = self._make_one(connection)
with mock.patch(
"google.cloud.spanner_dbapi.parse_utils.classify_stmt",
side_effect=exceptions.AlreadyExists("message"),
):
with self.assertRaises(IntegrityError):
cursor.execute(sql="sql")
with mock.patch(
"google.cloud.spanner_dbapi.parse_utils.classify_stmt",
side_effect=exceptions.FailedPrecondition("message"),
):
with self.assertRaises(IntegrityError):
cursor.execute(sql="sql")
def test_execute_invalid_argument(self):
from google.api_core import exceptions
from google.cloud.spanner_dbapi.exceptions import ProgrammingError
connection = self._make_connection(self.INSTANCE, mock.MagicMock())
cursor = self._make_one(connection)
with mock.patch(
"google.cloud.spanner_dbapi.parse_utils.classify_stmt",
side_effect=exceptions.InvalidArgument("message"),
):
with self.assertRaises(ProgrammingError):
cursor.execute(sql="sql")
def test_execute_internal_server_error(self):
from google.api_core import exceptions
from google.cloud.spanner_dbapi.exceptions import OperationalError
connection = self._make_connection(self.INSTANCE, mock.MagicMock())
cursor = self._make_one(connection)
with mock.patch(
"google.cloud.spanner_dbapi.parse_utils.classify_stmt",
side_effect=exceptions.InternalServerError("message"),
):
with self.assertRaises(OperationalError):
cursor.execute(sql="sql")
def test_executemany_on_closed_cursor(self):
from google.cloud.spanner_dbapi import InterfaceError
from google.cloud.spanner_dbapi import connect
with mock.patch(
"google.cloud.spanner_v1.instance.Instance.exists", return_value=True
):
with mock.patch(
"google.cloud.spanner_v1.database.Database.exists", return_value=True
):
connection = connect("test-instance", "test-database")
cursor = connection.cursor()
cursor.close()
with self.assertRaises(InterfaceError):
cursor.executemany("""SELECT * FROM table1 WHERE "col1" = @a1""", ())
def test_executemany(self):
from google.cloud.spanner_dbapi import connect
operation = """SELECT * FROM table1 WHERE "col1" = @a1"""
params_seq = ((1,), (2,))
with mock.patch(
"google.cloud.spanner_v1.instance.Instance.exists", return_value=True
):
with mock.patch(
"google.cloud.spanner_v1.database.Database.exists", return_value=True
):
connection = connect("test-instance", "test-database")
cursor = connection.cursor()
with mock.patch(
"google.cloud.spanner_dbapi.cursor.Cursor.execute"
) as execute_mock:
cursor.executemany(operation, params_seq)
execute_mock.assert_has_calls(
(mock.call(operation, (1,)), mock.call(operation, (2,)))
)
@unittest.skipIf(
sys.version_info[0] < 3, "Python 2 has an outdated iterator definition"
)
def test_fetchone(self):
connection = self._make_connection(self.INSTANCE, mock.MagicMock())
cursor = self._make_one(connection)
lst = [1, 2, 3]
cursor._itr = iter(lst)
for i in range(len(lst)):
self.assertEqual(cursor.fetchone(), lst[i])
self.assertIsNone(cursor.fetchone())
def test_fetchmany(self):
connection = self._make_connection(self.INSTANCE, mock.MagicMock())
cursor = self._make_one(connection)
lst = [(1,), (2,), (3,)]
cursor._itr = iter(lst)
self.assertEqual(cursor.fetchmany(), [lst[0]])
result = cursor.fetchmany(len(lst))
self.assertEqual(result, lst[1:])
def test_fetchall(self):
connection = self._make_connection(self.INSTANCE, mock.MagicMock())
cursor = self._make_one(connection)
lst = [(1,), (2,), (3,)]
cursor._itr = iter(lst)
self.assertEqual(cursor.fetchall(), lst)
def test_nextset(self):
from google.cloud.spanner_dbapi import exceptions
connection = self._make_connection(self.INSTANCE, mock.MagicMock())
cursor = self._make_one(connection)
cursor.close()
with self.assertRaises(exceptions.InterfaceError):
cursor.nextset()
def test_setinputsizes(self):
from google.cloud.spanner_dbapi import exceptions
connection = self._make_connection(self.INSTANCE, mock.MagicMock())
cursor = self._make_one(connection)
cursor.close()
with self.assertRaises(exceptions.InterfaceError):
cursor.setinputsizes(sizes=None)
def test_setoutputsize(self):
from google.cloud.spanner_dbapi import exceptions
connection = self._make_connection(self.INSTANCE, mock.MagicMock())
cursor = self._make_one(connection)
cursor.close()
with self.assertRaises(exceptions.InterfaceError):
cursor.setoutputsize(size=None)
# def test_handle_insert(self):
# pass
#
# def test_do_execute_insert_heterogenous(self):
# pass
#
# def test_do_execute_insert_homogenous(self):
# pass
def test_handle_dql(self):
from google.cloud.spanner_dbapi import utils
from google.cloud.spanner_dbapi.cursor import _UNSET_COUNT
connection = self._make_connection(self.INSTANCE, mock.MagicMock())
connection.database.snapshot.return_value.__enter__.return_value = (
mock_snapshot
) = mock.MagicMock()
cursor = self._make_one(connection)
mock_snapshot.execute_sql.return_value = int(0)
cursor._handle_DQL("sql", params=None)
self.assertEqual(cursor._row_count, 0)
self.assertIsNone(cursor._itr)
mock_snapshot.execute_sql.return_value = "0"
cursor._handle_DQL("sql", params=None)
self.assertEqual(cursor._result_set, "0")
self.assertIsInstance(cursor._itr, utils.PeekIterator)
self.assertEqual(cursor._row_count, _UNSET_COUNT)
def test_context(self):
connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
with cursor as c:
self.assertEqual(c, cursor)
self.assertTrue(c.is_closed)
def test_next(self):
from google.cloud.spanner_dbapi import exceptions
connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
with self.assertRaises(exceptions.ProgrammingError):
cursor.__next__()
lst = [(1,), (2,), (3,)]
cursor._itr = iter(lst)
i = 0
for c in cursor._itr:
self.assertEqual(c, lst[i])
i += 1
def test_iter(self):
from google.cloud.spanner_dbapi import exceptions
connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
with self.assertRaises(exceptions.ProgrammingError):
_ = iter(cursor)
iterator = iter([(1,), (2,), (3,)])
cursor._itr = iterator
self.assertEqual(iter(cursor), iterator)
def test_list_tables(self):
from google.cloud.spanner_dbapi import _helpers
connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
table_list = ["table1", "table2", "table3"]
with mock.patch(
"google.cloud.spanner_dbapi.cursor.Cursor.run_sql_in_snapshot",
return_value=table_list,
) as mock_run_sql:
cursor.list_tables()
mock_run_sql.assert_called_once_with(_helpers.SQL_LIST_TABLES)
def test_run_sql_in_snapshot(self):
connection = self._make_connection(self.INSTANCE, mock.MagicMock())
connection.database.snapshot.return_value.__enter__.return_value = (
mock_snapshot
) = mock.MagicMock()
cursor = self._make_one(connection)
results = 1, 2, 3
mock_snapshot.execute_sql.return_value = results
self.assertEqual(cursor.run_sql_in_snapshot("sql"), list(results))
def test_get_table_column_schema(self):
from google.cloud.spanner_dbapi.cursor import ColumnDetails
from google.cloud.spanner_dbapi import _helpers
from google.cloud.spanner_v1 import param_types
connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
column_name = "column1"
is_nullable = "YES"
spanner_type = "spanner_type"
rows = [(column_name, is_nullable, spanner_type)]
expected = {column_name: ColumnDetails(null_ok=True, spanner_type=spanner_type)}
with mock.patch(
"google.cloud.spanner_dbapi.cursor.Cursor.run_sql_in_snapshot",
return_value=rows,
) as mock_run_sql:
table_name = "table1"
result = cursor.get_table_column_schema(table_name=table_name)
mock_run_sql.assert_called_once_with(
sql=_helpers.SQL_GET_TABLE_COLUMN_SCHEMA,
params={"table_name": table_name},
param_types={"table_name": param_types.STRING},
)
self.assertEqual(result, expected)