Commit 15b13326 authored by Kevin Morris's avatar Kevin Morris
Browse files

add Package SQLAlchemy ORM model



Additionally, add an optional **kwargs passing via make_relationship.
This allows us to use things like `uselist=False`, which was needed
for test/test_package.py.
Signed-off-by: Kevin Morris's avatarKevin Morris <kevr@0cost.org>
parent 621e459d
......@@ -53,9 +53,10 @@ def make_random_value(table: str, column: str):
return string
def make_relationship(model, foreign_key, backref_):
def make_relationship(model, foreign_key: str, backref_: str, **kwargs):
return relationship(model, foreign_keys=[foreign_key],
backref=backref(backref_, lazy="dynamic"))
backref=backref(backref_, lazy="dynamic"),
**kwargs)
def query(model, *args, **kwargs):
......
from sqlalchemy.orm import mapper
from aurweb.db import make_relationship
from aurweb.models.package_base import PackageBase
from aurweb.schema import Packages
class Package:
def __init__(self,
PackageBase: PackageBase = None,
Name: str = None, Version: str = None,
Description: str = None, URL: str = None):
self.PackageBase = PackageBase
self.Name = Name
self.Version = Version
self.Description = Description
self.URL = URL
mapper(Package, Packages, properties={
"PackageBase": make_relationship(PackageBase,
Packages.c.PackageBaseID,
"package", uselist=False)
})
import pytest
from sqlalchemy import and_
from sqlalchemy.exc import IntegrityError
from aurweb.db import create, query
from aurweb.models.account_type import AccountType
from aurweb.models.package import Package
from aurweb.models.package_base import PackageBase
from aurweb.models.user import User
from aurweb.testing import setup_test_db
user = pkgbase = package = None
@pytest.fixture(autouse=True)
def setup():
global user, pkgbase, package
setup_test_db("Users", "PackageBases", "Packages")
account_type = query(AccountType,
AccountType.AccountType == "User").first()
user = create(User, Username="test", Email="test@example.org",
RealName="Test User", Passwd="testPassword",
AccountType=account_type)
pkgbase = create(PackageBase,
Name="beautiful-package",
Maintainer=user)
package = create(Package,
PackageBase=pkgbase,
Name=pkgbase.Name,
Description="Test description.",
URL="https://test.package")
yield package
def test_package():
from aurweb.db import session
assert pkgbase == package.PackageBase
assert package.Name == "beautiful-package"
assert package.Description == "Test description."
assert package.Version == str() # Default version.
assert package.URL == "https://test.package"
# Update package Version.
package.Version = "1.2.3"
session.commit()
# Make sure it got updated in the database.
record = query(Package,
and_(Package.ID == package.ID,
Package.Version == "1.2.3")).first()
assert record is not None
def test_package_null_pkgbase_raises_exception():
from aurweb.db import session
with pytest.raises(IntegrityError):
create(Package,
Name="some-package",
Description="Some description.",
URL="https://some.package")
session.rollback()
def test_package_null_name_raises_exception():
from aurweb.db import session
with pytest.raises(IntegrityError):
create(Package,
PackageBase=pkgbase,
Description="Some description.",
URL="https://some.package")
session.rollback()
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