""" Quick Test Script for Response Utilities Run this to verify the new response format utilities work correctly. Usage: python test_response_utilities.py """ import os import sys import django # Setup Django sys.path.insert(0, os.path.dirname(__file__)) os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'igny8_core.settings') django.setup() from rest_framework import status from igny8_core.api.response import success_response, error_response, paginated_response def test_success_response(): """Test success_response function""" print("\n" + "="*60) print("TEST 1: success_response()") print("="*60) # Test with data only response = success_response(data={"id": 1, "name": "Test"}) data = response.data print(f"✓ Status Code: {response.status_code}") print(f"✓ Success: {data.get('success')}") print(f"✓ Data: {data.get('data')}") assert data['success'] == True, "Success should be True" assert 'data' in data, "Should have data field" print("✅ Test 1.1: success_response with data - PASSED") # Test with data and message response = success_response( data={"id": 2, "name": "Test 2"}, message="Resource created successfully" ) data = response.data assert data['success'] == True, "Success should be True" assert data.get('message') == "Resource created successfully", "Should have message" print("✅ Test 1.2: success_response with data and message - PASSED") # Test with custom status code response = success_response( data={"id": 3}, status_code=status.HTTP_201_CREATED ) assert response.status_code == 201, "Status should be 201" print("✅ Test 1.3: success_response with custom status - PASSED") def test_error_response(): """Test error_response function""" print("\n" + "="*60) print("TEST 2: error_response()") print("="*60) # Test with error only response = error_response(error="Something went wrong") data = response.data print(f"✓ Status Code: {response.status_code}") print(f"✓ Success: {data.get('success')}") print(f"✓ Error: {data.get('error')}") assert data['success'] == False, "Success should be False" assert data['error'] == "Something went wrong", "Should have error message" print("✅ Test 2.1: error_response with error only - PASSED") # Test with error and errors dict response = error_response( error="Validation failed", errors={"email": ["Invalid email format"], "password": ["Too short"]} ) data = response.data assert data['success'] == False, "Success should be False" assert 'errors' in data, "Should have errors field" assert len(data['errors']) == 2, "Should have 2 field errors" print("✅ Test 2.2: error_response with errors dict - PASSED") # Test with custom status code response = error_response( error="Not found", status_code=status.HTTP_404_NOT_FOUND ) assert response.status_code == 404, "Status should be 404" print("✅ Test 2.3: error_response with custom status - PASSED") def test_paginated_response(): """Test paginated_response function""" print("\n" + "="*60) print("TEST 3: paginated_response()") print("="*60) paginated_data = { "count": 100, "next": "http://api.example.com/endpoint/?page=2", "previous": None, "results": [{"id": 1}, {"id": 2}] } response = paginated_response(paginated_data) data = response.data print(f"✓ Status Code: {response.status_code}") print(f"✓ Success: {data.get('success')}") print(f"✓ Count: {data.get('count')}") print(f"✓ Results: {len(data.get('results', []))} items") assert data['success'] == True, "Success should be True" assert data['count'] == 100, "Should have count" assert 'results' in data, "Should have results" assert len(data['results']) == 2, "Should have 2 results" print("✅ Test 3.1: paginated_response - PASSED") # Test with message response = paginated_response(paginated_data, message="Keywords retrieved") data = response.data assert data.get('message') == "Keywords retrieved", "Should have message" print("✅ Test 3.2: paginated_response with message - PASSED") def test_imports(): """Test that imports work correctly""" print("\n" + "="*60) print("TEST 4: Import Verification") print("="*60) try: from igny8_core.api import success_response, error_response, paginated_response print("✅ All imports successful") return True except ImportError as e: print(f"❌ Import failed: {e}") return False def main(): """Run all tests""" print("\n" + "="*60) print("RESPONSE UTILITIES TEST SUITE") print("="*60) print("Testing unified response format utilities") print("="*60) try: # Test imports if not test_imports(): print("\n❌ Import test failed. Exiting.") return # Test functions test_success_response() test_error_response() test_paginated_response() print("\n" + "="*60) print("✅ ALL TESTS PASSED!") print("="*60) print("\nNext steps:") print("1. Run Django unit tests: python manage.py test igny8_core.api.tests.test_response") print("2. Test /api/ping/ endpoint (if server is running)") print("3. Merge PR when ready") except AssertionError as e: print(f"\n❌ Test failed: {e}") sys.exit(1) except Exception as e: print(f"\n❌ Unexpected error: {e}") import traceback traceback.print_exc() sys.exit(1) if __name__ == '__main__': main()