diff --git a/gittip/billing/payday.py b/gittip/billing/payday.py index f28df7616a..292b1739b2 100644 --- a/gittip/billing/payday.py +++ b/gittip/billing/payday.py @@ -264,7 +264,7 @@ def transfer(self, tipper, tippee, amount): try: self.debit_participant(cursor, tipper, amount) - except ValueError: + except IntegrityError: return False self.credit_participant(cursor, tippee, amount) @@ -288,13 +288,13 @@ def debit_participant(self, cursor, participant, amount): RETURNING balance """ + + # This will fail with IntegrityError if the balanced goes below zero. + # We catch that and return false in our caller. cursor.execute(DECREMENT, (amount, participant)) + rec = cursor.fetchone() assert rec is not None, (amount, participant) # sanity check - if rec['balance'] < 0: - # User is out of money. Bail. The transaction will be rolled back - # by our context manager. - raise ValueError() # TODO: proper exception type def credit_participant(self, cursor, participant, amount): diff --git a/tests/test_billing.py b/tests/test_billing.py index 3e784a4222..4cb7d9c681 100644 --- a/tests/test_billing.py +++ b/tests/test_billing.py @@ -18,7 +18,7 @@ def setUp(self): super(TestCard, self).setUp() self.balanced_account_uri = '/v1/marketplaces/M123/accounts/A123' self.stripe_customer_id = 'deadbeef' - + @mock.patch('balanced.Account') def test_balanced_card(self, ba): card = mock.Mock() @@ -702,7 +702,7 @@ def get_balance_amount(participant): with self.db.get_connection() as conn: cur = conn.cursor() - with self.assertRaises(ValueError): + with self.assertRaises(IntegrityError): self.payday.debit_participant(cur, participant, amount) def test_credit_participant(self):