-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_cursor.py
More file actions
557 lines (475 loc) · 16.5 KB
/
test_cursor.py
File metadata and controls
557 lines (475 loc) · 16.5 KB
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# -*- coding: utf-8; -*-
#
# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. Crate 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.
#
# However, if you have executed another commercial license agreement
# with Crate these terms will supersede the license and you may use the
# software solely pursuant to the terms of the relevant commercial agreement.
import datetime
import sys
from ipaddress import IPv4Address
from unittest import mock
import pytest
from crate.client.exceptions import ProgrammingError
try:
import zoneinfo
except ImportError:
from backports import zoneinfo
import pytz
from crate.client import connect
from crate.client.converter import DataType, DefaultTypeConverter
def test_cursor_fetch(mocked_connection):
"""Verify fetchone/fetchmany behaviour"""
cursor = mocked_connection.cursor()
response = {
"col_types": [4, 5],
"cols": ["name", "address"],
"rows": [["foo", "10.10.10.1"], ["bar", "10.10.10.2"]],
"rowcount": 2,
"duration": 123,
}
with mock.patch.object(
mocked_connection.client, "sql", return_value=response
):
cursor.execute("")
assert cursor.fetchone() == ["foo", "10.10.10.1"]
assert cursor.fetchmany() == [
["bar", "10.10.10.2"],
]
@pytest.mark.skipif(
sys.version_info < (3, 10), reason="Test needs Python >= 3.10"
)
def test_cursor_description(mocked_connection):
cursor = mocked_connection.cursor()
response = {
"col_types": [4, 5],
"cols": ["name", "address"],
"rows": [["foo", "10.10.10.1"], ["bar", "10.10.10.2"]],
"rowcount": 2,
"duration": 123,
}
with mock.patch.object(
mocked_connection.client, "sql", return_value=response
):
cursor.execute("")
assert len(cursor.description) == len(response["cols"])
assert len(cursor.description[0]) == 7 # It's 7 by convention.
for expected_name, name in zip(
response["cols"], cursor.description, strict=False
):
assert expected_name == name[0]
cursor.close()
assert cursor.description is None
def test_cursor_rowcount(mocked_connection):
"""Verify the logic of cursor.rowcount"""
cursor = mocked_connection.cursor()
response = {
"col_types": [4, 5],
"cols": ["name", "address"],
"rows": [["foo", "10.10.10.1"], ["bar", "10.10.10.2"]],
"rowcount": 2,
"duration": 123,
}
with mock.patch.object(
mocked_connection.client, "sql", return_value=response
):
cursor.execute("")
assert cursor.rowcount == len(response["rows"])
cursor._result = None
assert cursor.rowcount == -1
cursor.execute("")
cursor._result = {}
assert cursor.rowcount == -1
cursor.execute("")
cursor.close()
assert cursor.rowcount == -1
def test_cursor_executemany(mocked_connection):
"""
Verify executemany.
"""
response = {
"col_types": [],
"cols": [],
"duration": 123,
"results": [{"rowcount": 1, "rowcount:": 1}],
}
with mock.patch.object(
mocked_connection.client, "sql", return_value=response
):
cursor = mocked_connection.cursor()
result = cursor.executemany("some sql", ())
assert isinstance(result, list)
assert response["results"] == result
def test_create_with_timezone_as_datetime_object(mocked_connection):
"""
The cursor can return timezone-aware `datetime` objects when requested.
Switching the time zone at runtime on the cursor object is possible.
Here: Use a `datetime.timezone` instance.
"""
tz_mst = datetime.timezone(datetime.timedelta(hours=7), name="MST")
cursor = mocked_connection.cursor(time_zone=tz_mst)
assert cursor.time_zone.tzname(None) == "MST"
assert cursor.time_zone.utcoffset(None) == datetime.timedelta(seconds=25200)
cursor.time_zone = datetime.timezone.utc
assert cursor.time_zone.tzname(None) == "UTC"
assert cursor.time_zone.utcoffset(None) == datetime.timedelta(0)
def test_create_with_timezone_as_pytz_object(mocked_connection):
"""
The cursor can return timezone-aware `datetime` objects when requested.
Here: Use a `pytz.timezone` instance.
"""
cursor = mocked_connection.cursor(
time_zone=pytz.timezone("Australia/Sydney")
)
assert cursor.time_zone.tzname(None) == "Australia/Sydney"
# Apparently, when using `pytz`, the timezone object does not return
# an offset. Nevertheless, it works, as demonstrated per doctest in
# `cursor.txt`.
assert cursor.time_zone.utcoffset(None) is None
def test_create_with_timezone_as_zoneinfo_object(mocked_connection):
"""
The cursor can return timezone-aware `datetime` objects when requested.
Here: Use a `zoneinfo.ZoneInfo` instance.
"""
cursor = mocked_connection.cursor(
time_zone=zoneinfo.ZoneInfo("Australia/Sydney")
)
assert cursor.time_zone.key == "Australia/Sydney"
def test_create_with_timezone_as_utc_offset_success(mocked_connection):
"""
Verify the cursor can return timezone-aware `datetime` objects when
requested.
Here: Use a UTC offset in string format.
"""
cursor = mocked_connection.cursor(time_zone="+0530")
assert cursor.time_zone.tzname(None) == "+0530"
assert cursor.time_zone.utcoffset(None) == datetime.timedelta(seconds=19800)
cursor = mocked_connection.cursor(time_zone="-1145")
assert cursor.time_zone.tzname(None) == "-1145"
assert cursor.time_zone.utcoffset(None) == datetime.timedelta(
days=-1, seconds=44100
)
def test_create_with_timezone_as_utc_offset_failure(mocked_connection):
"""
Verify the cursor trips when trying to use invalid UTC offset strings.
"""
with pytest.raises(ValueError) as err:
mocked_connection.cursor(time_zone="foobar")
assert err == "Time zone 'foobar' is given in invalid UTC offset format"
with pytest.raises(ValueError) as err:
mocked_connection.cursor(time_zone="+abcd")
assert (
err
== "Time zone '+abcd' is given in invalid UTC offset format: "
+ "invalid literal for int() with base 10: '+ab'"
)
def test_create_with_timezone_connection_cursor_precedence(mocked_connection):
"""
Verify that the time zone specified on the cursor object instance
takes precedence over the one specified on the connection instance.
"""
connection = connect(
client=mocked_connection.client,
time_zone=pytz.timezone("Australia/Sydney"),
)
cursor = connection.cursor(time_zone="+0530")
assert cursor.time_zone.tzname(None) == "+0530"
assert cursor.time_zone.utcoffset(None) == datetime.timedelta(seconds=19800)
def test_execute_with_args(mocked_connection):
"""
Verify that `cursor.execute` is called with the right parameters.
"""
cursor = mocked_connection.cursor()
statement = "select * from locations where position = ?"
cursor.execute(statement, 1)
mocked_connection.client.sql.assert_called_once_with(statement, 1, None)
def test_execute_with_bulk_args(mocked_connection):
"""
Verify that `cursor.execute` is called with the right parameters
when passing `bulk_parameters`.
"""
cursor = mocked_connection.cursor()
statement = "select * from locations where position = ?"
cursor.execute(statement, bulk_parameters=[[1]])
mocked_connection.client.sql.assert_called_once_with(statement, None, [[1]])
@pytest.mark.skipif(
sys.version_info < (3, 10), reason="Converter needs Python >= 3.10"
)
def test_execute_custom_converter(mocked_connection):
"""
Verify that a custom converter is correctly applied when passed to a cursor.
"""
# Extends the DefaultTypeConverter
converter = DefaultTypeConverter(
{
DataType.BIT: lambda value: value is not None
and int(value[2:-1], 2)
or None
}
)
cursor = mocked_connection.cursor(converter=converter)
response = {
"col_types": [4, 5, 11, 25],
"cols": ["name", "address", "timestamp", "bitmask"],
"rows": [
["foo", "10.10.10.1", 1658167836758, "B'0110'"],
[None, None, None, None],
],
"rowcount": 1,
"duration": 123,
}
with mock.patch.object(
mocked_connection.client, "sql", return_value=response
):
cursor.execute("")
result = cursor.fetchall()
assert result == [
[
"foo",
IPv4Address("10.10.10.1"),
datetime.datetime(
2022,
7,
18,
18,
10,
36,
758000,
tzinfo=datetime.timezone.utc,
),
6,
],
[None, None, None, None],
]
@pytest.mark.skipif(
sys.version_info < (3, 10), reason="Converter needs Python >= 3.10"
)
def test_execute_with_converter_and_invalid_data_type(mocked_connection):
converter = DefaultTypeConverter()
# Create a `Cursor` object with converter.
cursor = mocked_connection.cursor(converter=converter)
response = {
"col_types": [999],
"cols": ["foo"],
"rows": [
["n/a"],
],
"rowcount": 1,
"duration": 123,
}
with mock.patch.object(
mocked_connection.client, "sql", return_value=response
):
cursor.execute("")
with pytest.raises(ValueError) as e:
cursor.fetchone()
assert e.exception.args == "999 is not a valid DataType"
@pytest.mark.skipif(
sys.version_info < (3, 10), reason="Converter needs Python >= 3.10"
)
def test_execute_array_with_converter(mocked_connection):
converter = DefaultTypeConverter()
cursor = mocked_connection.cursor(converter=converter)
response = {
"col_types": [4, [100, 5]],
"cols": ["name", "address"],
"rows": [["foo", ["10.10.10.1", "10.10.10.2"]]],
"rowcount": 1,
"duration": 123,
}
with mock.patch.object(
mocked_connection.client, "sql", return_value=response
):
cursor.execute("")
result = cursor.fetchone()
assert result == [
"foo",
[IPv4Address("10.10.10.1"), IPv4Address("10.10.10.2")],
]
@pytest.mark.skipif(
sys.version_info < (3, 10), reason="Converter needs Python >= 3.10"
)
def test_execute_array_with_converter_invalid(mocked_connection):
converter = DefaultTypeConverter()
cursor = mocked_connection.cursor(converter=converter)
response = {
"col_types": [4, [6, 5]],
"cols": ["name", "address"],
"rows": [["foo", ["10.10.10.1", "10.10.10.2"]]],
"rowcount": 1,
"duration": 123,
}
# Converting collections only works for `ARRAY`s. (ID=100).
# When using `DOUBLE` (ID=6), it should raise an Exception.
with mock.patch.object(
mocked_connection.client, "sql", return_value=response
):
cursor.execute("")
with pytest.raises(ValueError) as e:
cursor.fetchone()
assert e.exception.args == (
"Data type 6 is not implemented as collection type"
)
@pytest.mark.skipif(
sys.version_info < (3, 10), reason="Converter needs Python >= 3.10"
)
def test_execute_nested_array_with_converter(mocked_connection):
converter = DefaultTypeConverter()
cursor = mocked_connection.cursor(converter=converter)
response = {
"col_types": [4, [100, [100, 5]]],
"cols": ["name", "address_buckets"],
"rows": [
[
"foo",
[
["10.10.10.1", "10.10.10.2"],
["10.10.10.3"],
[],
None,
],
]
],
"rowcount": 1,
"duration": 123,
}
with mock.patch.object(
mocked_connection.client, "sql", return_value=response
):
cursor.execute("")
result = cursor.fetchone()
assert result == [
"foo",
[
[IPv4Address("10.10.10.1"), IPv4Address("10.10.10.2")],
[IPv4Address("10.10.10.3")],
[],
None,
],
]
@pytest.mark.skipif(
sys.version_info < (3, 10), reason="Converter needs Python >= 3.10"
)
def test_executemany_with_converter(mocked_connection):
converter = DefaultTypeConverter()
cursor = mocked_connection.cursor(converter=converter)
response = {
"col_types": [4, 5],
"cols": ["name", "address"],
"rows": [["foo", "10.10.10.1"]],
"rowcount": 1,
"duration": 123,
}
with mock.patch.object(
mocked_connection.client, "sql", return_value=response
):
cursor.executemany("", [])
result = cursor.fetchall()
# ``executemany()`` is not intended to be used with statements
# returning result sets. The result will always be empty.
assert result == []
@pytest.mark.skipif(
sys.version_info < (3, 10), reason="Converter needs Python >= 3.10"
)
def test_execute_with_timezone(mocked_connection):
# Create a `Cursor` object with `time_zone`.
tz_mst = datetime.timezone(datetime.timedelta(hours=7), name="MST")
cursor = mocked_connection.cursor(time_zone=tz_mst)
# Make up a response using CrateDB data type `TIMESTAMP`.
response = {
"col_types": [4, 11],
"cols": ["name", "timestamp"],
"rows": [
["foo", 1658167836758],
[None, None],
],
}
with mock.patch.object(
mocked_connection.client, "sql", return_value=response
):
# Run execution and verify the returned `datetime` object is
# timezone-aware, using the designated timezone object.
cursor.execute("")
result = cursor.fetchall()
assert result == [
[
"foo",
datetime.datetime(
2022,
7,
19,
1,
10,
36,
758000,
tzinfo=datetime.timezone(
datetime.timedelta(seconds=25200), "MST"
),
),
],
[
None,
None,
],
]
assert result[0][1].tzname() == "MST"
# Change timezone and verify the returned `datetime` object is using it.
cursor.time_zone = datetime.timezone.utc
cursor.execute("")
result = cursor.fetchall()
assert result == [
[
"foo",
datetime.datetime(
2022,
7,
18,
18,
10,
36,
758000,
tzinfo=datetime.timezone.utc,
),
],
[
None,
None,
],
]
assert result[0][1].tzname() == "UTC"
def test_cursor_close(mocked_connection):
"""
Verify that a cursor is not closed if not specifically closed.
"""
cursor = mocked_connection.cursor()
cursor.execute("")
assert cursor._closed is False
cursor.close()
assert cursor._closed is True
assert not cursor._result
assert cursor.duration == -1
with pytest.raises(ProgrammingError, match="Connection closed"):
mocked_connection.close()
cursor.execute("")
def test_cursor_closes_access(mocked_connection):
"""
Verify that a cursor cannot be used once it is closed.
"""
cursor = mocked_connection.cursor()
cursor.execute("")
cursor.close()
with pytest.raises(ProgrammingError):
cursor.execute("s")