#!/usr/bin/env python """ Test script to verify cascading dropdown functionality in complaint form. """ 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.test import RequestFactory from apps.complaints.forms import ComplaintForm from apps.accounts.models import User def test_complaint_form_without_patient_dropdown(): """Test that ComplaintForm doesn't have patient dropdown""" print("Testing ComplaintForm fields...") # Create a test user user = User.objects.filter(is_staff=True).first() if not user: print("ERROR: No staff user found. Creating test user...") user = User.objects.create_user( username='testuser', email='test@example.com', password='testpass123', is_staff=True ) # Create form instance form = ComplaintForm(user=user) # Check that patient field does not exist if 'patient' in form.fields: print("❌ FAIL: 'patient' field still exists in form") return False else: print("✓ PASS: 'patient' field successfully removed") # Check that patient_name field exists if 'patient_name' not in form.fields: print("❌ FAIL: 'patient_name' field is missing") return False else: print("✓ PASS: 'patient_name' field exists") # Check that relation_to_patient field exists if 'relation_to_patient' not in form.fields: print("❌ FAIL: 'relation_to_patient' field is missing") return False else: print("✓ PASS: 'relation_to_patient' field exists") # Check that national_id field exists if 'national_id' not in form.fields: print("❌ FAIL: 'national_id' field is missing") return False else: print("✓ PASS: 'national_id' field exists") # Check that location hierarchy fields exist location_fields = ['location', 'main_section', 'subsection'] for field in location_fields: if field not in form.fields: print(f"❌ FAIL: '{field}' field is missing") return False else: print(f"✓ PASS: '{field}' field exists") return True def test_ajax_endpoints(): """Test that AJAX endpoints are accessible""" from django.test import Client from apps.organizations.models import Location, MainSection, SubSection print("\nTesting AJAX endpoints...") client = Client() # Test main sections endpoint response = client.get('/organizations/ajax/main-sections/') print(f"Main sections endpoint status: {response.status_code}") if response.status_code == 200: print("✓ PASS: Main sections endpoint accessible") else: print("❌ FAIL: Main sections endpoint not accessible") return False # Test subsections endpoint response = client.get('/organizations/ajax/subsections/') print(f"Subsections endpoint status: {response.status_code}") if response.status_code == 200: print("✓ PASS: Subsections endpoint accessible") else: print("❌ FAIL: Subsections endpoint not accessible") return False # Test with location filter location = Location.objects.first() if location: response = client.get(f'/organizations/ajax/main-sections/?location_id={location.id}') print(f"Main sections with location filter status: {response.status_code}") if response.status_code == 200: data = response.json() print(f"✓ PASS: Returns {len(data.get('sections', []))} sections") else: print("❌ FAIL: Location filter not working") return False return True if __name__ == '__main__': print("=" * 60) print("CASCADING DROPDOWN TESTS") print("=" * 60) # Test form form_ok = test_complaint_form_without_patient_dropdown() # Test AJAX endpoints ajax_ok = test_ajax_endpoints() print("\n" + "=" * 60) if form_ok and ajax_ok: print("✓ ALL TESTS PASSED") sys.exit(0) else: print("❌ SOME TESTS FAILED") sys.exit(1)