Repository navigation
Expand file tree
/
Copy pathtest_api_contract_docs.py
More file actions
331 lines (293 loc) · 10.5 KB
/
Copy pathtest_api_contract_docs.py
File metadata and controls
331 lines (293 loc) · 10.5 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"""Development-only semantic guards for the canonical REST API contract."""
from __future__ import annotations
import re
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[2]
API_ROOT = REPO_ROOT / "docs" / "api"
EXPECTED_ENDPOINTS = {
"endpoints-meta.md": {
"GET /api/meta/tickers/search",
"GET /api/meta/tickers/list",
},
"endpoints-prices.md": {
"GET /api/a-share/prices/snapshot",
"GET /api/a-share/prices/historical",
"GET /api/a-share/corporate-actions/adjustment-factors",
},
"endpoints-financials.md": {
"GET /api/a-share/financials/income-statements",
"GET /api/a-share/financials/balance-sheets",
"GET /api/a-share/financials/cash-flow-statements",
"GET /api/a-share/financials/indicators",
},
"endpoints-valuations.md": {
"GET /api/a-share/valuations/snapshot",
},
"endpoints-calendar.md": {"GET /api/a-share/calendar/trading-days"},
"endpoints-auction.md": {
"GET /api/a-share/auction/snapshot",
"GET /api/a-share/auction/short-term-benchmark",
},
"endpoints-index.md": {
"GET /api/a-share-index/catalog/ths-index-list",
"GET /api/a-share-index/constituents/ths-stock-list",
"GET /api/a-share-index/prices/snapshot",
"GET /api/a-share-index/prices/historical",
},
"endpoints-special-data.md": {
"GET /api/a-share/special-data/limit-up-pool",
"GET /api/a-share/special-data/limit-down-pool",
"GET /api/a-share/special-data/limit-break-pool",
"GET /api/a-share/special-data/limit-up-ladder",
"GET /api/a-share/special-data/anomaly-analysis-list",
"GET /api/a-share/special-data/anomaly-analysis-stock",
"GET /api/a-share/special-data/skyrocket-list",
"GET /api/a-share/special-data/hot-stock-list",
"GET /api/a-share/special-data/hot-stock-list-history",
"GET /api/a-share/special-data/hot-stock-rank-trend",
"GET /api/a-share/special-data/dragon-tiger-list",
},
"endpoints-fund.md": {
"GET /api/fund/profile/detail",
"GET /api/fund/portfolio/holdings",
"GET /api/fund/performance/nav",
"GET /api/fund/performance/returns",
"GET /api/fund/holders/detail",
"GET /api/fund/market/snapshot",
"GET /api/fund/market/historical",
"GET /api/fund/companies/detail",
"GET /api/fund/portfolio/industry-allocation",
"GET /api/fund/performance/indicators-historical",
"GET /api/fund/performance/drawdowns",
"GET /api/fund/holders/top",
"GET /api/fund/corporate-actions/dividends",
"GET /api/fund/diagnostics/detail",
"GET /api/fund/financials/indicators",
"GET /api/fund/financials/income-statements",
"GET /api/fund/financials/balance-sheets",
"GET /api/fund/managers/investment-style",
"GET /api/fund/managers/performance",
"GET /api/fund/managers/experience",
"GET /api/fund/managers/detail",
"GET /api/fund/news/article-list",
"GET /api/fund/offerings/list",
"GET /api/fund/portfolio/stock-history",
"GET /api/fund/portfolio/stock-report-dates",
"GET /api/fund/portfolio/bond-history",
"GET /api/fund/portfolio/bond-report-dates",
"GET /api/fund/portfolio/asset-allocation",
},
"endpoints-market-dumps.md": {
"GET /api/dump/market-dumps/daily-k/download-url",
"GET /api/dump/market-dumps/daily-k-10d/download-url",
"GET /api/dump/market-dumps/adjustment-factors/download-url",
},
}
def read(filename: str) -> str:
return (API_ROOT / filename).read_text(encoding="utf-8")
def test_all_59_endpoints_are_documented_once() -> None:
assert sum(map(len, EXPECTED_ENDPOINTS.values())) == 59
for filename in EXPECTED_ENDPOINTS:
assert (API_ROOT / filename).is_file(), filename
combined = "\n".join(read(filename) for filename in EXPECTED_ENDPOINTS)
for filename, endpoints in EXPECTED_ENDPOINTS.items():
text = read(filename)
for endpoint in endpoints:
assert re.search(rf"```\w*\s*\n\s*{re.escape(endpoint)}", text)
assert len(re.findall(rf"^{re.escape(endpoint)}$", combined, re.M)) == 1
def test_every_endpoint_group_has_examples_and_avoidance_guidance() -> None:
for filename in EXPECTED_ENDPOINTS:
text = read(filename)
assert "curl" in text, filename
assert "X-api-key" in text, filename
assert "避错要点" in text, filename
def test_financial_fields_match_the_validated_contract() -> None:
financials = read("endpoints-financials.md")
for field in (
"operating_income",
"parent_holder_net_profit",
"operating_profit",
"net_profit",
"basic_eps",
"research_and_development_expenses",
"assets_total",
"total_debt",
"holder_equity_total",
"accounts_receivable",
"act_cash_flow_net",
"financing_cash_flow_net",
"invest_cash_flow_net",
):
assert field in financials
for fictional in ("total_operate_income", "np_parent_company_owners"):
assert fictional not in financials
assert "array" in financials.lower() or "list" in financials.lower()
assert "ability" in financials and "indicators" in financials
assert 'abilities["growth"]' in financials
def test_special_data_and_index_edge_contracts_are_preserved() -> None:
special = read("endpoints-special-data.md")
index = read("endpoints-index.md")
for field in (
"rank_trend",
"rank_change",
"heat",
"buy_value",
"sell_value",
"net_value",
"net_rate",
"org_net_value",
"hot_money_net_value",
"hot_rank",
"range_days",
"limit_reason",
"concept_list",
"change",
"board_num",
"sign_level",
"seal_nextday",
):
assert field in special
for fictional in ("analyse_title", "`analyse`", "`tags`", "`topic`"):
assert fictional not in special
for required in (
"limit-down-pool",
"limit-break-pool",
"last_limit_time",
"first_limit_time",
"open_times",
"turnover_ratio_pct",
"1–200",
):
assert required in special
assert "1d" in index
assert "固定" in index or "fixed" in index.lower()
assert "5003" not in index
def test_fund_and_meta_contracts_preserve_backend_boundaries() -> None:
fund = read("endpoints-fund.md")
meta = read("endpoints-meta.md")
entry = read("README.md")
holders = fund.split("## 5. 基金持有人结构", maxsplit=1)[1].split(
"## 6. 场内基金行情快照", maxsplit=1
)[0]
for value in (
"otc",
"exchange",
"reits",
"unit,adj",
"twoyear",
"fund_name",
"hold_ratio",
"nav_date",
"return_now",
"ins_position",
"merge_scope",
"report_date_ms",
"all",
"merged",
"separate",
"turnover_ratio_pct",
"3001",
"3002",
"3004",
"5 年",
"company_id",
"manager_info",
"trade_rule",
"rate_info",
"total_stock_ratio_pct",
"total_bond_ratio_pct",
"total_fund_ratio_pct",
"peer_average_week",
"rank_total_fyear",
"manager_id",
"radar_comparison",
"subscribe",
"active",
"upcoming",
"report_type",
"end_date",
"dividend_count",
"dividend_total",
):
assert value in fund
assert "| `merge_scope` | string | 否 |" in holders
assert "`all`(默认" in holders
assert "`report_date_ms`" in holders
for asset_type in (
"a-share",
"a-share-index",
"forex",
"fund-otc",
"fund-etf",
"fund-lof",
"fund-reits",
):
assert asset_type in meta
assert "逗号" in meta and "多个" in meta
assert "3004" in entry
def test_auction_contract_preserves_batch_stage_and_timestamp_semantics() -> None:
auction = read("endpoints-auction.md")
for required in (
"get_a_share_auction_snapshot",
"get_a_share_auction_short_term_benchmark",
"thscodes",
"live",
"final",
"auction_unmatched",
"auction_turnover_pct",
"float_market_cap",
"auction_pct",
"tags",
"YYYY-MM-DD",
"timestamp",
):
assert required in auction
snapshot = auction.split("## 1. 个股/多股集合竞价快照", maxsplit=1)[1].split(
"## 2. 短线风向标竞价基准", maxsplit=1
)[0]
benchmark = auction.split("## 2. 短线风向标竞价基准", maxsplit=1)[1]
assert "接口响应组装时间" in snapshot
assert "上游行情时间仅用于判断数据新鲜度" in snapshot
assert "可信的竞价数据时间" not in snapshot
for required in ("Asia/Shanghai", "date_ms", "非交易日不自动回退"):
assert required in benchmark
def test_fund_news_and_historical_indicators_use_the_runtime_payload_shape() -> None:
fund = read("endpoints-fund.md")
indicators = fund.split("## 10. 历史业绩指标", maxsplit=1)[1].split(
"## 11. 最大回撤", maxsplit=1
)[0]
news = fund.split("## 22. 基金资讯列表", maxsplit=1)[1].split(
"## 23. 基金募集列表", maxsplit=1
)[0]
assert "`data` 仅包含 `timestamp` 和 `item[]`" in indicators
assert "不返回顶层 `thscode`、`interval`" in indicators
response_shape = next(line for line in news.splitlines() if line.startswith("`data` 含"))
assert "`total`" not in response_shape
assert "`has_more=false`" in news
def test_valuation_snapshot_contract_preserves_fixed_metrics_and_batch_boundaries() -> None:
path = API_ROOT / "endpoints-valuations.md"
assert path.is_file()
valuations = path.read_text(encoding="utf-8")
for required in (
"get_a_share_valuations_snapshot",
"GET /api/a-share/valuations/snapshot",
"thscodes",
"100",
"去重",
"timestamp",
"total",
"item",
"pe_ttm",
"pe_mrq",
"pb_mrq",
"ps_ttm",
"pcf_ttm",
"null",
"负数",
):
assert required in valuations
assert not re.search(r"^\| `roe_ttm` \|", valuations, re.M)
def test_error_envelope_always_keeps_null_data() -> None:
entry = read("README.md")
assert "`data` 字段始终存在" in entry
assert "业务错误时为 `null`" in entry