Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add equality test on Redis client and conn pool #1240

Merged
merged 2 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,12 @@ def __init__(self, host='localhost', port=6379,
def __repr__(self):
return "%s<%s>" % (type(self).__name__, repr(self.connection_pool))

def __eq__(self, other):
return (
isinstance(other, self.__class__)
and self.connection_pool == other.connection_pool
)

def set_response_callback(self, command, callback):
"Set a custom Response Callback"
self.response_callbacks[command] = callback
Expand Down
6 changes: 6 additions & 0 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,12 @@ def __repr__(self):
repr(self.connection_class(**self.connection_kwargs)),
)

def __eq__(self, other):
return (
isinstance(other, self.__class__)
and self.connection_kwargs == other.connection_kwargs
)

def reset(self):
self.pid = os.getpid()
self._created_connections = 0
Expand Down
27 changes: 27 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import redis


class TestClient(object):
def test_client_equality(self):
r1 = redis.Redis.from_url('redis://localhost:6379/9')
r2 = redis.Redis.from_url('redis://localhost:6379/9')
assert r1 == r2

def test_clients_unequal_if_different_types(self):
r = redis.Redis.from_url('redis://localhost:6379/9')
assert r != 0

def test_clients_unequal_if_different_hosts(self):
r1 = redis.Redis.from_url('redis://localhost:6379/9')
r2 = redis.Redis.from_url('redis://127.0.0.1:6379/9')
assert r1 != r2

def test_clients_unequal_if_different_ports(self):
r1 = redis.Redis.from_url('redis://localhost:6379/9')
r2 = redis.Redis.from_url('redis://localhost:6380/9')
assert r1 != r2

def test_clients_unequal_if_different_dbs(self):
r1 = redis.Redis.from_url('redis://localhost:6379/9')
r2 = redis.Redis.from_url('redis://localhost:6380/10')
assert r1 != r2
41 changes: 41 additions & 0 deletions tests/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,47 @@ def test_repr_contains_db_info_unix(self):
expected = 'ConnectionPool<UnixDomainSocketConnection<path=/abc,db=1>>'
assert repr(pool) == expected

def test_pool_equality(self):
connection_kwargs = {'host': 'localhost', 'port': 6379, 'db': 1}
pool1 = self.get_pool(connection_kwargs=connection_kwargs,
connection_class=redis.Connection)
pool2 = self.get_pool(connection_kwargs=connection_kwargs,
connection_class=redis.Connection)
assert pool1 == pool2

def test_pools_unequal_if_different_types(self):
connection_kwargs = {'host': 'localhost', 'port': 6379, 'db': 1}
pool = self.get_pool(connection_kwargs=connection_kwargs,
connection_class=redis.Connection)
assert pool != 0

def test_pools_unequal_if_different_hosts(self):
connection_kwargs1 = {'host': 'localhost', 'port': 6379, 'db': 1}
connection_kwargs2 = {'host': '127.0.0.1', 'port': 6379, 'db': 1}
pool1 = self.get_pool(connection_kwargs=connection_kwargs1,
connection_class=redis.Connection)
pool2 = self.get_pool(connection_kwargs=connection_kwargs2,
connection_class=redis.Connection)
assert pool1 != pool2

def test_pools_unequal_if_different_ports(self):
connection_kwargs1 = {'host': 'localhost', 'port': 6379, 'db': 1}
connection_kwargs2 = {'host': 'localhost', 'port': 6380, 'db': 1}
pool1 = self.get_pool(connection_kwargs=connection_kwargs1,
connection_class=redis.Connection)
pool2 = self.get_pool(connection_kwargs=connection_kwargs2,
connection_class=redis.Connection)
assert pool1 != pool2

def test_pools_unequal_if_different_dbs(self):
connection_kwargs1 = {'host': 'localhost', 'port': 6379, 'db': 1}
connection_kwargs2 = {'host': 'localhost', 'port': 6379, 'db': 2}
pool1 = self.get_pool(connection_kwargs=connection_kwargs1,
connection_class=redis.Connection)
pool2 = self.get_pool(connection_kwargs=connection_kwargs2,
connection_class=redis.Connection)
assert pool1 != pool2


class TestBlockingConnectionPool(object):
def get_pool(self, connection_kwargs=None, max_connections=10, timeout=20):
Expand Down