mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
This change utilizes pytest-xdist to perform a multiproc test run and reworks aurweb.db's code. We no longer use a global engine, session or Session, but we now use a memo of engines and sessions as they are requested, based on the PYTEST_CURRENT_TEST environment variable, which is available during testing. Additionally, this change strips several SQLite components out of the Python code-base. SQLite is still compatible with PHP and sharness tests, but not with our FastAPI implementation. More changes: ------------ - Remove use of aurweb.db.session global in other code. - Use new aurweb.db.name() dynamic db name function in env.py. - Added 'addopts' to pytest.ini which utilizes multiprocessing. - Highly recommended to leave this be or modify `-n auto` to `-n {cpu_threads}` where cpu_threads is at least 2. Signed-off-by: Kevin Morris <kevr@0cost.org>
72 lines
2.3 KiB
Python
72 lines
2.3 KiB
Python
import pytest
|
|
|
|
from sqlalchemy import and_
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
from aurweb import db
|
|
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
|
|
|
|
user = pkgbase = package = None
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def setup(db_test):
|
|
global user, pkgbase, package
|
|
|
|
account_type = db.query(AccountType,
|
|
AccountType.AccountType == "User").first()
|
|
|
|
with db.begin():
|
|
user = db.create(User, Username="test", Email="test@example.org",
|
|
RealName="Test User", Passwd="testPassword",
|
|
AccountType=account_type)
|
|
|
|
pkgbase = db.create(PackageBase,
|
|
Name="beautiful-package",
|
|
Maintainer=user)
|
|
package = db.create(Package,
|
|
PackageBase=pkgbase,
|
|
Name=pkgbase.Name,
|
|
Description="Test description.",
|
|
URL="https://test.package")
|
|
|
|
|
|
def test_package():
|
|
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.
|
|
with db.begin():
|
|
package.Version = "1.2.3"
|
|
|
|
# Make sure it got updated in the database.
|
|
record = db.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():
|
|
with pytest.raises(IntegrityError):
|
|
with db.begin():
|
|
db.create(Package,
|
|
Name="some-package",
|
|
Description="Some description.",
|
|
URL="https://some.package")
|
|
db.rollback()
|
|
|
|
|
|
def test_package_null_name_raises_exception():
|
|
with pytest.raises(IntegrityError):
|
|
with db.begin():
|
|
db.create(Package,
|
|
PackageBase=pkgbase,
|
|
Description="Some description.",
|
|
URL="https://some.package")
|
|
db.rollback()
|