diff --git a/aurweb/models/package_notification.py b/aurweb/models/package_notification.py new file mode 100644 index 0000000000000000000000000000000000000000..ab23a212a397f172c287e2b3cb5a425e99890a49 --- /dev/null +++ b/aurweb/models/package_notification.py @@ -0,0 +1,47 @@ +from sqlalchemy import Column, ForeignKey, Integer +from sqlalchemy.exc import IntegrityError +from sqlalchemy.orm import backref, relationship + +import aurweb.models.package_base +import aurweb.models.user + +from aurweb.models.declarative import Base + + +class PackageNotification(Base): + __tablename__ = "PackageNotifications" + + UserID = Column( + Integer, ForeignKey("Users.ID", ondelete="CASCADE"), + nullable=False) + User = relationship( + "User", backref=backref("package_notifications", lazy="dynamic"), + foreign_keys=[UserID]) + + PackageBaseID = Column( + Integer, ForeignKey("PackageBases.ID", ondelete="CASCADE"), + nullable=False) + PackageBase = relationship( + "PackageBase", + backref=backref("package_notifications", lazy="dynamic"), + foreign_keys=[PackageBaseID]) + + __mapper_args__ = {"primary_key": [UserID, PackageBaseID]} + + def __init__(self, + User: aurweb.models.user.User = None, + PackageBase: aurweb.models.package_base.PackageBase = None, + NotificationTS: int = None): + self.User = User + if not self.User: + raise IntegrityError( + statement="Foreign key UserID cannot be null.", + orig="PackageNotifications.UserID", + params=("NULL")) + + self.PackageBase = PackageBase + if not self.PackageBase: + raise IntegrityError( + statement="Foreign key PackageBaseID cannot be null.", + orig="PackageNotifications.PackageBaseID", + params=("NULL")) diff --git a/test/test_package_notification.py b/test/test_package_notification.py new file mode 100644 index 0000000000000000000000000000000000000000..2898a904d74781f3f5159ee10921d8c00fd5db58 --- /dev/null +++ b/test/test_package_notification.py @@ -0,0 +1,42 @@ +import pytest + +from sqlalchemy.exc import IntegrityError + +from aurweb.db import create, rollback +from aurweb.models.package_base import PackageBase +from aurweb.models.package_notification import PackageNotification +from aurweb.models.user import User +from aurweb.testing import setup_test_db + +user = pkgbase = None + + +@pytest.fixture(autouse=True) +def setup(): + global user, pkgbase + + setup_test_db("Users", "PackageBases", "PackageNotifications") + + user = create(User, Username="test", Email="test@example.org", + RealName="Test User", Passwd="testPassword") + pkgbase = create(PackageBase, Name="test-package", Maintainer=user) + + +def test_package_notification_creation(): + package_notification = create(PackageNotification, User=user, + PackageBase=pkgbase) + assert bool(package_notification) + assert package_notification.User == user + assert package_notification.PackageBase == pkgbase + + +def test_package_notification_null_user_raises_exception(): + with pytest.raises(IntegrityError): + create(PackageNotification, PackageBase=pkgbase) + rollback() + + +def test_package_notification_null_pkgbase_raises_exception(): + with pytest.raises(IntegrityError): + create(PackageNotification, User=user) + rollback()