#!/usr/bin/env python """ Test script to verify survey mapping dropdowns are working correctly. """ import os import sys import django # Setup Django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') sys.path.insert(0, '/home/ismail/projects/HH') django.setup() from django.contrib.auth import get_user_model from apps.organizations.models import Hospital from apps.surveys.models import SurveyTemplate from apps.integrations.models import SurveyTemplateMapping User = get_user_model() def test_hospital_dropdown(): """Test that hospitals are available for dropdown.""" print("\n" + "="*70) print("TEST 1: Hospital Dropdown Data") print("="*70) # Check if hospitals exist hospitals = Hospital.objects.all() print(f"\nTotal hospitals in database: {hospitals.count()}") if hospitals.exists(): print("\nAvailable hospitals:") for hospital in hospitals: print(f" - ID: {hospital.id}, Name: {hospital.name}, Status: {hospital.status}") else: print("\n⚠️ WARNING: No hospitals found!") print(" You may need to create hospitals first.") return hospitals def test_survey_templates(): """Test that survey templates exist and have correct options.""" print("\n" + "="*70) print("TEST 2: Survey Templates") print("="*70) # Check if survey templates exist templates = SurveyTemplate.objects.filter(is_active=True) print(f"\nTotal active survey templates: {templates.count()}") if templates.exists(): print("\nAvailable survey templates:") for template in templates: question_count = template.questions.count() print(f"\n - ID: {template.id}") print(f" Name: {template.name} ({template.name_ar})") print(f" Type: {template.survey_type}") print(f" Hospital: {template.hospital.name if template.hospital else 'None'}") print(f" Questions: {question_count}") # Check questions have satisfaction options if question_count > 0: first_question = template.questions.first() if first_question.choices_json: print(f" Satisfaction Options: {len(first_question.choices_json)} options") for choice in first_question.choices_json: print(f" - {choice['value']}: {choice['label']} / {choice['label_ar']}") else: print("\n⚠️ WARNING: No active survey templates found!") print(" Run: python manage.py update_survey_satisfaction_questions") return templates def test_user_hospital_access(): """Test user hospital access logic.""" print("\n" + "="*70) print("TEST 3: User Hospital Access") print("="*70) # Get users users = User.objects.filter(is_active=True) print(f"\nTotal active users: {users.count()}") for user in users[:5]: # Check first 5 users print(f"\n User: {user.email}") print(f" Role: {', '.join(user.get_role_names()) if user.get_role_names() else 'No roles'}") print(f" Is Superuser: {user.is_superuser}") if user.is_superuser: # Superuser should see all hospitals accessible_hospitals = Hospital.objects.all() print(f" Access: All hospitals ({accessible_hospitals.count()})") elif user.hospital: # Regular user should see only their hospital accessible_hospitals = Hospital.objects.filter(id=user.hospital.id) print(f" Access: {user.hospital.name} (assigned hospital)") else: # User without hospital accessible_hospitals = [] print(f" Access: None (no hospital assigned)") return users def test_survey_mapping_access(): """Test survey mapping access patterns.""" print("\n" + "="*70) print("TEST 4: Survey Template Mappings") print("="*70) mappings = SurveyTemplateMapping.objects.select_related('hospital', 'survey_template') print(f"\nTotal mappings: {mappings.count()}") if mappings.exists(): print("\nExisting mappings:") for mapping in mappings: print(f"\n - ID: {mapping.id}") print(f" Hospital: {mapping.hospital.name}") print(f" Patient Type: {mapping.patient_type_name} (Code: {mapping.patient_type_code})") print(f" Survey Template: {mapping.survey_template.name}") print(f" Priority: {mapping.priority}") print(f" Active: {mapping.is_active}") else: print("\nℹ️ No mappings configured yet.") print(" This is expected if you're setting up the system for the first time.") return mappings def test_dropdown_population(): """Simulate dropdown population like the UI does.""" print("\n" + "="*70) print("TEST 5: Dropdown Population Simulation") print("="*70) # Get a hospital for testing hospital = Hospital.objects.first() if not hospital: print("\n⚠️ Cannot test - no hospitals available") return print(f"\nTesting with hospital: {hospital.name} (ID: {hospital.id})") # Simulate: Get survey templates for this hospital survey_templates = SurveyTemplate.objects.filter( hospital=hospital, is_active=True ) print(f"\nSurvey templates for dropdown:") if survey_templates.exists(): for template in survey_templates: print(f" ") else: print(f" ⚠️ No templates found for hospital '{hospital.name}'") print(f" This means the Survey Template dropdown will be empty!") print(f" Make sure survey templates exist for this hospital.") def main(): """Run all tests.""" print("\n" + "="*70) print("SURVEY MAPPING DROPDOWN VERIFICATION") print("="*70) # Run tests hospitals = test_hospital_dropdown() templates = test_survey_templates() users = test_user_hospital_access() mappings = test_survey_mapping_access() test_dropdown_population() # Summary print("\n" + "="*70) print("SUMMARY") print("="*70) issues = [] if not hospitals.exists(): issues.append("❌ No hospitals in database") else: print(f"✓ Hospitals: {hospitals.count()} available") if not templates.exists(): issues.append("❌ No active survey templates") issues.append(" Run: python manage.py update_survey_satisfaction_questions") else: print(f"✓ Survey Templates: {templates.count()} available") if not mappings.exists(): issues.append("ℹ️ No survey template mappings configured") else: print(f"✓ Mappings: {mappings.count()} configured") print("\n" + "="*70) if issues: print("ISSUES FOUND:") for issue in issues: print(issue) print("\nPlease address the issues above.") else: print("✓ All dropdowns should be working correctly!") print("="*70) if __name__ == '__main__': main()