🔄 Reset Test Data - Documentation¶
Overview¶
Tools for resetting Dentists and Patients data during testing and development. This is particularly useful when testing imports to clear the database between test runs.
⚠️ IMPORTANT WARNING¶
These commands DELETE data permanently! - Always use on test/development environments only - Never use on production data - Always confirm before deletion
🎯 Quick Start¶
Option 1: Shell Script (Recommended)¶
# Reset all dentists and patients
./scripts/reset_test_data.sh
# Reset only dentists
./scripts/reset_test_data.sh --dentists-only
# Reset only patients
./scripts/reset_test_data.sh --patients-only
# Reset data for a specific practice
./scripts/reset_test_data.sh --practice "Practice Name"
# Skip confirmation prompt
./scripts/reset_test_data.sh --yes
Option 2: Django Management Command¶
# Reset all dentists and patients
python manage.py reset_test_data
# Reset only dentists
python manage.py reset_test_data --dentists-only
# Reset only patients
python manage.py reset_test_data --patients-only
# Reset data for a specific practice
python manage.py reset_test_data --practice "Practice Name"
# Skip confirmation prompt
python manage.py reset_test_data --yes
📋 Features¶
1. Smart Deletion Order¶
The command respects foreign key constraints and deletes in the correct order: 1. Dentist Contracts (if resetting dentists) 2. Dentists (if resetting dentists and no longer have contracts) 3. Patients (if resetting patients)
2. Safety Features¶
- Confirmation Prompt: Requires typing "yes" to proceed (unless
--yesflag is used) - Count Display: Shows exactly how many records will be deleted before proceeding
- Transaction Safety: All deletions happen in a single database transaction
- Detailed Summary: Shows results after completion
3. Flexible Filtering¶
By Entity Type¶
--dentists-only: Only delete dentists and contracts--patients-only: Only delete patients
By Practice¶
--practice "Name": Only delete data for a specific practice- Partial name matching (case-insensitive)
💡 Common Use Cases¶
Testing Import Flow¶
# 1. Test dentist import
./scripts/reset_test_data.sh --dentists-only --yes
python manage.py shell -c "from apps.imports.tasks import process_dentist_import_task; ..."
# 2. Test patient import
./scripts/reset_test_data.sh --patients-only --yes
python manage.py shell -c "from apps.imports.tasks import process_patient_import_task; ..."
# 3. Clean slate for both
./scripts/reset_test_data.sh --yes
Testing Multiple Practices¶
# Reset data for Practice A only
./scripts/reset_test_data.sh --practice "Practice A"
# Import test data for Practice A
# ... run import ...
# Reset data for Practice B only
./scripts/reset_test_data.sh --practice "Practice B"
# Import test data for Practice B
# ... run import ...
Development Workflow¶
📊 Example Output¶
============================================================
RESET TEST DATA
============================================================
Scope: ALL PRACTICES
Records to be DELETED:
- 15 Dentist Contracts
- 12 Dentists
- 453 Patients
TOTAL: 480 records will be deleted
============================================================
Are you sure you want to DELETE this data? (yes/no): yes
Deleting data...
============================================================
DELETION COMPLETE
============================================================
✓ Deleted 15 Dentist Contracts
✓ Deleted 12 Dentists
✓ Deleted 453 Patients
TOTAL: 480 records deleted
============================================================
🔧 Technical Details¶
Files Created¶
- Management Command: apps/dentists/management/commands/reset_test_data.py
- Django management command
- Handles all deletion logic
-
Provides filtering and confirmation
-
Shell Script: scripts/reset_test_data.sh
- Convenient wrapper
- Auto-activates virtual environment
- Colorized output
- Help documentation
Deletion Logic¶
All Practices (default)¶
# Delete in order:
1. DentistContract.objects.all().delete()
2. Dentist.objects.all().delete()
3. Patient.objects.all().delete()
Specific Practice¶
# Delete in order:
1. DentistContract.objects.filter(practice=practice).delete()
2. Dentist.objects.filter(contracts__isnull=True).delete() # Only orphans
3. Patient.objects.filter(practice=practice).delete()
Why Delete Contracts First?¶
DentistContracthas a foreign key toDentist- Must delete contracts before dentists to avoid foreign key violations
- When filtering by practice, only orphaned dentists (no remaining contracts) are deleted
🚫 What This Does NOT Delete¶
The following data is preserved: - ✓ Practices - ✓ Users - ✓ Import history (Import and ImportRow records) - ✓ Any other app data (treatments, appointments, etc.)
🎨 Integration with Import Testing¶
Typical Testing Workflow¶
# 1. Reset the database
./scripts/reset_test_data.sh --yes
# 2. Upload and test import via UI
# - Go to http://localhost:8000/imports/dentists/
# - Upload test file
# - Review results
# 3. Check import results
python manage.py shell
>>> from apps.dentists.models import Dentist
>>> Dentist.objects.count()
50
# 4. Reset and try again with different file
./scripts/reset_test_data.sh --yes
# 5. Repeat
Automated Testing¶
# In your test file
from django.core.management import call_command
class TestImports(TestCase):
def setUp(self):
# Reset data before each test
call_command('reset_test_data', '--yes', verbosity=0)
def test_dentist_import(self):
# Your test code here
pass
📝 Command Line Options Reference¶
| Option | Description | Example |
|---|---|---|
--dentists-only |
Only reset dentists and contracts | --dentists-only |
--patients-only |
Only reset patients | --patients-only |
--practice NAME |
Filter by practice name | --practice "My Practice" |
--yes |
Skip confirmation prompt | --yes |
-h, --help |
Show help message | --help |
🎯 Best Practices¶
-
Always use
--yesin scripts: Avoid hanging on confirmation prompts in automated workflows -
Use practice filtering for multi-tenant testing: Test different practices without affecting others
-
Combine with import logging: Keep import history for reference even after resetting data
-
Create test data fixtures: After resetting, use fixtures to quickly populate test data
-
Document your test scenarios: Keep notes on which files and configurations were used
🔍 Troubleshooting¶
"Management directory doesn't exist"¶
The management commands directory was created automatically. If you see this error, check that the directory structure exists:
"Virtual environment not activated"¶
The shell script will try to activate it automatically. If it fails, activate manually:
"Practice not found"¶
Use partial matching (case-insensitive):
# Instead of exact match
python manage.py reset_test_data --practice "practice"
# Will match "Practice A", "My Practice", etc.
Foreign Key Errors¶
If you encounter foreign key errors, it means there are dependencies not handled by the script. Please report this as it should handle all FK relationships.
🚀 Future Enhancements¶
Potential improvements for the future:
- Backup before reset: Automatically create a backup before deletion
- Selective field reset: Reset only specific fields instead of deleting entire records
- Date range filtering: Reset data imported within a date range
- Import-specific reset: Reset data from a specific import ID
- Dry run mode: Show what would be deleted without actually deleting
✅ Summary¶
You now have two convenient ways to reset test data:
- Quick Shell Script:
./scripts/reset_test_data.sh - Django Command:
python manage.py reset_test_data
Both support: - ✓ Filtering by entity type (dentists/patients) - ✓ Filtering by practice - ✓ Confirmation prompts - ✓ Detailed statistics - ✓ Safe deletion with transactions
Perfect for testing import flows! 🎉