housekeep(fastapi): rework aurweb.db session API

Changes:
-------
- Add aurweb.db.get_session()
    - Returns aurweb.db's global `session` instance
    - Provides us a way to change the implementation of the session
      instance without interrupting user code.
- Use aurweb.db.get_session() in session API methods
- Add docstrings to session API methods
- Refactor aurweb.db.delete
    - Normalize aurweb.db.delete to an alias of session.delete
- Refresh instances in places we depend on their non-PK columns
  being up to date.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-11-14 15:36:06 -08:00
parent f8ba2c5342
commit 4103ab49c9
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
22 changed files with 212 additions and 138 deletions

View file

@ -20,7 +20,7 @@ def setup():
yield account_type
with begin():
delete(AccountType, AccountType.ID == account_type.ID)
delete(account_type)
def test_account_type():
@ -50,4 +50,4 @@ def test_user_account_type_relationship():
# This must be deleted here to avoid foreign key issues when
# deleting the temporary AccountType in the fixture.
with begin():
delete(User, User.ID == user.ID)
delete(user)

View file

@ -279,13 +279,13 @@ def test_connection_execute_paramstyle_unsupported():
def test_create_delete():
with db.begin():
db.create(AccountType, AccountType="test")
account_type = db.create(AccountType, AccountType="test")
record = db.query(AccountType, AccountType.AccountType == "test").first()
assert record is not None
with db.begin():
db.delete(AccountType, AccountType.AccountType == "test")
db.delete(account_type)
record = db.query(AccountType, AccountType.AccountType == "test").first()
assert record is None
@ -306,7 +306,7 @@ def test_add_commit():
# Remove the record.
with db.begin():
db.delete(AccountType, AccountType.ID == account_type.ID)
db.delete(account_type)
def test_connection_executor_mysql_paramstyle():

View file

@ -24,7 +24,7 @@ def test_dependency_type_creation():
assert bool(dependency_type.ID)
assert dependency_type.Name == "Test Type"
with begin():
delete(DependencyType, DependencyType.ID == dependency_type.ID)
delete(dependency_type)
def test_dependency_type_null_name_uses_default():
@ -32,4 +32,4 @@ def test_dependency_type_null_name_uses_default():
dependency_type = create(DependencyType)
assert dependency_type.Name == str()
with begin():
delete(DependencyType, DependencyType.ID == dependency_type.ID)
delete(dependency_type)

View file

@ -2,6 +2,7 @@ from datetime import datetime
import pytest
from fastapi import HTTPException
from fastapi.testclient import TestClient
from aurweb import asgi, db
@ -98,3 +99,8 @@ def test_query_notified(maintainer: User, package: Package):
query = db.query(Package).filter(Package.ID == package.ID).all()
query_notified = util.query_notified(query, maintainer)
assert query_notified[package.PackageBase.ID]
def test_pkgreq_by_id_not_found():
with pytest.raises(HTTPException):
util.get_pkgreq_by_id(0)

View file

@ -103,7 +103,7 @@ def test_ratelimit_db(get: mock.MagicMock, getboolean: mock.MagicMock,
# Delete the ApiRateLimit record.
with db.begin():
db.delete(ApiRateLimit)
db.delete(db.query(ApiRateLimit).first())
# Should be good to go again!
assert not check_ratelimit(request)

View file

@ -18,7 +18,7 @@ def test_relation_type_creation():
assert relation_type.Name == "test-relation"
with db.begin():
db.delete(RelationType, RelationType.ID == relation_type.ID)
db.delete(relation_type)
def test_relation_types():

View file

@ -18,7 +18,7 @@ def test_request_type_creation():
assert request_type.Name == "Test Request"
with db.begin():
db.delete(RequestType, RequestType.ID == request_type.ID)
db.delete(request_type)
def test_request_type_null_name_returns_empty_string():
@ -29,7 +29,7 @@ def test_request_type_null_name_returns_empty_string():
assert request_type.Name == str()
with db.begin():
db.delete(RequestType, RequestType.ID == request_type.ID)
db.delete(request_type)
def test_request_type_name_display():