feat: implement MongoDB test database reset for clean test sessions
Chore App Build, Test, and Push Docker Images / build-and-push (push) Successful in 2m22s

This commit is contained in:
2026-07-31 17:47:06 -04:00
parent 4f9b01ce38
commit 59adf22d42
3 changed files with 41 additions and 29 deletions
+35 -26
View File
@@ -579,29 +579,38 @@ def ensure_mongodb_indexes(client=None, db_name=None):
coll.create_index(keys, **kwargs)
# Clear test data at import time so tests start with a clean slate.
if os.environ.get('DB_ENV', 'prod') == 'test':
if USE_MONGODB:
# Drop the entire MongoDB test database for a fresh start. Only do this
# for clearly test/e2e database names as a safety guard.
if _mongo_db_name and _mongo_db_name.endswith(('_test', '_e2e')):
get_mongo_client().drop_database(_mongo_db_name)
else:
child_db.truncate()
task_db.truncate()
routine_db.truncate()
routine_items_db.truncate()
routine_schedules_db.truncate()
routine_extensions_db.truncate()
reward_db.truncate()
image_db.truncate()
pending_reward_db.truncate()
pending_confirmations_db.truncate()
users_db.truncate()
tracking_events_db.truncate()
child_overrides_db.truncate()
chore_schedules_db.truncate()
task_extensions_db.truncate()
refresh_tokens_db.truncate()
push_subscriptions_db.truncate()
digest_action_tokens_db.truncate()
# Clear test collections at import time so tests start with a clean slate.
# Only TinyDB path — MongoDB cleanup is handled explicitly via reset_mongo_db().
if os.environ.get('DB_ENV', 'prod') == 'test' and not USE_MONGODB:
child_db.truncate()
task_db.truncate()
routine_db.truncate()
routine_items_db.truncate()
routine_schedules_db.truncate()
routine_extensions_db.truncate()
reward_db.truncate()
image_db.truncate()
pending_reward_db.truncate()
pending_confirmations_db.truncate()
users_db.truncate()
tracking_events_db.truncate()
child_overrides_db.truncate()
chore_schedules_db.truncate()
task_extensions_db.truncate()
refresh_tokens_db.truncate()
push_subscriptions_db.truncate()
digest_action_tokens_db.truncate()
def reset_mongo_db():
"""Drop the MongoDB test/e2e database for a totally fresh slate.
Only acts on database names ending with ``_test`` or ``_e2e`` as a
safety guard against accidentally dropping production data. Call this
once at the start of a test session or deployment seed step.
"""
if not USE_MONGODB:
return
if not _mongo_db_name or not _mongo_db_name.endswith(('_test', '_e2e')):
return
get_mongo_client().drop_database(_mongo_db_name)