Skip to content

Commit 0b0ec5a

Browse files
committed
test: test for on_connect to verify correct command sequence and responses connection
1 parent 7723420 commit 0b0ec5a

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

tests/test_connection.py

+50-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import itertools
12
import socket
23
import types
4+
from unittest import TestCase
35
from unittest import mock
4-
from unittest.mock import patch
5-
6+
from unittest.mock import patch, MagicMock
67
import pytest
78
import redis
89
from redis import ConnectionPool, Redis
@@ -13,6 +14,8 @@
1314
SSLConnection,
1415
UnixDomainSocketConnection,
1516
parse_url,
17+
UsernamePasswordCredentialProvider,
18+
AuthenticationError
1619
)
1720
from redis.exceptions import ConnectionError, InvalidResponse, TimeoutError
1821
from redis.retry import Retry
@@ -55,7 +58,7 @@ def inner():
5558
# assert mod.get('fookey') == d
5659

5760

58-
class TestConnection:
61+
class TestConnection(TestCase):
5962
def test_disconnect(self):
6063
conn = Connection()
6164
mock_sock = mock.Mock()
@@ -131,6 +134,50 @@ def test_connect_timeout_error_without_retry(self):
131134
assert str(e.value) == "Timeout connecting to server"
132135
self.clear(conn)
133136

137+
@patch.object(Connection, 'send_command')
138+
@patch.object(Connection, 'read_response')
139+
def test_on_connect(self, mock_read_response, mock_send_command):
140+
"""Test that the on_connect function sends the correct commands"""
141+
conn = Connection()
142+
143+
conn._parser = MagicMock()
144+
conn._parser.on_connect.return_value = None
145+
conn.credential_provider = None
146+
conn.username = "myuser"
147+
conn.password = "password"
148+
conn.protocol = 3
149+
conn.client_name = "test-client"
150+
conn.lib_name = "test"
151+
conn.lib_version = "1234"
152+
conn.db = 0
153+
conn.client_cache = True
154+
155+
# command response
156+
mock_read_response.side_effect = itertools.cycle([
157+
b'QUEUED', # MULTI
158+
b'QUEUED', # HELLO
159+
b'QUEUED', # AUTH
160+
b'QUEUED', # CLIENT SETNAME
161+
b'QUEUED', # CLIENT SETINFO LIB-NAME
162+
b'QUEUED', # CLIENT SETINFO LIB-VER
163+
b'QUEUED', # SELECT
164+
b'QUEUED', # CLIENT TRACKING ON
165+
[ # EXEC response list
166+
{"proto": 3, "version": "6"},
167+
b'OK',
168+
b'OK',
169+
b'OK',
170+
b'OK',
171+
b'OK',
172+
b'OK',
173+
b'OK'
174+
]
175+
])
176+
177+
conn.on_connect()
178+
179+
mock_read_response.side_effect = itertools.repeat("OK")
180+
134181

135182
@pytest.mark.onlynoncluster
136183
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)