refactor: 优化代码格式,提升可读性和一致性
This commit is contained in:
1 parent
5738d29bd6
commit
47e1a0cb2c
6 files changed
+172
-89
No files matched your search
+50
-30
@@ -28,7 +28,10 @@ import os
|
||||
# 添加项目根目录到路径
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from douyin.reservation.douyin_reservation_verify import validate_back_to_reservation, match_reservation
|
||||
from douyin.reservation.douyin_reservation_verify import (
|
||||
validate_back_to_reservation,
|
||||
match_reservation,
|
||||
)
|
||||
from douyin.reservation.douyin_reservation_logic import ReservationItem
|
||||
|
||||
|
||||
@@ -72,8 +75,11 @@ def test_reservation_verify():
|
||||
|
||||
# 模拟替换实际的读取函数
|
||||
import douyin.fudai.douyin_fudai_detail
|
||||
|
||||
original_func = douyin.fudai.douyin_fudai_detail.read_live_room_title_core
|
||||
douyin.fudai.douyin_fudai_detail.read_live_room_title_core = mock_read_live_room_title_core
|
||||
douyin.fudai.douyin_fudai_detail.read_live_room_title_core = (
|
||||
mock_read_live_room_title_core
|
||||
)
|
||||
|
||||
try:
|
||||
# 测试1:基本校验通过
|
||||
@@ -90,9 +96,7 @@ def test_reservation_verify():
|
||||
}
|
||||
|
||||
# 设置运行时映射表
|
||||
analyser._room_pos_to_title_map = {
|
||||
1: ("测试直播间", "测试直播间")
|
||||
}
|
||||
analyser._room_pos_to_title_map = {1: ("测试直播间", "测试直播间")}
|
||||
|
||||
result, detail = validate_back_to_reservation(analyser, reservation_item)
|
||||
print(f"校验结果: {result}")
|
||||
@@ -113,7 +117,9 @@ def test_reservation_verify():
|
||||
"""模拟读取到错误的直播间标题"""
|
||||
return "完全不同的直播间名称", "完全不同的直播间名称"
|
||||
|
||||
douyin.fudai.douyin_fudai_detail.read_live_room_title_core = mock_read_live_room_title_core_wrong
|
||||
douyin.fudai.douyin_fudai_detail.read_live_room_title_core = (
|
||||
mock_read_live_room_title_core_wrong
|
||||
)
|
||||
|
||||
result, detail = validate_back_to_reservation(analyser, reservation_item)
|
||||
print(f"校验结果: {result}")
|
||||
@@ -150,14 +156,16 @@ def test_reservation_verify():
|
||||
# 模拟福袋详情
|
||||
detail_tuple = (
|
||||
"测试福袋", # content
|
||||
"05:00", # time_text
|
||||
100, # participants
|
||||
10, # copies
|
||||
100.0, # prize_value_yuan
|
||||
(time.time(), time.time() + 300) # countdown_tuple
|
||||
"05:00", # time_text
|
||||
100, # participants
|
||||
10, # copies
|
||||
100.0, # prize_value_yuan
|
||||
(time.time(), time.time() + 300), # countdown_tuple
|
||||
)
|
||||
|
||||
match_result, reason, debug_info = match_reservation(reservation_item, detail_tuple)
|
||||
match_result, reason, debug_info = match_reservation(
|
||||
reservation_item, detail_tuple
|
||||
)
|
||||
print(f"匹配结果: {match_result}")
|
||||
print(f"原因: {reason}")
|
||||
print(f"调试信息: {debug_info}")
|
||||
@@ -176,14 +184,16 @@ def test_reservation_verify():
|
||||
# 模拟不匹配的福袋详情
|
||||
detail_tuple_mismatch = (
|
||||
"不同的福袋", # content
|
||||
"05:00", # time_text
|
||||
100, # participants
|
||||
10, # copies
|
||||
200.0, # prize_value_yuan (不匹配)
|
||||
(time.time(), time.time() + 600) # countdown_tuple (时间不匹配)
|
||||
"05:00", # time_text
|
||||
100, # participants
|
||||
10, # copies
|
||||
200.0, # prize_value_yuan (不匹配)
|
||||
(time.time(), time.time() + 600), # countdown_tuple (时间不匹配)
|
||||
)
|
||||
|
||||
match_result, reason, debug_info = match_reservation(reservation_item, detail_tuple_mismatch)
|
||||
match_result, reason, debug_info = match_reservation(
|
||||
reservation_item, detail_tuple_mismatch
|
||||
)
|
||||
print(f"匹配结果: {match_result}")
|
||||
print(f"原因: {reason}")
|
||||
print(f"调试信息: {debug_info}")
|
||||
@@ -197,14 +207,16 @@ def test_reservation_verify():
|
||||
# 模拟观看任务的福袋详情
|
||||
watch_task_detail = (
|
||||
"观看5分钟领取福袋奖励", # content
|
||||
"05:00", # time_text
|
||||
100, # participants
|
||||
10, # copies
|
||||
100.0, # prize_value_yuan
|
||||
(time.time(), time.time() + 300) # countdown_tuple
|
||||
"05:00", # time_text
|
||||
100, # participants
|
||||
10, # copies
|
||||
100.0, # prize_value_yuan
|
||||
(time.time(), time.time() + 300), # countdown_tuple
|
||||
)
|
||||
|
||||
match_result, reason, debug_info = match_reservation(reservation_item, watch_task_detail)
|
||||
match_result, reason, debug_info = match_reservation(
|
||||
reservation_item, watch_task_detail
|
||||
)
|
||||
print(f"匹配结果: {match_result}")
|
||||
print(f"原因: {reason}")
|
||||
print(f"调试信息: {debug_info}")
|
||||
@@ -218,7 +230,9 @@ def test_reservation_verify():
|
||||
print("\n测试7:预约队列排序")
|
||||
print("-" * 30)
|
||||
|
||||
from douyin.reservation.douyin_reservation_logic import _sort_reservations_inplace
|
||||
from douyin.reservation.douyin_reservation_logic import (
|
||||
_sort_reservations_inplace,
|
||||
)
|
||||
|
||||
reservations = [
|
||||
{
|
||||
@@ -240,18 +254,24 @@ def test_reservation_verify():
|
||||
|
||||
print("排序前:")
|
||||
for i, r in enumerate(reservations):
|
||||
print(f" {i+1}. 时间: {int((r['future_ts'] - time.time())/60)}分钟后, 价值: {r['prize_value_yuan']}元")
|
||||
print(
|
||||
f" {i+1}. 时间: {int((r['future_ts'] - time.time())/60)}分钟后, 价值: {r['prize_value_yuan']}元"
|
||||
)
|
||||
|
||||
_sort_reservations_inplace(reservations)
|
||||
|
||||
print("排序后:")
|
||||
for i, r in enumerate(reservations):
|
||||
print(f" {i+1}. 时间: {int((r['future_ts'] - time.time())/60)}分钟后, 价值: {r['prize_value_yuan']}元")
|
||||
print(
|
||||
f" {i+1}. 时间: {int((r['future_ts'] - time.time())/60)}分钟后, 价值: {r['prize_value_yuan']}元"
|
||||
)
|
||||
|
||||
# 验证排序:时间优先,其次价值
|
||||
expected_order = [1, 2, 0] # 索引顺序
|
||||
if (reservations[0]["future_ts"] <= reservations[1]["future_ts"] and
|
||||
reservations[1]["future_ts"] <= reservations[2]["future_ts"]):
|
||||
if (
|
||||
reservations[0]["future_ts"] <= reservations[1]["future_ts"]
|
||||
and reservations[1]["future_ts"] <= reservations[2]["future_ts"]
|
||||
):
|
||||
print("✓ 测试7通过:预约队列排序正确")
|
||||
else:
|
||||
print("✗ 测试7失败:预约队列排序不正确")
|
||||
@@ -262,4 +282,4 @@ def test_reservation_verify():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_reservation_verify()
|
||||
test_reservation_verify()
|
||||
Reference in new issue
Block a user