From 9474ae8c067c09021412dd371f274f45e5442f37 Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Sat, 6 Apr 2013 22:55:46 -0400 Subject: [PATCH] Test suite is passing! #287 --- gittip/models/participant.py | 24 ++++++++++++++- gittip/models/tip.py | 4 +-- gittip/participant.py | 2 +- tests/test_goal_json.py | 2 +- tests/test_participant.py | 26 ++++++++-------- tests/test_record_an_exchange.py | 8 ++--- tests/test_user.py | 18 +++++------ ...ipant_id_json.py => test_username_json.py} | 30 +++++++++---------- www/%username/history/record-an-exchange | 4 +-- www/%username/toggle-is-suspicious.json | 4 +-- www/%username/username.json | 6 ++-- 11 files changed, 75 insertions(+), 53 deletions(-) rename tests/{test_participant_id_json.py => test_username_json.py} (54%) diff --git a/gittip/models/participant.py b/gittip/models/participant.py index 6bdbcd4e6f..82589144c5 100644 --- a/gittip/models/participant.py +++ b/gittip/models/participant.py @@ -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 == '' ) diff --git a/gittip/models/tip.py b/gittip/models/tip.py index 6af5805614..194203c27c 100644 --- a/gittip/models/tip.py +++ b/gittip/models/tip.py @@ -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) diff --git a/gittip/participant.py b/gittip/participant.py index 8a109b3534..1bc9d4afb3 100644 --- a/gittip/participant.py +++ b/gittip/participant.py @@ -109,7 +109,7 @@ def get_details(self): SELECT * FROM participants - WHERE id = %s + WHERE username = %s """ return gittip.db.fetchone(SELECT, (self.username,)) diff --git a/tests/test_goal_json.py b/tests/test_goal_json.py index dc2becd420..7d732eddf3 100644 --- a/tests/test_goal_json.py +++ b/tests/test_goal_json.py @@ -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() diff --git a/tests/test_participant.py b/tests/test_participant.py index ceaa9b5219..c75caf580f 100644 --- a/tests/test_participant.py +++ b/tests/test_participant.py @@ -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): @@ -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() @@ -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') diff --git a/tests/test_record_an_exchange.py b/tests/test_record_an_exchange.py index fd01de46c6..7ef4a5bc1b 100644 --- a/tests/test_record_an_exchange.py +++ b/tests/test_record_an_exchange.py @@ -76,11 +76,11 @@ 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 @@ -88,7 +88,7 @@ def test_success_records_exchange(self): 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 @@ -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 diff --git a/tests/test_user.py b/tests/test_user.py index 37f1be3ec6..e804f22adc 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -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): @@ -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 diff --git a/tests/test_participant_id_json.py b/tests/test_username_json.py similarity index 54% rename from tests/test_participant_id_json.py rename to tests/test_username_json.py index 553be3b104..f768929ca2 100644 --- a/tests/test_participant_id_json.py +++ b/tests/test_username_json.py @@ -4,17 +4,17 @@ 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 @@ -22,31 +22,31 @@ def change_id(self, new_id, user='alice'): 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 diff --git a/www/%username/history/record-an-exchange b/www/%username/history/record-an-exchange index 1f4eb1be6c..ff9aa6496a 100644 --- a/www/%username/history/record-an-exchange +++ b/www/%username/history/record-an-exchange @@ -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) diff --git a/www/%username/toggle-is-suspicious.json b/www/%username/toggle-is-suspicious.json index dbd0f65dfc..1f3ea5c519 100644 --- a/www/%username/toggle-is-suspicious.json +++ b/www/%username/toggle-is-suspicious.json @@ -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'],)) @@ -22,7 +22,7 @@ else: UPDATE participants SET is_suspicious = %s - WHERE id=%s + WHERE username=%s RETURNING is_suspicious """, (to == 'true', path['username'],)) diff --git a/www/%username/username.json b/www/%username/username.json index a71e659c08..f647a08134 100644 --- a/www/%username/username.json +++ b/www/%username/username.json @@ -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)