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

Calculate 'receiving' as the result of last payday. #1413

Closed
wants to merge 2 commits into from
Closed
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: 4 additions & 11 deletions gittip/models/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,17 +362,10 @@ def get_dollars_receiving(self):
return self.db.one("""\

SELECT sum(amount)
FROM ( SELECT DISTINCT ON (tipper)
amount
, tipper
FROM tips
JOIN participants p ON p.username = tipper
WHERE tippee=%s
AND last_bill_result = ''
AND is_suspicious IS NOT true
ORDER BY tipper
, mtime DESC
) AS foo
FROM transfers
WHERE "timestamp">=(SELECT ts_start FROM paydays ORDER BY ts_end DESC LIMIT 1)
AND "timestamp"< (SELECT ts_end FROM paydays ORDER BY ts_end DESC LIMIT 1)
AND tippee=%s

""", (self.username,), default=Decimal('0.00'))

Expand Down
19 changes: 19 additions & 0 deletions gittip/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ def make_participant(self, username, **kw):

return participant

def make_payday(self, *transfers):

with self.db.get_cursor() as cursor:
last_end = datetime.datetime(year=2012, month=1, day=1)
last_end = cursor.one("SELECT ts_end FROM paydays ORDER BY ts_end DESC LIMIT 1", default=last_end)
ts_end = last_end + datetime.timedelta(days=7)
ts_start = ts_end - datetime.timedelta(hours=1)
transfer_volume = Decimal(0)
active = set()
for i, (f, t, amount) in enumerate(transfers):
cursor.run("INSERT INTO transfers (timestamp, tipper, tippee, amount)"
"VALUES (%s, %s, %s, %s)",
(ts_start + datetime.timedelta(seconds=i), f, t, amount))
transfer_volume += Decimal(amount)
active.add(f)
active.add(t)
cursor.run("INSERT INTO paydays (ts_start, ts_end, nactive, transfer_volume) VALUES (%s, %s, %s, %s)",
(ts_start, ts_end, len(active), transfer_volume))


class GittipPaydayTest(Harness):

Expand Down
78 changes: 4 additions & 74 deletions tests/test_participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ def test_participant_can_be_instantiated(self):
actual = Participant.from_username('alice').__class__
assert actual is expected, actual

def test_bob_has_two_dollars_in_tips(self):
expected = Decimal('2.00')
actual = Participant.from_username('bob').get_dollars_receiving()
assert_equals(actual, expected)

def test_alice_gives_to_bob_now(self):
expected = Decimal('1.00')
actual = Participant.from_username('alice').get_tip_to('bob')
Expand Down Expand Up @@ -310,79 +305,14 @@ def test_stt_fails_to_tip_unknown_people(self):

# get_dollars_receiving - gdr

def test_gdr_only_sees_latest_tip(self):
alice = self.make_participant('alice', last_bill_result='')
bob = self.make_participant('bob')
alice.set_tip_to('bob', '12.00')
alice.set_tip_to('bob', '3.00')

expected = Decimal('3.00')
actual = bob.get_dollars_receiving()
assert actual == expected, actual


def test_gdr_includes_tips_from_accounts_with_a_working_card(self):
alice = self.make_participant('alice', last_bill_result='')
bob = self.make_participant('bob')
alice.set_tip_to('bob', '3.00')

expected = Decimal('3.00')
actual = bob.get_dollars_receiving()
assert actual == expected, actual

def test_gdr_ignores_tips_from_accounts_with_no_card_on_file(self):
alice = self.make_participant('alice', last_bill_result=None)
bob = self.make_participant('bob')
alice.set_tip_to('bob', '3.00')

expected = Decimal('0.00')
actual = bob.get_dollars_receiving()
assert actual == expected, actual

def test_gdr_ignores_tips_from_accounts_with_a_failing_card_on_file(self):
alice = self.make_participant('alice', last_bill_result="Fail!")
bob = self.make_participant('bob')
alice.set_tip_to('bob', '3.00')

expected = Decimal('0.00')
actual = bob.get_dollars_receiving()
assert actual == expected, actual


def test_gdr_includes_tips_from_whitelisted_accounts(self):
alice = self.make_participant( 'alice'
, last_bill_result=''
, is_suspicious=False
)
def test_gdr(self):
alice = self.make_participant('alice')
bob = self.make_participant('bob')
alice.set_tip_to('bob', '3.00')

expected = Decimal('3.00')
actual = bob.get_dollars_receiving()
assert actual == expected, actual

def test_gdr_includes_tips_from_unreviewed_accounts(self):
alice = self.make_participant( 'alice'
, last_bill_result=''
, is_suspicious=None
)
bob = self.make_participant('bob')
alice.set_tip_to('bob', '3.00')
self.make_payday(('bob', 'alice', '3.00'))

expected = Decimal('3.00')
actual = bob.get_dollars_receiving()
assert actual == expected, actual

def test_gdr_ignores_tips_from_blacklisted_accounts(self):
alice = self.make_participant( 'alice'
, last_bill_result=''
, is_suspicious=True
)
bob = self.make_participant('bob')
alice.set_tip_to('bob', '3.00')

expected = Decimal('0.00')
actual = bob.get_dollars_receiving()
actual = alice.get_dollars_receiving()
assert actual == expected, actual


Expand Down
10 changes: 9 additions & 1 deletion tests/test_public_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_anonymous_gets_receiving(self):
alice = self.make_participant('alice', last_bill_result='')
self.make_participant('bob')

alice.set_tip_to('bob', '1.00')
self.make_payday(('alice', 'bob', '1.00'))

data = json.loads(TestClient().get('/bob/public.json').body)

Expand Down Expand Up @@ -77,6 +77,7 @@ def test_authenticated_user_gets_their_tip(self):
self.make_participant('bob')

alice.set_tip_to('bob', '1.00')
self.make_payday(('alice', 'bob', '1.00'))

raw = TestClient().get('/bob/public.json', user='alice').body

Expand All @@ -95,6 +96,11 @@ def test_authenticated_user_doesnt_get_other_peoples_tips(self):
bob.set_tip_to('dana', '3.00')
carl.set_tip_to('dana', '12.00')

self.make_payday(
('alice', 'dana', '1.00'),
('bob', 'dana', '3.00'),
('carl', 'dana', '12.00'))

raw = TestClient().get('/dana/public.json', user='alice').body

data = json.loads(raw)
Expand All @@ -108,6 +114,7 @@ def test_authenticated_user_gets_zero_if_they_dont_tip(self):
self.make_participant('carl')

bob.set_tip_to('carl', '3.00')
ts_start = self.make_payday(('bob', 'carl', '3.00'))

raw = TestClient().get('/carl/public.json', user='alice').body

Expand All @@ -121,6 +128,7 @@ def test_authenticated_user_gets_self_for_self(self):
self.make_participant('bob')

alice.set_tip_to('bob', '3.00')
self.make_payday(('alice', 'bob', '3.00'))

raw = TestClient().get('/bob/public.json', user='bob').body

Expand Down