Skip to content

Commit d854bbf

Browse files
committed
Rename REQUEST to REQUEST_PATH and move it to conftest.py with fake_response
1 parent c8dfa3a commit d854bbf

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

tests/client/test_http.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
_remove_certs_for_non_https,
5555
json_dumps,
5656
)
57+
from tests.conftest import REQUEST_PATH, fake_response
5758

58-
REQUEST = "crate.client.http.Server.request"
5959
CA_CERT_PATH = certifi.where()
6060

6161
mocked_request = MagicMock(spec=urllib3.response.HTTPResponse)
@@ -75,14 +75,6 @@ def request(*args, **kwargs):
7575
return request
7676

7777

78-
def fake_response(status, reason=None, content_type="application/json"):
79-
m = MagicMock(spec=urllib3.response.HTTPResponse)
80-
m.status = status
81-
m.reason = reason or ""
82-
m.headers = {"content-type": content_type}
83-
return m
84-
85-
8678
def fake_redirect(location: str) -> MagicMock:
8779
m = fake_response(307)
8880
m.get_redirect_location.return_value = location
@@ -117,7 +109,7 @@ def test_connection_reset_exception():
117109

118110
expected_exception_msg = ("No more Servers available,"
119111
" exception from last server: Service Unavailable")
120-
with patch(REQUEST, side_effect=[
112+
with patch(REQUEST_PATH, side_effect=[
121113
fake_response(200),
122114
fake_response(104, "Connection reset by peer"),
123115
fake_response(503, "Service Unavailable"),
@@ -148,7 +140,7 @@ def test_http_error_is_re_raised():
148140
"""
149141
client = Client()
150142

151-
with patch(REQUEST, side_effect=Exception):
143+
with patch(REQUEST_PATH, side_effect=Exception):
152144
client.sql("select foo")
153145
with pytest.raises(ProgrammingError) as e:
154146
client.sql("select foo")
@@ -161,15 +153,15 @@ def test_programming_error_contains_http_error_response_content():
161153
contains the error message from the original error.
162154
"""
163155
expected_msg = "this message should appear"
164-
with patch(REQUEST, side_effect=Exception(expected_msg)):
156+
with patch(REQUEST_PATH, side_effect=Exception(expected_msg)):
165157
client = Client()
166158
with pytest.raises(ProgrammingError, match=expected_msg):
167159
client.sql("select 1")
168160

169161

170162
def test_connect():
171163
"""
172-
Verify the correctness `server` parameter in `Client` instantiation.
164+
Verify the correctness of `server` parameter when `Client` is instantiated.
173165
"""
174166
client = Client(servers="localhost:4200 localhost:4201")
175167
assert client._active_servers == \
@@ -187,7 +179,7 @@ def test_redirect_handling():
187179
"""
188180
Verify that when a redirect happens, that redirect uri gets added to the server pool.
189181
"""
190-
with patch(REQUEST, return_value=fake_redirect("http://localhost:4201")):
182+
with patch(REQUEST_PATH, return_value=fake_redirect("http://localhost:4201")):
191183
client = Client(servers="localhost:4200")
192184

193185
# Don't try to print the exception or use `match`, otherwise
@@ -216,7 +208,7 @@ def test_server_infos():
216208
Verify that when a `MaxRetryError` is raised, a `ConnectionError` is raised.
217209
"""
218210
error = urllib3.exceptions.MaxRetryError(None, "/")
219-
with patch(REQUEST, side_effect=error):
211+
with patch(REQUEST_PATH, side_effect=error):
220212
client = Client(servers="localhost:4200 localhost:4201")
221213
with pytest.raises(ConnectionError):
222214
client.server_infos("http://localhost:4200")
@@ -227,7 +219,7 @@ def test_server_infos_401():
227219
Verify that when a 401 status code is returned, a `ProgrammingError` is raised.
228220
"""
229221
response = fake_response(401, "Unauthorized", "text/html")
230-
with patch(REQUEST, return_value=response):
222+
with patch(REQUEST_PATH, return_value=response):
231223
client = Client(servers="localhost:4200")
232224
with pytest.raises(ProgrammingError, match="401 Client Error: Unauthorized"):
233225
client.server_infos("http://localhost:4200")
@@ -253,7 +245,7 @@ def test_bad_bulk_400():
253245
).encode()
254246

255247
client = Client(servers="localhost:4200")
256-
with patch(REQUEST, return_value=response):
248+
with patch(REQUEST_PATH, return_value=response):
257249
with pytest.raises(ProgrammingError, match='an error occurred\nanother error'):
258250
client.sql(
259251
"Insert into users (name) values(?)",

tests/conftest.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
1+
import urllib3
12
from unittest.mock import MagicMock
23

34
import pytest
45

56
import crate
67

8+
REQUEST_PATH = "crate.client.http.Server.request"
9+
10+
11+
def fake_response(
12+
status: int,
13+
reason: str = None,
14+
content_type: str = "application/json"
15+
) -> MagicMock:
16+
"""
17+
Returns a mocked `urllib3.response.HTTPResponse` HTTP response.
18+
"""
19+
m = MagicMock(spec=urllib3.response.HTTPResponse)
20+
m.status = status
21+
m.reason = reason or ""
22+
m.headers = {"content-type": content_type}
23+
return m
24+
725

826
@pytest.fixture
927
def mocked_connection():
1028
"""
11-
Returns a crate connection with a mocked client
29+
Returns a crate `Connection` with a mocked `Client`
1230
1331
Example:
1432
def test_conn(mocked_connection):
1533
cursor = mocked_connection.cursor()
16-
cursor.execute("select 1")
34+
statement = "select * from locations where position = ?"
35+
cursor.execute(statement, 1)
36+
mocked_connection.client.sql.assert_called_once_with(statement, 1, None)
1737
"""
1838
yield crate.client.connect(client=MagicMock(spec=crate.client.http.Client))

0 commit comments

Comments
 (0)