-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_codegen.py
More file actions
92 lines (75 loc) · 2.67 KB
/
test_codegen.py
File metadata and controls
92 lines (75 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from __future__ import annotations
from pathlib import Path
from tools.sdk_codegen import (
build_diff_summary,
collect_resources,
load_json,
load_overrides,
normalize_spec,
)
RAW_SPEC_PATH = Path("openapi/public-api.json")
OVERRIDES_PATH = Path("openapi/sdk_overrides.yaml")
def count_operations(spec: dict) -> int:
return sum(
1
for path_item in spec.get("paths", {}).values()
for method in path_item
if not method.startswith("x-")
)
def collect_resource_namespaces(spec: dict) -> set[str]:
return {
operation["x-sdk-resource"]
for path_item in spec.get("paths", {}).values()
for method, operation in path_item.items()
if not method.startswith("x-")
}
def test_normalize_spec_removes_token_and_adds_security_scheme():
normalized = normalize_spec(
load_json(RAW_SPEC_PATH), load_overrides(OVERRIDES_PATH)
)
search_operation = normalized["paths"]["/api/search/v1"]["get"]
assert search_operation["x-sdk-resource"] == "search"
assert search_operation["x-sdk-method-name"] == "search_v1"
assert all(
parameter["name"] != "token" for parameter in search_operation["parameters"]
)
assert normalized["components"]["securitySchemes"]["tokenAuth"]["name"] == "token"
def test_collect_resources_matches_expected_counts():
normalized = normalize_spec(
load_json(RAW_SPEC_PATH), load_overrides(OVERRIDES_PATH)
)
resources = collect_resources(normalized)
expected_namespaces = collect_resource_namespaces(normalized)
namespace_set = {resource.namespace for resource in resources}
assert len(resources) == len(expected_namespaces)
assert sum(len(resource.operations) for resource in resources) == count_operations(
normalized
)
assert namespace_set == expected_namespaces
assert {"douyin", "douyin_xingtu", "tiktok_shop", "xiaohongshu_pgy"}.issubset(
namespace_set
)
def test_diff_summary_reports_operation_changes():
old_spec = {
"paths": {
"/api/demo/v1": {
"get": {"operationId": "demoV1", "summary": "old"},
}
},
"tags": [],
}
new_spec = {
"paths": {
"/api/demo/v1": {
"get": {"operationId": "demoV1", "summary": "new"},
},
"/api/demo/v2": {
"get": {"operationId": "demoV2", "summary": "added"},
},
},
"tags": [],
}
summary = build_diff_summary(old_spec, new_spec)
assert "demoV2 [GET /api/demo/v2]" in summary
assert "demoV1 [GET /api/demo/v1]" in summary
assert "Changed operationId count: 1" in summary