fleetmind-dispatch-ai / test_driver_validation.py
mashrur950's picture
feat: Add mandatory expected delivery time and SLA tracking to order management
1624f02
raw
history blame
6.63 kB
"""
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")