252 lines
8.3 KiB
Python
252 lines
8.3 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
Test script to verify the Survey Question Builder implementation
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import django
|
|
|
|
# Setup Django
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PX360.settings')
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
django.setup()
|
|
|
|
from django.test import Client, TestCase
|
|
from django.contrib.auth import get_user_model
|
|
from apps.surveys.models import SurveyTemplate, Question
|
|
from apps.organizations.models import Hospital, Department
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
def test_survey_template_creation():
|
|
"""Test that survey templates can be created with questions"""
|
|
print("\n" + "="*60)
|
|
print("TEST: Survey Template Creation with Questions")
|
|
print("="*60)
|
|
|
|
try:
|
|
# Get or create a hospital
|
|
hospital = Hospital.objects.first()
|
|
if not hospital:
|
|
hospital = Hospital.objects.create(
|
|
name="Test Hospital",
|
|
name_ar="مستشفى تجريبي",
|
|
code="TEST001"
|
|
)
|
|
print(f"✓ Created test hospital: {hospital.name}")
|
|
else:
|
|
print(f"✓ Using existing hospital: {hospital.name}")
|
|
|
|
# Create a survey template
|
|
template = SurveyTemplate.objects.create(
|
|
name="Patient Satisfaction Survey",
|
|
name_ar="استبيان رضا المرضى",
|
|
hospital=hospital,
|
|
survey_type="post_discharge",
|
|
scoring_method="average",
|
|
negative_threshold=3.0,
|
|
is_active=True
|
|
)
|
|
print(f"✓ Created survey template: {template.name}")
|
|
|
|
# Create various types of questions
|
|
questions_data = [
|
|
{
|
|
'text': "How would you rate your overall experience?",
|
|
'text_ar': "كيف تقي تجربتك العامة؟",
|
|
'question_type': 'rating',
|
|
'order': 1,
|
|
'is_required': True
|
|
},
|
|
{
|
|
'text': "What did you like most about our service?",
|
|
'text_ar': "ما الذي أعجبك أكثر في خدمتنا؟",
|
|
'question_type': 'text',
|
|
'order': 2,
|
|
'is_required': True
|
|
},
|
|
{
|
|
'text': "How did you hear about us?",
|
|
'text_ar': "كيف سمعت عنا؟",
|
|
'question_type': 'single_choice',
|
|
'order': 3,
|
|
'is_required': False,
|
|
'choices': [
|
|
{"value": "1", "label": "Friend/Family", "label_ar": "صديق/عائلة"},
|
|
{"value": "2", "label": "Doctor Referral", "label_ar": "إحالة طبيب"},
|
|
{"value": "3", "label": "Online", "label_ar": "عبر الإنترنت"},
|
|
{"value": "4", "label": "Other", "label_ar": "أخرى"}
|
|
]
|
|
},
|
|
{
|
|
'text': "Which services did you use? (Select all that apply)",
|
|
'text_ar': "ما الخدمات التي استخدمتها؟ (اختر جميع ما ينطبق)",
|
|
'question_type': 'multiple_choice',
|
|
'order': 4,
|
|
'is_required': False,
|
|
'choices': [
|
|
{"value": "1", "label": "Emergency", "label_ar": "الطوارئ"},
|
|
{"value": "2", "label": "Outpatient", "label_ar": "العيادات الخارجية"},
|
|
{"value": "3", "label": "Inpatient", "label_ar": "تنويم"},
|
|
{"value": "4", "label": "Surgery", "label_ar": "الجراحة"}
|
|
]
|
|
}
|
|
]
|
|
|
|
# Create questions
|
|
for q_data in questions_data:
|
|
choices = q_data.pop('choices', None)
|
|
question = Question.objects.create(
|
|
template=template,
|
|
**q_data
|
|
)
|
|
if choices:
|
|
question.choices_json = choices
|
|
question.save()
|
|
print(f"✓ Created question: {question.text} ({question.question_type})")
|
|
|
|
# Verify questions were created
|
|
questions_count = template.questions.count()
|
|
print(f"\n✓ Total questions created: {questions_count}")
|
|
|
|
# Display question details
|
|
print("\n--- Question Details ---")
|
|
for q in template.questions.all().order_by('order'):
|
|
print(f"\nQ{q.order}. {q.text}")
|
|
print(f" Type: {q.question_type}")
|
|
print(f" Required: {q.is_required}")
|
|
if q.choices_json:
|
|
print(f" Choices: {len(q.choices_json)} options")
|
|
for choice in q.choices_json:
|
|
print(f" - {choice.get('label', 'N/A')} ({choice.get('label_ar', 'N/A')})")
|
|
|
|
print("\n" + "="*60)
|
|
print("✅ TEST PASSED: Survey template with questions created successfully")
|
|
print("="*60 + "\n")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"\n❌ TEST FAILED: {str(e)}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
|
|
def test_template_form_access():
|
|
"""Test that the template form page loads correctly"""
|
|
print("\n" + "="*60)
|
|
print("TEST: Template Form Page Access")
|
|
print("="*60)
|
|
|
|
try:
|
|
client = Client()
|
|
|
|
# Test the list page
|
|
response = client.get('/surveys/templates/')
|
|
if response.status_code == 200:
|
|
print("✓ Template list page loads (200)")
|
|
elif response.status_code == 302:
|
|
print("✓ Template list page redirects (302) - likely requires authentication")
|
|
else:
|
|
print(f"⚠ Template list page status: {response.status_code}")
|
|
|
|
# Test the create page
|
|
response = client.get('/surveys/templates/create/')
|
|
if response.status_code == 200:
|
|
print("✓ Template create page loads (200)")
|
|
elif response.status_code == 302:
|
|
print("✓ Template create page redirects (302) - likely requires authentication")
|
|
else:
|
|
print(f"⚠ Template create page status: {response.status_code}")
|
|
|
|
print("\n" + "="*60)
|
|
print("✅ TEST PASSED: Template form pages accessible")
|
|
print("="*60 + "\n")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"\n❌ TEST FAILED: {str(e)}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
|
|
def test_javascript_files():
|
|
"""Test that all JavaScript files exist"""
|
|
print("\n" + "="*60)
|
|
print("TEST: JavaScript Files Existence")
|
|
print("="*60)
|
|
|
|
js_files = [
|
|
'static/surveys/js/builder.js',
|
|
'static/surveys/js/choices-builder.js',
|
|
'static/surveys/js/preview.js'
|
|
]
|
|
|
|
all_exist = True
|
|
for js_file in js_files:
|
|
if os.path.exists(js_file):
|
|
size = os.path.getsize(js_file)
|
|
print(f"✓ {js_file} exists ({size} bytes)")
|
|
else:
|
|
print(f"✗ {js_file} NOT FOUND")
|
|
all_exist = False
|
|
|
|
if all_exist:
|
|
print("\n" + "="*60)
|
|
print("✅ TEST PASSED: All JavaScript files exist")
|
|
print("="*60 + "\n")
|
|
else:
|
|
print("\n" + "="*60)
|
|
print("❌ TEST FAILED: Some JavaScript files missing")
|
|
print("="*60 + "\n")
|
|
|
|
return all_exist
|
|
|
|
|
|
def run_all_tests():
|
|
"""Run all tests"""
|
|
print("\n" + "="*60)
|
|
print("SURVEY BUILDER IMPLEMENTATION TESTS")
|
|
print("="*60)
|
|
|
|
results = []
|
|
|
|
# Test 1: JavaScript files
|
|
results.append(('JavaScript Files', test_javascript_files()))
|
|
|
|
# Test 2: Template creation with questions
|
|
results.append(('Survey Template Creation', test_survey_template_creation()))
|
|
|
|
# Test 3: Form access
|
|
results.append(('Template Form Access', test_template_form_access()))
|
|
|
|
# Summary
|
|
print("\n" + "="*60)
|
|
print("TEST SUMMARY")
|
|
print("="*60)
|
|
|
|
for test_name, passed in results:
|
|
status = "✅ PASSED" if passed else "❌ FAILED"
|
|
print(f"{status}: {test_name}")
|
|
|
|
all_passed = all(result[1] for result in results)
|
|
|
|
print("\n" + "="*60)
|
|
if all_passed:
|
|
print("🎉 ALL TESTS PASSED!")
|
|
else:
|
|
print("⚠️ SOME TESTS FAILED")
|
|
print("="*60 + "\n")
|
|
|
|
return all_passed
|
|
|
|
|
|
if __name__ == '__main__':
|
|
success = run_all_tests()
|
|
sys.exit(0 if success else 1)
|