-
Notifications
You must be signed in to change notification settings - Fork 54.3k
Expand file tree
/
Copy pathtest_stock_code_utils.py
More file actions
184 lines (127 loc) · 5.33 KB
/
Copy pathtest_stock_code_utils.py
File metadata and controls
184 lines (127 loc) · 5.33 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
# -*- coding: utf-8 -*-
"""
Tests for src/services/stock_code_utils.py
Covers: is_code_like, normalize_code - including exchange prefix handling.
"""
import pytest
from src.services.stock_code_utils import is_code_like, normalize_code
class TestIsCodeLike:
# --- Plain digit codes ---
def test_plain_6_digit(self):
assert is_code_like("600519") is True
def test_plain_5_digit(self):
assert is_code_like("00700") is True
def test_4_digit_rejected(self):
assert is_code_like("6001") is False
# --- Suffix format ---
def test_suffix_sh(self):
assert is_code_like("600519.SH") is True
def test_suffix_sz(self):
assert is_code_like("000001.SZ") is True
def test_suffix_bj(self):
assert is_code_like("920493.BJ") is True
def test_suffix_bj_rejects_non_bse_base(self):
assert is_code_like("600519.BJ") is False
def test_suffix_lowercase(self):
assert is_code_like("600519.sh") is True
# --- HK suffix format ---
def test_suffix_hk(self):
assert is_code_like("00700.HK") is True
def test_suffix_hk_lowercase(self):
assert is_code_like("00700.hk") is True
def test_suffix_hk_short_code(self):
assert is_code_like("1810.HK") is True
def test_suffix_hk_rejects_6_digit_base(self):
assert is_code_like("600519.HK") is False
def test_suffix_sh_rejects_5_digit_base(self):
assert is_code_like("00700.SH") is False
# --- Exchange prefix format (Issue #6 fix) ---
def test_prefix_sh_upper(self):
assert is_code_like("SH600519") is True
def test_prefix_sh_lower(self):
assert is_code_like("sh600519") is True
def test_prefix_sz(self):
assert is_code_like("SZ000001") is True
def test_prefix_bj(self):
assert is_code_like("BJ920493") is True
def test_prefix_bj_rejects_non_bse_base(self):
assert is_code_like("BJ600519") is False
def test_prefix_hk(self):
assert is_code_like("HK00700") is True
def test_prefix_hk_lower(self):
assert is_code_like("hk00700") is True
def test_prefix_hk_short_code(self):
assert is_code_like("HK700") is True
def test_prefix_hk_rejects_6_digit_base(self):
assert is_code_like("HK600519") is False
# --- US tickers ---
def test_us_ticker(self):
assert is_code_like("AAPL") is True
def test_us_ticker_with_exchange(self):
assert is_code_like("TSLA.O") is True
# --- Negative cases ---
def test_plain_text(self):
assert is_code_like("贵州茅台") is False
def test_empty(self):
assert is_code_like("") is False
def test_mixed_invalid(self):
assert is_code_like("abc123") is False
class TestNormalizeCode:
# --- Plain digit codes ---
def test_plain_6_digit(self):
assert normalize_code("600519") == "600519"
def test_plain_5_digit(self):
assert normalize_code("00700") == "00700"
def test_whitespace_stripped(self):
assert normalize_code(" 600519 ") == "600519"
# --- Suffix format ---
def test_suffix_sh_strips(self):
assert normalize_code("600519.SH") == "600519"
def test_suffix_sz_strips(self):
assert normalize_code("000001.SZ") == "000001"
def test_suffix_bj_strips(self):
assert normalize_code("920493.BJ") == "920493"
def test_suffix_bj_rejects_non_bse_base(self):
assert normalize_code("600519.BJ") is None
def test_suffix_ss_strips(self):
assert normalize_code("600000.SS") == "600000"
def test_suffix_hk_strips(self):
assert normalize_code("00700.HK") == "00700"
def test_suffix_hk_lowercase_strips(self):
assert normalize_code("00700.hk") == "00700"
def test_suffix_hk_short_code_is_zero_padded(self):
assert normalize_code("1810.HK") == "01810"
def test_suffix_hk_rejects_6_digit_base(self):
assert normalize_code("600519.HK") is None
def test_suffix_sh_rejects_5_digit_base(self):
assert normalize_code("00700.SH") is None
# --- Exchange prefix format (Issue #6 fix) ---
def test_prefix_sh_upper(self):
assert normalize_code("SH600519") == "600519"
def test_prefix_sh_lower(self):
assert normalize_code("sh600519") == "600519"
def test_prefix_sz(self):
assert normalize_code("SZ000001") == "000001"
def test_prefix_bj(self):
assert normalize_code("BJ920493") == "920493"
def test_prefix_bj_rejects_non_bse_base(self):
assert normalize_code("BJ600519") is None
def test_prefix_hk(self):
assert normalize_code("HK00700") == "00700"
def test_prefix_hk_lower(self):
assert normalize_code("hk00700") == "00700"
def test_prefix_hk_short_code_is_zero_padded(self):
assert normalize_code("HK700") == "00700"
def test_prefix_hk_rejects_6_digit_base(self):
assert normalize_code("HK600519") is None
# --- US tickers ---
def test_us_ticker(self):
assert normalize_code("AAPL") == "AAPL"
# --- Invalid inputs ---
def test_empty_returns_none(self):
assert normalize_code("") is None
def test_plain_text_returns_none(self):
assert normalize_code("贵州茅台") is None
def test_partial_prefix_no_digits_returns_none(self):
# SH followed by wrong digit count
assert normalize_code("SH6005") is None