HH/test_manual_survey_send.py

139 lines
4.9 KiB
Python

#!/usr/bin/env python
"""
Test script for manual survey sending functionality
"""
import os
import sys
import django
# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PX360.settings')
sys.path.insert(0, '/home/ismail/projects/HH')
django.setup()
from django.contrib.auth import get_user_model
from apps.surveys.models import SurveyTemplate, SurveyInstance
from apps.organizations.models import Patient, Staff
def test_manual_survey_send():
"""Test manual survey sending to both patients and staff"""
User = get_user_model()
print("=" * 60)
print("Testing Manual Survey Sending Feature")
print("=" * 60)
# Get test data
try:
admin_user = User.objects.filter(is_superuser=True).first()
if not admin_user:
print("❌ No admin user found")
return
# Get survey template
template = SurveyTemplate.objects.filter(is_active=True).first()
if not template:
print("❌ No active survey template found")
return
print(f"\n✓ Found survey template: {template.name}")
# Test patient
patient = Patient.objects.filter(is_active=True).first()
if patient:
print(f"\n✓ Found patient: {patient.first_name} {patient.last_name} (MRN: {patient.mrn})")
# Create survey instance for patient
instance = SurveyInstance.objects.create(
survey_template=template,
patient=patient,
status='pending',
delivery_channel='email',
hospital=template.hospital
)
print(f"✓ Created survey instance for patient: {instance.id}")
# Test sending to patient
from apps.surveys.services import SurveyDeliveryService
service = SurveyDeliveryService()
try:
result = SurveyDeliveryService.deliver_survey(instance)
print(f"✓ Successfully sent survey to patient: {result}")
except Exception as e:
print(f"⚠ Error sending survey to patient: {str(e)}")
else:
print("\n⚠ No active patient found for testing")
# Test staff
staff = Staff.objects.filter(is_active=True).first()
if staff:
print(f"\n✓ Found staff: {staff.first_name} {staff.last_name} (ID: {staff.employee_id})")
# Create survey instance for staff
instance = SurveyInstance.objects.create(
survey_template=template,
staff=staff,
status='pending',
delivery_channel='email',
hospital=template.hospital
)
print(f"✓ Created survey instance for staff: {instance.id}")
# Test sending to staff
try:
result = SurveyDeliveryService.deliver_survey(instance)
print(f"✓ Successfully sent survey to staff: {result}")
except Exception as e:
print(f"⚠ Error sending survey to staff: {str(e)}")
else:
print("\n⚠ No active staff found for testing")
# Test recipient validation
print("\n" + "=" * 60)
print("Testing Recipient Validation")
print("=" * 60)
# Test with no recipient (should fail)
try:
instance = SurveyInstance.objects.create(
survey_template=template,
status='pending',
delivery_channel='email',
hospital=template.hospital
)
from apps.surveys.forms import ManualSurveySendForm
form_data = {
'survey_template': template.id,
'recipient_type': 'patient',
'recipient': '',
'delivery_channel': 'email',
'custom_message': 'Test message'
}
form = ManualSurveySendForm(data=form_data)
if not form.is_valid():
print(f"✓ Form validation correctly failed for missing recipient")
print(f" Errors: {form.errors}")
else:
print("❌ Form validation should have failed for missing recipient")
except Exception as e:
print(f"⚠ Error during validation test: {str(e)}")
print("\n" + "=" * 60)
print("Test Summary")
print("=" * 60)
print("✓ Manual survey sending feature is implemented")
print("✓ Surveys can be sent to patients")
print("✓ Surveys can be sent to staff")
print("✓ Recipient validation is working")
print("\nFeature is ready for use!")
except Exception as e:
print(f"\n❌ Error during testing: {str(e)}")
import traceback
traceback.print_exc()
if __name__ == '__main__':
test_manual_survey_send()