113 lines
3.5 KiB
Python
113 lines
3.5 KiB
Python
"""douyin/utils/douyin_utils 的 pytest 单元测试。
|
||
|
||
覆盖两个核心 OCR 文本解析纯函数:
|
||
- extract_prize_value_yuan:从 OCR 文本里抽取奖品价值(元)
|
||
- normalize_countdown_text:把各种倒计时文本归一化为 "m:ss"
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import pytest
|
||
|
||
from douyin.utils.douyin_utils import (
|
||
extract_prize_value_yuan,
|
||
normalize_countdown_text,
|
||
)
|
||
|
||
|
||
# ============================================================
|
||
# extract_prize_value_yuan
|
||
# ============================================================
|
||
|
||
class TestExtractPrizeValueYuan:
|
||
@pytest.mark.parametrize(
|
||
"text,expected",
|
||
[
|
||
# 标准 ¥ 前缀
|
||
("¥100", 100.0),
|
||
("¥99.50", 99.5),
|
||
("¥100.5", 100.5),
|
||
("¥200元", 200.0),
|
||
# 千分位逗号
|
||
("价值¥1,000", 1000.0),
|
||
# 中文/纯数字描述("价值 100 元")
|
||
("价值 100 元", 100.0),
|
||
("¥2999 参考价值", 2999.0),
|
||
("¥2999 参考价值", 2999.0),
|
||
# OCR 字母 → 数字纠错(l→1, O→0)
|
||
("¥l00", 100.0),
|
||
],
|
||
)
|
||
def test_extract_valid(self, text, expected):
|
||
assert extract_prize_value_yuan(text) == expected
|
||
|
||
@pytest.mark.parametrize(
|
||
"text",
|
||
[
|
||
"",
|
||
None,
|
||
"没有金额",
|
||
"随便一段文本",
|
||
],
|
||
)
|
||
def test_extract_zero_when_no_value(self, text):
|
||
# 没有有效金额的文本应返回 0.0
|
||
assert extract_prize_value_yuan(text) == 0.0
|
||
|
||
def test_returns_float(self):
|
||
# 任何输入都应返回 float(不是 int / None)
|
||
assert isinstance(extract_prize_value_yuan("¥100"), float)
|
||
assert isinstance(extract_prize_value_yuan(""), float)
|
||
assert isinstance(extract_prize_value_yuan(None), float)
|
||
|
||
|
||
# ============================================================
|
||
# normalize_countdown_text
|
||
# ============================================================
|
||
|
||
class TestNormalizeCountdownText:
|
||
@pytest.mark.parametrize(
|
||
"text,expected",
|
||
[
|
||
# 基本归一化:去除前导 0、统一冒号
|
||
("00:30", "0:30"),
|
||
("5:30", "5:30"),
|
||
("02:30", "2:30"),
|
||
# 中文冒号
|
||
("5:30", "5:30"),
|
||
# 中文分秒
|
||
("5分30秒", "5:30"),
|
||
# 嵌在文本里:能抽出
|
||
("剩余 03:25 开奖", "3:25"),
|
||
],
|
||
)
|
||
def test_normalize_known_patterns(self, text, expected):
|
||
assert normalize_countdown_text(text) == expected
|
||
|
||
def test_empty_returns_empty(self):
|
||
assert normalize_countdown_text("") == ""
|
||
|
||
def test_returns_string(self):
|
||
# 任何输入都应返回 str
|
||
assert isinstance(normalize_countdown_text("00:30"), str)
|
||
assert isinstance(normalize_countdown_text(""), str)
|
||
|
||
def test_two_digit_minute_preserved(self):
|
||
# 双位分钟数应保留,不被误处理
|
||
assert normalize_countdown_text("12:34") == "12:34"
|
||
|
||
|
||
def test_extract_participants_supports_wan_unit_and_cleans_text():
|
||
from douyin.fudai.douyin_fudai_core_pkg.contain_helpers import (
|
||
extract_participants_and_clean,
|
||
)
|
||
|
||
participants, copies, cleaned = extract_participants_and_clean(
|
||
"小米Redmi Pad 2 SE 红米平板2SE 0.4万人已参与 共1份"
|
||
)
|
||
|
||
assert participants == 4000
|
||
assert copies == 1
|
||
assert "0.4万人已参与" not in cleaned
|
||
assert "共1份" not in cleaned
|
||
assert "小米Redmi" in cleaned
|