|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from types import SimpleNamespace |
| 4 | +from unittest.mock import AsyncMock, Mock |
| 5 | +from urllib.parse import unquote |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from examples.memory import redis_session_example as example |
| 10 | + |
| 11 | +URL = "rediss://synthetic-user:synthetic%2Fpassword@localhost:6380/2?password=query-secret" |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def redis_demo(monkeypatch): |
| 16 | + monkeypatch.setenv("REDIS_URL", URL) |
| 17 | + session = SimpleNamespace( |
| 18 | + ping=AsyncMock(return_value=True), |
| 19 | + clear_session=AsyncMock(), |
| 20 | + get_items=AsyncMock(return_value=[]), |
| 21 | + close=AsyncMock(), |
| 22 | + ) |
| 23 | + factory = Mock(return_value=session) |
| 24 | + monkeypatch.setattr(example.RedisSession, "from_url", factory) |
| 25 | + monkeypatch.setattr( |
| 26 | + example.Runner, "run", AsyncMock(return_value=SimpleNamespace(final_output="Demo response")) |
| 27 | + ) |
| 28 | + return session, factory |
| 29 | + |
| 30 | + |
| 31 | +def assert_safe_output(capsys, caplog): |
| 32 | + captured = capsys.readouterr() |
| 33 | + output = captured.out + captured.err + caplog.text |
| 34 | + for value in (URL, unquote(URL), "synthetic-user", "synthetic%2Fpassword", "query-secret"): |
| 35 | + assert value not in output |
| 36 | + return output |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.asyncio |
| 40 | +async def test_demos_preserve_connection_url_without_printing_it(redis_demo, capsys, caplog): |
| 41 | + session, factory = redis_demo |
| 42 | + await example.main() |
| 43 | + await example.demonstrate_advanced_features() |
| 44 | + |
| 45 | + output = assert_safe_output(capsys, caplog) |
| 46 | + assert "Conversation Complete" in output |
| 47 | + assert "custom key prefix created successfully" in output |
| 48 | + assert factory.call_count == 4 |
| 49 | + assert all(call.kwargs["url"] == URL for call in factory.call_args_list) |
| 50 | + assert factory.call_args_list[2].kwargs["ttl"] == 3600 |
| 51 | + assert factory.call_args_list[3].kwargs["key_prefix"] == "tenant_abc:sessions" |
| 52 | + assert session.close.await_count == 4 |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.asyncio |
| 56 | +@pytest.mark.parametrize("demo", [example.main, example.demonstrate_advanced_features]) |
| 57 | +@pytest.mark.parametrize("failure", ["construct", "operation", "unreachable"]) |
| 58 | +async def test_demo_failures_do_not_render_connection_details( |
| 59 | + redis_demo, capsys, caplog, demo, failure |
| 60 | +): |
| 61 | + session, factory = redis_demo |
| 62 | + # Include decoded data and a chained backend error, not just the complete URL. |
| 63 | + error = ValueError(f"Could not connect: {unquote(URL)}") |
| 64 | + error.__cause__ = RuntimeError("query-secret") |
| 65 | + if failure == "construct": |
| 66 | + factory.side_effect = error |
| 67 | + elif failure == "operation": |
| 68 | + session.ping.side_effect = error |
| 69 | + else: |
| 70 | + session.ping.return_value = False |
| 71 | + |
| 72 | + if demo is example.demonstrate_advanced_features and failure != "unreachable": |
| 73 | + with pytest.raises(SystemExit) as exit_info: |
| 74 | + await demo() |
| 75 | + assert exit_info.value.code == 1 |
| 76 | + assert exit_info.value.__context__ is None |
| 77 | + assert exit_info.value.__cause__ is None |
| 78 | + else: |
| 79 | + await demo() |
| 80 | + |
| 81 | + output = assert_safe_output(capsys, caplog) |
| 82 | + factory.assert_called_once() |
| 83 | + assert factory.call_args.kwargs["url"] == URL |
| 84 | + if failure != "unreachable": |
| 85 | + assert "Check the Redis configuration and connection." in output |
| 86 | + assert "Traceback" not in output |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.asyncio |
| 90 | +async def test_invalid_query_configuration_is_reported_safely(monkeypatch, capsys, caplog): |
| 91 | + # The real client parser includes invalid option values in its exception message. |
| 92 | + monkeypatch.setenv("REDIS_URL", "redis://localhost/0?socket_timeout=query-secret") |
| 93 | + await example.main() |
| 94 | + with pytest.raises(SystemExit) as exit_info: |
| 95 | + await example.demonstrate_advanced_features() |
| 96 | + assert exit_info.value.code == 1 |
| 97 | + assert exit_info.value.__context__ is None |
| 98 | + output = assert_safe_output(capsys, caplog) |
| 99 | + assert output.count("Check the Redis configuration and connection.") == 2 |
0 commit comments