Skip to content

Commit 10bfaec

Browse files
committed
Add more tests
1 parent 65255bc commit 10bfaec

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

tests/client/test_connection.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import datetime
22
from unittest.mock import MagicMock, patch
33

4+
import pytest
45
from urllib3 import Timeout
56

67
from crate.client import connect
78
from crate.client.connection import Connection
9+
from crate.client.exceptions import ProgrammingError
810
from crate.client.http import Client
911

1012
from .settings import crate_host
@@ -28,6 +30,37 @@ def test_lowest_server_version():
2830
assert (1, 0, 3) == connection.lowest_server_version.version
2931

3032

33+
34+
def test_connection_closes_access():
35+
"""
36+
Verify that a connection closes on exit and that it also closes
37+
the client.
38+
"""
39+
with patch('crate.client.connection.Client',
40+
spec=Client,
41+
return_value=MagicMock()) as client:
42+
43+
conn = connect()
44+
conn.close()
45+
46+
assert conn._closed
47+
client.assert_called_once()
48+
49+
# Should raise an exception if
50+
# we try to access a cursor now.
51+
with pytest.raises(ProgrammingError):
52+
conn.cursor()
53+
54+
with pytest.raises(ProgrammingError):
55+
conn.commit()
56+
57+
def test_connection_closes_context_manager():
58+
"""Verify that the context manager of the client closes the connection"""
59+
with patch.object(connect, 'close', autospec=True) as close_fn:
60+
with connect():
61+
pass
62+
close_fn.assert_called_once()
63+
3164
def test_invalid_server_version():
3265
"""
3366
Verify that when no correct version is set,

tests/client/test_cursor.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ def test_execute_with_args(mocked_connection):
141141
cursor.execute(statement, 1)
142142
mocked_connection.client.sql.assert_called_once_with(statement, 1, None)
143143

144-
145144
def test_execute_with_bulk_args(mocked_connection):
146145
"""
147146
Verify that `cursor.execute` is called with the right parameters
@@ -177,10 +176,9 @@ def test_execute_custom_converter(mocked_connection):
177176
"duration": 123,
178177
}
179178

180-
with mock.patch.object(
181-
mocked_connection.client,
182-
'sql',
183-
return_value=response):
179+
with mock.patch.object(mocked_connection.client,
180+
'sql',
181+
return_value=response):
184182
cursor.execute("")
185183
result = cursor.fetchall()
186184

@@ -222,8 +220,7 @@ def test_execute_with_converter_and_invalid_data_type(mocked_connection):
222220
with mock.patch.object(
223221
mocked_connection.client,
224222
'sql',
225-
return_value=response
226-
):
223+
return_value=response):
227224
cursor.execute("")
228225
with pytest.raises(ValueError) as e:
229226
cursor.fetchone()
@@ -240,10 +237,9 @@ def test_execute_array_with_converter(mocked_connection):
240237
"rowcount": 1,
241238
"duration": 123,
242239
}
243-
with mock.patch.object(
244-
mocked_connection.client,
245-
'sql',
246-
return_value=response):
240+
with mock.patch.object(mocked_connection.client,
241+
'sql',
242+
return_value=response):
247243
cursor.execute("")
248244
result = cursor.fetchone()
249245

0 commit comments

Comments
 (0)