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>
70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
""" Test our Session model. """
|
|
from datetime import datetime
|
|
from unittest import mock
|
|
|
|
import pytest
|
|
|
|
from aurweb import db
|
|
from aurweb.models.account_type import AccountType
|
|
from aurweb.models.session import Session, generate_unique_sid
|
|
from aurweb.models.user import User
|
|
|
|
account_type = user = session = None
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def setup(db_test):
|
|
global account_type, user, session
|
|
|
|
account_type = db.query(AccountType,
|
|
AccountType.AccountType == "User").first()
|
|
with db.begin():
|
|
user = db.create(User, Username="test", Email="test@example.org",
|
|
ResetKey="testReset", Passwd="testPassword",
|
|
AccountType=account_type)
|
|
|
|
with db.begin():
|
|
session = db.create(Session, UsersID=user.ID, SessionID="testSession",
|
|
LastUpdateTS=datetime.utcnow().timestamp())
|
|
|
|
|
|
def test_session():
|
|
assert session.SessionID == "testSession"
|
|
assert session.UsersID == user.ID
|
|
|
|
|
|
def test_session_cs():
|
|
""" Test case sensitivity of the database table. """
|
|
with db.begin():
|
|
user2 = db.create(User, Username="test2", Email="test2@example.org",
|
|
ResetKey="testReset2", Passwd="testPassword",
|
|
AccountType=account_type)
|
|
|
|
with db.begin():
|
|
session_cs = db.create(Session, UsersID=user2.ID,
|
|
SessionID="TESTSESSION",
|
|
LastUpdateTS=datetime.utcnow().timestamp())
|
|
assert session_cs.SessionID == "TESTSESSION"
|
|
assert session.SessionID == "testSession"
|
|
|
|
|
|
def test_session_user_association():
|
|
# Make sure that the Session user attribute is correct.
|
|
assert session.User == user
|
|
|
|
|
|
def test_generate_unique_sid():
|
|
# Mock up aurweb.models.session.generate_sid by returning
|
|
# sids[i % 2] from 0 .. n. This will swap between each sid
|
|
# between each call.
|
|
sids = ["testSession", "realSession"]
|
|
i = 0
|
|
|
|
def mock_generate_sid(length):
|
|
nonlocal i
|
|
sid = sids[i % 2]
|
|
i += 1
|
|
return sid
|
|
|
|
with mock.patch("aurweb.util.make_random_string", mock_generate_sid):
|
|
assert generate_unique_sid() == "realSession"
|