aurweb/test/test_package_base.py
Kevin Morris fa43f6bc3e
change(aurweb): add parallel tests and improve aurweb.db
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>
2021-11-17 01:34:59 -08:00

82 lines
2.4 KiB
Python

import pytest
from sqlalchemy.exc import IntegrityError
import aurweb.config
from aurweb import db
from aurweb.models.account_type import AccountType
from aurweb.models.package_base import PackageBase
from aurweb.models.user import User
user = None
@pytest.fixture(autouse=True)
def setup(db_test):
global user
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)
def test_package_base():
with db.begin():
pkgbase = db.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
# Set Popularity to a string, then get it by attribute to
# exercise the string -> float conversion path.
with db.begin():
pkgbase.Popularity = "0.0"
assert pkgbase.Popularity == 0.0
def test_package_base_ci():
""" Test case insensitivity of the database table. """
if aurweb.config.get("database", "backend") == "sqlite":
return None # SQLite doesn't seem handle this.
with db.begin():
pkgbase = db.create(PackageBase,
Name="beautiful-package",
Maintainer=user)
assert bool(pkgbase.ID)
with pytest.raises(IntegrityError):
with db.begin():
db.create(PackageBase,
Name="Beautiful-Package",
Maintainer=user)
db.rollback()
def test_package_base_relationships():
with db.begin():
pkgbase = db.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():
with pytest.raises(IntegrityError):
with db.begin():
db.create(PackageBase)
db.rollback()