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
|
@ -4,7 +4,7 @@ from sqlalchemy.exc import IntegrityError
|
|||
|
||||
import aurweb.config
|
||||
|
||||
from aurweb.db import create, query
|
||||
from aurweb import db
|
||||
from aurweb.models.account_type import AccountType
|
||||
from aurweb.models.package_base import PackageBase
|
||||
from aurweb.models.user import User
|
||||
|
@ -19,17 +19,19 @@ def setup():
|
|||
|
||||
setup_test_db("Users", "PackageBases")
|
||||
|
||||
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)
|
||||
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():
|
||||
pkgbase = create(PackageBase,
|
||||
Name="beautiful-package",
|
||||
Maintainer=user)
|
||||
with db.begin():
|
||||
pkgbase = db.create(PackageBase,
|
||||
Name="beautiful-package",
|
||||
Maintainer=user)
|
||||
assert pkgbase in user.maintained_bases
|
||||
|
||||
assert not pkgbase.OutOfDateTS
|
||||
|
@ -38,7 +40,8 @@ def test_package_base():
|
|||
|
||||
# Set Popularity to a string, then get it by attribute to
|
||||
# exercise the string -> float conversion path.
|
||||
pkgbase.Popularity = "0.0"
|
||||
with db.begin():
|
||||
pkgbase.Popularity = "0.0"
|
||||
assert pkgbase.Popularity == 0.0
|
||||
|
||||
|
||||
|
@ -47,27 +50,28 @@ def test_package_base_ci():
|
|||
if aurweb.config.get("database", "backend") == "sqlite":
|
||||
return None # SQLite doesn't seem handle this.
|
||||
|
||||
from aurweb.db import session
|
||||
|
||||
pkgbase = create(PackageBase,
|
||||
Name="beautiful-package",
|
||||
Maintainer=user)
|
||||
with db.begin():
|
||||
pkgbase = db.create(PackageBase,
|
||||
Name="beautiful-package",
|
||||
Maintainer=user)
|
||||
assert bool(pkgbase.ID)
|
||||
|
||||
with pytest.raises(IntegrityError):
|
||||
create(PackageBase,
|
||||
Name="Beautiful-Package",
|
||||
Maintainer=user)
|
||||
session.rollback()
|
||||
with db.begin():
|
||||
db.create(PackageBase,
|
||||
Name="Beautiful-Package",
|
||||
Maintainer=user)
|
||||
db.rollback()
|
||||
|
||||
|
||||
def test_package_base_relationships():
|
||||
pkgbase = create(PackageBase,
|
||||
Name="beautiful-package",
|
||||
Flagger=user,
|
||||
Maintainer=user,
|
||||
Submitter=user,
|
||||
Packager=user)
|
||||
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
|
||||
|
@ -75,8 +79,7 @@ def test_package_base_relationships():
|
|||
|
||||
|
||||
def test_package_base_null_name_raises_exception():
|
||||
from aurweb.db import session
|
||||
|
||||
with pytest.raises(IntegrityError):
|
||||
create(PackageBase)
|
||||
session.rollback()
|
||||
with db.begin():
|
||||
db.create(PackageBase)
|
||||
db.rollback()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue