mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
Bump sqlalchemy to version 2.0.22 There are quite some changes that happened with v2. We are currently relying on the "auto-commit" feature which was removed. For the moment we can use a wrapper class to mimic the auto-commit behavior allowing us to move to v2. Ultimately, the (db) session management needs some overhaul though. Signed-off-by: moson <moson@archlinux.org>
67 lines
2.4 KiB
Python
67 lines
2.4 KiB
Python
from sqlalchemy import text
|
|
|
|
import aurweb.db
|
|
from aurweb import models
|
|
|
|
|
|
def setup_test_db(*args):
|
|
"""This function is to be used to setup a test database before
|
|
using it. It takes a variable number of table strings, and for
|
|
each table in that set of table strings, it deletes all records.
|
|
|
|
The primary goal of this method is to configure empty tables
|
|
that tests can use from scratch. This means that tests using
|
|
this function should make sure they do not depend on external
|
|
records and keep their logic self-contained.
|
|
|
|
Generally used inside of pytest fixtures, this function
|
|
can be used anywhere, but keep in mind its functionality when
|
|
doing so.
|
|
|
|
Examples:
|
|
setup_test_db("Users", "Sessions")
|
|
|
|
test_tables = ["Users", "Sessions"];
|
|
setup_test_db(*test_tables)
|
|
"""
|
|
# Make sure that we've grabbed the engine before using the session.
|
|
aurweb.db.get_engine()
|
|
|
|
tables = list(args)
|
|
if not tables:
|
|
tables = [
|
|
models.AcceptedTerm.__tablename__,
|
|
models.ApiRateLimit.__tablename__,
|
|
models.Ban.__tablename__,
|
|
models.Group.__tablename__,
|
|
models.License.__tablename__,
|
|
models.OfficialProvider.__tablename__,
|
|
models.Package.__tablename__,
|
|
models.PackageBase.__tablename__,
|
|
models.PackageBlacklist.__tablename__,
|
|
models.PackageComaintainer.__tablename__,
|
|
models.PackageComment.__tablename__,
|
|
models.PackageDependency.__tablename__,
|
|
models.PackageGroup.__tablename__,
|
|
models.PackageKeyword.__tablename__,
|
|
models.PackageLicense.__tablename__,
|
|
models.PackageNotification.__tablename__,
|
|
models.PackageRelation.__tablename__,
|
|
models.PackageRequest.__tablename__,
|
|
models.PackageSource.__tablename__,
|
|
models.PackageVote.__tablename__,
|
|
models.Session.__tablename__,
|
|
models.SSHPubKey.__tablename__,
|
|
models.Term.__tablename__,
|
|
models.Vote.__tablename__,
|
|
models.VoteInfo.__tablename__,
|
|
models.User.__tablename__,
|
|
]
|
|
|
|
session = aurweb.db.get_session()
|
|
session.execute(text("SET FOREIGN_KEY_CHECKS = 0"))
|
|
for table in tables:
|
|
session.execute(text(f"DELETE FROM {table}"))
|
|
session.execute(text("SET FOREIGN_KEY_CHECKS = 1"))
|
|
session.expunge_all()
|
|
session.commit()
|