staran 1.0.7__py3-none-any.whl → 1.0.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,400 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ Date类v1.0.8新功能测试
6
+ ====================
7
+
8
+ 测试v1.0.8版本新增的农历支持和多语言功能:
9
+ - 农历与公历互转
10
+ - 农历日期格式化
11
+ - 农历日期比较
12
+ - 多语言本地化
13
+ - 全局语言设置
14
+ """
15
+
16
+ import unittest
17
+ import datetime
18
+ import sys
19
+ import os
20
+
21
+ # 添加项目根目录到路径
22
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', '..'))
23
+
24
+ from staran.date.core import Date
25
+ from staran.date.lunar import LunarDate
26
+ from staran.date.i18n import Language
27
+
28
+
29
+ class TestLunarSupport(unittest.TestCase):
30
+ """测试农历支持功能"""
31
+
32
+ def test_create_from_lunar(self):
33
+ """测试从农历日期创建"""
34
+ # 农历2025年正月初一
35
+ date = Date.from_lunar(2025, 1, 1)
36
+ self.assertIsInstance(date, Date)
37
+
38
+ # 农历2025年闰四月十五
39
+ leap_date = Date.from_lunar(2025, 4, 15, is_leap=True)
40
+ self.assertIsInstance(leap_date, Date)
41
+
42
+ def test_create_from_lunar_string(self):
43
+ """测试从农历字符串创建"""
44
+ # 正常月份
45
+ date = Date.from_lunar_string("20250315")
46
+ self.assertIsInstance(date, Date)
47
+
48
+ # 闰月
49
+ leap_date = Date.from_lunar_string("2025闰0415")
50
+ self.assertIsInstance(leap_date, Date)
51
+
52
+ def test_to_lunar(self):
53
+ """测试转为农历"""
54
+ date = Date("20250129") # 2025年1月29日
55
+ lunar = date.to_lunar()
56
+ self.assertIsInstance(lunar, LunarDate)
57
+ # 2025年1月29日对应农历2025年三月初十
58
+ self.assertEqual(lunar.year, 2025)
59
+ self.assertEqual(lunar.month, 3)
60
+ self.assertEqual(lunar.day, 10)
61
+
62
+ def test_to_lunar_string(self):
63
+ """测试转为农历字符串"""
64
+ date = Date("20250129")
65
+ lunar_compact = date.to_lunar_string(compact=True)
66
+ lunar_full = date.to_lunar_string(compact=False)
67
+
68
+ self.assertIsInstance(lunar_compact, str)
69
+ self.assertIsInstance(lunar_full, str)
70
+ self.assertTrue("农历" in lunar_full)
71
+
72
+ def test_format_lunar(self):
73
+ """测试农历格式化"""
74
+ date = Date("20250129")
75
+
76
+ # 基本格式
77
+ lunar_basic = date.format_lunar()
78
+ self.assertIn("农历", lunar_basic)
79
+
80
+ # 包含生肖
81
+ lunar_zodiac = date.format_lunar(include_zodiac=True)
82
+ self.assertIsInstance(lunar_zodiac, str)
83
+
84
+ # 紧凑格式
85
+ lunar_compact = date.format_lunar_compact()
86
+ self.assertTrue(lunar_compact.isdigit() or '闰' in lunar_compact)
87
+
88
+ def test_lunar_judgments(self):
89
+ """测试农历判断方法"""
90
+ # 找一个农历正月初一的日期进行测试
91
+ date = Date.from_lunar(2025, 1, 1)
92
+ self.assertTrue(date.is_lunar_new_year())
93
+ self.assertTrue(date.is_lunar_month_start())
94
+ self.assertFalse(date.is_lunar_month_mid())
95
+
96
+ # 测试农历十五
97
+ mid_date = Date.from_lunar(2025, 1, 15)
98
+ self.assertFalse(mid_date.is_lunar_new_year())
99
+ self.assertFalse(mid_date.is_lunar_month_start())
100
+ self.assertTrue(mid_date.is_lunar_month_mid())
101
+
102
+ def test_lunar_comparison(self):
103
+ """测试农历比较"""
104
+ date1 = Date.from_lunar(2025, 1, 1)
105
+ date2 = Date.from_lunar(2025, 1, 15)
106
+ date3 = Date.from_lunar(2025, 1, 1)
107
+
108
+ # 比较测试
109
+ self.assertEqual(date1.compare_lunar(date2), -1) # date1 < date2
110
+ self.assertEqual(date2.compare_lunar(date1), 1) # date2 > date1
111
+ self.assertEqual(date1.compare_lunar(date3), 0) # date1 == date3
112
+
113
+ # 同月测试
114
+ self.assertTrue(date1.is_same_lunar_month(date2))
115
+ self.assertTrue(date1.is_same_lunar_month(date3))
116
+
117
+ # 同日测试
118
+ self.assertFalse(date1.is_same_lunar_day(date2))
119
+ self.assertTrue(date1.is_same_lunar_day(date3))
120
+
121
+
122
+ class TestMultiLanguageSupport(unittest.TestCase):
123
+ """测试多语言支持功能"""
124
+
125
+ def setUp(self):
126
+ """设置测试环境"""
127
+ # 保存原始语言设置
128
+ self.original_language = Date.get_language()
129
+
130
+ def tearDown(self):
131
+ """清理测试环境"""
132
+ # 恢复原始语言设置
133
+ Date.set_language(self.original_language)
134
+
135
+ def test_set_global_language(self):
136
+ """测试设置全局语言"""
137
+ # 测试设置不同语言
138
+ Date.set_language('en_US')
139
+ self.assertEqual(Date.get_language(), 'en_US')
140
+
141
+ Date.set_language('zh_TW')
142
+ self.assertEqual(Date.get_language(), 'zh_TW')
143
+
144
+ Date.set_language('ja_JP')
145
+ self.assertEqual(Date.get_language(), 'ja_JP')
146
+
147
+ # 测试无效语言
148
+ with self.assertRaises(ValueError):
149
+ Date.set_language('invalid_lang')
150
+
151
+ def test_get_supported_languages(self):
152
+ """测试获取支持的语言"""
153
+ languages = Date.get_supported_languages()
154
+ self.assertIsInstance(languages, dict)
155
+ self.assertIn('zh_CN', languages)
156
+ self.assertIn('zh_TW', languages)
157
+ self.assertIn('ja_JP', languages)
158
+ self.assertIn('en_US', languages)
159
+
160
+ def test_format_localized(self):
161
+ """测试本地化格式"""
162
+ date = Date("20250415")
163
+
164
+ # 中文简体
165
+ Date.set_language('zh_CN')
166
+ cn_format = date.format_localized()
167
+ self.assertIn('年', cn_format)
168
+
169
+ # 英语
170
+ Date.set_language('en_US')
171
+ en_format = date.format_localized()
172
+ self.assertTrue('/' in en_format or '-' in en_format)
173
+
174
+ # 日语
175
+ Date.set_language('ja_JP')
176
+ jp_format = date.format_localized()
177
+ self.assertIn('年', jp_format)
178
+
179
+ def test_format_weekday_localized(self):
180
+ """测试本地化星期格式"""
181
+ date = Date("20250415") # 2025年4月15日,星期二
182
+
183
+ # 中文简体
184
+ Date.set_language('zh_CN')
185
+ weekday_cn = date.format_weekday_localized()
186
+ self.assertIn('星期', weekday_cn)
187
+
188
+ # 英语
189
+ Date.set_language('en_US')
190
+ weekday_en = date.format_weekday_localized()
191
+ self.assertIn('day', weekday_en.lower())
192
+
193
+ # 日语
194
+ Date.set_language('ja_JP')
195
+ weekday_jp = date.format_weekday_localized()
196
+ self.assertIn('曜日', weekday_jp)
197
+
198
+ # 测试短格式
199
+ weekday_short = date.format_weekday_localized(short=True)
200
+ self.assertTrue(len(weekday_short) <= 3)
201
+
202
+ def test_format_month_localized(self):
203
+ """测试本地化月份格式"""
204
+ date = Date("20250415") # 4月
205
+
206
+ # 中文
207
+ Date.set_language('zh_CN')
208
+ month_cn = date.format_month_localized()
209
+ self.assertIn('月', month_cn)
210
+
211
+ # 英语
212
+ Date.set_language('en_US')
213
+ month_en = date.format_month_localized()
214
+ self.assertIn('Apr', month_en)
215
+
216
+ def test_format_quarter_localized(self):
217
+ """测试本地化季度格式"""
218
+ date = Date("20250415") # 第2季度
219
+
220
+ # 中文
221
+ Date.set_language('zh_CN')
222
+ quarter_cn = date.format_quarter_localized()
223
+ self.assertIn('季度', quarter_cn)
224
+
225
+ # 英语
226
+ Date.set_language('en_US')
227
+ quarter_en = date.format_quarter_localized()
228
+ self.assertIn('Quarter', quarter_en)
229
+
230
+ # 短格式
231
+ quarter_short = date.format_quarter_localized(short=True)
232
+ self.assertIn('Q', quarter_short)
233
+
234
+ def test_format_relative_localized(self):
235
+ """测试本地化相对时间格式"""
236
+ today = Date.today()
237
+ tomorrow = today.add_days(1)
238
+ yesterday = today.add_days(-1)
239
+
240
+ # 中文
241
+ Date.set_language('zh_CN')
242
+ self.assertIn('今', today.format_relative_localized())
243
+ self.assertIn('明', tomorrow.format_relative_localized())
244
+ self.assertIn('昨', yesterday.format_relative_localized())
245
+
246
+ # 英语
247
+ Date.set_language('en_US')
248
+ self.assertEqual('today', today.format_relative_localized())
249
+ self.assertEqual('tomorrow', tomorrow.format_relative_localized())
250
+ self.assertEqual('yesterday', yesterday.format_relative_localized())
251
+
252
+ def test_language_consistency(self):
253
+ """测试语言一致性"""
254
+ date = Date("20250415")
255
+
256
+ # 设置一次语言,多个方法应该保持一致
257
+ Date.set_language('ja_JP')
258
+
259
+ weekday = date.format_weekday_localized()
260
+ month = date.format_month_localized()
261
+ relative = date.format_relative_localized()
262
+
263
+ # 都应该是日语格式
264
+ self.assertIn('曜日', weekday)
265
+ self.assertIn('月', month)
266
+ self.assertIsInstance(relative, str)
267
+
268
+
269
+ class TestLanguageDirectOverride(unittest.TestCase):
270
+ """测试语言覆盖功能"""
271
+
272
+ def test_language_override(self):
273
+ """测试单次使用时覆盖语言设置"""
274
+ Date.set_language('zh_CN') # 设置全局为中文
275
+ date = Date("20250415")
276
+
277
+ # 全局中文格式
278
+ cn_format = date.format_weekday_localized()
279
+ self.assertIn('星期', cn_format)
280
+
281
+ # 单次覆盖为英语
282
+ en_format = date.format_weekday_localized(language_code='en_US')
283
+ self.assertIn('day', en_format.lower())
284
+
285
+ # 全局设置仍然是中文
286
+ self.assertEqual(Date.get_language(), 'zh_CN')
287
+ cn_format2 = date.format_weekday_localized()
288
+ self.assertIn('星期', cn_format2)
289
+
290
+
291
+ class TestLunarDateClass(unittest.TestCase):
292
+ """测试LunarDate类功能"""
293
+
294
+ def test_lunar_date_creation(self):
295
+ """测试农历日期创建"""
296
+ lunar = LunarDate(2025, 3, 15)
297
+ self.assertEqual(lunar.year, 2025)
298
+ self.assertEqual(lunar.month, 3)
299
+ self.assertEqual(lunar.day, 15)
300
+ self.assertFalse(lunar.is_leap)
301
+
302
+ # 测试闰月
303
+ leap_lunar = LunarDate(2025, 4, 15, is_leap=True)
304
+ self.assertTrue(leap_lunar.is_leap)
305
+
306
+ def test_lunar_solar_conversion(self):
307
+ """测试农历公历互转"""
308
+ # 创建农历日期
309
+ lunar = LunarDate(2025, 1, 1) # 农历正月初一
310
+
311
+ # 转为公历
312
+ solar = lunar.to_solar()
313
+ self.assertIsInstance(solar, datetime.date)
314
+
315
+ # 再转回农历
316
+ lunar2 = LunarDate.from_solar(solar)
317
+ self.assertEqual(lunar.year, lunar2.year)
318
+ self.assertEqual(lunar.month, lunar2.month)
319
+ self.assertEqual(lunar.day, lunar2.day)
320
+ self.assertEqual(lunar.is_leap, lunar2.is_leap)
321
+
322
+ def test_lunar_formatting(self):
323
+ """测试农历格式化"""
324
+ lunar = LunarDate(2025, 3, 15)
325
+
326
+ # 中文格式
327
+ chinese = lunar.format_chinese()
328
+ self.assertIn('农历', chinese)
329
+ self.assertIn('三月', chinese)
330
+ self.assertIn('十五', chinese)
331
+
332
+ # 紧凑格式
333
+ compact = lunar.format_compact()
334
+ self.assertEqual(compact, '20250315')
335
+
336
+ # ISO样式格式
337
+ iso_like = lunar.format_iso_like()
338
+ self.assertEqual(iso_like, '2025-03-15')
339
+
340
+ def test_lunar_comparison(self):
341
+ """测试农历比较"""
342
+ lunar1 = LunarDate(2025, 1, 1)
343
+ lunar2 = LunarDate(2025, 1, 15)
344
+ lunar3 = LunarDate(2025, 1, 1)
345
+
346
+ self.assertTrue(lunar1 < lunar2)
347
+ self.assertTrue(lunar2 > lunar1)
348
+ self.assertEqual(lunar1, lunar3)
349
+ self.assertTrue(lunar1 <= lunar2)
350
+ self.assertTrue(lunar2 >= lunar1)
351
+
352
+ def test_ganzhi_zodiac(self):
353
+ """测试天干地支和生肖"""
354
+ lunar = LunarDate(2025, 1, 1)
355
+
356
+ ganzhi = lunar.get_ganzhi_year()
357
+ self.assertIsInstance(ganzhi, str)
358
+ self.assertEqual(len(ganzhi), 2)
359
+
360
+ zodiac = lunar.get_zodiac()
361
+ self.assertIsInstance(zodiac, str)
362
+ self.assertEqual(len(zodiac), 1)
363
+
364
+
365
+ if __name__ == '__main__':
366
+ # 运行测试
367
+ loader = unittest.TestLoader()
368
+ suite = unittest.TestSuite()
369
+
370
+ # 添加测试类
371
+ suite.addTests(loader.loadTestsFromTestCase(TestLunarSupport))
372
+ suite.addTests(loader.loadTestsFromTestCase(TestMultiLanguageSupport))
373
+ suite.addTests(loader.loadTestsFromTestCase(TestLanguageDirectOverride))
374
+ suite.addTests(loader.loadTestsFromTestCase(TestLunarDateClass))
375
+
376
+ # 运行测试
377
+ runner = unittest.TextTestRunner(verbosity=2)
378
+ result = runner.run(suite)
379
+
380
+ # 输出结果统计
381
+ print(f"\n{'='*60}")
382
+ print(f"🧪 Staran v1.0.8 新功能测试结果")
383
+ print(f"{'='*60}")
384
+ print(f"测试总数: {result.testsRun}")
385
+ print(f"成功: {result.testsRun - len(result.failures) - len(result.errors)}")
386
+ print(f"失败: {len(result.failures)}")
387
+ print(f"错误: {len(result.errors)}")
388
+
389
+ if result.failures:
390
+ print(f"\n失败的测试:")
391
+ for test, trace in result.failures:
392
+ print(f" - {test}")
393
+
394
+ if result.errors:
395
+ print(f"\n错误的测试:")
396
+ for test, trace in result.errors:
397
+ print(f" - {test}")
398
+
399
+ success_rate = (result.testsRun - len(result.failures) - len(result.errors)) / result.testsRun * 100
400
+ print(f"成功率: {success_rate:.1f}%")