This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfbdaf1
commit f0d2c1c
Showing
7 changed files
with
144 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
CREATE FUNCTION update_current_payment_instructions() | ||
RETURNS trigger AS $$ | ||
BEGIN | ||
|
||
-- Allow updating is_funded and due via the current_payment_instructions view for convenience. | ||
UPDATE payment_instructions | ||
SET is_funded = NEW.is_funded | ||
, due = NEW.due | ||
WHERE id = NEW.id; | ||
|
||
RETURN NULL; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
|
||
CREATE TRIGGER update_current_payment_instructions | ||
INSTEAD OF UPDATE ON current_payment_instructions | ||
FOR EACH ROW EXECUTE PROCEDURE update_current_payment_instructions(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
CREATE FUNCTION propagate_payment_instructions_insert() | ||
RETURNS trigger AS $$ | ||
DECLARE | ||
is_new_giver bool; | ||
delta_n int; | ||
delta_amount numeric(35,2); | ||
BEGIN | ||
is_new_giver := ( SELECT id | ||
FROM current_payment_instructions | ||
WHERE participant_id=NEW.participant_id | ||
AND team_id=NEW.team_id | ||
AND amount > 0 | ||
) IS NULL; | ||
|
||
delta_amount := NEW.amount; | ||
|
||
IF NEW.amount = 0 THEN | ||
IF is_new_giver THEN | ||
delta_n := 0; | ||
ELSE | ||
delta_n := -1; | ||
END IF; | ||
ELSE | ||
IF is_new_giver THEN | ||
delta_n := 1; | ||
ELSE | ||
delta_n := 0; | ||
END IF; | ||
END IF; | ||
|
||
UPDATE participants | ||
SET giving = giving + delta_amount | ||
, ngiving_to = ngiving_to + delta_n | ||
WHERE id = NEW.participant_id; | ||
|
||
UPDATE teams | ||
SET receiving = receiving + delta_amount | ||
, nreceiving_from = nreceiving_from + delta_n | ||
WHERE id = NEW.team_id; | ||
|
||
RETURN NEW; | ||
|
||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
|
||
CREATE TRIGGER propagate_payment_instructions_insert | ||
BEFORE INSERT ON payment_instructions | ||
FOR EACH ROW EXECUTE PROCEDURE propagate_payment_instructions_insert(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
from decimal import Decimal as D | ||
from gratipay.testing import Harness | ||
from gratipay.models.participant import Participant | ||
from gratipay.models.team import Team | ||
|
||
|
||
P = Participant.from_username | ||
T = Team.from_slug | ||
|
||
|
||
class Tests(Harness): | ||
|
||
def setUp(self): | ||
self.alice = self.make_participant('alice', claimed_time='now', last_bill_result='') | ||
self.bob = self.make_participant('bob', claimed_time='now', last_bill_result='') | ||
self.enterprise = self.make_team() | ||
|
||
|
||
# g - giving | ||
|
||
def test_g_starts_out_zero(self): | ||
assert P('alice').giving == 0 | ||
|
||
def test_g_increases_with_payment_instruction(self): | ||
self.alice.set_payment_instruction(self.enterprise, '1.00') | ||
assert P('alice').giving == D('1.00') | ||
|
||
|
||
# ngt - ngiving_to | ||
|
||
def test_ngt_starts_out_zero(self): | ||
assert P('alice').ngiving_to == 0 | ||
|
||
def test_ngt_increases_with_payment_instruction(self): | ||
self.alice.set_payment_instruction(self.enterprise, '1.00') | ||
assert P('alice').ngiving_to == 1 | ||
|
||
|
||
# r - receiving | ||
|
||
def test_r_starts_out_zero(self): | ||
assert T('TheEnterprise').receiving == 0 | ||
|
||
def test_r_increases_with_payment_instruction(self): | ||
self.alice.set_payment_instruction(self.enterprise, '1.00') | ||
assert T('TheEnterprise').receiving == D('1.00') | ||
|
||
|
||
# nrf - nreceiving_from | ||
|
||
def test_nrf_starts_out_zero(self): | ||
assert T('TheEnterprise').nreceiving_from == 0 | ||
|
||
def test_nrf_increases_with_payment_instruction(self): | ||
self.alice.set_payment_instruction(self.enterprise, '1.00') | ||
assert T('TheEnterprise').nreceiving_from == 1 |