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

Commit

Permalink
Convert clear_tips_giving to clear_subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Jul 6, 2015
1 parent 3baef5f commit afe4f37
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 45 deletions.
26 changes: 13 additions & 13 deletions gratipay/models/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def close(self):
"""Close the participant's account.
"""
with self.db.get_cursor() as cursor:
self.clear_tips_giving(cursor)
self.clear_subscriptions(cursor)
self.clear_tips_receiving(cursor)
self.clear_personal_information(cursor)
self.final_check(cursor)
Expand All @@ -342,22 +342,22 @@ def update_is_closed(self, is_closed, cursor=None):
self.set_attributes(is_closed=is_closed)


def clear_tips_giving(self, cursor):
"""Zero out tips from a given user.
def clear_subscriptions(self, cursor):
"""Zero out the participant's subscriptions.
"""
tippees = cursor.all("""
SELECT ( SELECT participants.*::participants
FROM participants
WHERE username=tippee
) AS tippee
FROM current_tips
WHERE tipper = %s
teams = cursor.all("""
SELECT ( SELECT teams.*::teams
FROM teams
WHERE slug=team
) AS team
FROM current_subscriptions
WHERE subscriber = %s
AND amount > 0
""", (self.username,))
for tippee in tippees:
self.set_tip_to(tippee, '0.00', update_self=False, cursor=cursor)
for team in teams:
self.set_subscription_to(team, '0.00', update_self=False, cursor=cursor)

def clear_tips_receiving(self, cursor):
"""Zero out tips to a given user.
Expand Down
66 changes: 34 additions & 32 deletions tests/py/test_close.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,50 +52,52 @@ def test_cant_post_to_close_page_during_payday(self):
assert 'Try Again Later' in body


# ctg - clear_tips_giving
# cs - clear_subscriptions

def test_ctg_clears_tips_giving(self):
def test_cs_clears_subscriptions(self):
alice = self.make_participant('alice', claimed_time='now', last_bill_result='')
alice.set_tip_to(self.make_participant('bob', claimed_time='now').username, D('1.00'))
ntips = lambda: self.db.one("SELECT count(*) FROM current_tips "
"WHERE tipper='alice' AND amount > 0")
assert ntips() == 1
alice.set_subscription_to(self.make_team(), D('1.00'))
nsubscriptions = lambda: self.db.one("SELECT count(*) FROM current_subscriptions "
"WHERE subscriber='alice' AND amount > 0")
assert nsubscriptions() == 1
with self.db.get_cursor() as cursor:
alice.clear_tips_giving(cursor)
assert ntips() == 0
alice.clear_subscriptions(cursor)
assert nsubscriptions() == 0

def test_ctg_doesnt_duplicate_zero_tips(self):
def test_cs_doesnt_duplicate_zero_subscriptions(self):
alice = self.make_participant('alice', claimed_time='now')
bob = self.make_participant('bob')
alice.set_tip_to(bob, D('1.00'))
alice.set_tip_to(bob, D('0.00'))
ntips = lambda: self.db.one("SELECT count(*) FROM tips WHERE tipper='alice'")
assert ntips() == 2
A = self.make_team()
alice.set_subscription_to(A, D('1.00'))
alice.set_subscription_to(A, D('0.00'))
nsubscriptions = lambda: self.db.one("SELECT count(*) FROM subscriptions "
"WHERE subscriber='alice'")
assert nsubscriptions() == 2
with self.db.get_cursor() as cursor:
alice.clear_tips_giving(cursor)
assert ntips() == 2
alice.clear_subscriptions(cursor)
assert nsubscriptions() == 2

def test_ctg_doesnt_zero_when_theres_no_tip(self):
def test_cs_doesnt_zero_when_theres_no_subscription(self):
alice = self.make_participant('alice')
ntips = lambda: self.db.one("SELECT count(*) FROM tips WHERE tipper='alice'")
assert ntips() == 0
nsubscriptions = lambda: self.db.one("SELECT count(*) FROM subscriptions "
"WHERE subscriber='alice'")
assert nsubscriptions() == 0
with self.db.get_cursor() as cursor:
alice.clear_tips_giving(cursor)
assert ntips() == 0
alice.clear_subscriptions(cursor)
assert nsubscriptions() == 0

def test_ctg_clears_multiple_tips_giving(self):
def test_cs_clears_multiple_subscriptions(self):
alice = self.make_participant('alice', claimed_time='now')
alice.set_tip_to(self.make_participant('bob', claimed_time='now').username, D('1.00'))
alice.set_tip_to(self.make_participant('carl', claimed_time='now').username, D('1.00'))
alice.set_tip_to(self.make_participant('darcy', claimed_time='now').username, D('1.00'))
alice.set_tip_to(self.make_participant('evelyn', claimed_time='now').username, D('1.00'))
alice.set_tip_to(self.make_participant('francis', claimed_time='now').username, D('1.00'))
ntips = lambda: self.db.one("SELECT count(*) FROM current_tips "
"WHERE tipper='alice' AND amount > 0")
assert ntips() == 5
alice.set_subscription_to(self.make_team('A'), D('1.00'))
alice.set_subscription_to(self.make_team('B'), D('1.00'))
alice.set_subscription_to(self.make_team('C'), D('1.00'))
alice.set_subscription_to(self.make_team('D'), D('1.00'))
alice.set_subscription_to(self.make_team('E'), D('1.00'))
nsubscriptions = lambda: self.db.one("SELECT count(*) FROM current_subscriptions "
"WHERE subscriber='alice' AND amount > 0")
assert nsubscriptions() == 5
with self.db.get_cursor() as cursor:
alice.clear_tips_giving(cursor)
assert ntips() == 0
alice.clear_subscriptions(cursor)
assert nsubscriptions() == 0


# ctr - clear_tips_receiving
Expand Down

0 comments on commit afe4f37

Please sign in to comment.