diff --git a/backend/db/db.py b/backend/db/db.py index 207b2ff..5bfb2e6 100644 --- a/backend/db/db.py +++ b/backend/db/db.py @@ -579,23 +579,29 @@ def ensure_mongodb_indexes(client=None, db_name=None): coll.create_index(keys, **kwargs) -# Clear test collections at import time so tests start with a clean slate. +# Clear test data at import time so tests start with a clean slate. if os.environ.get('DB_ENV', 'prod') == 'test': - 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() + 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()