198 lines
6.5 KiB
Python
198 lines
6.5 KiB
Python
"""douyin/reservation 模块单元测试。
|
|
|
|
覆盖:
|
|
- douyin_reservation_verify: _compact_text 纯函数
|
|
- douyin_reservation: 文件操作函数签名验证
|
|
- reservation shim 与子模块导出一致性
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
|
|
class TestCompactText:
|
|
"""_compact_text 纯函数测试。"""
|
|
|
|
def test_removes_spaces(self):
|
|
from douyin.reservation.douyin_reservation_verify import _compact_text
|
|
|
|
result = _compact_text("hello world")
|
|
assert result == "helloworld"
|
|
|
|
def test_removes_multiple_spaces(self):
|
|
from douyin.reservation.douyin_reservation_verify import _compact_text
|
|
|
|
result = _compact_text("hello world test")
|
|
assert result == "helloworldtest"
|
|
|
|
def test_removes_tabs_and_newlines(self):
|
|
from douyin.reservation.douyin_reservation_verify import _compact_text
|
|
|
|
result = _compact_text("hello\tworld\ntest")
|
|
assert result == "helloworldtest"
|
|
|
|
def test_empty_string(self):
|
|
from douyin.reservation.douyin_reservation_verify import _compact_text
|
|
|
|
result = _compact_text("")
|
|
assert result == ""
|
|
|
|
def test_none_input(self):
|
|
from douyin.reservation.douyin_reservation_verify import _compact_text
|
|
|
|
result = _compact_text(None)
|
|
assert result == ""
|
|
|
|
def test_only_spaces(self):
|
|
from douyin.reservation.douyin_reservation_verify import _compact_text
|
|
|
|
result = _compact_text(" ")
|
|
assert result == ""
|
|
|
|
def test_chinese_text(self):
|
|
from douyin.reservation.douyin_reservation_verify import _compact_text
|
|
|
|
result = _compact_text("福袋 内容 测试")
|
|
assert result == "福袋内容测试"
|
|
|
|
|
|
class TestMatchReservation:
|
|
"""match_reservation 函数测试。"""
|
|
|
|
def test_function_exists(self):
|
|
from douyin.reservation.douyin_reservation_verify import match_reservation
|
|
|
|
assert callable(match_reservation)
|
|
|
|
def test_returns_tuple(self):
|
|
from douyin.reservation.douyin_reservation_verify import match_reservation
|
|
|
|
# 构造一个简单的 item 和 detail
|
|
item = {"content": "test", "value": 10.0}
|
|
detail = ("test", "5:00", 100, 10, 10.0, (300, 1700000000))
|
|
result = match_reservation(item, detail)
|
|
assert isinstance(result, tuple)
|
|
assert len(result) == 3
|
|
|
|
|
|
class TestValidateBackToReservation:
|
|
"""validate_back_to_reservation 函数测试。"""
|
|
|
|
def test_function_exists(self):
|
|
from douyin.reservation.douyin_reservation_verify import validate_back_to_reservation
|
|
|
|
assert callable(validate_back_to_reservation)
|
|
|
|
|
|
class TestDouyinReservation:
|
|
"""douyin_reservation 模块测试。"""
|
|
|
|
def test_write_reservations_state_exists(self):
|
|
from douyin.reservation.douyin_reservation import write_reservations_state
|
|
|
|
assert callable(write_reservations_state)
|
|
|
|
def test_write_reservations_state_preserves_button_fields(self, tmp_path):
|
|
from douyin.reservation.douyin_reservation import write_reservations_state
|
|
|
|
state_path = tmp_path / "reservations.json"
|
|
write_reservations_state(
|
|
state_path=str(state_path),
|
|
reservations=[
|
|
{
|
|
"future_ts": 9999999999.0,
|
|
"prize_value_yuan": 899.0,
|
|
"content": "X",
|
|
"button_text": "一键发表评论",
|
|
"button_status": "需要评论",
|
|
}
|
|
],
|
|
room_pos=1,
|
|
current_room_title="room",
|
|
current_room_title_raw="room",
|
|
reason="unit",
|
|
)
|
|
|
|
data = json.loads(state_path.read_text(encoding="utf-8"))
|
|
item = data["reservations"][0]
|
|
assert item["button_text"] == "一键发表评论"
|
|
assert item["button_status"] == "需要评论"
|
|
|
|
def test_load_high_value_rooms_map_exists(self):
|
|
from douyin.reservation.douyin_reservation import load_high_value_rooms_map
|
|
|
|
assert callable(load_high_value_rooms_map)
|
|
|
|
def test_write_high_value_rooms_state_exists(self):
|
|
from douyin.reservation.douyin_reservation import write_high_value_rooms_state
|
|
|
|
assert callable(write_high_value_rooms_state)
|
|
|
|
|
|
class TestReservationLogicHelpers:
|
|
"""reservation_logic 子模块测试。"""
|
|
|
|
def test_helpers_module_importable(self):
|
|
from douyin.reservation.reservation_logic import helpers
|
|
|
|
assert helpers is not None
|
|
|
|
def test_calculate_string_similarity_exists(self):
|
|
from douyin.reservation.reservation_logic.helpers import _calculate_string_similarity
|
|
|
|
assert callable(_calculate_string_similarity)
|
|
|
|
def test_normalize_room_title_exists(self):
|
|
from douyin.reservation.reservation_logic.helpers import _normalize_room_title_for_matching
|
|
|
|
assert callable(_normalize_room_title_for_matching)
|
|
|
|
|
|
class TestReservationHandlerShimConsistency:
|
|
"""验证 reservation_handler shim 与子模块导出一致。"""
|
|
|
|
def test_find_room_pos_by_title_same_object(self):
|
|
from douyin.reservation.douyin_reservation_handler import find_room_pos_by_title as f1
|
|
from douyin.reservation.douyin_reservation_handler_pkg import find_room_pos_by_title as f2
|
|
|
|
assert f1 is f2
|
|
|
|
def test_record_room_title_and_pos_same_object(self):
|
|
from douyin.reservation.douyin_reservation_handler import record_room_title_and_pos as f1
|
|
from douyin.reservation.douyin_reservation_handler_pkg import record_room_title_and_pos as f2
|
|
|
|
assert f1 is f2
|
|
|
|
def test_seek_reservation_room_same_object(self):
|
|
from douyin.reservation.douyin_reservation_handler import seek_reservation_room as f1
|
|
from douyin.reservation.douyin_reservation_handler_pkg import seek_reservation_room as f2
|
|
|
|
assert f1 is f2
|
|
|
|
|
|
class TestReservationModules:
|
|
"""reservation 子模块存在性测试。"""
|
|
|
|
def test_douyin_reservation_importable(self):
|
|
from douyin.reservation import douyin_reservation
|
|
|
|
assert douyin_reservation is not None
|
|
|
|
def test_douyin_reservation_handler_importable(self):
|
|
from douyin.reservation import douyin_reservation_handler
|
|
|
|
assert douyin_reservation_handler is not None
|
|
|
|
def test_douyin_reservation_logic_importable(self):
|
|
from douyin.reservation import douyin_reservation_logic
|
|
|
|
assert douyin_reservation_logic is not None
|
|
|
|
def test_douyin_reservation_verify_importable(self):
|
|
from douyin.reservation import douyin_reservation_verify
|
|
|
|
assert douyin_reservation_verify is not None
|