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

Commit

Permalink
Try to trigger an IntegrityError
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Apr 24, 2017
1 parent 28dffdd commit 3d8fb44
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
12 changes: 9 additions & 3 deletions gratipay/models/package/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,16 @@ def ensure_team(self, cursor, owner):
, owner=owner
, _cursor=cursor
)
cursor.run('UPDATE packages SET team_id=%s WHERE id=%s', (team.id, self.id))
self.set_attributes(team_id=team.id)
cursor.run('INSERT INTO teams_to_packages (team_id, package_id) '
'VALUES (%s, %s)', (team.id, self.id))
from aspen import log
log('inserted into teams')
log(cursor.all('SELECT * FROM teams_to_packages'))
return team


def _load_team(self, cursor):
return cursor.one('SELECT t.*::teams FROM teams t WHERE t.id=%s', (self.team_id,))
return cursor.one( 'SELECT t.*::teams FROM teams t WHERE t.id='
'(SELECT team_id FROM teams_to_packages tp WHERE tp.package_id=%s)'
, (self.id,)
)
1 change: 1 addition & 0 deletions gratipay/models/participant/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def finish_email_verification(self, email, nonce):
try:
self.finish_package_claims(cursor, nonce, *packages)
self.save_email_address(cursor, email)
cursor.connection.commit()
except IntegrityError:
return VERIFICATION_STYMIED
return VERIFICATION_SUCCEEDED
Expand Down
5 changes: 4 additions & 1 deletion gratipay/models/team/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ def package(self):


def _load_package(self, cursor):
return cursor.one('SELECT p.*::packages FROM packages p WHERE p.team_id=%s', (self.id,))
return cursor.one( 'SELECT p.*::packages FROM packages p WHERE p.id='
'(SELECT package_id FROM teams_to_packages tp WHERE tp.team_id=%s)'
, (self.id,)
)
5 changes: 4 additions & 1 deletion sql/branch.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ BEGIN;
, UNIQUE(nonce, package_id)
);

ALTER TABLE packages ADD COLUMN team_id bigint UNIQUE REFERENCES teams(id) ON DELETE RESTRICT;
CREATE TABLE teams_to_packages
( team_id bigint UNIQUE REFERENCES teams(id) ON DELETE RESTRICT
, package_id bigint UNIQUE REFERENCES packages(id) ON DELETE RESTRICT
);

END;
11 changes: 10 additions & 1 deletion tests/py/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ def test_team_can_only_be_linked_from_one_package(self):
bar = self.make_package(name='bar')
raises( IntegrityError
, self.db.run
, 'UPDATE packages SET team_id=%s WHERE id=%s'
, 'INSERT INTO teams_to_packages (team_id, package_id) VALUES (%s, %s)'
, (team.id, bar.id)
)

def test_package_can_only_be_linked_from_one_team(self):
alice, package, team = self.test_can_link_to_a_new_team()
bar = self.make_team(name='Bar')
raises( IntegrityError
, self.db.run
, 'INSERT INTO teams_to_packages (team_id, package_id) VALUES (%s, %s)'
, (bar.id, package.id)
)

0 comments on commit 3d8fb44

Please sign in to comment.