aurweb.db: add query, create, delete helpers

Takes sqlalchemy kwargs or stanzas:

query(Model, Model.Column == value)
query(Model, and_(Model.Column == value, Model.Column != "BAD!"))

Updated tests to reflect the new utility and a comment about upcoming
function deprecation is added to get_account_type().

From here on, phase out the use of get_account_type().

+ aurweb.db: Added create utility function
+ aurweb.db: Added delete utility function

The `delete` function can be used to delete a record by search
kwargs directly.

Example:
    delete(User, User.ID == 6)

All three functions added in this commit are typically useful to
perform these operations without having to import aurweb.db.session.
Removes a bit of redundancy overall.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-01-14 21:06:41 -08:00
parent 5185df629e
commit a836892cde
5 changed files with 84 additions and 9 deletions

View file

@ -3,6 +3,7 @@ import re
import sqlite3
import tempfile
from datetime import datetime
from unittest import mock
import mysql.connector
@ -11,6 +12,7 @@ import pytest
import aurweb.config
from aurweb import db
from aurweb.models.account_type import AccountType
from aurweb.testing import setup_test_db
@ -39,7 +41,7 @@ class DBConnection:
@pytest.fixture(autouse=True)
def setup_db():
setup_test_db()
setup_test_db("Bans")
def test_sqlalchemy_sqlite_url():
@ -174,3 +176,12 @@ def test_connection_execute_paramstyle_unsupported():
"SELECT * FROM AccountTypes WHERE AccountType = ?",
["User"]
).fetchall()
def test_create_delete():
db.create(AccountType, AccountType="test")
record = db.query(AccountType, AccountType.AccountType == "test").first()
assert record is not None
db.delete(AccountType, AccountType.AccountType == "test")
record = db.query(AccountType, AccountType.AccountType == "test").first()
assert record is None