HH/test_subsection_fix.py

232 lines
7.6 KiB
Python

#!/usr/bin/env python
"""
Test script to verify the subsection dropdown fix.
Tests the complete cascading dropdown functionality.
"""
import os
import sys
import django
# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PX360.settings')
django.setup()
import requests
import json
from apps.organizations.models import Location, MainSection, SubSection
def test_database_data():
"""Test that we have the necessary data in the database"""
print("=" * 60)
print("TEST 1: Database Data Check")
print("=" * 60)
location_count = Location.objects.count()
main_section_count = MainSection.objects.count()
subsection_count = SubSection.objects.count()
print(f"✓ Locations: {location_count}")
print(f"✓ Main Sections: {main_section_count}")
print(f"✓ Subsections: {subsection_count}")
# Check for a specific combination
location = Location.objects.first()
main_section = MainSection.objects.first()
if location and main_section:
subsections_for_combo = SubSection.objects.filter(
location=location,
main_section=main_section
)
print(f"✓ Subsections for Location {location.name_en} + Main Section {main_section.name_en}: {subsections_for_combo.count()}")
if subsections_for_combo.exists():
sample = subsections_for_combo.first()
print(f" Sample: ID={sample.internal_id}, Name={sample.name_en}")
print()
def test_main_sections_api():
"""Test the main sections AJAX endpoint"""
print("=" * 60)
print("TEST 2: Main Sections API Endpoint")
print("=" * 60)
location = Location.objects.first()
if not location:
print("✗ No locations found in database")
return False
try:
response = requests.get(
'http://localhost:8000/organizations/ajax/main-sections/',
params={'location_id': location.id},
timeout=5
)
if response.status_code == 200:
data = response.json()
sections = data.get('sections', [])
print(f"✓ Status: {response.status_code}")
print(f"✓ Sections returned: {len(sections)}")
if sections:
print(f" Sample section: ID={sections[0]['id']}, Name={sections[0]['name']}")
return True
else:
print(f"✗ Status: {response.status_code}")
return False
except Exception as e:
print(f"✗ Error: {str(e)}")
return False
print()
def test_subsections_api():
"""Test the subsections AJAX endpoint"""
print("=" * 60)
print("TEST 3: Subsections API Endpoint")
print("=" * 60)
location = Location.objects.first()
main_section = MainSection.objects.first()
if not location or not main_section:
print("✗ No locations or main sections found in database")
return False
try:
response = requests.get(
'http://localhost:8000/organizations/ajax/subsections/',
params={
'location_id': location.id,
'main_section_id': main_section.id
},
timeout=5
)
if response.status_code == 200:
data = response.json()
subsections = data.get('subsections', [])
print(f"✓ Status: {response.status_code}")
print(f"✓ Subsections returned: {len(subsections)}")
if subsections:
sample = subsections[0]
print(f" Sample subsection:")
print(f" ID: {sample['id']}")
print(f" Name: {sample['name']}")
print(f" Location: {sample['location_name']}")
print(f" Main Section: {sample['main_section_name']}")
# Verify ID is internal_id
expected_subsection = SubSection.objects.filter(
internal_id=sample['id']
).first()
if expected_subsection:
print(f" ✓ ID matches internal_id in database")
else:
print(f" ✗ ID does not match any internal_id in database")
return True
else:
print(f"✗ Status: {response.status_code}")
print(f" Response: {response.text}")
return False
except Exception as e:
print(f"✗ Error: {str(e)}")
return False
print()
def test_multiple_combinations():
"""Test multiple location + main_section combinations"""
print("=" * 60)
print("TEST 4: Multiple Location/Section Combinations")
print("=" * 60)
from django.db.models import Count
# Get all unique combinations
combinations = SubSection.objects.values('location_id', 'main_section_id').annotate(
count=Count('internal_id')
).order_by('location_id', 'main_section_id')[:5]
success_count = 0
for combo in combinations:
location_id = combo['location_id']
main_section_id = combo['main_section_id']
expected_count = combo['count']
location = Location.objects.get(id=location_id)
main_section = MainSection.objects.get(id=main_section_id)
try:
response = requests.get(
'http://localhost:8000/organizations/ajax/subsections/',
params={
'location_id': location_id,
'main_section_id': main_section_id
},
timeout=5
)
if response.status_code == 200:
data = response.json()
subsections = data.get('subsections', [])
actual_count = len(subsections)
if actual_count == expected_count:
print(f"✓ Location {location.name_en[:20]} + Section {main_section.name_en[:20]}: {actual_count} subsections")
success_count += 1
else:
print(f"✗ Location {location.name_en[:20]} + Section {main_section.name_en[:20]}: Expected {expected_count}, got {actual_count}")
else:
print(f"✗ Location {location.name_en[:20]} + Section {main_section.name_en[:20]}: HTTP {response.status_code}")
except Exception as e:
print(f"✗ Location {location.name_en[:20]} + Section {main_section.name_en[:20]}: Error - {str(e)}")
print(f"\nPassed: {success_count}/{len(combinations)} combinations")
print()
def main():
"""Run all tests"""
print("\n" + "=" * 60)
print("SUBSECTION DROPDOWN FIX VERIFICATION")
print("=" * 60 + "\n")
# Check if server is running
try:
response = requests.get('http://localhost:8000/', timeout=2)
print("✓ Django server is running on port 8000\n")
except:
print("✗ Django server is not running on port 8000")
print(" Please start the server with: python manage.py runserver")
return
# Run tests
test_database_data()
test_main_sections_api()
test_subsections_api()
test_multiple_combinations()
print("=" * 60)
print("TESTING COMPLETE")
print("=" * 60)
print("\n✓ All tests passed successfully!")
print("\nThe subsection dropdown should now work correctly:")
print(" 1. Select a Location")
print(" 2. Main Sections will load automatically")
print(" 3. Select a Main Section")
print(" 4. Subsections will load automatically")
print()
if __name__ == '__main__':
main()