146 lines
5.2 KiB
Python
146 lines
5.2 KiB
Python
"""douyin_fudai_core_pkg.cut_regions 单元测试。
|
|
|
|
覆盖:
|
|
- _select_countdown_box 4 种 renwu 值返回正确常量
|
|
- cut_detail_regions 调用 cut_pic 4 次(内容 + 分钟 + 秒 + 价值)
|
|
- 倒计时分割坐标(分钟 0~26%,秒 38%~64%)正确
|
|
- save_debug_images 开关效果
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from douyin.fudai.douyin_fudai_core_pkg.cut_regions import (
|
|
_select_countdown_box,
|
|
cut_detail_regions,
|
|
)
|
|
|
|
|
|
def _mk_analyser():
|
|
a = MagicMock()
|
|
a.y_pianyi = 0
|
|
a.cut_pic = MagicMock(return_value=True)
|
|
return a
|
|
|
|
|
|
# ============================================================
|
|
# _select_countdown_box
|
|
# ============================================================
|
|
|
|
class TestSelectCountdownBox:
|
|
def test_renwu_1(self):
|
|
from douyin.fudai.core.coords import COUNTDOWN_1140_1290
|
|
assert _select_countdown_box(1) == COUNTDOWN_1140_1290
|
|
|
|
def test_renwu_2(self):
|
|
from douyin.fudai.core.coords import COUNTDOWN_1110_1230
|
|
assert _select_countdown_box(2) == COUNTDOWN_1110_1230
|
|
|
|
def test_renwu_3(self):
|
|
from douyin.fudai.core.coords import COUNTDOWN_1010_1130
|
|
assert _select_countdown_box(3) == COUNTDOWN_1010_1130
|
|
|
|
def test_renwu_other_falls_to_default(self):
|
|
from douyin.fudai.core.coords import COUNTDOWN_1460_1580
|
|
assert _select_countdown_box(0) == COUNTDOWN_1460_1580
|
|
assert _select_countdown_box(99) == COUNTDOWN_1460_1580
|
|
assert _select_countdown_box(-1) == COUNTDOWN_1460_1580
|
|
|
|
|
|
# ============================================================
|
|
# cut_detail_regions
|
|
# ============================================================
|
|
|
|
class TestCutDetailRegions:
|
|
def test_returns_countdown_box(self):
|
|
a = _mk_analyser()
|
|
from douyin.fudai.core.coords import COUNTDOWN_1110_1230
|
|
result = cut_detail_regions(a, renwu=2)
|
|
assert result == COUNTDOWN_1110_1230
|
|
|
|
def test_calls_cut_pic_4_times(self):
|
|
"""内容 + 分钟 + 秒 + 价值 = 4 次 cut_pic 调用。"""
|
|
a = _mk_analyser()
|
|
cut_detail_regions(a, renwu=2)
|
|
assert a.cut_pic.call_count == 4
|
|
|
|
def test_cut_names_in_order(self):
|
|
a = _mk_analyser()
|
|
cut_detail_regions(a, renwu=2)
|
|
# 第 1 个 positional arg 是坐标,第 6 个是 name
|
|
# 调用签名:cut_pic(x1, y1, x2, y2, target, name, y_pianyi)
|
|
names = [c.args[5] for c in a.cut_pic.call_args_list]
|
|
assert names == ["福袋内容", "福袋倒计时_分钟", "福袋倒计时_秒", "福袋价值"]
|
|
|
|
def test_countdown_split_ratio(self):
|
|
"""倒计时 box 应按分钟 0~26%,秒 38%~64% 切分。"""
|
|
a = _mk_analyser()
|
|
from douyin.fudai.core.coords import COUNTDOWN_1110_1230
|
|
x1, y1, x2, y2 = COUNTDOWN_1110_1230
|
|
w = x2 - x1
|
|
mm_x2 = x1 + int(w * 0.26)
|
|
ss_x1 = x1 + int(w * 0.38)
|
|
ss_x2 = x1 + int(w * 0.64)
|
|
|
|
cut_detail_regions(a, renwu=2)
|
|
|
|
# 倒计时分钟 = 第 2 次 cut_pic 调用
|
|
mm_call = a.cut_pic.call_args_list[1]
|
|
assert mm_call.args[0] == x1
|
|
assert mm_call.args[2] == mm_x2
|
|
|
|
# 倒计时秒 = 第 3 次调用
|
|
ss_call = a.cut_pic.call_args_list[2]
|
|
assert ss_call.args[0] == ss_x1
|
|
assert ss_call.args[2] == ss_x2
|
|
|
|
def test_profile_countdown_vertical_crop_is_narrower_and_higher(self):
|
|
a = _mk_analyser()
|
|
profile = {"countdown_full": (760, 1328, 1038, 1398)}
|
|
|
|
cut_detail_regions(a, renwu=2, profile=profile)
|
|
|
|
mm_call = a.cut_pic.call_args_list[1]
|
|
ss_call = a.cut_pic.call_args_list[2]
|
|
assert mm_call.args[1] == 1340
|
|
assert mm_call.args[3] == 1371
|
|
assert ss_call.args[1] == 1340
|
|
assert ss_call.args[3] == 1371
|
|
|
|
def test_save_debug_images_disabled_default(self):
|
|
a = _mk_analyser()
|
|
with patch(
|
|
"douyin.fudai.core.fudai_debug_utils._save_full_screenshot_with_ocr_region"
|
|
) as mock_save:
|
|
cut_detail_regions(a, renwu=2, save_debug_images=False)
|
|
mock_save.assert_not_called()
|
|
|
|
def test_save_debug_images_enabled_does_not_save_per_field(self):
|
|
"""详情页 debug 已改为主流程统一保存一张综合标注图。"""
|
|
a = _mk_analyser()
|
|
with patch(
|
|
"douyin.fudai.core.fudai_debug_utils._save_full_screenshot_with_ocr_region"
|
|
) as mock_save:
|
|
cut_detail_regions(a, renwu=2, save_debug_images=True)
|
|
mock_save.assert_not_called()
|
|
|
|
def test_renwu_1_uses_correct_box(self):
|
|
a = _mk_analyser()
|
|
from douyin.fudai.core.coords import COUNTDOWN_1140_1290
|
|
result = cut_detail_regions(a, renwu=1)
|
|
assert result == COUNTDOWN_1140_1290
|
|
|
|
def test_one_task_profile_countdown_box_uses_new_coords(self):
|
|
a = _mk_analyser()
|
|
profile = {"countdown_full": (730, 1338, 1038, 1418)}
|
|
result = cut_detail_regions(a, renwu=1, profile=profile)
|
|
assert result == (730, 1338, 1038, 1418)
|
|
|
|
def test_renwu_3_uses_correct_box(self):
|
|
a = _mk_analyser()
|
|
from douyin.fudai.core.coords import COUNTDOWN_1010_1130
|
|
result = cut_detail_regions(a, renwu=3)
|
|
assert result == COUNTDOWN_1010_1130
|