add PackageNotification SQLAlchemy ORM model

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-06-11 17:14:28 -07:00
parent 229df1adef
commit 5b856c7af2
2 changed files with 89 additions and 0 deletions

View file

@ -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"))

View file

@ -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()