Skip to content

Commit 3d723fd

Browse files
committed
format code
1 parent 7427104 commit 3d723fd

File tree

7 files changed

+144
-121
lines changed

7 files changed

+144
-121
lines changed

src/crate/client/cursor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,10 @@ def _convert_rows(self):
236236

237237
# Process result rows with conversion.
238238
for row in self._result["rows"]:
239-
yield [convert(value) for convert, value
240-
in zip(converters, row, strict=False)]
239+
yield [
240+
convert(value)
241+
for convert, value in zip(converters, row, strict=False)
242+
]
241243

242244
@property
243245
def time_zone(self):

tests/client/test_connection.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ def test_lowest_server_version():
3030
assert (1, 0, 3) == connection.lowest_server_version.version
3131

3232

33-
3433
def test_connection_closes_access():
3534
"""
3635
Verify that a connection closes on exit and that it also closes
3736
the client.
3837
"""
39-
with patch('crate.client.connection.Client',
40-
spec=Client,
41-
return_value=MagicMock()) as client:
42-
38+
with patch(
39+
"crate.client.connection.Client", spec=Client, return_value=MagicMock()
40+
) as client:
4341
conn = connect()
4442
conn.close()
4543

@@ -54,13 +52,15 @@ def test_connection_closes_access():
5452
with pytest.raises(ProgrammingError):
5553
conn.commit()
5654

55+
5756
def test_connection_closes_context_manager():
5857
"""Verify that the context manager of the client closes the connection"""
59-
with patch.object(connect, 'close', autospec=True) as close_fn:
58+
with patch.object(connect, "close", autospec=True) as close_fn:
6059
with connect():
6160
pass
6261
close_fn.assert_called_once()
6362

63+
6464
def test_invalid_server_version():
6565
"""
6666
Verify that when no correct version is set,
@@ -76,7 +76,7 @@ def test_context_manager():
7676
"""
7777
Verify the context manager implementation of `Connection`.
7878
"""
79-
close_method = 'crate.client.http.Client.close'
79+
close_method = "crate.client.http.Client.close"
8080
with patch(close_method, return_value=MagicMock()) as close_func:
8181
with connect("localhost:4200") as conn:
8282
assert not conn._closed
@@ -104,9 +104,12 @@ def test_connection_mock():
104104
connection = connect(crate_host, client=mock)
105105

106106
assert isinstance(connection, Connection)
107-
assert connection.client.server_infos("foo") == ("localhost:4200",
108-
"my server",
109-
"0.42.0")
107+
assert connection.client.server_infos("foo") == (
108+
"localhost:4200",
109+
"my server",
110+
"0.42.0",
111+
)
112+
110113

111114
def test_default_repr():
112115
"""
@@ -115,6 +118,7 @@ def test_default_repr():
115118
conn = connect()
116119
assert repr(conn) == "<Connection <Client ['http://127.0.0.1:4200']>>"
117120

121+
118122
def test_with_timezone():
119123
"""
120124
Verify the logic of passing timezone objects to the client.
@@ -140,7 +144,6 @@ def test_with_timezone():
140144
assert cursor.time_zone.utcoffset(None) == datetime.timedelta(0)
141145

142146

143-
144147
def test_timeout_float():
145148
"""
146149
Verify setting the timeout value as a scalar (float) works.

tests/client/test_cursor.py

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ def test_cursor_fetch(mocked_connection):
4848
"rowcount": 2,
4949
"duration": 123,
5050
}
51-
with mock.patch.object(mocked_connection.client,
52-
'sql',
53-
return_value=response):
51+
with mock.patch.object(
52+
mocked_connection.client, "sql", return_value=response
53+
):
5454
cursor.execute("")
55-
assert cursor.fetchone() == ['foo', '10.10.10.1']
56-
assert cursor.fetchmany() == [["bar", "10.10.10.2"], ]
55+
assert cursor.fetchone() == ["foo", "10.10.10.1"]
56+
assert cursor.fetchmany() == [
57+
["bar", "10.10.10.2"],
58+
]
5759

5860

5961
def test_cursor_executemany(mocked_connection):
@@ -64,25 +66,24 @@ def test_cursor_executemany(mocked_connection):
6466
"col_types": [],
6567
"cols": [],
6668
"duration": 123,
67-
"results": [{'rowcount': 1, 'rowcount:': 1}]
69+
"results": [{"rowcount": 1, "rowcount:": 1}],
6870
}
69-
with mock.patch.object(mocked_connection.client,
70-
'sql',
71-
return_value=response):
72-
71+
with mock.patch.object(
72+
mocked_connection.client, "sql", return_value=response
73+
):
7374
cursor = mocked_connection.cursor()
7475
result = cursor.executemany("some sql", ())
7576

7677
assert isinstance(result, list)
77-
assert response['results'] == result
78+
assert response["results"] == result
7879

7980

8081
def test_create_with_timezone_as_datetime_object(mocked_connection):
8182
"""
82-
The cursor can return timezone-aware `datetime` objects when requested.
83-
Switching the time zone at runtime on the cursor object is possible.
84-
Here: Use a `datetime.timezone` instance.
85-
"""
83+
The cursor can return timezone-aware `datetime` objects when requested.
84+
Switching the time zone at runtime on the cursor object is possible.
85+
Here: Use a `datetime.timezone` instance.
86+
"""
8687
tz_mst = datetime.timezone(datetime.timedelta(hours=7), name="MST")
8788
cursor = mocked_connection.cursor(time_zone=tz_mst)
8889

@@ -101,8 +102,9 @@ def test_create_with_timezone_as_pytz_object(mocked_connection):
101102
Here: Use a `pytz.timezone` instance.
102103
"""
103104

104-
cursor = (mocked_connection
105-
.cursor(time_zone=pytz.timezone("Australia/Sydney")))
105+
cursor = mocked_connection.cursor(
106+
time_zone=pytz.timezone("Australia/Sydney")
107+
)
106108
assert cursor.time_zone.tzname(None) == "Australia/Sydney"
107109

108110
# Apparently, when using `pytz`, the timezone object does not return
@@ -136,8 +138,9 @@ def test_create_with_timezone_as_utc_offset_success(mocked_connection):
136138

137139
cursor = mocked_connection.cursor(time_zone="-1145")
138140
assert cursor.time_zone.tzname(None) == "-1145"
139-
assert cursor.time_zone.utcoffset(None) == datetime.timedelta(days=-1,
140-
seconds=44100)
141+
assert cursor.time_zone.utcoffset(None) == datetime.timedelta(
142+
days=-1, seconds=44100
143+
)
141144

142145

143146
def test_create_with_timezone_as_utc_offset_failure(mocked_connection):
@@ -147,14 +150,15 @@ def test_create_with_timezone_as_utc_offset_failure(mocked_connection):
147150

148151
with pytest.raises(ValueError) as err:
149152
mocked_connection.cursor(time_zone="foobar")
150-
assert err == \
151-
"Time zone 'foobar' is given in invalid UTC offset format"
153+
assert err == "Time zone 'foobar' is given in invalid UTC offset format"
152154

153155
with pytest.raises(ValueError) as err:
154156
mocked_connection.cursor(time_zone="+abcd")
155-
assert err == \
156-
"Time zone '+abcd' is given in invalid UTC offset format: " + \
157-
"invalid literal for int() with base 10: '+ab'"
157+
assert (
158+
err
159+
== "Time zone '+abcd' is given in invalid UTC offset format: "
160+
+ "invalid literal for int() with base 10: '+ab'"
161+
)
158162

159163

160164
def test_create_with_timezone_connection_cursor_precedence(mocked_connection):
@@ -164,7 +168,7 @@ def test_create_with_timezone_connection_cursor_precedence(mocked_connection):
164168
"""
165169
connection = connect(
166170
client=mocked_connection.client,
167-
time_zone=pytz.timezone("Australia/Sydney")
171+
time_zone=pytz.timezone("Australia/Sydney"),
168172
)
169173
cursor = connection.cursor(time_zone="+0530")
170174
assert cursor.time_zone.tzname(None) == "+0530"
@@ -200,8 +204,8 @@ def test_execute_custom_converter(mocked_connection):
200204
converter = DefaultTypeConverter(
201205
{
202206
DataType.BIT: lambda value: value is not None
203-
and int(value[2:-1], 2)
204-
or None
207+
and int(value[2:-1], 2)
208+
or None
205209
}
206210
)
207211
cursor = mocked_connection.cursor(converter=converter)
@@ -216,9 +220,9 @@ def test_execute_custom_converter(mocked_connection):
216220
"duration": 123,
217221
}
218222

219-
with mock.patch.object(mocked_connection.client,
220-
'sql',
221-
return_value=response):
223+
with mock.patch.object(
224+
mocked_connection.client, "sql", return_value=response
225+
):
222226
cursor.execute("")
223227
result = cursor.fetchall()
224228

@@ -258,9 +262,8 @@ def test_execute_with_converter_and_invalid_data_type(mocked_connection):
258262
"duration": 123,
259263
}
260264
with mock.patch.object(
261-
mocked_connection.client,
262-
'sql',
263-
return_value=response):
265+
mocked_connection.client, "sql", return_value=response
266+
):
264267
cursor.execute("")
265268
with pytest.raises(ValueError) as e:
266269
cursor.fetchone()
@@ -277,9 +280,9 @@ def test_execute_array_with_converter(mocked_connection):
277280
"rowcount": 1,
278281
"duration": 123,
279282
}
280-
with mock.patch.object(mocked_connection.client,
281-
'sql',
282-
return_value=response):
283+
with mock.patch.object(
284+
mocked_connection.client, "sql", return_value=response
285+
):
283286
cursor.execute("")
284287
result = cursor.fetchone()
285288

@@ -301,14 +304,15 @@ def test_execute_array_with_converter_invalid(mocked_connection):
301304
}
302305
# Converting collections only works for `ARRAY`s. (ID=100).
303306
# When using `DOUBLE` (ID=6), it should raise an Exception.
304-
with mock.patch.object(mocked_connection.client,
305-
'sql',
306-
return_value=response):
307+
with mock.patch.object(
308+
mocked_connection.client, "sql", return_value=response
309+
):
307310
cursor.execute("")
308311
with pytest.raises(ValueError) as e:
309312
cursor.fetchone()
310-
assert e.exception.args == ("Data type 6 is not implemented"
311-
" as collection type")
313+
assert e.exception.args == (
314+
"Data type 6 is not implemented as collection type"
315+
)
312316

313317

314318
def test_execute_nested_array_with_converter(mocked_connection):
@@ -332,9 +336,9 @@ def test_execute_nested_array_with_converter(mocked_connection):
332336
"duration": 123,
333337
}
334338

335-
with mock.patch.object(mocked_connection.client,
336-
'sql',
337-
return_value=response):
339+
with mock.patch.object(
340+
mocked_connection.client, "sql", return_value=response
341+
):
338342
cursor.execute("")
339343
result = cursor.fetchone()
340344
assert result == [
@@ -358,9 +362,9 @@ def test_executemany_with_converter(mocked_connection):
358362
"rowcount": 1,
359363
"duration": 123,
360364
}
361-
with mock.patch.object(mocked_connection.client,
362-
'sql',
363-
return_value=response):
365+
with mock.patch.object(
366+
mocked_connection.client, "sql", return_value=response
367+
):
364368
cursor.executemany("", [])
365369
result = cursor.fetchall()
366370

@@ -383,9 +387,9 @@ def test_execute_with_timezone(mocked_connection):
383387
[None, None],
384388
],
385389
}
386-
with mock.patch.object(mocked_connection.client,
387-
'sql',
388-
return_value=response):
390+
with mock.patch.object(
391+
mocked_connection.client, "sql", return_value=response
392+
):
389393
# Run execution and verify the returned `datetime` object is
390394
# timezone-aware, using the designated timezone object.
391395
cursor.execute("")
@@ -456,7 +460,7 @@ def test_cursor_close(mocked_connection):
456460
assert not cursor._result
457461
assert cursor.duration == -1
458462

459-
with pytest.raises(ProgrammingError, match='Connection closed'):
463+
with pytest.raises(ProgrammingError, match="Connection closed"):
460464
mocked_connection.close()
461465
cursor.execute("")
462466

tests/client/test_exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def test_error_with_error_trace():
1111
err = Error("foo", error_trace="### TRACE ###")
1212
assert str(err), "foo\n### TRACE ###"
1313

14+
1415
def test_blob_exception():
1516
err = BlobException(table="sometable", digest="somedigest")
1617
assert str(err) == "BlobException('sometable/somedigest)'"

0 commit comments

Comments
 (0)