feat(fulfillment): 增加账号商品能力索引与生命周期事件
This commit is contained in:
+164
-1
@@ -25,8 +25,12 @@ import web.backend.database as database_module
|
||||
from tests.support.db import get_mysql_credentials, get_test_engine
|
||||
from web.backend.database import Base
|
||||
from web.backend.models import (
|
||||
Account,
|
||||
AccountEvent,
|
||||
AccountLease,
|
||||
AuditLog,
|
||||
HuyaExternalOrder,
|
||||
PlatformFulfillmentTask,
|
||||
User,
|
||||
WeiboAccount,
|
||||
)
|
||||
@@ -35,7 +39,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261118_0089"
|
||||
HEAD_REVISION = "20261118_0093"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
@@ -521,3 +525,162 @@ class TestMigrationSmoke:
|
||||
mig_engine.dispose()
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(text(f"DROP DATABASE IF EXISTS `{mig_db}`;"))
|
||||
|
||||
def test_0090_restores_allocated_tasks_with_active_leases(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()
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(
|
||||
text(
|
||||
f"CREATE DATABASE `{mig_db}` "
|
||||
"CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;"
|
||||
)
|
||||
)
|
||||
|
||||
original_url = database_module.DATABASE_URL
|
||||
mig_engine = create_engine(mig_url)
|
||||
try:
|
||||
database_module.DATABASE_URL = mig_url
|
||||
cfg = _migration_config()
|
||||
cfg.set_main_option("sqlalchemy.url", mig_url)
|
||||
command.upgrade(cfg, "20261118_0089")
|
||||
|
||||
with Session(mig_engine) as session:
|
||||
user = User(
|
||||
username="migration-pf-actor",
|
||||
password_hash="hash",
|
||||
role="super_admin",
|
||||
is_active=True,
|
||||
)
|
||||
account = Account(
|
||||
username="migration-pf-account",
|
||||
password="p",
|
||||
email="migration-pf@example.com",
|
||||
email_password="ep",
|
||||
)
|
||||
session.add_all([user, account])
|
||||
session.commit()
|
||||
lease = AccountLease(
|
||||
account_id=account.id,
|
||||
holder_user_id=user.id,
|
||||
scope="elite",
|
||||
purpose="platform_fulfillment",
|
||||
status="active",
|
||||
)
|
||||
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
|
||||
|
||||
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"
|
||||
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_0092_backfills_official_exchange_subject_key(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()
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(
|
||||
text(
|
||||
f"CREATE DATABASE `{mig_db}` "
|
||||
"CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;"
|
||||
)
|
||||
)
|
||||
|
||||
original_url = database_module.DATABASE_URL
|
||||
mig_engine = create_engine(mig_url)
|
||||
try:
|
||||
database_module.DATABASE_URL = mig_url
|
||||
cfg = _migration_config()
|
||||
cfg.set_main_option("sqlalchemy.url", mig_url)
|
||||
command.upgrade(cfg, "20261118_0091")
|
||||
|
||||
with Session(mig_engine) as session:
|
||||
account = Account(
|
||||
username="migration-exchange-account",
|
||||
password="p",
|
||||
email="migration-exchange@example.com",
|
||||
email_password="ep",
|
||||
)
|
||||
session.add(account)
|
||||
session.commit()
|
||||
event = AccountEvent(
|
||||
account_id=account.id,
|
||||
event_type="exchange",
|
||||
category="business",
|
||||
occurred_at=datetime.now(UTC),
|
||||
title="兑换测试商品",
|
||||
detail={"commodity_id": "MIG-COMMODITY-0092"},
|
||||
source="official_sync",
|
||||
subject_type="goods",
|
||||
subject_key="",
|
||||
)
|
||||
session.add(event)
|
||||
session.commit()
|
||||
event_id = event.id
|
||||
|
||||
command.upgrade(cfg, "20261118_0092")
|
||||
|
||||
with Session(mig_engine) as session:
|
||||
repaired = session.get(AccountEvent, event_id)
|
||||
assert repaired is not None
|
||||
assert repaired.subject_type == "commodity"
|
||||
assert repaired.subject_key == "MIG-COMMODITY-0092"
|
||||
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}`;"))
|
||||
|
||||
Reference in New Issue
Block a user