add aurweb.testing, a module with testing utilities

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-03-29 15:20:28 -07:00
parent 32f2893095
commit e860d828b6

30
aurweb/testing.py Normal file
View file

@ -0,0 +1,30 @@
from aurweb.db import get_engine
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)
"""
engine = get_engine()
conn = engine.connect()
tables = list(args)
for table in tables:
conn.execute(f"DELETE FROM {table}")
conn.close()