fix(platform): 持久化并完善斗鱼兑换结果图片
This commit is contained in:
+139
-38
@@ -41,7 +41,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261121_0104"
|
||||
HEAD_REVISION = "20261121_0105"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
@@ -314,6 +314,63 @@ class TestMigrationSmoke:
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(text(f"DROP DATABASE IF EXISTS `{mig_db}`;"))
|
||||
|
||||
def test_0105_adds_exchange_result_image_columns_and_roundtrips(self):
|
||||
creds = get_mysql_credentials()
|
||||
mig_db = f"douyu_login_mig_{secrets.token_hex(4)}"
|
||||
mig_url = (
|
||||
f"mysql+pymysql://{quote_plus(creds['user'])}:"
|
||||
f"{quote_plus(creds['password'])}@{creds['host']}:{creds['port']}/"
|
||||
f"{mig_db}?charset=utf8mb4"
|
||||
)
|
||||
admin_engine = get_test_engine()
|
||||
mig_engine = create_engine(mig_url)
|
||||
original_url = database_module.DATABASE_URL
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(
|
||||
text(
|
||||
f"CREATE DATABASE `{mig_db}` "
|
||||
"CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;"
|
||||
)
|
||||
)
|
||||
try:
|
||||
database_module.DATABASE_URL = mig_url
|
||||
cfg = _migration_config()
|
||||
cfg.set_main_option("sqlalchemy.url", mig_url)
|
||||
command.upgrade(cfg, "20261121_0104")
|
||||
before = {
|
||||
row["name"]
|
||||
for row in inspect(mig_engine).get_columns(
|
||||
"platform_fulfillment_tasks"
|
||||
)
|
||||
}
|
||||
assert "exchange_result_image_key" not in before
|
||||
assert "exchange_result_image_saved_at" not in before
|
||||
|
||||
command.upgrade(cfg, "head")
|
||||
after = {
|
||||
row["name"]
|
||||
for row in inspect(mig_engine).get_columns(
|
||||
"platform_fulfillment_tasks"
|
||||
)
|
||||
}
|
||||
assert "exchange_result_image_key" in after
|
||||
assert "exchange_result_image_saved_at" in after
|
||||
|
||||
command.downgrade(cfg, "20261121_0104")
|
||||
downgraded = {
|
||||
row["name"]
|
||||
for row in inspect(mig_engine).get_columns(
|
||||
"platform_fulfillment_tasks"
|
||||
)
|
||||
}
|
||||
assert "exchange_result_image_key" not in downgraded
|
||||
assert "exchange_result_image_saved_at" not in downgraded
|
||||
finally:
|
||||
database_module.DATABASE_URL = original_url
|
||||
mig_engine.dispose()
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(text(f"DROP DATABASE IF EXISTS `{mig_db}`;"))
|
||||
|
||||
def test_0077_converts_super_admin_membership_to_overlay(self):
|
||||
creds = get_mysql_credentials()
|
||||
mig_db = f"douyu_login_mig_{secrets.token_hex(4)}"
|
||||
@@ -723,46 +780,90 @@ class TestMigrationSmoke:
|
||||
)
|
||||
session.add(lease)
|
||||
session.commit()
|
||||
broken = PlatformFulfillmentTask(
|
||||
source_system="order_site",
|
||||
source_order_id="MIG-0090",
|
||||
source_order_item_id="MIG-0090-1",
|
||||
account_id=account.id,
|
||||
lease_id=lease.id,
|
||||
execution_status="ready",
|
||||
binding_status="not_started",
|
||||
exchange_status="points_short",
|
||||
issue_code="",
|
||||
last_error="",
|
||||
)
|
||||
unassigned = PlatformFulfillmentTask(
|
||||
source_system="order_site",
|
||||
source_order_id="MIG-0090",
|
||||
source_order_item_id="MIG-0090-2",
|
||||
execution_status="ready",
|
||||
binding_status="not_started",
|
||||
exchange_status="not_started",
|
||||
issue_code="no_eligible_account",
|
||||
last_error="当前没有可用账号",
|
||||
)
|
||||
session.add_all([broken, unassigned])
|
||||
session.commit()
|
||||
broken_id = broken.id
|
||||
unassigned_id = unassigned.id
|
||||
broken_id = 9001
|
||||
unassigned_id = 9002
|
||||
account_id = account.id
|
||||
lease_id = lease.id
|
||||
|
||||
# 该用例固定在 0089 架构上验证历史迁移;不能用当前 ORM 插入,
|
||||
# 否则后续新增列会被 SQLAlchemy 一并写进旧表。
|
||||
with mig_engine.begin() as conn:
|
||||
for task_id, item_id, extra in (
|
||||
(
|
||||
broken_id,
|
||||
"MIG-0090-1",
|
||||
{
|
||||
"account_id": account_id,
|
||||
"lease_id": lease_id,
|
||||
"exchange_status": "points_short",
|
||||
"issue_code": "",
|
||||
"last_error": "",
|
||||
},
|
||||
),
|
||||
(
|
||||
unassigned_id,
|
||||
"MIG-0090-2",
|
||||
{
|
||||
"account_id": None,
|
||||
"lease_id": None,
|
||||
"exchange_status": "not_started",
|
||||
"issue_code": "no_eligible_account",
|
||||
"last_error": "当前没有可用账号",
|
||||
},
|
||||
),
|
||||
):
|
||||
conn.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO platform_fulfillment_tasks (
|
||||
id, source_system, source_order_id,
|
||||
source_order_item_id, account_id, lease_id,
|
||||
execution_status, binding_status, exchange_status,
|
||||
issue_code, last_error, created_at, updated_at
|
||||
) VALUES (
|
||||
:id, 'order_site', 'MIG-0090',
|
||||
:source_order_item_id, :account_id, :lease_id,
|
||||
'ready', 'not_started', :exchange_status,
|
||||
:issue_code, :last_error,
|
||||
UTC_TIMESTAMP(), UTC_TIMESTAMP()
|
||||
)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": task_id,
|
||||
"source_order_item_id": item_id,
|
||||
**extra,
|
||||
},
|
||||
)
|
||||
|
||||
command.upgrade(cfg, "20261118_0090")
|
||||
|
||||
with Session(mig_engine) as session:
|
||||
repaired = session.get(PlatformFulfillmentTask, broken_id)
|
||||
untouched = session.get(PlatformFulfillmentTask, unassigned_id)
|
||||
assert repaired is not None
|
||||
assert repaired.execution_status == "allocated"
|
||||
assert repaired.issue_code == "points_short"
|
||||
assert repaired.last_error == "账号积分不足,可充值后继续"
|
||||
assert repaired.next_allocate_at is None
|
||||
assert untouched is not None
|
||||
assert untouched.execution_status == "ready"
|
||||
assert untouched.issue_code == "no_eligible_account"
|
||||
with mig_engine.connect() as conn:
|
||||
rows = {
|
||||
int(row["id"]): row
|
||||
for row in conn.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT id, execution_status, issue_code, last_error,
|
||||
next_allocate_at
|
||||
FROM platform_fulfillment_tasks
|
||||
WHERE id IN (:broken_id, :unassigned_id)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"broken_id": broken_id,
|
||||
"unassigned_id": unassigned_id,
|
||||
},
|
||||
).mappings()
|
||||
}
|
||||
repaired = rows[broken_id]
|
||||
untouched = rows[unassigned_id]
|
||||
assert repaired["execution_status"] == "allocated"
|
||||
assert repaired["issue_code"] == "points_short"
|
||||
assert repaired["last_error"] == "账号积分不足,可充值后继续"
|
||||
assert repaired["next_allocate_at"] is None
|
||||
assert untouched["execution_status"] == "ready"
|
||||
assert untouched["issue_code"] == "no_eligible_account"
|
||||
finally:
|
||||
database_module.DATABASE_URL = original_url
|
||||
mig_engine.dispose()
|
||||
|
||||
Reference in New Issue
Block a user