Download test_api.py from Techta/backend-code-generator-model: direct link, hf CLI and curl.
- Browser
- Download file 5.21 kB
-
https://huggingface.co/Techta/backend-code-generator-model/resolve/main/test_api.py
- Command line
-
hf download hf://Techta/backend-code-generator-model/test_api.py
-
curl -L -o test_api.py https://huggingface.co/Techta/backend-code-generator-model/resolve/main/test_api.py
5.21 kB
| #!/usr/bin/env python3 | |
| """ | |
| Test script for the Backend Code Generation API | |
| =============================================== | |
| Simple test script to verify the API is working correctly. | |
| """ | |
| import requests | |
| import json | |
| import time | |
| import os | |
| # API base URL | |
| BASE_URL = "http://localhost:8000" | |
| def test_health(): | |
| """Test the health endpoint""" | |
| print("Testing health endpoint...") | |
| response = requests.get(f"{BASE_URL}/health") | |
| print(f"Status: {response.status_code}") | |
| print(f"Response: {response.json()}") | |
| return response.status_code == 200 | |
| def test_generate_code(): | |
| """Test code generation""" | |
| print("\nTesting code generation...") | |
| # Test request | |
| request_data = { | |
| "description": "Simple REST API for task management", | |
| "framework": "fastapi", | |
| "language": "python", | |
| "requirements": [ | |
| "User authentication", | |
| "Task CRUD operations", | |
| "Task status tracking" | |
| ], | |
| "project_name": "task-manager-api" | |
| } | |
| # Submit generation request | |
| response = requests.post(f"{BASE_URL}/api/v1/generate", json=request_data) | |
| print(f"Generation request status: {response.status_code}") | |
| if response.status_code == 200: | |
| result = response.json() | |
| task_id = result["task_id"] | |
| print(f"Task ID: {task_id}") | |
| # Poll for completion | |
| print("Polling for completion...") | |
| for i in range(30): # Wait up to 5 minutes | |
| status_response = requests.get(f"{BASE_URL}/api/v1/status/{task_id}") | |
| if status_response.status_code == 200: | |
| status = status_response.json() | |
| print(f"Status: {status['status']} - {status['message']} ({status['progress']}%)") | |
| if status["status"] == "completed": | |
| print("β Generation completed!") | |
| if status.get("download_url"): | |
| print(f"Download URL: {status['download_url']}") | |
| return True | |
| elif status["status"] == "failed": | |
| print(f"β Generation failed: {status.get('error', 'Unknown error')}") | |
| return False | |
| else: | |
| print(f"Failed to get status: {status_response.status_code}") | |
| return False | |
| time.sleep(10) # Wait 10 seconds between polls | |
| print("β° Timeout waiting for completion") | |
| return False | |
| else: | |
| print(f"β Generation request failed: {response.text}") | |
| return False | |
| def test_frameworks(): | |
| """Test frameworks endpoint""" | |
| print("\nTesting frameworks endpoint...") | |
| response = requests.get(f"{BASE_URL}/api/v1/frameworks") | |
| print(f"Status: {response.status_code}") | |
| if response.status_code == 200: | |
| frameworks = response.json() | |
| print(f"Supported frameworks: {len(frameworks['frameworks'])}") | |
| for fw in frameworks['frameworks']: | |
| print(f" - {fw['name']} ({fw['language']})") | |
| return True | |
| return False | |
| def test_examples(): | |
| """Test examples endpoint""" | |
| print("\nTesting examples endpoint...") | |
| response = requests.get(f"{BASE_URL}/api/v1/examples") | |
| print(f"Status: {response.status_code}") | |
| if response.status_code == 200: | |
| examples = response.json() | |
| print(f"Available examples: {len(examples['examples'])}") | |
| for ex in examples['examples']: | |
| print(f" - {ex['name']}") | |
| return True | |
| return False | |
| def main(): | |
| """Run all tests""" | |
| print("π Testing Backend Code Generation API") | |
| print("=" * 50) | |
| # Check if API is running | |
| try: | |
| response = requests.get(f"{BASE_URL}/", timeout=5) | |
| if response.status_code != 200: | |
| print("β API is not running. Please start it with: python api_service.py") | |
| return | |
| except requests.exceptions.RequestException: | |
| print("β Cannot connect to API. Please start it with: python api_service.py") | |
| return | |
| print("β API is running") | |
| # Run tests | |
| tests = [ | |
| ("Health Check", test_health), | |
| ("Frameworks List", test_frameworks), | |
| ("Examples List", test_examples), | |
| ("Code Generation", test_generate_code), | |
| ] | |
| results = [] | |
| for test_name, test_func in tests: | |
| print(f"\n{'='*20} {test_name} {'='*20}") | |
| try: | |
| result = test_func() | |
| results.append((test_name, result)) | |
| except Exception as e: | |
| print(f"β Test failed with error: {e}") | |
| results.append((test_name, False)) | |
| # Summary | |
| print(f"\n{'='*50}") | |
| print("π Test Results Summary:") | |
| print("=" * 50) | |
| passed = 0 | |
| for test_name, result in results: | |
| status = "β PASS" if result else "β FAIL" | |
| print(f"{test_name}: {status}") | |
| if result: | |
| passed += 1 | |
| print(f"\nPassed: {passed}/{len(results)} tests") | |
| if passed == len(results): | |
| print("π All tests passed!") | |
| else: | |
| print("β οΈ Some tests failed. Check the output above for details.") | |
| if __name__ == "__main__": | |
| main() | |