Skip to content

Commit d657027

Browse files
committed
Unifying the exception msg validation pattern for both test_connection.py files
1 parent 12e5055 commit d657027

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

tests/test_asyncio/test_connection.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ async def test_invalid_response(create_redis):
3737
fake_stream = MockStream(raw + b"\r\n")
3838

3939
parser: _AsyncRESPBase = r.connection._parser
40-
with mock.patch.object(parser, "_stream", fake_stream):
41-
with pytest.raises(InvalidResponse) as cm:
42-
await parser.read_response()
40+
4341
if isinstance(parser, _AsyncRESPBase):
44-
assert str(cm.value) == f"Protocol Error: {raw!r}"
42+
exp_err = f"Protocol Error: {raw!r}"
4543
else:
46-
assert (
47-
str(cm.value) == f'Protocol error, got "{raw.decode()}" as reply type byte'
48-
)
44+
exp_err = f'Protocol error, got "{raw.decode()}" as reply type byte'
45+
46+
with mock.patch.object(parser, "_stream", fake_stream):
47+
with pytest.raises(InvalidResponse, match=exp_err):
48+
await parser.read_response()
49+
4950
await r.connection.disconnect()
5051

5152

@@ -171,10 +172,9 @@ async def test_connect_timeout_error_without_retry():
171172
conn._connect = mock.AsyncMock()
172173
conn._connect.side_effect = socket.timeout
173174

174-
with pytest.raises(TimeoutError) as e:
175+
with pytest.raises(TimeoutError, match="Timeout connecting to server"):
175176
await conn.connect()
176177
assert conn._connect.call_count == 1
177-
assert str(e.value) == "Timeout connecting to server"
178178

179179

180180
@pytest.mark.onlynoncluster
@@ -532,17 +532,14 @@ async def test_format_error_message(conn, error, expected_message):
532532

533533

534534
async def test_network_connection_failure():
535-
exp_err = fr"^Error {ECONNREFUSED} connecting to 127.0.0.1:9999.(.+)$"
535+
exp_err = rf"^Error {ECONNREFUSED} connecting to 127.0.0.1:9999.(.+)$"
536536
with pytest.raises(ConnectionError, match=exp_err):
537537
redis = Redis(host="127.0.0.1", port=9999)
538538
await redis.set("a", "b")
539539

540540

541541
async def test_unix_socket_connection_failure():
542-
with pytest.raises(ConnectionError) as e:
542+
exp_err = "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
543+
with pytest.raises(ConnectionError, match=exp_err):
543544
redis = Redis(unix_socket_path="unix:///tmp/a.sock")
544545
await redis.set("a", "b")
545-
assert (
546-
str(e.value)
547-
== "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
548-
)

tests/test_connection.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ def test_invalid_response(r):
4545
raw = b"x"
4646
parser = r.connection._parser
4747
with mock.patch.object(parser._buffer, "readline", return_value=raw):
48-
with pytest.raises(InvalidResponse) as cm:
48+
with pytest.raises(InvalidResponse, match=f"Protocol Error: {raw!r}"):
4949
parser.read_response()
50-
assert str(cm.value) == f"Protocol Error: {raw!r}"
5150

5251

5352
@skip_if_server_version_lt("4.0.0")
@@ -142,10 +141,9 @@ def test_connect_timeout_error_without_retry(self):
142141
conn._connect = mock.Mock()
143142
conn._connect.side_effect = socket.timeout
144143

145-
with pytest.raises(TimeoutError) as e:
144+
with pytest.raises(TimeoutError, match="Timeout connecting to server"):
146145
conn.connect()
147146
assert conn._connect.call_count == 1
148-
assert str(e.value) == "Timeout connecting to server"
149147
self.clear(conn)
150148

151149

@@ -357,13 +355,10 @@ def test_network_connection_failure():
357355

358356

359357
def test_unix_socket_connection_failure():
360-
with pytest.raises(ConnectionError) as e:
358+
exp_err = "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
359+
with pytest.raises(ConnectionError, match=exp_err):
361360
redis = Redis(unix_socket_path="unix:///tmp/a.sock")
362361
redis.set("a", "b")
363-
assert (
364-
str(e.value)
365-
== "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
366-
)
367362

368363

369364
class TestUnitConnectionPool:

0 commit comments

Comments
 (0)