Spaces:
Paused
Paused
| #!/usr/bin/env python3 | |
| """ | |
| Comprehensive test script for all POST and PUT reference data endpoints. | |
| This script tests: | |
| 1. POST endpoints (create) for all reference data types | |
| 2. PUT endpoints (update) for all reference data types | |
| 3. Validates responses and error handling | |
| Reference data types tested: | |
| - States | |
| - Countries | |
| - Company Types | |
| - Lead Sources | |
| - Payment Terms | |
| - Purchase Prices | |
| - Rental Prices | |
| - Barrier Sizes | |
| - Product Applications | |
| - FOBs | |
| - Estimated Ship Dates | |
| - Estimated Freights | |
| - Status Info | |
| - Priorities | |
| Usage: | |
| python test_reference_crud.py | |
| """ | |
| import requests | |
| import json | |
| import sys | |
| from typing import Dict, Any | |
| BASE_URL = "http://localhost:8001" # Adjust port as needed | |
| # Test data for creating reference records | |
| TEST_DATA = { | |
| "states": { | |
| "create": {"description": "Test State", "customer_type_id": 1, "enabled": True}, | |
| "update": {"description": "Updated Test State", "enabled": False} | |
| }, | |
| "countries": { | |
| "create": {"description": "Test Country", "customer_type_id": 1, "enabled": True}, | |
| "update": {"description": "Updated Test Country", "enabled": False} | |
| }, | |
| "company_types": { | |
| "create": {"description": "Test Company Type", "customer_type_id": 1, "inactive": False}, | |
| "update": {"description": "Updated Test Company Type", "inactive": True} | |
| }, | |
| "lead_sources": { | |
| "create": {"source_name": "Test Lead Source", "description": "Test description", "enabled": True}, | |
| "update": {"source_name": "Updated Test Lead Source", "description": "Updated description", "enabled": False} | |
| }, | |
| "payment_terms": { | |
| "create": {"description": "Test Payment Term", "enabled": True}, | |
| "update": {"description": "Updated Test Payment Term", "enabled": False} | |
| }, | |
| "purchase_prices": { | |
| "create": {"price": "100.00", "enabled": True}, | |
| "update": {"price": "150.00", "enabled": False} | |
| }, | |
| "rental_prices": { | |
| "create": {"price": "50.00", "enabled": True}, | |
| "update": {"price": "75.00", "enabled": False} | |
| }, | |
| "barrier_sizes": { | |
| "create": {"length": 10.5, "height": 5.0, "width": 2.0, "cableunits": 1.0, "price": 100.00, "is_standard": True}, | |
| "update": {"length": 12.0, "height": 6.0, "price": 120.00, "is_standard": False} | |
| }, | |
| "product_applications": { | |
| "create": {"customer_type_id": "COM", "description": "Test Application", "enabled": True}, | |
| "update": {"customer_type_id": "RES", "description": "Updated Application", "enabled": False} | |
| }, | |
| "fobs": { | |
| "create": {"fob_description": "Test FOB"}, | |
| "update": {"fob_description": "Updated Test FOB"} | |
| }, | |
| "est_ship_dates": { | |
| "create": {"est_ship_date_description": "Test Ship Date"}, | |
| "update": {"est_ship_date_description": "Updated Test Ship Date"} | |
| }, | |
| "est_freights": { | |
| "create": {"est_freight_description": "Test Freight"}, | |
| "update": {"est_freight_description": "Updated Test Freight"} | |
| }, | |
| "status_info": { | |
| "create": {"description": "Test Status", "abrv": "TST"}, | |
| "update": {"description": "Updated Test Status", "abrv": "UTST"} | |
| }, | |
| "priorities": { | |
| "create": {"customer_type_id": 1, "description": "Test Priority"}, | |
| "update": {"customer_type_id": 2, "description": "Updated Test Priority"} | |
| } | |
| } | |
| # Endpoint mappings | |
| ENDPOINTS = { | |
| "states": {"post": "/api/v1/reference/states", "put": "/api/v1/reference/states/{id}"}, | |
| "countries": {"post": "/api/v1/reference/countries", "put": "/api/v1/reference/countries/{id}"}, | |
| "company_types": {"post": "/api/v1/reference/company-types", "put": "/api/v1/reference/company-types/{id}"}, | |
| "lead_sources": {"post": "/api/v1/reference/lead-sources", "put": "/api/v1/reference/lead-sources/{id}"}, | |
| "payment_terms": {"post": "/api/v1/reference/payment-terms", "put": "/api/v1/reference/payment-terms/{id}"}, | |
| "purchase_prices": {"post": "/api/v1/reference/purchase-prices", "put": "/api/v1/reference/purchase-prices/{id}"}, | |
| "rental_prices": {"post": "/api/v1/reference/rental-prices", "put": "/api/v1/reference/rental-prices/{id}"}, | |
| "barrier_sizes": {"post": "/api/v1/reference/barrier-sizes", "put": "/api/v1/reference/barrier-sizes/{id}"}, | |
| "product_applications": {"post": "/api/v1/reference/product-applications", "put": "/api/v1/reference/product-applications/{id}"}, | |
| "fobs": {"post": "/api/v1/reference/fobs", "put": "/api/v1/reference/fobs/{id}"}, | |
| "est_ship_dates": {"post": "/api/v1/reference/est-ship-dates", "put": "/api/v1/reference/est-ship-dates/{id}"}, | |
| "est_freights": {"post": "/api/v1/reference/est-freights", "put": "/api/v1/reference/est-freights/{id}"}, | |
| "status_info": {"post": "/api/v1/reference/status-info", "put": "/api/v1/reference/status-info/{id}"}, | |
| "priorities": {"post": "/api/v1/reference/priorities", "put": "/api/v1/reference/priorities/{id}"} | |
| } | |
| # ID field mappings for PUT requests | |
| ID_FIELDS = { | |
| "states": "state_id", | |
| "countries": "country_id", | |
| "company_types": "company_type_id", | |
| "lead_sources": "lead_generated_from_id", | |
| "payment_terms": "payment_term_id", | |
| "purchase_prices": "purchase_price_id", | |
| "rental_prices": "rental_price_id", | |
| "barrier_sizes": "barrier_size_id", | |
| "product_applications": "application_id", | |
| "fobs": "fob_id", | |
| "est_ship_dates": "est_ship_date_id", | |
| "est_freights": "est_freight_id", | |
| "status_info": "status_info_id", | |
| "priorities": "priority_id" | |
| } | |
| def test_post_endpoint(endpoint_name: str, endpoint_url: str, test_data: Dict[str, Any]) -> Dict[str, Any]: | |
| """Test POST endpoint for creating a record""" | |
| print(f"\nπ§ͺ Testing POST {endpoint_name}") | |
| print(f" URL: {endpoint_url}") | |
| print(f" Data: {json.dumps(test_data, indent=2, default=str)}") | |
| try: | |
| response = requests.post(f"{BASE_URL}{endpoint_url}", json=test_data, timeout=10) | |
| print(f" Status Code: {response.status_code}") | |
| if response.status_code == 201: | |
| result = response.json() | |
| print(f" β POST {endpoint_name} successful") | |
| print(f" Created record: {json.dumps(result, indent=2, default=str)}") | |
| return result | |
| else: | |
| print(f" β POST {endpoint_name} failed") | |
| print(f" Response: {response.text}") | |
| return None | |
| except Exception as e: | |
| print(f" β POST {endpoint_name} error: {str(e)}") | |
| return None | |
| def test_put_endpoint(endpoint_name: str, endpoint_url: str, record_id: int, test_data: Dict[str, Any]) -> bool: | |
| """Test PUT endpoint for updating a record""" | |
| url = endpoint_url.replace("{id}", str(record_id)) | |
| print(f"\nπ Testing PUT {endpoint_name}") | |
| print(f" URL: {BASE_URL}{url}") | |
| print(f" Data: {json.dumps(test_data, indent=2, default=str)}") | |
| try: | |
| response = requests.put(f"{BASE_URL}{url}", json=test_data, timeout=10) | |
| print(f" Status Code: {response.status_code}") | |
| if response.status_code == 200: | |
| result = response.json() | |
| print(f" β PUT {endpoint_name} successful") | |
| print(f" Updated record: {json.dumps(result, indent=2, default=str)}") | |
| return True | |
| else: | |
| print(f" β PUT {endpoint_name} failed") | |
| print(f" Response: {response.text}") | |
| return False | |
| except Exception as e: | |
| print(f" β PUT {endpoint_name} error: {str(e)}") | |
| return False | |
| def main(): | |
| """Main test function""" | |
| print("π Starting Comprehensive Reference Data CRUD Tests") | |
| print("=" * 60) | |
| # Skip server check for now | |
| print("β Assuming server is running on localhost:8001") | |
| results = { | |
| "post_success": 0, | |
| "post_total": 0, | |
| "put_success": 0, | |
| "put_total": 0, | |
| "created_records": {} | |
| } | |
| # Test POST endpoints | |
| print("\nπ Testing POST (Create) Endpoints") | |
| print("=" * 40) | |
| for ref_type, endpoints in ENDPOINTS.items(): | |
| results["post_total"] += 1 | |
| test_data = TEST_DATA[ref_type]["create"] | |
| result = test_post_endpoint(ref_type, endpoints["post"], test_data) | |
| if result: | |
| results["post_success"] += 1 | |
| # Store the created record for PUT testing | |
| id_field = ID_FIELDS[ref_type] | |
| record_id = result.get(id_field) | |
| if record_id: | |
| results["created_records"][ref_type] = {"id": record_id, "data": result} | |
| # Test PUT endpoints | |
| print("\nπ Testing PUT (Update) Endpoints") | |
| print("=" * 40) | |
| for ref_type, record_info in results["created_records"].items(): | |
| results["put_total"] += 1 | |
| record_id = record_info["id"] | |
| test_data = TEST_DATA[ref_type]["update"] | |
| endpoints = ENDPOINTS[ref_type] | |
| success = test_put_endpoint(ref_type, endpoints["put"], record_id, test_data) | |
| if success: | |
| results["put_success"] += 1 | |
| # Summary | |
| print("\nπ Test Results Summary") | |
| print("=" * 40) | |
| print(f"POST Tests: {results['post_success']}/{results['post_total']} passed") | |
| print(f"PUT Tests: {results['put_success']}/{results['put_total']} passed") | |
| total_success = results['post_success'] + results['put_success'] | |
| total_tests = results['post_total'] + results['put_total'] | |
| print(f"\nOverall: {total_success}/{total_tests} tests passed") | |
| if total_success == total_tests: | |
| print("π All tests passed! Reference data CRUD operations are working correctly.") | |
| return 0 | |
| else: | |
| print("β οΈ Some tests failed. Check the output above for details.") | |
| return 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) |