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

Commit

Permalink
Merge pull request #4058 from gratipay/835-payment_instructions-1
Browse files Browse the repository at this point in the history
first step of 835 for payment_instructions
  • Loading branch information
Paul Kuruvilla authored Jun 17, 2016
2 parents f697ef3 + 4246b29 commit b92450e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
7 changes: 4 additions & 3 deletions gratipay/models/participant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,18 +839,19 @@ def set_payment_instruction(self, team, amount, update_self=True, update_team=Tr
NEW_PAYMENT_INSTRUCTION = """\
INSERT INTO payment_instructions
(ctime, participant, team, amount)
(ctime, participant, participant_id, team, team_id, amount)
VALUES ( COALESCE (( SELECT ctime
FROM payment_instructions
WHERE (participant=%(participant)s AND team=%(team)s)
LIMIT 1
), CURRENT_TIMESTAMP)
, %(participant)s, %(team)s, %(amount)s
, %(participant)s, %(participant_id)s, %(team)s, %(team_id)s, %(amount)s
)
RETURNING *
"""
args = dict(participant=self.username, team=team.slug, amount=amount)
args = dict(participant=self.username, participant_id=self.id, team=team.slug,
team_id=team.id, amount=amount)
t = (cursor or self.db).one(NEW_PAYMENT_INSTRUCTION, args)
t_dict = t._asdict()

Expand Down
7 changes: 5 additions & 2 deletions gratipay/models/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,14 @@ def migrate_tips(self):
WITH rows AS (
INSERT INTO payment_instructions
(ctime, mtime, participant, team, amount, is_funded)
(ctime, mtime, participant, participant_id, team, team_id, amount,
is_funded)
SELECT ct.ctime
, ct.mtime
, ct.tipper
, (SELECT id FROM participants WHERE username=ct.tipper)
, %(slug)s
, %(team_id)s
, ct.amount
, ct.is_funded
FROM current_tips ct
Expand All @@ -345,7 +348,7 @@ def migrate_tips(self):
RETURNING 1
) SELECT count(*) FROM rows;
""", {'slug': self.slug, 'owner': self.owner})
""", {'slug': self.slug, 'team_id': self.id, 'owner': self.owner})


# Images
Expand Down
2 changes: 2 additions & 0 deletions gratipay/utils/fake_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ def fake_payment_instruction(db, participant, team):
, ctime=faker.date_time_this_year()
, mtime=faker.date_time_this_month()
, participant=participant.username
, participant_id=participant.id
, team=team.slug
, team_id=team.id
, amount=fake_tip_amount()
)

Expand Down
8 changes: 8 additions & 0 deletions sql/branch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BEGIN;

ALTER TABLE payment_instructions ADD COLUMN participant_id bigint DEFAULT NULL
REFERENCES participants(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
ALTER TABLE payment_instructions ADD COLUMN team_id bigint DEFAULT NULL
REFERENCES teams(id) ON UPDATE RESTRICT ON DELETE RESTRICT;

END;
7 changes: 7 additions & 0 deletions tests/py/test_participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,13 @@ def test_spi_resets_is_free_rider_to_null(self):
assert alice.is_free_rider is None
assert Participant.from_username('alice').is_free_rider is None

def test_spi_sets_id_fields(self):
alice = self.make_participant('alice', claimed_time='now', last_bill_result='')
team = self.make_team()
actual = alice.set_payment_instruction(team, '1.00')
assert actual['participant_id'] == alice.id
assert actual['team_id'] == team.id


# get_teams - gt

Expand Down
15 changes: 10 additions & 5 deletions tests/py/test_tip_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class Tests(Harness):

def setUp(self):
self.admin = self.make_participant('admin', is_admin=True)
alice = self.make_participant('alice', claimed_time='now')
bob = self.make_participant('bob', claimed_time='now')
self.alice = self.make_participant('alice', claimed_time='now')
self.bob = self.make_participant('bob', claimed_time='now')
self.make_participant('old_team')
self.make_tip(alice, 'old_team', '1.00')
self.make_tip(bob, 'old_team', '2.00')
self.make_tip(self.alice, 'old_team', '1.00')
self.make_tip(self.bob, 'old_team', '2.00')
self.new_team = self.make_team('new_team', owner='old_team', is_approved=True)

def setTeamStatus(self, status):
Expand All @@ -31,13 +31,18 @@ def capture(line):
def test_mt_migrates_tips_to_payment_instructions(self):
assert self.new_team.migrate_tips() == 2

payment_instructions = self.db.all("SELECT * FROM payment_instructions ORDER BY participant ASC")
payment_instructions = self.db.all("SELECT * FROM payment_instructions "
"ORDER BY participant ASC")
assert len(payment_instructions) == 2
assert payment_instructions[0].participant == 'alice'
assert payment_instructions[0].participant_id == self.alice.id
assert payment_instructions[0].team == 'new_team'
assert payment_instructions[0].team_id == self.new_team.id
assert payment_instructions[0].amount == 1
assert payment_instructions[1].participant == 'bob'
assert payment_instructions[1].participant_id == self.bob.id
assert payment_instructions[1].team == 'new_team'
assert payment_instructions[1].team_id == self.new_team.id
assert payment_instructions[1].amount == 2

def test_mt_only_runs_once(self):
Expand Down

0 comments on commit b92450e

Please sign in to comment.