Tortoise/tests/test_tencent.py
2026-06-23 10:57:20 +08:00

67 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""腾讯财经适配器测试。"""
from sidecar.sources.tencent import kline_from_tencent_payload, parse_tencent_payload, snapshot_from_tencent_fields
def test_1_1_tencent_snapshot_uses_correct_pb_index() -> None:
"""
功能说明:验证腾讯财经快照字段映射。
参数说明:无。
返回值说明:无返回值。
注意事项:用例编号 1-1重点确认索引 46 才是 PB索引 43 不是 PB。
"""
fields = [""] * 88
fields[1] = "浦发银行"
fields[3] = "10.50"
fields[4] = "10.00"
fields[30] = "20260623150102"
fields[31] = "0.50"
fields[32] = "5.00"
fields[39] = "6.70"
fields[43] = "2.22"
fields[44] = "1000"
fields[45] = "800"
fields[46] = "0.66"
fields[47] = "11.00"
fields[48] = "9.00"
fields[52] = "7.80"
snapshot = snapshot_from_tencent_fields("sh600000", fields)
assert snapshot.name == "浦发银行"
assert snapshot.pe_ttm == 6.70
assert snapshot.pb == 0.66
assert snapshot.pe_static == 7.80
def test_1_2_parse_tencent_payload() -> None:
"""
功能说明:验证腾讯财经原始响应解析。
参数说明:无。
返回值说明:无返回值。
注意事项:用例编号 1-2响应至少包含 53 个字段。
"""
fields = ["v"] * 88
payload = 'v_sh600000="' + "~".join(fields) + '";'
parsed = parse_tencent_payload(payload)
assert len(parsed) == 88
assert parsed[0] == "v"
def test_1_3_parse_tencent_kline_payload() -> None:
"""
功能说明:验证腾讯财经 K 线 JSON 可转换为统一 K 线。
参数说明:无。
返回值说明:无返回值。
注意事项:用例编号 1-3字段顺序为日期、开盘、收盘、最高、最低、成交量。
"""
payload = '{"code":0,"msg":"","data":{"sh000510":{"day":[["2024-09-23","3392.95","3368.53","3392.95","3368.53","1"]]}}}'
bars = kline_from_tencent_payload("sh000510", "day", payload)
assert len(bars) == 1
assert bars[0].symbol == "sh000510"
assert bars[0].close == 3368.53
assert bars[0].source == "tencent"