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 <kevr@0cost.org>
parent c5fcf685
Pipeline #7868 passed with stage
in 2 minutes and 47 seconds
import math
from sqlalchemy.orm import backref, relationship
import aurweb.config
import aurweb.util
......@@ -51,6 +53,11 @@ def make_random_value(table: str, column: str):
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):
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",
datetime.utcnow().timestamp())
self.ModifiedTS = kwargs.get("ModifiedTS",
datetime.utcnow().timestamp())
mapper(PackageBase, PackageBases, properties={
"Flagger": make_relationship(User, PackageBases.c.FlaggerUID,
"flagged_bases"),
"Submitter": make_relationship(User, PackageBases.c.SubmitterUID,
"submitted_bases"),
"Maintainer": make_relationship(User, PackageBases.c.MaintainerUID,
"maintained_bases"),
"Packager": make_relationship(User, PackageBases.c.PackagerUID,
"package_bases")
})
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
@pytest.fixture(autouse=True)
def setup():
global user
setup_test_db("Users", "PackageBases")
account_type = query(AccountType,
AccountType.AccountType == "User").first()
user = make_user(Username="test", Email="test@example.org",
RealName="Test User", Passwd="testPassword",
AccountType=account_type)
yield user
def test_package_base():
pkgbase = create(PackageBase,
Name="beautiful-package",
Maintainer=user)
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,
Name="beautiful-package",
Flagger=user,
Maintainer=user,
Submitter=user,
Packager=user)
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):
create(PackageBase)
session.rollback()
Markdown is supported
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