Commit 950659f2 authored by Kevin Morris's avatar Kevin Morris
Browse files

add PackageBase SQLAlchemy ORM model

Signed-off-by: Kevin Morris's avatarKevin Morris <>
parent c5fcf685
Pipeline #7868 passed with stage
in 2 minutes and 47 seconds
import math import math
from sqlalchemy.orm import backref, relationship
import aurweb.config import aurweb.config
import aurweb.util import aurweb.util
...@@ -51,6 +53,11 @@ def make_random_value(table: str, column: str): ...@@ -51,6 +53,11 @@ def make_random_value(table: str, column: str):
return string return string
def make_relationship(model, foreign_key, backref_):
return relationship(model, foreign_keys=[foreign_key],
backref=backref(backref_, lazy="dynamic"))
def query(model, *args, **kwargs): def query(model, *args, **kwargs):
return session.query(model).filter(*args, **kwargs) return session.query(model).filter(*args, **kwargs)
from datetime import datetime
from sqlalchemy.orm import mapper
from aurweb.db import make_relationship
from aurweb.models.user import User
from aurweb.schema import PackageBases
class PackageBase:
def __init__(self, Name: str = None, Flagger: User = None,
Maintainer: User = None, Submitter: User = None,
Packager: User = None, **kwargs):
self.Name = Name
self.Flagger = Flagger
self.Maintainer = Maintainer
self.Submitter = Submitter
self.Packager = Packager
self.NumVotes = kwargs.get("NumVotes")
self.Popularity = kwargs.get("Popularity")
self.OutOfDateTS = kwargs.get("OutOfDateTS")
self.FlaggerComment = kwargs.get("FlaggerComment", str())
self.SubmittedTS = kwargs.get("SubmittedTS",
self.ModifiedTS = kwargs.get("ModifiedTS",
mapper(PackageBase, PackageBases, properties={
"Flagger": make_relationship(User, PackageBases.c.FlaggerUID,
"Submitter": make_relationship(User, PackageBases.c.SubmitterUID,
"Maintainer": make_relationship(User, PackageBases.c.MaintainerUID,
"Packager": make_relationship(User, PackageBases.c.PackagerUID,
import pytest
from sqlalchemy.exc import IntegrityError
from aurweb.db import create, query
from aurweb.models.account_type import AccountType
from aurweb.models.package_base import PackageBase
from aurweb.testing import setup_test_db
from aurweb.testing.models import make_user
user = None
def setup():
global user
setup_test_db("Users", "PackageBases")
account_type = query(AccountType,
AccountType.AccountType == "User").first()
user = make_user(Username="test", Email="",
RealName="Test User", Passwd="testPassword",
yield user
def test_package_base():
pkgbase = create(PackageBase,
assert pkgbase in user.maintained_bases
assert not pkgbase.OutOfDateTS
assert pkgbase.SubmittedTS > 0
assert pkgbase.ModifiedTS > 0
def test_package_base_relationships():
pkgbase = create(PackageBase,
assert pkgbase in user.flagged_bases
assert pkgbase in user.maintained_bases
assert pkgbase in user.submitted_bases
assert pkgbase in user.package_bases
def test_package_base_null_name_raises_exception():
from aurweb.db import session
with pytest.raises(IntegrityError):
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment