mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
[FastAPI] Refactor db modifications
For SQLAlchemy to automatically understand updates from the external world, it must use an `autocommit=True` in its session. This change breaks how we were using commit previously, as `autocommit=True` causes SQLAlchemy to commit when a SessionTransaction context hits __exit__. So, a refactoring was required of our tests: All usage of any `db.{create,delete}` must be called **within** a SessionTransaction context, created via new `db.begin()`. From this point forward, we're going to require: ``` with db.begin(): db.create(...) db.delete(...) db.session.delete(object) ``` With this, we now get external DB modifications automatically without reloading or restarting the FastAPI server, which we absolutely need for production. Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
b52059d437
commit
a5943bf2ad
37 changed files with 998 additions and 902 deletions
|
@ -2,7 +2,8 @@ import pytest
|
|||
|
||||
from sqlalchemy.exc import IntegrityError, OperationalError
|
||||
|
||||
from aurweb.db import commit, create, query
|
||||
from aurweb import db
|
||||
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
|
||||
|
@ -22,25 +23,28 @@ def setup():
|
|||
|
||||
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="test-package",
|
||||
Maintainer=user)
|
||||
package = create(Package,
|
||||
PackageBase=pkgbase,
|
||||
Name=pkgbase.Name,
|
||||
Description="Test description.",
|
||||
URL="https://test.package")
|
||||
with db.begin():
|
||||
user = create(User, Username="test", Email="test@example.org",
|
||||
RealName="Test User", Passwd="testPassword",
|
||||
AccountType=account_type)
|
||||
pkgbase = create(PackageBase,
|
||||
Name="test-package",
|
||||
Maintainer=user)
|
||||
package = create(Package,
|
||||
PackageBase=pkgbase,
|
||||
Name=pkgbase.Name,
|
||||
Description="Test description.",
|
||||
URL="https://test.package")
|
||||
|
||||
|
||||
def test_package_relation():
|
||||
conflicts = query(RelationType, RelationType.Name == "conflicts").first()
|
||||
pkgrel = create(PackageRelation, Package=package,
|
||||
RelationType=conflicts,
|
||||
RelName="test-relation")
|
||||
|
||||
with db.begin():
|
||||
pkgrel = create(PackageRelation, Package=package,
|
||||
RelationType=conflicts,
|
||||
RelName="test-relation")
|
||||
assert pkgrel.RelName == "test-relation"
|
||||
assert pkgrel.Package == package
|
||||
assert pkgrel.RelationType == conflicts
|
||||
|
@ -48,8 +52,8 @@ def test_package_relation():
|
|||
assert pkgrel in package.package_relations
|
||||
|
||||
provides = query(RelationType, RelationType.Name == "provides").first()
|
||||
pkgrel.RelationType = provides
|
||||
commit()
|
||||
with db.begin():
|
||||
pkgrel.RelationType = provides
|
||||
assert pkgrel.RelName == "test-relation"
|
||||
assert pkgrel.Package == package
|
||||
assert pkgrel.RelationType == provides
|
||||
|
@ -57,8 +61,8 @@ def test_package_relation():
|
|||
assert pkgrel in package.package_relations
|
||||
|
||||
replaces = query(RelationType, RelationType.Name == "replaces").first()
|
||||
pkgrel.RelationType = replaces
|
||||
commit()
|
||||
with db.begin():
|
||||
pkgrel.RelationType = replaces
|
||||
assert pkgrel.RelName == "test-relation"
|
||||
assert pkgrel.Package == package
|
||||
assert pkgrel.RelationType == replaces
|
||||
|
@ -67,36 +71,33 @@ def test_package_relation():
|
|||
|
||||
|
||||
def test_package_relation_null_package_raises_exception():
|
||||
from aurweb.db import session
|
||||
|
||||
conflicts = query(RelationType, RelationType.Name == "conflicts").first()
|
||||
assert conflicts is not None
|
||||
|
||||
with pytest.raises(IntegrityError):
|
||||
create(PackageRelation,
|
||||
RelationType=conflicts,
|
||||
RelName="test-relation")
|
||||
session.rollback()
|
||||
with db.begin():
|
||||
create(PackageRelation,
|
||||
RelationType=conflicts,
|
||||
RelName="test-relation")
|
||||
db.rollback()
|
||||
|
||||
|
||||
def test_package_relation_null_relation_type_raises_exception():
|
||||
from aurweb.db import session
|
||||
|
||||
with pytest.raises(IntegrityError):
|
||||
create(PackageRelation,
|
||||
Package=package,
|
||||
RelName="test-relation")
|
||||
session.rollback()
|
||||
with db.begin():
|
||||
create(PackageRelation,
|
||||
Package=package,
|
||||
RelName="test-relation")
|
||||
db.rollback()
|
||||
|
||||
|
||||
def test_package_relation_null_relname_raises_exception():
|
||||
from aurweb.db import session
|
||||
|
||||
depends = query(RelationType, RelationType.Name == "conflicts").first()
|
||||
assert depends is not None
|
||||
|
||||
with pytest.raises((OperationalError, IntegrityError)):
|
||||
create(PackageRelation,
|
||||
Package=package,
|
||||
RelationType=depends)
|
||||
session.rollback()
|
||||
with db.begin():
|
||||
create(PackageRelation,
|
||||
Package=package,
|
||||
RelationType=depends)
|
||||
db.rollback()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue