Skip to content

Commit 132549a

Browse files
fix(python): move verify_request_count to conftest.py in wire tests (#10841)
* fix(python): move verify_request_count to conftest.py in wire tests Co-Authored-By: thomas@buildwithfern.com <tjb9dcshop@gmail.com> * fix: add explicit import for verify_request_count in wire test files Co-Authored-By: thomas@buildwithfern.com <tjb9dcshop@gmail.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 8f56db9 commit 132549a

18 files changed

+72
-445
lines changed

generators/python-v2/sdk/src/wire-tests/WireTestGenerator.ts

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,10 @@ export class WireTestGenerator {
132132

133133
// Add raw imports that the AST doesn't support (simple "import X" statements)
134134
statements.push(python.codeBlock("import pytest"));
135-
statements.push(python.codeBlock("import requests"));
136135

137136
// Add an import registration statement (for "from X import Y" style imports)
138137
statements.push(this.createImportRegistration());
139138

140-
// Add helper function for verifying request count
141-
statements.push(this.generateVerifyRequestCountFunction());
142-
143139
// Add test functions for each endpoint
144140
for (const { endpoint, example, service, exampleIndex } of testCases) {
145141
const testFunction = this.generateEndpointTestFunction(
@@ -170,73 +166,14 @@ export class WireTestGenerator {
170166

171167
// Manually add references for "from X import Y" style imports
172168
// Note: simple "import X" statements are added as raw code blocks separately
173-
node.addReference(python.reference({ name: "Optional", modulePath: ["typing"] }));
174-
node.addReference(python.reference({ name: "Dict", modulePath: ["typing"] }));
175-
node.addReference(python.reference({ name: "Any", modulePath: ["typing"] }));
176-
177169
const clientModulePath = this.getClientModulePath();
178170
const clientName = this.getClientClassName();
179171
node.addReference(python.reference({ name: clientName, modulePath: clientModulePath }));
180172

181-
return node;
182-
}
183-
184-
// =============================================================================
185-
// HELPER FUNCTION GENERATION
186-
// =============================================================================
173+
// Import verify_request_count from conftest (pytest makes conftest importable)
174+
node.addReference(python.reference({ name: "verify_request_count", modulePath: ["conftest"] }));
187175

188-
private generateVerifyRequestCountFunction(): python.Method {
189-
const params = [
190-
python.parameter({
191-
name: "test_id",
192-
type: python.Type.str()
193-
}),
194-
python.parameter({
195-
name: "method",
196-
type: python.Type.str()
197-
}),
198-
python.parameter({
199-
name: "url_path",
200-
type: python.Type.str()
201-
}),
202-
python.parameter({
203-
name: "query_params",
204-
type: python.Type.optional(python.Type.dict(python.Type.str(), python.Type.str()))
205-
}),
206-
python.parameter({
207-
name: "expected",
208-
type: python.Type.int()
209-
})
210-
];
211-
212-
const statements = [
213-
python.codeBlock(`wiremock_admin_url = "http://localhost:8080/__admin"`),
214-
python.codeBlock(`request_body: Dict[str, Any] = {
215-
"method": method,
216-
"urlPath": url_path,
217-
"headers": {"X-Test-Id": {"equalTo": test_id}}
218-
}`),
219-
python.codeBlock(`if query_params:
220-
query_parameters = {k: {"equalTo": v} for k, v in query_params.items()}
221-
request_body["queryParameters"] = query_parameters`),
222-
python.codeBlock(`response = requests.post(f"{wiremock_admin_url}/requests/find", json=request_body)`),
223-
python.codeBlock(`assert response.status_code == 200, "Failed to query WireMock requests"`),
224-
python.codeBlock(`result = response.json()`),
225-
python.codeBlock(`requests_found = len(result.get("requests", []))`),
226-
python.codeBlock(
227-
`assert requests_found == expected, f"Expected {expected} requests, found {requests_found}"`
228-
)
229-
];
230-
231-
const method = python.method({
232-
name: "verify_request_count",
233-
parameters: params,
234-
return_: python.Type.none(),
235-
docstring: "Verifies the number of requests made to WireMock filtered by test ID for concurrency safety"
236-
});
237-
238-
statements.forEach((stmt) => method.addStatement(stmt));
239-
return method;
176+
return node;
240177
}
241178

242179
// =============================================================================

generators/python-v2/sdk/src/wire-tests/WireTestSetupGenerator.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ This module manages the WireMock container lifecycle for integration tests.
101101
"""
102102
import os
103103
import subprocess
104+
from typing import Any, Dict, Optional
105+
104106
import pytest
107+
import requests
105108
106109
107110
@pytest.fixture(scope="session", autouse=True)
@@ -143,6 +146,30 @@ def wiremock_container():
143146
check=False,
144147
capture_output=True
145148
)
149+
150+
151+
def verify_request_count(
152+
test_id: str,
153+
method: str,
154+
url_path: str,
155+
query_params: Optional[Dict[str, str]],
156+
expected: int,
157+
) -> None:
158+
"""Verifies the number of requests made to WireMock filtered by test ID for concurrency safety"""
159+
wiremock_admin_url = "http://localhost:8080/__admin"
160+
request_body: Dict[str, Any] = {
161+
"method": method,
162+
"urlPath": url_path,
163+
"headers": {"X-Test-Id": {"equalTo": test_id}}
164+
}
165+
if query_params:
166+
query_parameters = {k: {"equalTo": v} for k, v in query_params.items()}
167+
request_body["queryParameters"] = query_parameters
168+
response = requests.post(f"{wiremock_admin_url}/requests/find", json=request_body)
169+
assert response.status_code == 200, "Failed to query WireMock requests"
170+
result = response.json()
171+
requests_found = len(result.get("requests", []))
172+
assert requests_found == expected, f"Expected {expected} requests, found {requests_found}"
146173
`;
147174
}
148175
}

seed/python-sdk/exhaustive/no-custom-config/tests/wire/conftest.py

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

seed/python-sdk/exhaustive/no-custom-config/tests/wire/test_endpoints_container.py

Lines changed: 1 addition & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

seed/python-sdk/exhaustive/no-custom-config/tests/wire/test_endpoints_contentType.py

Lines changed: 1 addition & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

seed/python-sdk/exhaustive/no-custom-config/tests/wire/test_endpoints_enum.py

Lines changed: 1 addition & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

seed/python-sdk/exhaustive/no-custom-config/tests/wire/test_endpoints_httpMethods.py

Lines changed: 1 addition & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

seed/python-sdk/exhaustive/no-custom-config/tests/wire/test_endpoints_object.py

Lines changed: 1 addition & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

seed/python-sdk/exhaustive/no-custom-config/tests/wire/test_endpoints_params.py

Lines changed: 1 addition & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)