139 lines
5.3 KiB
Python
139 lines
5.3 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
Test script for the new location hierarchy dropdown functionality
|
|
in the 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 Client
|
|
from django.urls import reverse
|
|
from apps.organizations.models import Location, MainSection, SubSection
|
|
import json
|
|
|
|
def test_location_hierarchy():
|
|
"""Test the location hierarchy API endpoints"""
|
|
print("=" * 80)
|
|
print("TESTING LOCATION HIERARCHY DROPDOWN FUNCTIONALITY")
|
|
print("=" * 80)
|
|
|
|
client = Client()
|
|
|
|
# Test 1: Load all locations
|
|
print("\n[TEST 1] Loading all locations...")
|
|
url = reverse('complaints:api_locations')
|
|
response = client.get(url)
|
|
print(f" Status: {response.status_code}")
|
|
print(f" Content-Type: {response.get('Content-Type')}")
|
|
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
print(f" ✓ Success! Retrieved {data.get('count', 0)} locations")
|
|
if data.get('locations'):
|
|
print(f" Sample locations:")
|
|
for loc in data['locations'][:3]:
|
|
print(f" - ID {loc['id']}: {loc['name']}")
|
|
first_location_id = data['locations'][0]['id']
|
|
else:
|
|
print(f" ✗ Failed: {response.content}")
|
|
return False
|
|
|
|
# Test 2: Load sections for a location
|
|
print(f"\n[TEST 2] Loading sections for location ID {first_location_id}...")
|
|
url = reverse('complaints:api_sections', kwargs={'location_id': first_location_id})
|
|
response = client.get(url)
|
|
print(f" Status: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
print(f" ✓ Success! Retrieved {data.get('count', 0)} sections")
|
|
if data.get('sections'):
|
|
print(f" Sample sections:")
|
|
for sec in data['sections'][:3]:
|
|
print(f" - ID {sec['id']}: {sec['name']}")
|
|
first_section_id = data['sections'][0]['id']
|
|
else:
|
|
print(f" ✗ Failed: {response.content}")
|
|
return False
|
|
|
|
# Test 3: Load subsections for location and section
|
|
print(f"\n[TEST 3] Loading subsections for location {first_location_id} and section {first_section_id}...")
|
|
url = reverse('complaints:api_subsections', kwargs={
|
|
'location_id': first_location_id,
|
|
'section_id': first_section_id
|
|
})
|
|
response = client.get(url)
|
|
print(f" Status: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
print(f" ✓ Success! Retrieved {data.get('count', 0)} subsections")
|
|
if data.get('subsections'):
|
|
print(f" Sample subsections:")
|
|
for sub in data['subsections'][:3]:
|
|
print(f" - ID {sub['id']}: {sub['name']}")
|
|
else:
|
|
print(f" ✗ Failed: {response.content}")
|
|
return False
|
|
|
|
# Test 4: Test invalid location ID
|
|
print(f"\n[TEST 4] Testing invalid location ID...")
|
|
url = reverse('complaints:api_sections', kwargs={'location_id': 99999})
|
|
response = client.get(url)
|
|
print(f" Status: {response.status_code}")
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
print(f" ✓ Success! Returns empty list for invalid location")
|
|
print(f" Count: {data.get('count', 0)}")
|
|
else:
|
|
print(f" ✗ Failed: {response.content}")
|
|
|
|
# Test 5: Verify database models have data
|
|
print("\n[TEST 5] Verifying database has location data...")
|
|
location_count = Location.objects.count()
|
|
section_count = MainSection.objects.count()
|
|
subsection_count = SubSection.objects.count()
|
|
|
|
print(f" Locations in database: {location_count}")
|
|
print(f" Main Sections in database: {section_count}")
|
|
print(f" Subsections in database: {subsection_count}")
|
|
|
|
if location_count == 0:
|
|
print(" ⚠ Warning: No locations found in database!")
|
|
print(" Run: python manage.py populate_location_data")
|
|
else:
|
|
print(f" ✓ Database has location data")
|
|
|
|
# Test 6: Check subsection relationships
|
|
if subsection_count > 0:
|
|
print("\n[TEST 6] Verifying subsection relationships...")
|
|
sample_subsection = SubSection.objects.first()
|
|
print(f" Sample subsection: {sample_subsection.name}")
|
|
print(f" Location: {sample_subsection.location.name}")
|
|
print(f" Main Section: {sample_subsection.main_section.name}")
|
|
print(f" ✓ Relationships are correctly set up")
|
|
|
|
print("\n" + "=" * 80)
|
|
print("ALL TESTS COMPLETED SUCCESSFULLY!")
|
|
print("=" * 80)
|
|
print("\nSUMMARY:")
|
|
print(" ✓ Location hierarchy API endpoints are working")
|
|
print(" ✓ Dependent dropdown logic is functioning")
|
|
print(" ✓ Database relationships are correctly configured")
|
|
print("\nThe complaint form with location hierarchy dropdowns is ready to use!")
|
|
print("\nTo test the form:")
|
|
print(" 1. Start the development server: python manage.py runserver")
|
|
print(" 2. Navigate to: http://localhost:8000/complaints/public/submit/")
|
|
print(" 3. Try selecting different locations to see the dependent dropdowns")
|
|
|
|
return True
|
|
|
|
if __name__ == '__main__':
|
|
success = test_location_hierarchy()
|
|
sys.exit(0 if success else 1) |