diff options
author | 2010-07-28 20:04:42 +0300 | |
---|---|---|
committer | 2010-07-28 20:04:42 +0300 | |
commit | 7aa3f21e98ab55a5c6cbe2d3fce1da16e0393266 (patch) | |
tree | d6500f5bffce5d3b188812295d5c931a0cda0ebe | |
parent | Added user "Favorite" packages (diff) | |
download | gsoc2010-grumpy-7aa3f21e98ab55a5c6cbe2d3fce1da16e0393266.tar.gz gsoc2010-grumpy-7aa3f21e98ab55a5c6cbe2d3fce1da16e0393266.tar.bz2 gsoc2010-grumpy-7aa3f21e98ab55a5c6cbe2d3fce1da16e0393266.zip |
Added uniqueness constraint to Favorites
-rw-r--r-- | grumpy/models.py | 3 | ||||
-rw-r--r-- | grumpy/testsuite/favorites.py | 15 |
2 files changed, 18 insertions, 0 deletions
diff --git a/grumpy/models.py b/grumpy/models.py index 52420c7..37870d2 100644 --- a/grumpy/models.py +++ b/grumpy/models.py @@ -90,6 +90,9 @@ class Favorite(db.Model): """Represents packages that user can add in their watch list.""" __tablename__ = 'favorites' + __table_args__ = ( + db.UniqueConstraint('package_id', 'user_id'), + {}) id = db.Column('id', db.Integer, primary_key=True) _package = db.Column('package_id', db.Integer, db.ForeignKey('packages.id')) diff --git a/grumpy/testsuite/favorites.py b/grumpy/testsuite/favorites.py index 6f75ca1..ab9548f 100644 --- a/grumpy/testsuite/favorites.py +++ b/grumpy/testsuite/favorites.py @@ -11,6 +11,7 @@ from . import GrumpyTestCase from grumpy.models import Ebuild, Favorite, Package, User +from sqlalchemy.exc import IntegrityError import time, unittest @@ -52,6 +53,20 @@ class FavoritesTestCase(GrumpyTestCase): assert Favorite.query.count() == 2 assert len(u.favorites) == 2 + def test_favorites_uniqueness(self): + with self.app.test_request_context(): + self.make_users() + self.make_packages() + u = User.query.filter_by(email='user1@gentoo.org').first() + p = Package.query.filter_by(cp='testpkg/sys-devel').first() + u.favorites.append(Favorite(p)) + self.db.session.commit() + u.favorites.append(Favorite(p)) + self.assertRaises(IntegrityError, self.db.session.commit) + self.db.session.rollback() + assert len(u.favorites) == 1 + + def suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(FavoritesTestCase)) |