99 lines
3.5 KiB
Python
99 lines
3.5 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
Test script to verify the API endpoint returns categories correctly
|
|
"""
|
|
import os
|
|
import django
|
|
import sys
|
|
|
|
# Setup Django
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PX360.settings')
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
django.setup()
|
|
|
|
from django.test import Client
|
|
from apps.complaints.models import ComplaintCategory
|
|
|
|
def test_api_endpoint():
|
|
"""Test the API endpoint without hospital ID (system-wide)"""
|
|
print("=" * 60)
|
|
print("Testing API Endpoint: api_load_categories")
|
|
print("=" * 60)
|
|
|
|
# Create a client
|
|
client = Client()
|
|
|
|
# Test 1: Call without hospital_id (should return system-wide categories)
|
|
print("\n1. Testing without hospital_id (system-wide categories):")
|
|
print("-" * 60)
|
|
response = client.get('/complaints/public/api/load-categories/')
|
|
|
|
print(f"Status Code: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
print(f"✓ Response is JSON")
|
|
print(f"✓ Total categories: {len(data.get('categories', []))}")
|
|
|
|
categories = data.get('categories', [])
|
|
if categories:
|
|
print(f"\nSample categories:")
|
|
for cat in categories[:5]:
|
|
print(f" - ID: {cat['id']}, Level: {cat['level']}, Name: {cat['name_en']}")
|
|
|
|
# Count by level
|
|
level_counts = {}
|
|
for cat in categories:
|
|
level = cat['level']
|
|
level_counts[level] = level_counts.get(level, 0) + 1
|
|
|
|
print(f"\nCategories by level:")
|
|
for level in sorted(level_counts.keys()):
|
|
print(f" - Level {level}: {level_counts[level]} categories")
|
|
else:
|
|
print("✗ No categories returned!")
|
|
else:
|
|
print(f"✗ Failed to get response: {response.status_code}")
|
|
print(f"Response: {response.content.decode()}")
|
|
|
|
# Test 2: Call with hospital_id (should still return system-wide)
|
|
print("\n\n2. Testing with hospital_id (should still work):")
|
|
print("-" * 60)
|
|
response = client.get('/complaints/public/api/load-categories/', {'hospital_id': '12345'})
|
|
|
|
print(f"Status Code: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
print(f"✓ Response is JSON")
|
|
print(f"✓ Total categories: {len(data.get('categories', []))}")
|
|
else:
|
|
print(f"✗ Failed to get response: {response.status_code}")
|
|
print(f"Response: {response.content.decode()}")
|
|
|
|
# Test 3: Verify database has categories
|
|
print("\n\n3. Verifying database:")
|
|
print("-" * 60)
|
|
|
|
total = ComplaintCategory.objects.count()
|
|
print(f"Total categories in database: {total}")
|
|
|
|
# Count by level
|
|
for level in [1, 2, 3, 4]:
|
|
count = ComplaintCategory.objects.filter(level=level).count()
|
|
print(f" - Level {level}: {count} categories")
|
|
|
|
# Check if any are system-wide (hospitals__isnull=True)
|
|
system_wide = ComplaintCategory.objects.filter(hospitals__isnull=True, is_active=True).distinct().count()
|
|
print(f"\nSystem-wide categories: {system_wide}")
|
|
|
|
# Check if any are hospital-specific
|
|
hospital_specific = ComplaintCategory.objects.filter(hospitals__isnull=False, is_active=True).distinct().count()
|
|
print(f"Hospital-specific categories: {hospital_specific}")
|
|
|
|
print("\n" + "=" * 60)
|
|
print("Test Complete!")
|
|
print("=" * 60)
|
|
|
|
if __name__ == '__main__':
|
|
test_api_endpoint() |