From 794868b20f700461fd8978be9c162c708db43c57 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Mon, 31 May 2021 22:41:06 -0700 Subject: [PATCH] aurweb.testing.setup_test_db: Expunge objects This is needed to avoid redundant objects in SQLAlchemy's IdentityMap, since we pass a direct .execute to delete the tables passed in. Additionally, remove our engine.connect() call in favor of relying on the already-established Session. Signed-off-by: Kevin Morris --- aurweb/testing/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/aurweb/testing/__init__.py b/aurweb/testing/__init__.py index 0a807b40..02c21a4c 100644 --- a/aurweb/testing/__init__.py +++ b/aurweb/testing/__init__.py @@ -21,10 +21,12 @@ def setup_test_db(*args): test_tables = ["Users", "Sessions"]; setup_test_db(*test_tables) """ - engine = aurweb.db.get_engine() - conn = engine.connect() + # Make sure that we've grabbed the engine before using the session. + aurweb.db.get_engine() tables = list(args) for table in tables: - conn.execute(f"DELETE FROM {table}") - conn.close() + aurweb.db.session.execute(f"DELETE FROM {table}") + + # Expunge all objects from SQLAlchemy's IdentityMap. + aurweb.db.session.expunge_all()