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
+2
View File
@@ -166,6 +166,8 @@ jobs:
echo "Waiting for backend to be ready..." echo "Waiting for backend to be ready..."
sleep 10 sleep 10
echo "Resetting MongoDB test database..."
docker-compose -f docker-compose.test.yml exec -T chores-test-app-backend python -c "from db.db import reset_mongo_db; reset_mongo_db()"
echo "Seeding test user..." echo "Seeding test user..."
docker-compose -f docker-compose.test.yml exec -T chores-test-app-backend python scripts/seed_test_user.py docker-compose -f docker-compose.test.yml exec -T chores-test-app-backend python scripts/seed_test_user.py
echo "Creating admin user..." echo "Creating admin user..."
+17 -8
View File
@@ -579,14 +579,9 @@ def ensure_mongodb_indexes(client=None, db_name=None):
coll.create_index(keys, **kwargs) coll.create_index(keys, **kwargs)
# Clear test data at import time so tests start with a clean slate. # Clear test collections at import time so tests start with a clean slate.
if os.environ.get('DB_ENV', 'prod') == 'test': # Only TinyDB path — MongoDB cleanup is handled explicitly via reset_mongo_db().
if USE_MONGODB: if os.environ.get('DB_ENV', 'prod') == 'test' and not 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() child_db.truncate()
task_db.truncate() task_db.truncate()
routine_db.truncate() routine_db.truncate()
@@ -605,3 +600,17 @@ if os.environ.get('DB_ENV', 'prod') == 'test':
refresh_tokens_db.truncate() refresh_tokens_db.truncate()
push_subscriptions_db.truncate() push_subscriptions_db.truncate()
digest_action_tokens_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)
+4 -3
View File
@@ -24,7 +24,8 @@ def set_test_db_env():
os.environ['MONGO_URI'] = 'mongomock' os.environ['MONGO_URI'] = 'mongomock'
os.environ['SECRET_KEY'] = TEST_SECRET_KEY os.environ['SECRET_KEY'] = TEST_SECRET_KEY
os.environ['REFRESH_TOKEN_EXPIRY_DAYS'] = str(TEST_REFRESH_TOKEN_EXPIRY_DAYS) os.environ['REFRESH_TOKEN_EXPIRY_DAYS'] = str(TEST_REFRESH_TOKEN_EXPIRY_DAYS)
# Ensure indexes are created once for the test session. This is safe to # Drop the MongoDB test database once at the start of the session so every
# call repeatedly because MongoDB treats index creation as idempotent. # test begins from a clean slate. Indexes are recreated afterward.
from db.db import ensure_mongodb_indexes from db.db import reset_mongo_db, ensure_mongodb_indexes
reset_mongo_db()
ensure_mongodb_indexes() ensure_mongodb_indexes()