|
70 | 70 | "component": None, |
71 | 71 | "resource_owner": None, |
72 | 72 | "current_revision": "rev-1", |
| 73 | + "external_id": None, |
73 | 74 | } |
74 | 75 |
|
75 | 76 | REVISION_JSON = { |
@@ -1099,6 +1100,107 @@ def handler(request: httpx.Request) -> httpx.Response: |
1099 | 1100 | await client.aclose() |
1100 | 1101 |
|
1101 | 1102 |
|
| 1103 | +@pytest.mark.asyncio |
| 1104 | +async def test_async_create_resource_with_external_id(): |
| 1105 | + captured: dict[str, Any] = {} |
| 1106 | + |
| 1107 | + def handler(request: httpx.Request) -> httpx.Response: |
| 1108 | + captured["url"] = str(request.url) |
| 1109 | + captured["body"] = json.loads(request.content.decode()) |
| 1110 | + resource_json = {**RESOURCE_JSON, "external_id": "ext-1"} |
| 1111 | + return httpx.Response(201, json=resource_json) |
| 1112 | + |
| 1113 | + client = build_async_management_client(handler) |
| 1114 | + result = await client.create_resource( |
| 1115 | + "folder-1", {"data": {"title": "Hello"}}, external_id="ext-1" |
| 1116 | + ) |
| 1117 | + assert result.key == "resource-1" |
| 1118 | + assert result.external_id == "ext-1" |
| 1119 | + assert captured["body"]["external_id"] == "ext-1" |
| 1120 | + assert captured["body"]["data"]["title"] == "Hello" |
| 1121 | + # external_id goes in the body, not in query params |
| 1122 | + assert "external_id=" not in captured["url"] |
| 1123 | + await client.aclose() |
| 1124 | + |
| 1125 | + |
| 1126 | +@pytest.mark.asyncio |
| 1127 | +async def test_async_create_resource_without_external_id_omits_field(): |
| 1128 | + captured: dict[str, Any] = {} |
| 1129 | + |
| 1130 | + def handler(request: httpx.Request) -> httpx.Response: |
| 1131 | + captured["body"] = json.loads(request.content.decode()) |
| 1132 | + return httpx.Response(201, json=RESOURCE_JSON) |
| 1133 | + |
| 1134 | + client = build_async_management_client(handler) |
| 1135 | + await client.create_resource("folder-1", {"data": {"title": "Hello"}}) |
| 1136 | + assert "external_id" not in captured["body"] |
| 1137 | + await client.aclose() |
| 1138 | + |
| 1139 | + |
| 1140 | +@pytest.mark.asyncio |
| 1141 | +async def test_async_create_resource_does_not_mutate_payload(): |
| 1142 | + def handler(request: httpx.Request) -> httpx.Response: |
| 1143 | + return httpx.Response(201, json=RESOURCE_JSON) |
| 1144 | + |
| 1145 | + client = build_async_management_client(handler) |
| 1146 | + original = {"data": {"title": "Hello"}} |
| 1147 | + await client.create_resource("folder-1", original, external_id="ext-1") |
| 1148 | + assert "external_id" not in original |
| 1149 | + await client.aclose() |
| 1150 | + |
| 1151 | + |
| 1152 | +@pytest.mark.asyncio |
| 1153 | +async def test_async_upsert_resource_sends_put_with_external_id(): |
| 1154 | + captured: dict[str, Any] = {} |
| 1155 | + |
| 1156 | + def handler(request: httpx.Request) -> httpx.Response: |
| 1157 | + captured["method"] = request.method |
| 1158 | + captured["url"] = str(request.url) |
| 1159 | + captured["body"] = json.loads(request.content.decode()) |
| 1160 | + resource_json = {**RESOURCE_JSON, "external_id": "my-ext-id"} |
| 1161 | + return httpx.Response(200, json=resource_json) |
| 1162 | + |
| 1163 | + client = build_async_management_client(handler) |
| 1164 | + result = await client.upsert_resource( |
| 1165 | + "folder-1", |
| 1166 | + {"data": {"title": "Upserted"}}, |
| 1167 | + external_id="my-ext-id", |
| 1168 | + ) |
| 1169 | + assert captured["method"] == "PUT" |
| 1170 | + assert captured["url"].endswith("/resources/?external_id=my-ext-id") |
| 1171 | + assert captured["body"]["data"]["title"] == "Upserted" |
| 1172 | + # upsert sends external_id as query param, not in body |
| 1173 | + assert "external_id" not in captured["body"] |
| 1174 | + assert result.key == "resource-1" |
| 1175 | + assert result.external_id == "my-ext-id" |
| 1176 | + await client.aclose() |
| 1177 | + |
| 1178 | + |
| 1179 | +@pytest.mark.asyncio |
| 1180 | +async def test_async_upsert_resource_with_component(): |
| 1181 | + captured: dict[str, Any] = {} |
| 1182 | + |
| 1183 | + def handler(request: httpx.Request) -> httpx.Response: |
| 1184 | + captured["url"] = str(request.url) |
| 1185 | + captured["method"] = request.method |
| 1186 | + resource_json = {**RESOURCE_JSON, "external_id": "ext-2", "component": "comp-1"} |
| 1187 | + return httpx.Response(201, json=resource_json) |
| 1188 | + |
| 1189 | + client = build_async_management_client(handler) |
| 1190 | + result = await client.upsert_resource( |
| 1191 | + "folder-1", |
| 1192 | + {"data": {"title": "New"}}, |
| 1193 | + external_id="ext-2", |
| 1194 | + component="comp-1", |
| 1195 | + ) |
| 1196 | + assert captured["method"] == "PUT" |
| 1197 | + assert "external_id=ext-2" in captured["url"] |
| 1198 | + assert "component=comp-1" in captured["url"] |
| 1199 | + assert result.external_id == "ext-2" |
| 1200 | + assert result.component == "comp-1" |
| 1201 | + await client.aclose() |
| 1202 | + |
| 1203 | + |
1102 | 1204 | @pytest.mark.asyncio |
1103 | 1205 | async def test_async_update_delete_resource_and_get_data(): |
1104 | 1206 | captured: list[tuple[str, str]] = [] |
|
0 commit comments