fix: add ledger refund uniqueness guard

This commit is contained in:
QiuSW
2026-07-02 16:59:02 +08:00
parent be97a35360
commit 4e8c1b45a4
7 changed files with 93 additions and 8 deletions
+35
View File
@@ -143,6 +143,41 @@ class BillingCoreModelTests(TestCase):
balance_after=-1,
)
def test_points_ledger_allows_consume_and_refund_but_rejects_duplicate_refund(self):
call = CallRecord.objects.create(
user=self.user,
operation_type=CallRecord.OperationType.TITLE,
alias="title-standard",
model_used="gpt-5.5",
points_cost=2,
status=CallRecord.Status.FAILED,
)
PointsLedger.objects.create(
user=self.user,
change_type=PointsLedger.ChangeType.CONSUME,
points_delta=-2,
balance_after=98,
ref_call=call,
)
PointsLedger.objects.create(
user=self.user,
change_type=PointsLedger.ChangeType.REFUND,
points_delta=2,
balance_after=100,
ref_call=call,
)
with self.assertRaises(IntegrityError):
with transaction.atomic():
PointsLedger.objects.create(
user=self.user,
change_type=PointsLedger.ChangeType.REFUND,
points_delta=2,
balance_after=102,
ref_call=call,
)
def test_billing_models_are_registered_in_admin(self):
self.assertIn(UserWallet, admin.site._registry)
self.assertIn(ApiKey, admin.site._registry)