fix(douyu): 统一外部协作账号租约
This commit is contained in:
+122
-1
@@ -40,7 +40,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261119_0097"
|
||||
HEAD_REVISION = "20261119_0098"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
@@ -758,3 +758,124 @@ class TestMigrationSmoke:
|
||||
mig_engine.dispose()
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(text(f"DROP DATABASE IF EXISTS `{mig_db}`;"))
|
||||
|
||||
def test_0098_backfills_collab_leases_without_stealing_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'])}:{quote_plus(creds['password'])}"
|
||||
f"@{creds['host']}:{creds['port']}/{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, "20261119_0097")
|
||||
|
||||
with Session(mig_engine) as session:
|
||||
owner = User(
|
||||
username="migration-0098-owner",
|
||||
password_hash="hash",
|
||||
role="operation",
|
||||
is_active=True,
|
||||
)
|
||||
session.add(owner)
|
||||
session.flush()
|
||||
free_account = Account(
|
||||
username="migration-0098-free",
|
||||
password="p",
|
||||
email="free@example.com",
|
||||
email_password="ep",
|
||||
)
|
||||
busy_account = Account(
|
||||
username="migration-0098-busy",
|
||||
password="p",
|
||||
email="busy@example.com",
|
||||
email_password="ep",
|
||||
)
|
||||
session.add_all([free_account, busy_account])
|
||||
session.flush()
|
||||
session.add_all(
|
||||
[
|
||||
DouyuExternalOrder(
|
||||
order_no="MIG-0098-FREE",
|
||||
owner_id=owner.id,
|
||||
account_id=free_account.id,
|
||||
account_username=free_account.username,
|
||||
status="to_recharge",
|
||||
),
|
||||
DouyuExternalOrder(
|
||||
order_no="MIG-0098-BUSY",
|
||||
owner_id=owner.id,
|
||||
account_id=busy_account.id,
|
||||
account_username=busy_account.username,
|
||||
status="incomplete",
|
||||
),
|
||||
AccountLease(
|
||||
account_id=busy_account.id,
|
||||
holder_user_id=owner.id,
|
||||
scope="elite",
|
||||
purpose="platform_fulfillment",
|
||||
batch_id="pf:0098",
|
||||
status="active",
|
||||
),
|
||||
]
|
||||
)
|
||||
session.commit()
|
||||
free_account_id = free_account.id
|
||||
busy_account_id = busy_account.id
|
||||
|
||||
command.upgrade(cfg, "head")
|
||||
|
||||
with Session(mig_engine) as session:
|
||||
free_lease = (
|
||||
session.query(AccountLease)
|
||||
.filter_by(account_id=free_account_id, status="active")
|
||||
.one()
|
||||
)
|
||||
busy_lease = (
|
||||
session.query(AccountLease)
|
||||
.filter_by(account_id=busy_account_id, status="active")
|
||||
.one()
|
||||
)
|
||||
assert free_lease.purpose == "douyu_collab"
|
||||
assert free_lease.batch_id == f"collab:{free_account_id}"
|
||||
assert free_lease.release_reason == "migration_backfill"
|
||||
assert busy_lease.purpose == "platform_fulfillment"
|
||||
assert (
|
||||
session.query(AccountEvent)
|
||||
.filter(
|
||||
AccountEvent.account_id == free_account_id,
|
||||
AccountEvent.event_type == "lease_acquired",
|
||||
AccountEvent.source == "migration",
|
||||
)
|
||||
.count()
|
||||
== 1
|
||||
)
|
||||
assert (
|
||||
session.query(AccountEvent)
|
||||
.filter(
|
||||
AccountEvent.account_id == busy_account_id,
|
||||
AccountEvent.event_type == "lease_conflict",
|
||||
AccountEvent.source == "migration",
|
||||
)
|
||||
.count()
|
||||
== 1
|
||||
)
|
||||
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