summaryrefslogtreecommitdiff
path: root/tests/test_encoding.py
diff options
context:
space:
mode:
authorCody-G <codyjgreer@gmail.com>2020-02-08 13:19:17 -0800
committerAndy McCurdy <andy@andymccurdy.com>2020-02-24 15:38:05 -0800
commit3310fe415a4c3a6ec9a9fcaf9a9fa24808219429 (patch)
tree9f02ba16ce16ed29a86a0482a6b237c38ed6752d /tests/test_encoding.py
parent03260002ded57223c5d05b2c99a0a710ee5d34f3 (diff)
downloadredis-py-3310fe415a4c3a6ec9a9fcaf9a9fa24808219429.tar.gz
Support memoryview encoding/decoding as a no-op
This allows memoryview instances to be passed to Redis command args that expect strings or bytes. The memoryview instance is sent directly to the socket such that there are zero copies made of the underlying data during command packing. Fixes #1265 Fixes #1285
Diffstat (limited to 'tests/test_encoding.py')
-rw-r--r--tests/test_encoding.py47
1 files changed, 46 insertions, 1 deletions
diff --git a/tests/test_encoding.py b/tests/test_encoding.py
index 3f43006..ea7db7f 100644
--- a/tests/test_encoding.py
+++ b/tests/test_encoding.py
@@ -3,6 +3,7 @@ import pytest
import redis
from redis._compat import unichr, unicode
+from redis.connection import Connection
from .conftest import _get_client
@@ -11,13 +12,45 @@ class TestEncoding(object):
def r(self, request):
return _get_client(redis.Redis, request=request, decode_responses=True)
- def test_simple_encoding(self, r):
+ @pytest.fixture()
+ def r_no_decode(self, request):
+ return _get_client(
+ redis.Redis,
+ request=request,
+ decode_responses=False,
+ )
+
+ def test_simple_encoding(self, r_no_decode):
+ unicode_string = unichr(3456) + 'abcd' + unichr(3421)
+ r_no_decode['unicode-string'] = unicode_string.encode('utf-8')
+ cached_val = r_no_decode['unicode-string']
+ assert isinstance(cached_val, bytes)
+ assert unicode_string == cached_val.decode('utf-8')
+
+ def test_simple_encoding_and_decoding(self, r):
unicode_string = unichr(3456) + 'abcd' + unichr(3421)
r['unicode-string'] = unicode_string
cached_val = r['unicode-string']
assert isinstance(cached_val, unicode)
assert unicode_string == cached_val
+ def test_memoryview_encoding(self, r_no_decode):
+ unicode_string = unichr(3456) + 'abcd' + unichr(3421)
+ unicode_string_view = memoryview(unicode_string.encode('utf-8'))
+ r_no_decode['unicode-string-memoryview'] = unicode_string_view
+ cached_val = r_no_decode['unicode-string-memoryview']
+ # The cached value won't be a memoryview because it's a copy from Redis
+ assert isinstance(cached_val, bytes)
+ assert unicode_string == cached_val.decode('utf-8')
+
+ def test_memoryview_encoding_and_decoding(self, r):
+ unicode_string = unichr(3456) + 'abcd' + unichr(3421)
+ unicode_string_view = memoryview(unicode_string.encode('utf-8'))
+ r['unicode-string-memoryview'] = unicode_string_view
+ cached_val = r['unicode-string-memoryview']
+ assert isinstance(cached_val, unicode)
+ assert unicode_string == cached_val
+
def test_list_encoding(self, r):
unicode_string = unichr(3456) + 'abcd' + unichr(3421)
result = [unicode_string, unicode_string, unicode_string]
@@ -39,6 +72,18 @@ class TestEncodingErrors(object):
assert r.get('a') == 'foo\ufffd'
+class TestMemoryviewsAreNotPacked(object):
+ def test_memoryviews_are_not_packed(self):
+ c = Connection()
+ arg = memoryview(b'some_arg')
+ arg_list = ['SOME_COMMAND', arg]
+ cmd = c.pack_command(*arg_list)
+ assert cmd[1] is arg
+ cmds = c.pack_commands([arg_list, arg_list])
+ assert cmds[1] is arg
+ assert cmds[3] is arg
+
+
class TestCommandsAreNotEncoded(object):
@pytest.fixture()
def r(self, request):