mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
add aurweb.testing, a module with testing utilities
Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
32f2893095
commit
e860d828b6
1 changed files with 30 additions and 0 deletions
30
aurweb/testing.py
Normal file
30
aurweb/testing.py
Normal 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()
|
Loading…
Add table
Reference in a new issue