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

Commit

Permalink
Test suite is passing! #287
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Apr 7, 2013
1 parent 3982930 commit 9474ae8
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 53 deletions.
24 changes: 23 additions & 1 deletion gittip/models/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,30 @@ def tips_receiving(self):

@property
def valid_tips_receiving(self):
'''
SELECT count(anon_1.amount) AS count_1
FROM ( SELECT DISTINCT ON (tips.tipper)
tips.id AS id
, tips.ctime AS ctime
, tips.mtime AS mtime
, tips.tipper AS tipper
, tips.tippee AS tippee
, tips.amount AS amount
FROM tips
JOIN participants ON tips.tipper = participants.username
WHERE %(param_1)s = tips.tippee
AND participants.is_suspicious IS NOT true
AND participants.last_bill_result = %(last_bill_result_1)s
ORDER BY tips.tipper, tips.mtime DESC
) AS anon_1
WHERE anon_1.amount > %(amount_1)s
'''
return self.tips_receiving \
.join(Participant, Tip.tipper == Participant.username) \
.join( Participant
, Tip.tipper.op('=')(Participant.username)
) \
.filter( 'participants.is_suspicious IS NOT true'
, Participant.last_bill_result == ''
)
Expand Down
4 changes: 2 additions & 2 deletions gittip/models/tip.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class Tip(db.Model):
id = Column(Integer, nullable=False, primary_key=True)
ctime = Column(TIMESTAMP(timezone=True), nullable=False)
mtime = Column(TIMESTAMP(timezone=True), nullable=False, default="now()")
tipper = Column(Text, ForeignKey("participants.id", onupdate="CASCADE",
tipper = Column(Text, ForeignKey("participants.username", onupdate="CASCADE",
ondelete="RESTRICT"), nullable=False)
tippee = Column(Text, ForeignKey("participants.id", onupdate="CASCADE",
tippee = Column(Text, ForeignKey("participants.username", onupdate="CASCADE",
ondelete="RESTRICT"), nullable=False)
amount = Column(Numeric(precision=35, scale=2), nullable=False)
2 changes: 1 addition & 1 deletion gittip/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def get_details(self):
SELECT *
FROM participants
WHERE id = %s
WHERE username = %s
"""
return gittip.db.fetchone(SELECT, (self.username,))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_goal_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def make_alice(self):

def change_goal(self, goal, goal_custom="", user="alice"):
if isinstance(user, Participant):
user = user.id
user = user.username
else:
self.make_alice()

Expand Down
26 changes: 13 additions & 13 deletions tests/test_participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ def setUp(self):
super(Harness, self).setUp()
now = utcnow()
hour_ago = now - datetime.timedelta(hours=1)
for pid in ['alice', 'bob', 'carl']:
self.make_participant(pid, claimed_time=hour_ago,
for username in ['alice', 'bob', 'carl']:
self.make_participant(username, claimed_time=hour_ago,
last_bill_result='')
deadbeef = TwitterAccount('1', {'screen_name': 'deadbeef'})
self.deadbeef_original_pid = deadbeef.participant_id
self.deadbeef_original_username = deadbeef.participant

Participant('carl').set_tip_to('bob', '1.00')
Participant('alice').set_tip_to(self.deadbeef_original_pid, '1.00')
Participant('alice').set_tip_to(self.deadbeef_original_username, '1.00')
Participant('bob').take_over(deadbeef, have_confirmation=True)

def test_participant_can_be_instantiated(self):
Expand All @@ -79,25 +79,25 @@ def test_alice_gives_to_bob_now(self):
def test_deadbeef_is_archived(self):
actual = Absorption.query\
.filter_by(absorbed_by='bob',
absorbed_was=self.deadbeef_original_pid)\
absorbed_was=self.deadbeef_original_username)\
.count()
expected = 1
assert_equals(actual, expected)

def test_alice_doesnt_gives_to_deadbeef_anymore(self):
expected = Decimal('0.00')
actual = Participant('alice').get_tip_to(self.deadbeef_original_pid)
actual = Participant('alice').get_tip_to(self.deadbeef_original_username)
assert actual == expected, actual

def test_alice_doesnt_give_to_whatever_deadbeef_was_archived_as_either(self):
expected = Decimal('0.00')
actual = Participant('alice').get_tip_to(self.deadbeef_original_pid)
actual = Participant('alice').get_tip_to(self.deadbeef_original_username)
assert actual == expected, actual

def test_attempts_to_change_archived_deadbeef_fail(self):
participant = Participant(self.deadbeef_original_pid)
participant = Participant(self.deadbeef_original_username)
with assert_raises(AssertionError):
participant.change_id('zombeef')
participant.change_username('zombeef')

def test_there_is_no_more_deadbeef(self):
actual = Participant('deadbeef').get_details()
Expand All @@ -108,10 +108,10 @@ class TestParticipant(Harness):
def setUp(self):
super(Harness, self).setUp()
now = utcnow()
for idx, pid in enumerate(['alice', 'bob', 'carl'], start=1):
self.make_participant(pid, claimed_time=now)
twitter_account = TwitterAccount(idx, {'screen_name': pid})
Participant(pid).take_over(twitter_account)
for idx, username in enumerate(['alice', 'bob', 'carl'], start=1):
self.make_participant(username, claimed_time=now)
twitter_account = TwitterAccount(idx, {'screen_name': username})
Participant(username).take_over(twitter_account)

def test_cant_take_over_claimed_participant_without_confirmation(self):
bob_twitter = StubAccount('twitter', '2')
Expand Down
8 changes: 4 additions & 4 deletions tests/test_record_an_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,19 @@ def test_success_records_exchange(self):
self.record_an_exchange('10', '0.50', 'noted')
expected = [{ "amount": Decimal('10.00')
, "fee": Decimal('0.50')
, "participant_id": "bob"
, "participant": "bob"
, "recorder": "alice"
, "note": "noted"
}]
SQL = "SELECT amount, fee, participant_id, recorder, note " \
SQL = "SELECT amount, fee, participant, recorder, note " \
"FROM exchanges"
actual = list(gittip.db.fetchall(SQL))
assert actual == expected, actual

def test_success_updates_balance(self):
self.record_an_exchange('10', '0', 'noted')
expected = [{"balance": Decimal('10.00')}]
SQL = "SELECT balance FROM participants WHERE id='bob'"
SQL = "SELECT balance FROM participants WHERE username='bob'"
actual = list(gittip.db.fetchall(SQL))
assert actual == expected, actual

Expand All @@ -97,6 +97,6 @@ def test_withdrawals_work(self):
self.make_participant('bob', claimed_time=utcnow(), balance=20)
self.record_an_exchange('-7', '0', 'noted', False)
expected = [{"balance": Decimal('13.00')}]
SQL = "SELECT balance FROM participants WHERE id='bob'"
SQL = "SELECT balance FROM participants WHERE username='bob'"
actual = list(gittip.db.fetchall(SQL))
assert actual == expected, actual
18 changes: 9 additions & 9 deletions tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ def test_anonymous_user_is_not_admin(self):

def test_known_user_is_known(self):
self.make_participant('alice')
alice = User.from_id('alice')
alice = User.from_username('alice')
assert not alice.ANON

def test_known_user_is_not_admin(self):
self.make_participant('alice')
alice = User.from_id('alice')
alice = User.from_username('alice')
assert not alice.ADMIN

def test_admin_user_is_admin(self):
self.make_participant('alice', is_admin=True)
alice = User.from_id('alice')
alice = User.from_username('alice')
assert alice.ADMIN

def test_user_from_bad_token_is_anonymous(self):
Expand All @@ -38,23 +38,23 @@ def test_user_from_None_token_is_anonymous(self):
assert user.ANON

def test_user_from_bad_id_is_anonymous(self):
user = User.from_id('deadbeef')
user = User.from_username('deadbeef')
assert user.ANON

def test_suspicious_user_from_id_is_anonymous(self):
def test_suspicious_user_from_username_is_anonymous(self):
self.make_participant('alice', is_suspicious=True)
user = User.from_id('alice')
user = User.from_username('alice')
assert user.ANON

def test_user_can_be_loaded_from_session_token(self):
self.make_participant('alice')
token = User.from_id('alice').session_token
actual = User.from_session_token(token).id
token = User.from_username('alice').session_token
actual = User.from_session_token(token).username
assert actual == 'alice', actual

def test_signed_out_user_is_anonymous(self):
self.make_participant('alice')
alice = User.from_id('alice')
alice = User.from_username('alice')
assert not alice.ANON
alice = alice.sign_out()
assert alice.ANON
Expand Down
30 changes: 15 additions & 15 deletions tests/test_participant_id_json.py → tests/test_username_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,49 @@
from gittip.testing.client import TestClient


class TestParticipantIdJson(Harness):
class Tests(Harness):

def change_id(self, new_id, user='alice'):
def change_username(self, new_username, user='alice'):
self.make_participant('alice')

client = TestClient()
response = client.get('/')
csrf_token = response.request.context['csrf_token']

response = client.post( "/alice/participant_id.json"
, { 'participant_id': new_id
response = client.post( "/alice/username.json"
, { 'username': new_username
, 'csrf_token': csrf_token
}
, user=user
)
return response


def test_participant_can_change_their_id(self):
response = self.change_id("bob")
actual = json.loads(response.body)['participant_id']
def test_participant_can_change_their_username(self):
response = self.change_username("bob")
actual = json.loads(response.body)['username']
assert actual == "bob", actual

def test_anonymous_gets_404(self):
response = self.change_id("bob", user=None)
response = self.change_username("bob", user=None)
assert response.code == 404, response.code

def test_invalid_is_400(self):
response = self.change_id("\u2034")
response = self.change_username("\u2034")
assert response.code == 400, response.code

def test_restricted_id_is_400(self):
response = self.change_id("assets")
def test_restricted_username_is_400(self):
response = self.change_username("assets")
assert response.code == 400, response.code

def test_unavailable_is_409(self):
self.make_participant("bob")
response = self.change_id("bob")
response = self.change_username("bob")
assert response.code == 409, response.code

def test_too_long_is_413(self):
self.make_participant("bob")
response = self.change_id("I am way too long, and you know it, "
"and I know it, and the American people "
"know it.")
response = self.change_username("I am way too long, and you know it, "
"and I know it, and the American "
"people know it.")
assert response.code == 413, response.code
4 changes: 2 additions & 2 deletions www/%username/history/record-an-exchange
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ if not note:

with gittip.db.get_transaction() as txn:
SQL = "INSERT INTO exchanges " \
"(amount, fee, username, recorder, note) " \
"(amount, fee, participant, recorder, note) " \
"VALUES (%s, %s, %s, %s, %s)"
txn.execute(SQL, (amount, fee, participant.username, user.username, note))

SQL = "UPDATE participants SET balance = balance + %s WHERE id=%s"
SQL = "UPDATE participants SET balance = balance + %s WHERE username=%s"
txn.execute(SQL, (amount, participant.username))

request.redirect('/%s/history/' % participant.username)
Expand Down
4 changes: 2 additions & 2 deletions www/%username/toggle-is-suspicious.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if to is None:

UPDATE participants
SET is_suspicious = (is_suspicious IS NULL) OR (is_suspicious IS false)
WHERE id=%s
WHERE username=%s
RETURNING is_suspicious

""", (path['username'],))
Expand All @@ -22,7 +22,7 @@ else:

UPDATE participants
SET is_suspicious = %s
WHERE id=%s
WHERE username=%s
RETURNING is_suspicious

""", (to == 'true', path['username'],))
Expand Down
6 changes: 3 additions & 3 deletions www/%username/username.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ try:
user.change_username(new_username)
response.body = {"username": new_username}
log_dammit("user with username %s has become username %s" % (old_username, new_username))
except (Participant.IdContainsInvalidCharacters, Participant.IdIsRestricted):
except (Participant.UsernameContainsInvalidCharacters, Participant.UsernameIsRestricted):
raise Response(400)
except Participant.IdAlreadyTaken:
except Participant.UsernameAlreadyTaken:
raise Response(409)
except Participant.IdTooLong:
except Participant.UsernameTooLong:
raise Response(413)

0 comments on commit 9474ae8

Please sign in to comment.