Skip to content

Commit

Permalink
support disable_decoding in async read_response (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
Redoubts authored Dec 18, 2024
1 parent 7a1ec28 commit 72e2fa9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions fakeredis/aioredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ async def read_response(self, **kwargs: Any) -> Any: # type: ignore
response = await self._reader.read(0) if can_read and self._reader else None
if isinstance(response, redis_async.ResponseError):
raise response
if kwargs.get("disable_decoding", False):
return response
return self._decode(response)

def repr_pieces(self) -> List[Tuple[str, Any]]:
Expand Down
6 changes: 4 additions & 2 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ async def _req_aioredis2(request) -> redis.asyncio.Redis:
server_type, server_version = request.getfixturevalue("real_redis_version")
if request.param != "fake" and not server_version:
pytest.skip("Redis is not running")

decode_responses = bool(request.node.get_closest_marker("decode_responses"))
unsupported_server_types = request.node.get_closest_marker("unsupported_server_types")
if unsupported_server_types and server_type in unsupported_server_types.args:
pytest.skip(f"Server type {server_type} is not supported")
Expand All @@ -131,9 +133,9 @@ async def _req_aioredis2(request) -> redis.asyncio.Redis:
fake_server: Optional[fakeredis.FakeServer]
if request.param == "fake":
fake_server = request.getfixturevalue("fake_server")
ret = fakeredis.FakeAsyncRedis(server=fake_server, lua_modules=lua_modules)
ret = fakeredis.FakeAsyncRedis(server=fake_server, lua_modules=lua_modules, decode_responses=decode_responses)
else:
ret = redis.asyncio.Redis(host="localhost", port=6390, db=2)
ret = redis.asyncio.Redis(host="localhost", port=6390, db=2, decode_responses=decode_responses)
fake_server = None
if not fake_server or fake_server.connected:
await ret.flushall()
Expand Down
11 changes: 11 additions & 0 deletions test/test_asyncredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ async def test_syntax_error(async_redis: redis.asyncio.Redis):
await async_redis.execute_command("get")


@pytest.mark.decode_responses
async def test_never_decode(async_redis: redis.asyncio.Redis):
assert async_redis.connection_pool.get_encoder().decode_responses

await async_redis.execute_command("set", "key", "some ascii")
text = await async_redis.execute_command("get", "key")
assert isinstance(text, str)
bytestr = await async_redis.execute_command("get", "key", NEVER_DECODE=True)
assert isinstance(bytestr, bytes)


@testtools.run_test_if_lupa
class TestScripts:
async def test_no_script_error(self, async_redis: redis.asyncio.Redis):
Expand Down

0 comments on commit 72e2fa9

Please sign in to comment.