66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
Test the API endpoint that loads categories for the public form
|
|
"""
|
|
|
|
import os
|
|
import django
|
|
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PX360.settings')
|
|
django.setup()
|
|
|
|
from django.test import RequestFactory
|
|
from apps.complaints.ui_views import api_load_categories
|
|
from apps.organizations.models import Hospital
|
|
|
|
print("=" * 60)
|
|
print("TESTING API_LOAD_CATEGORIES ENDPOINT")
|
|
print("=" * 60)
|
|
|
|
# Create a request factory
|
|
factory = RequestFactory()
|
|
|
|
# Test 1: Load without hospital (system-wide)
|
|
print("\nTest 1: Loading categories without hospital (system-wide)")
|
|
request = factory.get('/complaints/api/categories/', {'hospital_id': ''})
|
|
try:
|
|
response = api_load_categories(request)
|
|
print(f" Status Code: {response.status_code}")
|
|
import json
|
|
data = json.loads(response.content)
|
|
print(f" Success: {data.get('success')}")
|
|
categories = data.get('categories', [])
|
|
print(f" Total categories: {len(categories)}")
|
|
level_1 = [c for c in categories if c.get('level') == 1]
|
|
print(f" Level 1 (domains): {len(level_1)}")
|
|
if level_1:
|
|
print(f" Sample domains:")
|
|
for cat in level_1[:3]:
|
|
print(f" - {cat.get('name_en')}: id={cat.get('id')}, level={cat.get('level')}")
|
|
except Exception as e:
|
|
print(f" ERROR: {e}")
|
|
|
|
# Test 2: Load with hospital
|
|
print("\nTest 2: Loading categories with hospital")
|
|
hospital = Hospital.objects.filter(status='active').first()
|
|
if hospital:
|
|
print(f" Using hospital: {hospital.name}")
|
|
request = factory.get('/complaints/api/categories/', {'hospital_id': str(hospital.id)})
|
|
try:
|
|
response = api_load_categories(request)
|
|
print(f" Status Code: {response.status_code}")
|
|
import json
|
|
data = json.loads(response.content)
|
|
print(f" Success: {data.get('success')}")
|
|
categories = data.get('categories', [])
|
|
print(f" Total categories: {len(categories)}")
|
|
level_1 = [c for c in categories if c.get('level') == 1]
|
|
print(f" Level 1 (domains): {len(level_1)}")
|
|
except Exception as e:
|
|
print(f" ERROR: {e}")
|
|
else:
|
|
print(" No active hospital found to test")
|
|
|
|
print("\n" + "=" * 60)
|
|
print("API TEST COMPLETE")
|
|
print("=" * 60) |