Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Fix payment table semantics #3430

Merged
merged 4 commits into from
May 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion gratipay/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def _check_balances(cursor):

https://github.com/gratipay/gratipay.com/issues/1118
"""
return # XXX Bring me back!
b = cursor.all("""
select p.username, expected, balance as actual
from (
Expand Down Expand Up @@ -95,6 +94,20 @@ def _check_balances(cursor):

union all

select participant as username, sum(amount) as a
from payments
where direction='to-participant'
group by participant

union all

select participant as username, sum(-amount) as a
from payments
where direction='to-team'
group by participant

union all

select tippee as username, sum(amount) as a
from transfers
group by tippee
Expand Down
18 changes: 15 additions & 3 deletions sql/payday.sql
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,25 @@ CREATE TEMPORARY TABLE payday_payments

CREATE OR REPLACE FUNCTION pay(text, text, numeric, payment_direction)
RETURNS void AS $$
DECLARE
participant_delta numeric;
team_delta numeric;
BEGIN
IF ($3 = 0) THEN RETURN; END IF;

IF ($4 = 'to-team') THEN
participant_delta := -$3;
team_delta := $3;
ELSE
participant_delta := $3;
team_delta := -$3;
END IF;

UPDATE payday_participants
SET new_balance = (new_balance - $3)
SET new_balance = (new_balance + participant_delta)
WHERE username = $1;
UPDATE payday_teams
SET balance = (balance + $3)
SET balance = (balance + team_delta)
WHERE slug = $2;
INSERT INTO payday_payments
(participant, team, amount, direction)
Expand Down Expand Up @@ -176,7 +188,7 @@ CREATE TRIGGER process_take AFTER INSERT ON payday_takes

CREATE OR REPLACE FUNCTION process_draw() RETURNS trigger AS $$
BEGIN
EXECUTE pay(NEW.owner, NEW.slug, -NEW.balance, 'to-participant');
EXECUTE pay(NEW.owner, NEW.slug, NEW.balance, 'to-participant');
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
Expand Down
18 changes: 13 additions & 5 deletions tests/py/test_billing_payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,16 @@ def test_card_hold_error(self, tfh, fch):
payday = self.fetch_payday()
assert payday['ncc_failing'] == 1

def test_payin_doesnt_make_null_transfers(self):
def test_payin_doesnt_make_null_payments(self):
team = self.make_team('Gratiteam', is_approved=True)
alice = self.make_participant('alice', claimed_time='now')
alice.set_tip_to(self.homer, 1)
alice.set_tip_to(self.homer, 0)
alice.set_subscription_to(team, 1)
alice.set_subscription_to(team, 0)
a_team = self.make_participant('a_team', claimed_time='now', number='plural')
a_team.add_member(alice)
Payday.start().payin()
transfers0 = self.db.all("SELECT * FROM transfers WHERE amount = 0")
assert not transfers0
payments = self.db.all("SELECT * FROM payments WHERE amount = 0")
assert not payments

def test_process_subscriptions(self):
alice = self.make_participant('alice', claimed_time='now', balance=1)
Expand All @@ -390,6 +391,10 @@ def test_process_subscriptions(self):
assert Participant.from_username('hannibal').balance == 0
assert Participant.from_username('lecter').balance == 0

payment = self.db.one("SELECT * FROM payments")
assert payment.amount == D('0.51')
assert payment.direction == 'to-team'

@pytest.mark.xfail(reason="haven't migrated_transfer_takes yet")
def test_transfer_takes(self):
a_team = self.make_participant('a_team', claimed_time='now', number='plural', balance=20)
Expand Down Expand Up @@ -443,6 +448,9 @@ def test_process_draws(self):
assert Participant.from_id(alice.id).balance == D('0.49')
assert Participant.from_username('hannibal').balance == D('0.51')

payment = self.db.one("SELECT * FROM payments WHERE direction='to-participant'")
assert payment.amount == D('0.51')

@pytest.mark.xfail(reason="haven't migrated_transfer_takes yet")
@mock.patch.object(Payday, 'fetch_card_holds')
def test_transfer_takes_doesnt_make_negative_transfers(self, fch):
Expand Down