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

Commit

Permalink
Remove all disbursement strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Jul 6, 2015
1 parent 91cae6d commit 3baef5f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 250 deletions.
60 changes: 1 addition & 59 deletions gratipay/models/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,21 +319,10 @@ def set_as_claimed(self):
# Closing
# =======

class UnknownDisbursementStrategy(Exception): pass

def close(self, disbursement_strategy):
def close(self):
"""Close the participant's account.
"""
with self.db.get_cursor() as cursor:
if disbursement_strategy == None:
pass # No balance, supposedly. final_check will make sure.
# XXX Bring me back!
#elif disbursement_strategy == 'downstream':
# # This in particular needs to come before clear_tips_giving.
# self.distribute_balance_as_final_gift(cursor)
else:
raise self.UnknownDisbursementStrategy

self.clear_tips_giving(cursor)
self.clear_tips_receiving(cursor)
self.clear_personal_information(cursor)
Expand All @@ -353,53 +342,6 @@ def update_is_closed(self, is_closed, cursor=None):
self.set_attributes(is_closed=is_closed)


class NoOneToGiveFinalGiftTo(Exception): pass

def distribute_balance_as_final_gift(self, cursor):
"""Distribute a balance as a final gift.
"""
raise NotImplementedError # XXX Bring me back!
if self.balance == 0:
return

claimed_tips, claimed_total = self.get_giving_for_profile()
transfers = []
distributed = Decimal('0.00')

for tip in claimed_tips:
rate = tip.amount / claimed_total
pro_rated = (self.balance * rate).quantize(Decimal('0.01'), ROUND_DOWN)
if pro_rated == 0:
continue
distributed += pro_rated
transfers.append([tip.tippee, pro_rated])

if not transfers:
raise self.NoOneToGiveFinalGiftTo

diff = self.balance - distributed
if diff != 0:
transfers[0][1] += diff # Give it to the highest receiver.

for tippee, amount in transfers:
assert amount > 0
balance = cursor.one( "UPDATE participants SET balance=balance - %s "
"WHERE username=%s RETURNING balance"
, (amount, self.username)
)
assert balance >= 0 # sanity check
cursor.run( "UPDATE participants SET balance=balance + %s WHERE username=%s"
, (amount, tippee)
)
cursor.run( "INSERT INTO transfers (tipper, tippee, amount, context) "
"VALUES (%s, %s, %s, 'final-gift')"
, (self.username, tippee, amount)
)

assert balance == 0
self.set_attributes(balance=balance)


def clear_tips_giving(self, cursor):
"""Zero out tips from a given user.
"""
Expand Down
126 changes: 10 additions & 116 deletions tests/py/test_close.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,16 @@ class TestClosing(Harness):

# close

@pytest.mark.xfail(reason='https://github.com/gratipay/gratipay.com/pull/3454')
def test_close_closes(self):
team = self.make_participant('team', claimed_time='now', number='plural', balance=50)
alice = self.make_participant('alice', claimed_time='now', balance=D('10.00'))
bob = self.make_participant('bob', claimed_time='now')
carl = self.make_participant('carl', claimed_time='now')

alice.set_tip_to(bob, D('3.00'))
carl.set_tip_to(alice, D('2.00'))

team.add_member(alice)
team.add_member(bob)
assert len(team.get_current_takes()) == 2 # sanity check

alice.close('downstream')

assert carl.get_tip_to('alice')['amount'] == 0
assert alice.balance == 0
assert len(team.get_current_takes()) == 1
alice = self.make_participant('alice', claimed_time='now')
alice.close()
assert Participant.from_username('alice').is_closed

def test_close_raises_for_unknown_disbursement_strategy(self):
alice = self.make_participant('alice', balance=D('0.00'))
with pytest.raises(alice.UnknownDisbursementStrategy):
alice.close('cheese')
def test_close_fails_if_still_a_balance(self):
alice = self.make_participant('alice', claimed_time='now', balance=D('10.00'))
with pytest.raises(alice.BalanceIsNotZero):
alice.close()

@pytest.mark.xfail(reason='https://github.com/gratipay/gratipay.com/pull/3454')
def test_close_page_is_usually_available(self):
self.make_participant('alice', claimed_time='now')
body = self.client.GET('/~alice/settings/close', auth_as='alice').body
Expand All @@ -54,18 +38,12 @@ def test_close_page_is_not_available_during_payday(self):
assert 'Personal Information' not in body
assert 'Try Again Later' in body

@pytest.mark.xfail(reason='https://github.com/gratipay/gratipay.com/pull/3454')
def test_can_post_to_close_page(self):
alice = self.make_participant('alice', claimed_time='now', balance=7)
bob = self.make_participant('bob', claimed_time='now')
alice.set_tip_to(bob, D('10.00'))

data = {'disbursement_strategy': 'downstream'}
response = self.client.PxST('/~alice/settings/close', auth_as='alice', data=data)
self.make_participant('alice', claimed_time='now')
response = self.client.PxST('/~alice/settings/close', auth_as='alice')
assert response.code == 302
assert response.headers['Location'] == '/~alice/'
assert Participant.from_username('alice').balance == 0
assert Participant.from_username('bob').balance == 7
assert Participant.from_username('alice').is_closed

def test_cant_post_to_close_page_during_payday(self):
Payday.start()
Expand All @@ -74,90 +52,6 @@ def test_cant_post_to_close_page_during_payday(self):
assert 'Try Again Later' in body


# dbafg - distribute_balance_as_final_gift

@pytest.mark.xfail(reason='https://github.com/gratipay/gratipay.com/pull/3467')
def test_dbafg_distributes_balance_as_final_gift(self):
alice = self.make_participant('alice', claimed_time='now', balance=D('10.00'))
bob = self.make_participant('bob', claimed_time='now')
carl = self.make_participant('carl', claimed_time='now')
alice.set_tip_to(bob, D('3.00'))
alice.set_tip_to(carl, D('2.00'))
with self.db.get_cursor() as cursor:
alice.distribute_balance_as_final_gift(cursor)
assert Participant.from_username('bob').balance == D('6.00')
assert Participant.from_username('carl').balance == D('4.00')
assert Participant.from_username('alice').balance == D('0.00')

@pytest.mark.xfail(reason='https://github.com/gratipay/gratipay.com/pull/3467')
def test_dbafg_needs_claimed_tips(self):
alice = self.make_participant('alice', claimed_time='now', balance=D('10.00'))
bob = self.make_participant('bob')
carl = self.make_participant('carl')
alice.set_tip_to(bob, D('3.00'))
alice.set_tip_to(carl, D('2.00'))
with self.db.get_cursor() as cursor:
with pytest.raises(alice.NoOneToGiveFinalGiftTo):
alice.distribute_balance_as_final_gift(cursor)
assert Participant.from_username('bob').balance == D('0.00')
assert Participant.from_username('carl').balance == D('0.00')
assert Participant.from_username('alice').balance == D('10.00')

@pytest.mark.xfail(reason='https://github.com/gratipay/gratipay.com/pull/3467')
def test_dbafg_gives_all_to_claimed(self):
alice = self.make_participant('alice', claimed_time='now', balance=D('10.00'))
bob = self.make_participant('bob', claimed_time='now')
carl = self.make_participant('carl')
alice.set_tip_to(bob, D('3.00'))
alice.set_tip_to(carl, D('2.00'))
with self.db.get_cursor() as cursor:
alice.distribute_balance_as_final_gift(cursor)
assert Participant.from_username('bob').balance == D('10.00')
assert Participant.from_username('carl').balance == D('0.00')
assert Participant.from_username('alice').balance == D('0.00')

@pytest.mark.xfail(reason='https://github.com/gratipay/gratipay.com/pull/3467')
def test_dbafg_skips_zero_tips(self):
alice = self.make_participant('alice', claimed_time='now', balance=D('10.00'))
bob = self.make_participant('bob', claimed_time='now')
carl = self.make_participant('carl', claimed_time='now')
alice.set_tip_to(bob, D('0.00'))
alice.set_tip_to(carl, D('2.00'))
with self.db.get_cursor() as cursor:
alice.distribute_balance_as_final_gift(cursor)
assert self.db.one("SELECT count(*) FROM tips WHERE tippee='bob'") == 1
assert Participant.from_username('bob').balance == D('0.00')
assert Participant.from_username('carl').balance == D('10.00')
assert Participant.from_username('alice').balance == D('0.00')

@pytest.mark.xfail(reason='https://github.com/gratipay/gratipay.com/pull/3467')
def test_dbafg_favors_highest_tippee_in_rounding_errors(self):
alice = self.make_participant('alice', claimed_time='now', balance=D('10.00'))
bob = self.make_participant('bob', claimed_time='now')
carl = self.make_participant('carl', claimed_time='now')
alice.set_tip_to(bob, D('3.00'))
alice.set_tip_to(carl, D('6.00'))
with self.db.get_cursor() as cursor:
alice.distribute_balance_as_final_gift(cursor)
assert Participant.from_username('bob').balance == D('3.33')
assert Participant.from_username('carl').balance == D('6.67')
assert Participant.from_username('alice').balance == D('0.00')

@pytest.mark.xfail(reason='https://github.com/gratipay/gratipay.com/pull/3467')
def test_dbafg_with_zero_balance_is_a_noop(self):
alice = self.make_participant('alice', claimed_time='now', balance=D('0.00'))
bob = self.make_participant('bob', claimed_time='now')
carl = self.make_participant('carl', claimed_time='now')
alice.set_tip_to(bob, D('3.00'))
alice.set_tip_to(carl, D('6.00'))
with self.db.get_cursor() as cursor:
alice.distribute_balance_as_final_gift(cursor)
assert self.db.one("SELECT count(*) FROM tips") == 2
assert Participant.from_username('bob').balance == D('0.00')
assert Participant.from_username('carl').balance == D('0.00')
assert Participant.from_username('alice').balance == D('0.00')


# ctg - clear_tips_giving

def test_ctg_clears_tips_giving(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/py/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ def test_receipt_page_loads_for_braintree_cards(self):
ex_id = self.make_exchange(self.obama_route, 113, 30, self.obama)
url_receipt = '/~obama/receipts/{}.html'.format(ex_id)
actual = self.client.GET(url_receipt, auth_as='obama').body.decode('utf8')
assert self.bt_card.card_type in actual
assert self.bt_card.card_type in actual
Loading

0 comments on commit 3baef5f

Please sign in to comment.