|
| 1 | +"""Executor behavior tests at the real HTTP boundary (respx). |
| 2 | +
|
| 3 | +Unlike tests/core/test_handler.py (which unit-tests against a mocked |
| 4 | +APIHttpClient), these drive execute_operation through the actual httpx |
| 5 | +transport: URL building, headers, body encoding, and response parsing |
| 6 | +are all real. |
| 7 | +""" |
| 8 | + |
| 9 | +import json |
| 10 | +from types import NoneType |
| 11 | + |
| 12 | +import pytest |
| 13 | +from pydantic import BaseModel, ValidationError |
| 14 | + |
| 15 | +from codesphere.core.base import BoundModel, ResourceBase, ResourceList |
| 16 | +from codesphere.core.operations import APIOperation |
| 17 | + |
| 18 | + |
| 19 | +class Thing(BoundModel): |
| 20 | + id: int |
| 21 | + name: str |
| 22 | + |
| 23 | + async def rename(self, name: str) -> None: |
| 24 | + await self._execute(RENAME_THING, thing_id=self.id, data={"name": name}) |
| 25 | + |
| 26 | + |
| 27 | +class ThingCreate(BaseModel): |
| 28 | + name: str |
| 29 | + size: int |
| 30 | + |
| 31 | + |
| 32 | +GET_THING = APIOperation( |
| 33 | + method="GET", endpoint_template="/things/{thing_id}", response_model=Thing |
| 34 | +) |
| 35 | +LIST_THINGS = APIOperation( |
| 36 | + method="GET", endpoint_template="/things", response_model=ResourceList[Thing] |
| 37 | +) |
| 38 | +CREATE_THING = APIOperation( |
| 39 | + method="POST", |
| 40 | + endpoint_template="/things", |
| 41 | + input_model=ThingCreate, |
| 42 | + response_model=Thing, |
| 43 | +) |
| 44 | +RENAME_THING = APIOperation( |
| 45 | + method="PATCH", endpoint_template="/things/{thing_id}", response_model=NoneType |
| 46 | +) |
| 47 | + |
| 48 | + |
| 49 | +class ThingsResource(ResourceBase): |
| 50 | + async def get(self, thing_id: int) -> Thing: |
| 51 | + return await self._execute(GET_THING, thing_id=thing_id) |
| 52 | + |
| 53 | + async def list(self) -> list[Thing]: |
| 54 | + return (await self._execute(LIST_THINGS)).root |
| 55 | + |
| 56 | + async def create(self, payload) -> Thing: |
| 57 | + return await self._execute(CREATE_THING, data=payload) |
| 58 | + |
| 59 | + async def rename(self, thing_id: int, data) -> None: |
| 60 | + await self._execute(RENAME_THING, thing_id=thing_id, data=data) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.fixture |
| 64 | +def things(sdk): |
| 65 | + return ThingsResource(sdk._http_client) |
| 66 | + |
| 67 | + |
| 68 | +class TestRequestConstruction: |
| 69 | + async def test_path_params_format_the_real_url(self, things, mock_api): |
| 70 | + route = mock_api.get("/things/42").respond(200, json={"id": 42, "name": "x"}) |
| 71 | + await things.get(thing_id=42) |
| 72 | + assert route.called |
| 73 | + |
| 74 | + async def test_auth_header_reaches_the_wire(self, things, mock_api): |
| 75 | + route = mock_api.get("/things/1").respond(200, json={"id": 1, "name": "x"}) |
| 76 | + await things.get(thing_id=1) |
| 77 | + request = route.calls.last.request |
| 78 | + assert request.headers["authorization"] == "Bearer test-token" |
| 79 | + |
| 80 | + async def test_model_payload_is_serialized(self, things, mock_api): |
| 81 | + route = mock_api.post("/things").respond(201, json={"id": 1, "name": "a"}) |
| 82 | + await things.create(ThingCreate(name="a", size=3)) |
| 83 | + assert json.loads(route.calls.last.request.content) == {"name": "a", "size": 3} |
| 84 | + |
| 85 | + async def test_dict_payload_is_validated_against_input_model( |
| 86 | + self, things, mock_api |
| 87 | + ): |
| 88 | + route = mock_api.post("/things").respond(201, json={"id": 1, "name": "a"}) |
| 89 | + await things.create({"name": "a", "size": 3}) |
| 90 | + assert json.loads(route.calls.last.request.content) == {"name": "a", "size": 3} |
| 91 | + |
| 92 | + async def test_invalid_payload_never_reaches_the_wire(self, things, mock_api): |
| 93 | + route = mock_api.post("/things").respond(201, json={}) |
| 94 | + with pytest.raises(ValidationError): |
| 95 | + await things.create({"name": "missing size"}) |
| 96 | + assert not route.called |
| 97 | + |
| 98 | + async def test_empty_dict_body_is_sent(self, things, mock_api): |
| 99 | + """Regression: falsy payloads must still be sent as the JSON body.""" |
| 100 | + route = mock_api.patch("/things/1").respond(204) |
| 101 | + await things.rename(1, data={}) |
| 102 | + assert route.calls.last.request.content == b"{}" |
| 103 | + |
| 104 | + |
| 105 | +class TestResponseHandling: |
| 106 | + async def test_response_is_validated_into_model(self, things, mock_api): |
| 107 | + mock_api.get("/things/7").respond(200, json={"id": 7, "name": "seven"}) |
| 108 | + thing = await things.get(thing_id=7) |
| 109 | + assert isinstance(thing, Thing) |
| 110 | + assert thing.name == "seven" |
| 111 | + |
| 112 | + async def test_none_response_model_returns_none(self, things, mock_api): |
| 113 | + mock_api.patch("/things/1").respond(204) |
| 114 | + assert await things.rename(1, data={"name": "n"}) is None |
| 115 | + |
| 116 | + async def test_invalid_response_shape_raises(self, things, mock_api): |
| 117 | + mock_api.get("/things/1").respond(200, json={"unexpected": True}) |
| 118 | + with pytest.raises(ValidationError): |
| 119 | + await things.get(thing_id=1) |
| 120 | + |
| 121 | + async def test_list_items_are_bound_and_can_make_calls(self, things, mock_api): |
| 122 | + mock_api.get("/things").respond( |
| 123 | + 200, json=[{"id": 1, "name": "a"}, {"id": 2, "name": "b"}] |
| 124 | + ) |
| 125 | + rename_route = mock_api.patch("/things/2").respond(204) |
| 126 | + |
| 127 | + items = await things.list() |
| 128 | + await items[1].rename("renamed") |
| 129 | + |
| 130 | + assert rename_route.called |
| 131 | + assert json.loads(rename_route.calls.last.request.content) == { |
| 132 | + "name": "renamed" |
| 133 | + } |
0 commit comments