File size: 6,628 Bytes
1624f02 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
"""
Test script for driver creation validation
Verifies that drivers cannot be created without required fields:
- name
- vehicle_type
- current_lat
- current_lng
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from chat.tools import handle_create_driver
print("=" * 70)
print("Testing Driver Creation Validation")
print("=" * 70)
# Test 1: Create driver WITHOUT name (should fail)
print("\n[1] Testing: Create driver WITHOUT name (should fail)...")
result = handle_create_driver({
"vehicle_type": "motorcycle",
"current_lat": 23.8103,
"current_lng": 90.4125
})
if not result.get("success"):
print(f"EXPECTED FAILURE: {result.get('error')}")
if "name" in result.get('error', '').lower():
print("SUCCESS: Correctly requires name!")
else:
print("WARNING: Error message should mention 'name'")
else:
print(f"FAILED: Should have required name! Created: {result.get('driver_id')}")
sys.exit(1)
# Test 2: Create driver WITHOUT vehicle_type (should fail)
print("\n[2] Testing: Create driver WITHOUT vehicle_type (should fail)...")
result = handle_create_driver({
"name": "Test Driver",
"current_lat": 23.8103,
"current_lng": 90.4125
})
if not result.get("success"):
print(f"EXPECTED FAILURE: {result.get('error')}")
if "vehicle_type" in result.get('error', '').lower():
print("SUCCESS: Correctly requires vehicle_type!")
else:
print("WARNING: Error message should mention 'vehicle_type'")
else:
print(f"FAILED: Should have required vehicle_type! Created: {result.get('driver_id')}")
sys.exit(1)
# Test 3: Create driver WITHOUT current_lat (should fail)
print("\n[3] Testing: Create driver WITHOUT current_lat (should fail)...")
result = handle_create_driver({
"name": "Test Driver",
"vehicle_type": "motorcycle",
"current_lng": 90.4125
})
if not result.get("success"):
print(f"EXPECTED FAILURE: {result.get('error')}")
if "current_lat" in result.get('error', '').lower():
print("SUCCESS: Correctly requires current_lat!")
else:
print("WARNING: Error message should mention 'current_lat'")
else:
print(f"FAILED: Should have required current_lat! Created: {result.get('driver_id')}")
sys.exit(1)
# Test 4: Create driver WITHOUT current_lng (should fail)
print("\n[4] Testing: Create driver WITHOUT current_lng (should fail)...")
result = handle_create_driver({
"name": "Test Driver",
"vehicle_type": "motorcycle",
"current_lat": 23.8103
})
if not result.get("success"):
print(f"EXPECTED FAILURE: {result.get('error')}")
if "current_lng" in result.get('error', '').lower():
print("SUCCESS: Correctly requires current_lng!")
else:
print("WARNING: Error message should mention 'current_lng'")
else:
print(f"FAILED: Should have required current_lng! Created: {result.get('driver_id')}")
sys.exit(1)
# Test 5: Create driver with INVALID latitude (should fail)
print("\n[5] Testing: Create driver with invalid latitude (should fail)...")
result = handle_create_driver({
"name": "Test Driver",
"vehicle_type": "motorcycle",
"current_lat": 95.0, # Invalid - must be -90 to 90
"current_lng": 90.4125
})
if not result.get("success"):
print(f"EXPECTED FAILURE: {result.get('error')}")
if "latitude" in result.get('error', '').lower() or "-90" in result.get('error', ''):
print("SUCCESS: Correctly validates latitude range!")
else:
print("WARNING: Error message should mention latitude validation")
else:
print(f"FAILED: Should have rejected invalid latitude!")
sys.exit(1)
# Test 6: Create driver with INVALID longitude (should fail)
print("\n[6] Testing: Create driver with invalid longitude (should fail)...")
result = handle_create_driver({
"name": "Test Driver",
"vehicle_type": "motorcycle",
"current_lat": 23.8103,
"current_lng": 200.0 # Invalid - must be -180 to 180
})
if not result.get("success"):
print(f"EXPECTED FAILURE: {result.get('error')}")
if "longitude" in result.get('error', '').lower() or "-180" in result.get('error', ''):
print("SUCCESS: Correctly validates longitude range!")
else:
print("WARNING: Error message should mention longitude validation")
else:
print(f"FAILED: Should have rejected invalid longitude!")
sys.exit(1)
# Test 7: Create driver with NON-NUMERIC coordinates (should fail)
print("\n[7] Testing: Create driver with non-numeric coordinates (should fail)...")
result = handle_create_driver({
"name": "Test Driver",
"vehicle_type": "motorcycle",
"current_lat": "not a number",
"current_lng": 90.4125
})
if not result.get("success"):
print(f"EXPECTED FAILURE: {result.get('error')}")
if "number" in result.get('error', '').lower() or "valid" in result.get('error', '').lower():
print("SUCCESS: Correctly validates coordinates are numbers!")
else:
print("WARNING: Error message should mention coordinates must be numbers")
else:
print(f"FAILED: Should have rejected non-numeric coordinates!")
sys.exit(1)
# Test 8: Create driver WITH all required fields (should succeed)
print("\n[8] Testing: Create driver with ALL required fields (should succeed)...")
result = handle_create_driver({
"name": "Valid Test Driver",
"phone": "+8801812345678",
"vehicle_type": "motorcycle",
"current_lat": 23.8103,
"current_lng": 90.4125
})
if result.get("success"):
driver_id = result.get("driver_id")
print(f"SUCCESS: Driver created: {driver_id}")
print(f" Name: {result.get('name')}")
print(f" Vehicle: {result.get('vehicle_type')}")
print(f" Location: ({result.get('location', {}).get('latitude')}, {result.get('location', {}).get('longitude')})")
# Cleanup
print("\nCleaning up test driver...")
from chat.tools import handle_delete_driver
handle_delete_driver({"driver_id": driver_id, "confirm": True})
print("Cleanup complete!")
else:
print(f"FAILED: Should have created driver with all required fields!")
print(f"Error: {result.get('error')}")
sys.exit(1)
print("\n" + "=" * 70)
print("Driver Creation Validation Test Complete!")
print("=" * 70)
print("\nSummary:")
print(" - name is mandatory: YES")
print(" - vehicle_type is mandatory: YES")
print(" - current_lat is mandatory: YES")
print(" - current_lng is mandatory: YES")
print(" - Latitude range validated (-90 to 90): YES")
print(" - Longitude range validated (-180 to 180): YES")
print(" - Coordinates must be numeric: YES")
print(" - Valid driver can be created: YES")
|