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,8 @@ import pytest
|
|||
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from aurweb.db import commit, create, query, rollback
|
||||
from aurweb import db
|
||||
from aurweb.db import create, query, rollback
|
||||
from aurweb.models.account_type import AccountType
|
||||
from aurweb.models.tu_voteinfo import TUVoteInfo
|
||||
from aurweb.models.user import User
|
||||
|
@ -21,19 +22,21 @@ def setup():
|
|||
|
||||
tu_type = query(AccountType,
|
||||
AccountType.AccountType == "Trusted User").first()
|
||||
user = create(User, Username="test", Email="test@example.org",
|
||||
RealName="Test User", Passwd="testPassword",
|
||||
AccountType=tu_type)
|
||||
with db.begin():
|
||||
user = create(User, Username="test", Email="test@example.org",
|
||||
RealName="Test User", Passwd="testPassword",
|
||||
AccountType=tu_type)
|
||||
|
||||
|
||||
def test_tu_voteinfo_creation():
|
||||
ts = int(datetime.utcnow().timestamp())
|
||||
tu_voteinfo = create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
Submitted=ts, End=ts + 5,
|
||||
Quorum=0.5,
|
||||
Submitter=user)
|
||||
with db.begin():
|
||||
tu_voteinfo = create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
Submitted=ts, End=ts + 5,
|
||||
Quorum=0.5,
|
||||
Submitter=user)
|
||||
assert bool(tu_voteinfo.ID)
|
||||
assert tu_voteinfo.Agenda == "Blah blah."
|
||||
assert tu_voteinfo.User == user.Username
|
||||
|
@ -51,32 +54,33 @@ def test_tu_voteinfo_creation():
|
|||
|
||||
def test_tu_voteinfo_is_running():
|
||||
ts = int(datetime.utcnow().timestamp())
|
||||
tu_voteinfo = create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
Submitted=ts, End=ts + 1000,
|
||||
Quorum=0.5,
|
||||
Submitter=user)
|
||||
with db.begin():
|
||||
tu_voteinfo = create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
Submitted=ts, End=ts + 1000,
|
||||
Quorum=0.5,
|
||||
Submitter=user)
|
||||
assert tu_voteinfo.is_running() is True
|
||||
|
||||
tu_voteinfo.End = ts - 5
|
||||
commit()
|
||||
with db.begin():
|
||||
tu_voteinfo.End = ts - 5
|
||||
assert tu_voteinfo.is_running() is False
|
||||
|
||||
|
||||
def test_tu_voteinfo_total_votes():
|
||||
ts = int(datetime.utcnow().timestamp())
|
||||
tu_voteinfo = create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
Submitted=ts, End=ts + 1000,
|
||||
Quorum=0.5,
|
||||
Submitter=user)
|
||||
with db.begin():
|
||||
tu_voteinfo = create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
Submitted=ts, End=ts + 1000,
|
||||
Quorum=0.5,
|
||||
Submitter=user)
|
||||
|
||||
tu_voteinfo.Yes = 1
|
||||
tu_voteinfo.No = 3
|
||||
tu_voteinfo.Abstain = 5
|
||||
commit()
|
||||
tu_voteinfo.Yes = 1
|
||||
tu_voteinfo.No = 3
|
||||
tu_voteinfo.Abstain = 5
|
||||
|
||||
# total_votes() should be the sum of Yes, No and Abstain: 1 + 3 + 5 = 9.
|
||||
assert tu_voteinfo.total_votes() == 9
|
||||
|
@ -84,61 +88,67 @@ def test_tu_voteinfo_total_votes():
|
|||
|
||||
def test_tu_voteinfo_null_submitter_raises_exception():
|
||||
with pytest.raises(IntegrityError):
|
||||
create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
Submitted=0, End=0,
|
||||
Quorum=0.50)
|
||||
with db.begin():
|
||||
create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
Submitted=0, End=0,
|
||||
Quorum=0.50)
|
||||
rollback()
|
||||
|
||||
|
||||
def test_tu_voteinfo_null_agenda_raises_exception():
|
||||
with pytest.raises(IntegrityError):
|
||||
create(TUVoteInfo,
|
||||
User=user.Username,
|
||||
Submitted=0, End=0,
|
||||
Quorum=0.50,
|
||||
Submitter=user)
|
||||
with db.begin():
|
||||
create(TUVoteInfo,
|
||||
User=user.Username,
|
||||
Submitted=0, End=0,
|
||||
Quorum=0.50,
|
||||
Submitter=user)
|
||||
rollback()
|
||||
|
||||
|
||||
def test_tu_voteinfo_null_user_raises_exception():
|
||||
with pytest.raises(IntegrityError):
|
||||
create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
Submitted=0, End=0,
|
||||
Quorum=0.50,
|
||||
Submitter=user)
|
||||
with db.begin():
|
||||
create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
Submitted=0, End=0,
|
||||
Quorum=0.50,
|
||||
Submitter=user)
|
||||
rollback()
|
||||
|
||||
|
||||
def test_tu_voteinfo_null_submitted_raises_exception():
|
||||
with pytest.raises(IntegrityError):
|
||||
create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
End=0,
|
||||
Quorum=0.50,
|
||||
Submitter=user)
|
||||
with db.begin():
|
||||
create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
End=0,
|
||||
Quorum=0.50,
|
||||
Submitter=user)
|
||||
rollback()
|
||||
|
||||
|
||||
def test_tu_voteinfo_null_end_raises_exception():
|
||||
with pytest.raises(IntegrityError):
|
||||
create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
Submitted=0,
|
||||
Quorum=0.50,
|
||||
Submitter=user)
|
||||
with db.begin():
|
||||
create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
Submitted=0,
|
||||
Quorum=0.50,
|
||||
Submitter=user)
|
||||
rollback()
|
||||
|
||||
|
||||
def test_tu_voteinfo_null_quorum_raises_exception():
|
||||
with pytest.raises(IntegrityError):
|
||||
create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
Submitted=0, End=0,
|
||||
Submitter=user)
|
||||
with db.begin():
|
||||
create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
Submitted=0, End=0,
|
||||
Submitter=user)
|
||||
rollback()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue