summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy McCurdy <andy@andymccurdy.com>2020-08-15 17:23:52 -0700
committerAndy McCurdy <andy@andymccurdy.com>2020-08-15 17:23:52 -0700
commit997e205beba545409aa697a13e164d687ce85509 (patch)
tree4cbf09169c0f7bb72bf828bea8432fa4e3c0b4d8
parente80aa3d951226e501c9e7c95a25731d1d663b4aa (diff)
downloadredis-py-acl-log.tar.gz
Make _get_client more configurable with kwargs that override url optionsacl-log
This allows tests to create clients with specific configurations based on the specified --redis-url.
-rw-r--r--tests/conftest.py29
-rw-r--r--tests/test_commands.py21
2 files changed, 34 insertions, 16 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 26893db..cd4d489 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -2,6 +2,7 @@ import pytest
import random
import redis
from distutils.version import StrictVersion
+from redis.connection import parse_url
from unittest.mock import Mock
from urllib.parse import urlparse
@@ -60,19 +61,31 @@ def skip_unless_arch_bits(arch_bits):
reason="server is not {}-bit".format(arch_bits))
-def _get_client(cls, request, single_connection_client=True, **kwargs):
+def _get_client(cls, request, single_connection_client=True, flushdb=True,
+ **kwargs):
+ """
+ Helper for fixtures or tests that need a Redis client
+
+ Uses the "--redis-url" command line argument for connection info. Unlike
+ ConnectionPool.from_url, keyword arguments to this function override
+ values specified in the URL.
+ """
redis_url = request.config.getoption("--redis-url")
- client = cls.from_url(redis_url, **kwargs)
+ url_options = parse_url(redis_url)
+ url_options.update(kwargs)
+ pool = redis.ConnectionPool(**url_options)
+ client = cls(connection_pool=pool)
if single_connection_client:
client = client.client()
if request:
def teardown():
- try:
- client.flushdb()
- except redis.ConnectionError:
- # handle cases where a test disconnected a client
- # just manually retry the flushdb
- client.flushdb()
+ if flushdb:
+ try:
+ client.flushdb()
+ except redis.ConnectionError:
+ # handle cases where a test disconnected a client
+ # just manually retry the flushdb
+ client.flushdb()
client.close()
client.connection_pool.disconnect()
request.addfinalizer(teardown)
diff --git a/tests/test_commands.py b/tests/test_commands.py
index 5507f2c..2113078 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -9,8 +9,13 @@ from string import ascii_letters
from redis.client import parse_info
from redis import exceptions
-from .conftest import (skip_if_server_version_lt, skip_if_server_version_gte,
- skip_unless_arch_bits, REDIS_6_VERSION)
+from .conftest import (
+ _get_client,
+ REDIS_6_VERSION,
+ skip_if_server_version_gte,
+ skip_if_server_version_lt,
+ skip_unless_arch_bits,
+)
@pytest.fixture()
@@ -206,20 +211,20 @@ class TestRedisCommands:
keys=['cache:*'], nopass=True)
r.acl_log_reset()
- r_test = redis.Redis(host='master', port=6379, db=9,
- username=username)
+ user_client = _get_client(redis.Redis, request, flushdb=False,
+ username=username)
# Valid operation and key
- r_test.set('cache:0', 1)
- r_test.get('cache:0')
+ assert user_client.set('cache:0', 1)
+ assert user_client.get('cache:0') == b'1'
# Invalid key
with pytest.raises(exceptions.NoPermissionError):
- r_test.get('violated_cache:0')
+ user_client.get('violated_cache:0')
# Invalid operation
with pytest.raises(exceptions.NoPermissionError):
- r_test.hset('cache:0', 'hkey', 'hval')
+ user_client.hset('cache:0', 'hkey', 'hval')
assert isinstance(r.acl_log(), list)
assert len(r.acl_log()) == 2