test_connect.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """Unit tests for the hand-rolled Connect client (chattolib._connect)."""
  2. import asyncio
  3. import pytest
  4. from chattolib._connect import ConnectClientSync
  5. from chattolib.exceptions import ChattoError
  6. def test_sync_client_round_trips_through_async(monkeypatch):
  7. """A sync execute_unary delegates to the shared async execute_unary."""
  8. from google.protobuf import struct_pb2
  9. from chattolib._connect import MethodInfo
  10. captured = {}
  11. async def fake_execute_unary(self, *, request, method, headers=None):
  12. captured["request"] = request
  13. captured["method"] = method
  14. return request # echo the request back as the "response"
  15. monkeypatch.setattr("chattolib._connect.ConnectClient.execute_unary", fake_execute_unary)
  16. client = ConnectClientSync("https://example.test")
  17. msg = struct_pb2.Struct()
  18. out = client.execute_unary(
  19. request=msg,
  20. method=MethodInfo(name="M", service_name="s.S", input=msg, output=msg),
  21. )
  22. assert out is msg
  23. assert captured["request"] is msg
  24. client.close()
  25. def test_sync_client_refuses_inside_running_loop():
  26. """Calling the sync client from within a running loop raises ChattoError."""
  27. client = ConnectClientSync("https://example.test")
  28. async def call_from_async():
  29. from google.protobuf import struct_pb2
  30. from chattolib._connect import MethodInfo
  31. msg = struct_pb2.Struct()
  32. client.execute_unary(
  33. request=msg,
  34. method=MethodInfo(name="M", service_name="s.S", input=msg, output=msg),
  35. )
  36. with pytest.raises(ChattoError):
  37. asyncio.run(call_from_async())