mirror of
https://ghfast.top/https://github.com/aeroxw/easy-tdx.git
synced 2026-09-12 16:54:17 +08:00
1.17.5 引入的 tests/fixtures/ex_history_transaction.json 含中文注释,Windows CI 默认用 cp1252 解码 UTF-8 文件触发 UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f,导致 3 个 Windows 矩阵的 test_parse_ex_history_transaction_hk 失败(Linux 矩阵因默认 utf-8 不受影响)。 修复:所有 fixture 读取显式指定 encoding='utf-8': - test_hk_transaction.py 的 load_hex/load_json(直接触发点) - test_commands_offline.py 的 load_hex(纯 ASCII hex,当前未触发但一并统一) - test_decode_errors.py 的 _load_hex(同上) 867 单测全绿,ruff format/check / mypy strict 通过。
52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
"""坏包与解码异常回归测试。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from easy_tdx.codec.frame import FrameHeader, decompress_body
|
|
from easy_tdx.commands.company_info import GetCompanyInfoCategoryCmd
|
|
from easy_tdx.commands.security_count import GetSecurityCountCmd
|
|
from easy_tdx.commands.xdxr_info import GetXdxrInfoCmd
|
|
from easy_tdx.exceptions import TdxDecodeError
|
|
from easy_tdx.models.enums import Market
|
|
|
|
FIXTURES = Path(__file__).parent.parent / "fixtures"
|
|
|
|
|
|
def _load_hex(name: str) -> bytes:
|
|
return bytes.fromhex((FIXTURES / f"{name}.hex").read_text(encoding="utf-8").strip())
|
|
|
|
|
|
def test_security_count_truncated_raises_tdxdecodeerror() -> None:
|
|
with pytest.raises(TdxDecodeError):
|
|
GetSecurityCountCmd(Market.SH).parse_response(b"")
|
|
|
|
|
|
def test_company_info_category_truncated_raises_tdxdecodeerror() -> None:
|
|
body = _load_hex("company_info_category")
|
|
cmd = GetCompanyInfoCategoryCmd(Market.SH, "600000")
|
|
|
|
with pytest.raises(TdxDecodeError):
|
|
cmd.parse_response(body[:-10])
|
|
|
|
|
|
def test_xdxr_info_truncated_raises_tdxdecodeerror() -> None:
|
|
body = _load_hex("xdxr_info")
|
|
cmd = GetXdxrInfoCmd(Market.SH, "600000")
|
|
|
|
with pytest.raises(TdxDecodeError):
|
|
cmd.parse_response(body[:-10])
|
|
|
|
|
|
def test_frame_bad_zlib_raises_tdxdecodeerror() -> None:
|
|
with pytest.raises(TdxDecodeError):
|
|
decompress_body(FrameHeader(0, 0, 0, 4, 8), b"xxxx")
|
|
|
|
|
|
def test_frame_unzipsize_mismatch_raises_tdxdecodeerror() -> None:
|
|
with pytest.raises(TdxDecodeError):
|
|
decompress_body(FrameHeader(0, 0, 0, 3, 4), b"abc")
|