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

support disable_decoding in async read_response #349

Merged
merged 2 commits into from
Dec 18, 2024
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
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
Loading