171 lines
5.7 KiB
Python
171 lines
5.7 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
Simple test script to verify the subsection API fix.
|
|
Tests the API endpoints directly without Django model imports.
|
|
"""
|
|
import requests
|
|
import json
|
|
|
|
def test_main_sections_api():
|
|
"""Test the main sections AJAX endpoint"""
|
|
print("=" * 60)
|
|
print("TEST 1: Main Sections API Endpoint")
|
|
print("=" * 60)
|
|
|
|
try:
|
|
response = requests.get(
|
|
'http://localhost:8000/organizations/ajax/main-sections/',
|
|
params={'location_id': 48},
|
|
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}")
|
|
print(f" Response: {response.text}")
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"✗ Error: {str(e)}")
|
|
return False
|
|
|
|
def test_subsections_api():
|
|
"""Test the subsections AJAX endpoint"""
|
|
print("\n" + "=" * 60)
|
|
print("TEST 2: Subsections API Endpoint")
|
|
print("=" * 60)
|
|
|
|
try:
|
|
response = requests.get(
|
|
'http://localhost:8000/organizations/ajax/subsections/',
|
|
params={
|
|
'location_id': 48,
|
|
'main_section_id': 1
|
|
},
|
|
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 numeric (internal_id)
|
|
if isinstance(sample['id'], int):
|
|
print(f" ✓ ID is numeric (internal_id)")
|
|
else:
|
|
print(f" ✗ ID is not numeric: {type(sample['id'])}")
|
|
|
|
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
|
|
|
|
def test_multiple_combinations():
|
|
"""Test multiple location + main_section combinations"""
|
|
print("\n" + "=" * 60)
|
|
print("TEST 3: Multiple Location/Section Combinations")
|
|
print("=" * 60)
|
|
|
|
# Test a few known combinations
|
|
test_cases = [
|
|
{'location_id': 48, 'main_section_id': 1, 'name': 'Inpatient + Medical'},
|
|
{'location_id': 48, 'main_section_id': 2, 'name': 'Inpatient + Surgical'},
|
|
{'location_id': 49, 'main_section_id': 1, 'name': 'Outpatient + Medical'},
|
|
{'location_id': 49, 'main_section_id': 3, 'name': 'Outpatient + Diagnostic'},
|
|
{'location_id': 50, 'main_section_id': 1, 'name': 'Emergency + Medical'},
|
|
]
|
|
|
|
success_count = 0
|
|
|
|
for test_case in test_cases:
|
|
try:
|
|
response = requests.get(
|
|
'http://localhost:8000/organizations/ajax/subsections/',
|
|
params={
|
|
'location_id': test_case['location_id'],
|
|
'main_section_id': test_case['main_section_id']
|
|
},
|
|
timeout=5
|
|
)
|
|
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
subsections = data.get('subsections', [])
|
|
count = len(subsections)
|
|
|
|
print(f"✓ {test_case['name']}: {count} subsections")
|
|
success_count += 1
|
|
else:
|
|
print(f"✗ {test_case['name']}: HTTP {response.status_code}")
|
|
|
|
except Exception as e:
|
|
print(f"✗ {test_case['name']}: Error - {str(e)}")
|
|
|
|
print(f"\nPassed: {success_count}/{len(test_cases)} combinations")
|
|
return success_count == len(test_cases)
|
|
|
|
def main():
|
|
"""Run all tests"""
|
|
print("\n" + "=" * 60)
|
|
print("SUBSECTION API 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
|
|
test1 = test_main_sections_api()
|
|
test2 = test_subsections_api()
|
|
test3 = test_multiple_combinations()
|
|
|
|
print("\n" + "=" * 60)
|
|
print("TESTING COMPLETE")
|
|
print("=" * 60)
|
|
|
|
if test1 and test2 and test3:
|
|
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("\nThe fix was: Updated SubSectionSerializer to use internal_id instead of id")
|
|
else:
|
|
print("\n✗ Some tests failed")
|
|
|
|
print()
|
|
|
|
if __name__ == '__main__':
|
|
main() |