Spaces:
Paused
Paused
Commit Β·
f833e7a
1
Parent(s): 0f2e4e2
feat(reference): add comprehensive test script for POST and PUT reference data endpoints
Browse files- test_reference_crud.py +253 -0
test_reference_crud.py
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Comprehensive test script for all POST and PUT reference data endpoints.
|
| 4 |
+
|
| 5 |
+
This script tests:
|
| 6 |
+
1. POST endpoints (create) for all reference data types
|
| 7 |
+
2. PUT endpoints (update) for all reference data types
|
| 8 |
+
3. Validates responses and error handling
|
| 9 |
+
|
| 10 |
+
Reference data types tested:
|
| 11 |
+
- States
|
| 12 |
+
- Countries
|
| 13 |
+
- Company Types
|
| 14 |
+
- Lead Sources
|
| 15 |
+
- Payment Terms
|
| 16 |
+
- Purchase Prices
|
| 17 |
+
- Rental Prices
|
| 18 |
+
- Barrier Sizes
|
| 19 |
+
- Product Applications
|
| 20 |
+
- FOBs
|
| 21 |
+
- Estimated Ship Dates
|
| 22 |
+
- Estimated Freights
|
| 23 |
+
- Status Info
|
| 24 |
+
- Priorities
|
| 25 |
+
|
| 26 |
+
Usage:
|
| 27 |
+
python test_reference_crud.py
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
import requests
|
| 31 |
+
import json
|
| 32 |
+
import sys
|
| 33 |
+
from typing import Dict, Any
|
| 34 |
+
|
| 35 |
+
BASE_URL = "http://localhost:8001" # Adjust port as needed
|
| 36 |
+
|
| 37 |
+
# Test data for creating reference records
|
| 38 |
+
TEST_DATA = {
|
| 39 |
+
"states": {
|
| 40 |
+
"create": {"description": "Test State", "customer_type_id": 1, "enabled": True},
|
| 41 |
+
"update": {"description": "Updated Test State", "enabled": False}
|
| 42 |
+
},
|
| 43 |
+
"countries": {
|
| 44 |
+
"create": {"description": "Test Country", "customer_type_id": 1, "enabled": True},
|
| 45 |
+
"update": {"description": "Updated Test Country", "enabled": False}
|
| 46 |
+
},
|
| 47 |
+
"company_types": {
|
| 48 |
+
"create": {"description": "Test Company Type", "customer_type_id": 1, "inactive": False},
|
| 49 |
+
"update": {"description": "Updated Test Company Type", "inactive": True}
|
| 50 |
+
},
|
| 51 |
+
"lead_sources": {
|
| 52 |
+
"create": {"source_name": "Test Lead Source", "description": "Test description", "enabled": True},
|
| 53 |
+
"update": {"source_name": "Updated Test Lead Source", "description": "Updated description", "enabled": False}
|
| 54 |
+
},
|
| 55 |
+
"payment_terms": {
|
| 56 |
+
"create": {"description": "Test Payment Term", "enabled": True},
|
| 57 |
+
"update": {"description": "Updated Test Payment Term", "enabled": False}
|
| 58 |
+
},
|
| 59 |
+
"purchase_prices": {
|
| 60 |
+
"create": {"price": "100.00", "enabled": True},
|
| 61 |
+
"update": {"price": "150.00", "enabled": False}
|
| 62 |
+
},
|
| 63 |
+
"rental_prices": {
|
| 64 |
+
"create": {"price": "50.00", "enabled": True},
|
| 65 |
+
"update": {"price": "75.00", "enabled": False}
|
| 66 |
+
},
|
| 67 |
+
"barrier_sizes": {
|
| 68 |
+
"create": {"length": 10.5, "height": 5.0, "width": 2.0, "cableunits": 1.0, "price": 100.00, "is_standard": True},
|
| 69 |
+
"update": {"length": 12.0, "height": 6.0, "price": 120.00, "is_standard": False}
|
| 70 |
+
},
|
| 71 |
+
"product_applications": {
|
| 72 |
+
"create": {"customer_type_id": "COM", "description": "Test Application", "enabled": True},
|
| 73 |
+
"update": {"customer_type_id": "RES", "description": "Updated Application", "enabled": False}
|
| 74 |
+
},
|
| 75 |
+
"fobs": {
|
| 76 |
+
"create": {"fob_description": "Test FOB"},
|
| 77 |
+
"update": {"fob_description": "Updated Test FOB"}
|
| 78 |
+
},
|
| 79 |
+
"est_ship_dates": {
|
| 80 |
+
"create": {"est_ship_date_description": "Test Ship Date"},
|
| 81 |
+
"update": {"est_ship_date_description": "Updated Test Ship Date"}
|
| 82 |
+
},
|
| 83 |
+
"est_freights": {
|
| 84 |
+
"create": {"est_freight_description": "Test Freight"},
|
| 85 |
+
"update": {"est_freight_description": "Updated Test Freight"}
|
| 86 |
+
},
|
| 87 |
+
"status_info": {
|
| 88 |
+
"create": {"description": "Test Status", "abrv": "TST"},
|
| 89 |
+
"update": {"description": "Updated Test Status", "abrv": "UTST"}
|
| 90 |
+
},
|
| 91 |
+
"priorities": {
|
| 92 |
+
"create": {"customer_type_id": 1, "description": "Test Priority"},
|
| 93 |
+
"update": {"customer_type_id": 2, "description": "Updated Test Priority"}
|
| 94 |
+
}
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
# Endpoint mappings
|
| 98 |
+
ENDPOINTS = {
|
| 99 |
+
"states": {"post": "/api/v1/reference/states", "put": "/api/v1/reference/states/{id}"},
|
| 100 |
+
"countries": {"post": "/api/v1/reference/countries", "put": "/api/v1/reference/countries/{id}"},
|
| 101 |
+
"company_types": {"post": "/api/v1/reference/company-types", "put": "/api/v1/reference/company-types/{id}"},
|
| 102 |
+
"lead_sources": {"post": "/api/v1/reference/lead-sources", "put": "/api/v1/reference/lead-sources/{id}"},
|
| 103 |
+
"payment_terms": {"post": "/api/v1/reference/payment-terms", "put": "/api/v1/reference/payment-terms/{id}"},
|
| 104 |
+
"purchase_prices": {"post": "/api/v1/reference/purchase-prices", "put": "/api/v1/reference/purchase-prices/{id}"},
|
| 105 |
+
"rental_prices": {"post": "/api/v1/reference/rental-prices", "put": "/api/v1/reference/rental-prices/{id}"},
|
| 106 |
+
"barrier_sizes": {"post": "/api/v1/reference/barrier-sizes", "put": "/api/v1/reference/barrier-sizes/{id}"},
|
| 107 |
+
"product_applications": {"post": "/api/v1/reference/product-applications", "put": "/api/v1/reference/product-applications/{id}"},
|
| 108 |
+
"fobs": {"post": "/api/v1/reference/fobs", "put": "/api/v1/reference/fobs/{id}"},
|
| 109 |
+
"est_ship_dates": {"post": "/api/v1/reference/est-ship-dates", "put": "/api/v1/reference/est-ship-dates/{id}"},
|
| 110 |
+
"est_freights": {"post": "/api/v1/reference/est-freights", "put": "/api/v1/reference/est-freights/{id}"},
|
| 111 |
+
"status_info": {"post": "/api/v1/reference/status-info", "put": "/api/v1/reference/status-info/{id}"},
|
| 112 |
+
"priorities": {"post": "/api/v1/reference/priorities", "put": "/api/v1/reference/priorities/{id}"}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
# ID field mappings for PUT requests
|
| 116 |
+
ID_FIELDS = {
|
| 117 |
+
"states": "state_id",
|
| 118 |
+
"countries": "country_id",
|
| 119 |
+
"company_types": "company_type_id",
|
| 120 |
+
"lead_sources": "lead_generated_from_id",
|
| 121 |
+
"payment_terms": "payment_term_id",
|
| 122 |
+
"purchase_prices": "purchase_price_id",
|
| 123 |
+
"rental_prices": "rental_price_id",
|
| 124 |
+
"barrier_sizes": "barrier_size_id",
|
| 125 |
+
"product_applications": "application_id",
|
| 126 |
+
"fobs": "fob_id",
|
| 127 |
+
"est_ship_dates": "est_ship_date_id",
|
| 128 |
+
"est_freights": "est_freight_id",
|
| 129 |
+
"status_info": "status_info_id",
|
| 130 |
+
"priorities": "priority_id"
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
def test_post_endpoint(endpoint_name: str, endpoint_url: str, test_data: Dict[str, Any]) -> Dict[str, Any]:
|
| 134 |
+
"""Test POST endpoint for creating a record"""
|
| 135 |
+
print(f"\nπ§ͺ Testing POST {endpoint_name}")
|
| 136 |
+
print(f" URL: {endpoint_url}")
|
| 137 |
+
print(f" Data: {json.dumps(test_data, indent=2, default=str)}")
|
| 138 |
+
|
| 139 |
+
try:
|
| 140 |
+
response = requests.post(f"{BASE_URL}{endpoint_url}", json=test_data, timeout=10)
|
| 141 |
+
|
| 142 |
+
print(f" Status Code: {response.status_code}")
|
| 143 |
+
|
| 144 |
+
if response.status_code == 201:
|
| 145 |
+
result = response.json()
|
| 146 |
+
print(f" β
POST {endpoint_name} successful")
|
| 147 |
+
print(f" Created record: {json.dumps(result, indent=2, default=str)}")
|
| 148 |
+
return result
|
| 149 |
+
else:
|
| 150 |
+
print(f" β POST {endpoint_name} failed")
|
| 151 |
+
print(f" Response: {response.text}")
|
| 152 |
+
return None
|
| 153 |
+
|
| 154 |
+
except Exception as e:
|
| 155 |
+
print(f" β POST {endpoint_name} error: {str(e)}")
|
| 156 |
+
return None
|
| 157 |
+
|
| 158 |
+
def test_put_endpoint(endpoint_name: str, endpoint_url: str, record_id: int, test_data: Dict[str, Any]) -> bool:
|
| 159 |
+
"""Test PUT endpoint for updating a record"""
|
| 160 |
+
url = endpoint_url.replace("{id}", str(record_id))
|
| 161 |
+
print(f"\nπ Testing PUT {endpoint_name}")
|
| 162 |
+
print(f" URL: {BASE_URL}{url}")
|
| 163 |
+
print(f" Data: {json.dumps(test_data, indent=2, default=str)}")
|
| 164 |
+
|
| 165 |
+
try:
|
| 166 |
+
response = requests.put(f"{BASE_URL}{url}", json=test_data, timeout=10)
|
| 167 |
+
|
| 168 |
+
print(f" Status Code: {response.status_code}")
|
| 169 |
+
|
| 170 |
+
if response.status_code == 200:
|
| 171 |
+
result = response.json()
|
| 172 |
+
print(f" β
PUT {endpoint_name} successful")
|
| 173 |
+
print(f" Updated record: {json.dumps(result, indent=2, default=str)}")
|
| 174 |
+
return True
|
| 175 |
+
else:
|
| 176 |
+
print(f" β PUT {endpoint_name} failed")
|
| 177 |
+
print(f" Response: {response.text}")
|
| 178 |
+
return False
|
| 179 |
+
|
| 180 |
+
except Exception as e:
|
| 181 |
+
print(f" β PUT {endpoint_name} error: {str(e)}")
|
| 182 |
+
return False
|
| 183 |
+
|
| 184 |
+
def main():
|
| 185 |
+
"""Main test function"""
|
| 186 |
+
print("π Starting Comprehensive Reference Data CRUD Tests")
|
| 187 |
+
print("=" * 60)
|
| 188 |
+
|
| 189 |
+
# Skip server check for now
|
| 190 |
+
print("β
Assuming server is running on localhost:8001")
|
| 191 |
+
|
| 192 |
+
results = {
|
| 193 |
+
"post_success": 0,
|
| 194 |
+
"post_total": 0,
|
| 195 |
+
"put_success": 0,
|
| 196 |
+
"put_total": 0,
|
| 197 |
+
"created_records": {}
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
# Test POST endpoints
|
| 201 |
+
print("\nπ Testing POST (Create) Endpoints")
|
| 202 |
+
print("=" * 40)
|
| 203 |
+
|
| 204 |
+
for ref_type, endpoints in ENDPOINTS.items():
|
| 205 |
+
results["post_total"] += 1
|
| 206 |
+
|
| 207 |
+
test_data = TEST_DATA[ref_type]["create"]
|
| 208 |
+
result = test_post_endpoint(ref_type, endpoints["post"], test_data)
|
| 209 |
+
|
| 210 |
+
if result:
|
| 211 |
+
results["post_success"] += 1
|
| 212 |
+
# Store the created record for PUT testing
|
| 213 |
+
id_field = ID_FIELDS[ref_type]
|
| 214 |
+
record_id = result.get(id_field)
|
| 215 |
+
if record_id:
|
| 216 |
+
results["created_records"][ref_type] = {"id": record_id, "data": result}
|
| 217 |
+
|
| 218 |
+
# Test PUT endpoints
|
| 219 |
+
print("\nπ Testing PUT (Update) Endpoints")
|
| 220 |
+
print("=" * 40)
|
| 221 |
+
|
| 222 |
+
for ref_type, record_info in results["created_records"].items():
|
| 223 |
+
results["put_total"] += 1
|
| 224 |
+
|
| 225 |
+
record_id = record_info["id"]
|
| 226 |
+
test_data = TEST_DATA[ref_type]["update"]
|
| 227 |
+
endpoints = ENDPOINTS[ref_type]
|
| 228 |
+
|
| 229 |
+
success = test_put_endpoint(ref_type, endpoints["put"], record_id, test_data)
|
| 230 |
+
|
| 231 |
+
if success:
|
| 232 |
+
results["put_success"] += 1
|
| 233 |
+
|
| 234 |
+
# Summary
|
| 235 |
+
print("\nπ Test Results Summary")
|
| 236 |
+
print("=" * 40)
|
| 237 |
+
print(f"POST Tests: {results['post_success']}/{results['post_total']} passed")
|
| 238 |
+
print(f"PUT Tests: {results['put_success']}/{results['put_total']} passed")
|
| 239 |
+
|
| 240 |
+
total_success = results['post_success'] + results['put_success']
|
| 241 |
+
total_tests = results['post_total'] + results['put_total']
|
| 242 |
+
|
| 243 |
+
print(f"\nOverall: {total_success}/{total_tests} tests passed")
|
| 244 |
+
|
| 245 |
+
if total_success == total_tests:
|
| 246 |
+
print("π All tests passed! Reference data CRUD operations are working correctly.")
|
| 247 |
+
return 0
|
| 248 |
+
else:
|
| 249 |
+
print("β οΈ Some tests failed. Check the output above for details.")
|
| 250 |
+
return 1
|
| 251 |
+
|
| 252 |
+
if __name__ == "__main__":
|
| 253 |
+
sys.exit(main())
|