From e860d828b6f9babaa5829db92951087de3774b78 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Mon, 29 Mar 2021 15:20:28 -0700 Subject: [PATCH] add aurweb.testing, a module with testing utilities Signed-off-by: Kevin Morris --- aurweb/testing.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 aurweb/testing.py diff --git a/aurweb/testing.py b/aurweb/testing.py new file mode 100644 index 00000000..7516d918 --- /dev/null +++ b/aurweb/testing.py @@ -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()