Skip to content

Commit 75c3b35

Browse files
Copilotmsyyc
andauthored
[python] Add mock API tests for service/multiple-services Spector scenario (#10258)
Adds Python SDK mock API tests for the [`service/multiple-services`](Azure/typespec-azure#4158) Spector scenario, which tests that multiple services defined without an explicit `@client` decorator generate separate root clients. ## Changes - **`regenerate-common.ts`**: Added `service/multiple-services` entry with namespace `service.multipleservices` - **New tests** (`test_service_multiple_services.py` + async counterpart): Validates that both `ServiceAClient` and `ServiceBClient` are generated as separate root clients, and verifies `ServiceBClient.operations.op_b()` and `ServiceBClient.sub_namespace.sub_op_b()` — correct `api-version=bv2` succeeds (204), wrong `api-version=bv1` raises `HttpResponseError` - **Changelog entry**: Added internal changelog for `@typespec/http-client-python` --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com> Co-authored-by: Yuchao Yan <yuchaoyan@microsoft.com>
1 parent 4ba1822 commit 75c3b35

4 files changed

Lines changed: 94 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: internal
3+
packages:
4+
- "@typespec/http-client-python"
5+
---
6+
7+
Add mock API tests for `service/multiple-services` scenario (multiple services without explicit `@client`).

packages/http-client-python/eng/scripts/ci/regenerate-common.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ export const BASE_AZURE_EMITTER_OPTIONS: Record<
135135
"service/multi-service": {
136136
namespace: "service.multiservice",
137137
},
138+
"service/multiple-services": {
139+
namespace: "service.multipleservices",
140+
},
138141
};
139142

140143
export const BASE_EMITTER_OPTIONS: Record<
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
import pytest
7+
from azure.core.exceptions import HttpResponseError
8+
from service.multipleservices.aio import ServiceAClient, ServiceBClient
9+
from service.multipleservices.models import VersionsB
10+
11+
12+
@pytest.fixture
13+
def service_b_client():
14+
"""Fixture that creates a ServiceBClient for testing."""
15+
return ServiceBClient(endpoint="http://localhost:3000")
16+
17+
18+
@pytest.mark.asyncio
19+
async def test_service_multiple_services_both_clients_exist():
20+
"""Verify that multiple services without explicit @client create separate root clients."""
21+
async with ServiceAClient(endpoint="http://localhost:3000") as a_client:
22+
assert a_client is not None
23+
async with ServiceBClient(endpoint="http://localhost:3000") as b_client:
24+
assert b_client is not None
25+
26+
27+
@pytest.mark.asyncio
28+
async def test_service_multiple_services_operation_b(service_b_client):
29+
async with service_b_client:
30+
async with ServiceBClient(endpoint="http://localhost:3000", api_version=VersionsB.BV1) as wrong_client:
31+
with pytest.raises(HttpResponseError):
32+
await wrong_client.operations.op_b()
33+
34+
await service_b_client.operations.op_b()
35+
36+
37+
@pytest.mark.asyncio
38+
async def test_service_multiple_services_sub_namespace_operation_b(service_b_client):
39+
async with service_b_client:
40+
async with ServiceBClient(endpoint="http://localhost:3000", api_version=VersionsB.BV1) as wrong_client:
41+
with pytest.raises(HttpResponseError):
42+
await wrong_client.sub_namespace.sub_op_b()
43+
44+
await service_b_client.sub_namespace.sub_op_b()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
import pytest
7+
from azure.core.exceptions import HttpResponseError
8+
from service.multipleservices import ServiceAClient, ServiceBClient
9+
from service.multipleservices.models import VersionsB
10+
11+
12+
@pytest.fixture
13+
def service_b_client():
14+
"""Fixture that creates a ServiceBClient for testing."""
15+
with ServiceBClient(endpoint="http://localhost:3000") as client:
16+
yield client
17+
18+
19+
def test_service_multiple_services_both_clients_exist():
20+
"""Verify that multiple services without explicit @client create separate root clients."""
21+
with ServiceAClient(endpoint="http://localhost:3000") as a_client:
22+
assert a_client is not None
23+
with ServiceBClient(endpoint="http://localhost:3000") as b_client:
24+
assert b_client is not None
25+
26+
27+
def test_service_multiple_services_operation_b(service_b_client):
28+
with ServiceBClient(endpoint="http://localhost:3000", api_version=VersionsB.BV1) as wrong_client:
29+
with pytest.raises(HttpResponseError):
30+
wrong_client.operations.op_b()
31+
32+
service_b_client.operations.op_b()
33+
34+
35+
def test_service_multiple_services_sub_namespace_operation_b(service_b_client):
36+
with ServiceBClient(endpoint="http://localhost:3000", api_version=VersionsB.BV1) as wrong_client:
37+
with pytest.raises(HttpResponseError):
38+
wrong_client.sub_namespace.sub_op_b()
39+
40+
service_b_client.sub_namespace.sub_op_b()

0 commit comments

Comments
 (0)