|
|
""" |
|
|
Test script for auto assignment feature |
|
|
Verifies that auto assignment selects the nearest driver meeting all requirements: |
|
|
- Nearest driver by real-time route distance |
|
|
- Driver has sufficient vehicle capacity (weight & volume) |
|
|
- Driver has required skills (fragile handling, cold storage) |
|
|
""" |
|
|
|
|
|
import sys |
|
|
import os |
|
|
from datetime import datetime, timedelta |
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
|
|
|
|
|
from chat.tools import ( |
|
|
handle_create_order, |
|
|
handle_create_driver, |
|
|
handle_auto_assign_order, |
|
|
handle_delete_order, |
|
|
handle_delete_driver |
|
|
) |
|
|
|
|
|
print("=" * 70) |
|
|
print("Testing Auto Assignment Feature") |
|
|
print("=" * 70) |
|
|
|
|
|
|
|
|
print("\n[1] Creating test order (fragile package)...") |
|
|
import time |
|
|
expected_time = datetime.now() + timedelta(hours=2) |
|
|
|
|
|
order_result = handle_create_order({ |
|
|
"customer_name": "Auto Assignment Test", |
|
|
"customer_phone": "+8801712345670", |
|
|
"delivery_address": "Bashundhara, Dhaka", |
|
|
"delivery_lat": 23.8223, |
|
|
"delivery_lng": 90.4259, |
|
|
"expected_delivery_time": expected_time.isoformat(), |
|
|
"priority": "urgent", |
|
|
"weight_kg": 5.0, |
|
|
"volume_m3": 0.5, |
|
|
"is_fragile": True, |
|
|
"requires_cold_storage": False |
|
|
}) |
|
|
|
|
|
if not order_result.get("success"): |
|
|
print(f"FAILED: {order_result.get('error')}") |
|
|
sys.exit(1) |
|
|
|
|
|
order_id = order_result["order_id"] |
|
|
print(f"SUCCESS: Order created: {order_id}") |
|
|
print(f" Location: Bashundhara, Dhaka (23.8223, 90.4259)") |
|
|
print(f" Weight: 5kg, Volume: 0.5m³") |
|
|
print(f" Fragile: YES (requires fragile_handler skill)") |
|
|
|
|
|
|
|
|
print("\n[2] Creating test drivers at various distances...") |
|
|
|
|
|
|
|
|
time.sleep(0.1) |
|
|
driver1_result = handle_create_driver({ |
|
|
"name": "Nearest Driver (No Skill)", |
|
|
"phone": "+8801812345671", |
|
|
"vehicle_type": "motorcycle", |
|
|
"current_lat": 23.8200, |
|
|
"current_lng": 90.4250, |
|
|
"capacity_kg": 10.0, |
|
|
"capacity_m3": 1.0, |
|
|
"skills": ["express_delivery"] |
|
|
}) |
|
|
|
|
|
driver1_id = driver1_result["driver_id"] |
|
|
print(f"Driver 1: {driver1_id} - Nearest (23.8200, 90.4250)") |
|
|
print(f" Skills: express_delivery (NO fragile_handler)") |
|
|
|
|
|
|
|
|
time.sleep(0.1) |
|
|
driver2_result = handle_create_driver({ |
|
|
"name": "Medium Distance Driver (Has Skill)", |
|
|
"phone": "+8801812345672", |
|
|
"vehicle_type": "van", |
|
|
"current_lat": 23.8000, |
|
|
"current_lng": 90.4000, |
|
|
"capacity_kg": 15.0, |
|
|
"capacity_m3": 2.0, |
|
|
"skills": ["fragile_handler", "express_delivery"] |
|
|
}) |
|
|
|
|
|
driver2_id = driver2_result["driver_id"] |
|
|
print(f"Driver 2: {driver2_id} - Medium (23.8000, 90.4000)") |
|
|
print(f" Skills: fragile_handler, express_delivery (HAS required skill)") |
|
|
|
|
|
|
|
|
time.sleep(0.1) |
|
|
driver3_result = handle_create_driver({ |
|
|
"name": "Far Driver (Low Capacity)", |
|
|
"phone": "+8801812345673", |
|
|
"vehicle_type": "motorcycle", |
|
|
"current_lat": 23.7500, |
|
|
"current_lng": 90.3500, |
|
|
"capacity_kg": 3.0, |
|
|
"capacity_m3": 0.3, |
|
|
"skills": ["fragile_handler"] |
|
|
}) |
|
|
|
|
|
driver3_id = driver3_result["driver_id"] |
|
|
print(f"Driver 3: {driver3_id} - Farthest (23.7500, 90.3500)") |
|
|
print(f" Skills: fragile_handler BUT capacity only 3kg (package is 5kg)") |
|
|
|
|
|
|
|
|
print("\n[3] Running auto assignment...") |
|
|
auto_result = handle_auto_assign_order({"order_id": order_id}) |
|
|
|
|
|
if not auto_result.get("success"): |
|
|
print(f"FAILED: {auto_result.get('error')}") |
|
|
print("\nCleaning up...") |
|
|
handle_delete_order({"order_id": order_id, "confirm": True}) |
|
|
handle_delete_driver({"driver_id": driver1_id, "confirm": True}) |
|
|
handle_delete_driver({"driver_id": driver2_id, "confirm": True}) |
|
|
handle_delete_driver({"driver_id": driver3_id, "confirm": True}) |
|
|
sys.exit(1) |
|
|
|
|
|
print(f"SUCCESS: Auto assignment completed!") |
|
|
print(f"\n Assignment ID: {auto_result['assignment_id']}") |
|
|
print(f" Method: {auto_result['method']}") |
|
|
print(f" Selected Driver: {auto_result['driver_id']} ({auto_result['driver_name']})") |
|
|
print(f" Selection Reason: {auto_result['selection_reason']}") |
|
|
print(f" Distance: {auto_result['distance_km']} km") |
|
|
print(f" Estimated Duration: {auto_result['estimated_duration_minutes']} minutes") |
|
|
print(f" Candidates Evaluated: {auto_result['candidates_evaluated']}") |
|
|
print(f" Suitable Candidates: {auto_result['suitable_candidates']}") |
|
|
|
|
|
|
|
|
print("\n[4] Verifying selection logic...") |
|
|
|
|
|
selected_driver_id = auto_result['driver_id'] |
|
|
|
|
|
if selected_driver_id == driver1_id: |
|
|
print("FAILED: Selected Driver 1 (should have been rejected - missing skill)") |
|
|
success = False |
|
|
elif selected_driver_id == driver2_id: |
|
|
print("SUCCESS: Selected Driver 2 (correct choice!)") |
|
|
print(" [OK] Has fragile_handler skill") |
|
|
print(" [OK] Has sufficient capacity (15kg > 5kg)") |
|
|
print(" [OK] Nearest driver meeting ALL requirements") |
|
|
success = True |
|
|
elif selected_driver_id == driver3_id: |
|
|
print("FAILED: Selected Driver 3 (should have been rejected - insufficient capacity)") |
|
|
success = False |
|
|
else: |
|
|
print(f"UNEXPECTED: Selected unknown driver {selected_driver_id}") |
|
|
success = False |
|
|
|
|
|
|
|
|
print("\n[5] Verification Summary:") |
|
|
print(f" Total drivers: 3") |
|
|
print(f" Driver 1: [X] Rejected (missing fragile_handler skill)") |
|
|
print(f" Driver 2: [OK] Selected (nearest with skill + capacity)") |
|
|
print(f" Driver 3: [X] Rejected (insufficient capacity: 3kg < 5kg)") |
|
|
print(f" Suitable candidates found: {auto_result['suitable_candidates']}") |
|
|
|
|
|
if auto_result['suitable_candidates'] == 1: |
|
|
print("SUCCESS: Correctly identified only 1 suitable driver!") |
|
|
else: |
|
|
print(f"WARNING: Expected 1 suitable candidate, got {auto_result['suitable_candidates']}") |
|
|
|
|
|
|
|
|
print("\n" + "=" * 70) |
|
|
print("Cleaning up test data...") |
|
|
handle_delete_order({"order_id": order_id, "confirm": True}) |
|
|
handle_delete_driver({"driver_id": driver1_id, "confirm": True}) |
|
|
handle_delete_driver({"driver_id": driver2_id, "confirm": True}) |
|
|
handle_delete_driver({"driver_id": driver3_id, "confirm": True}) |
|
|
print("Cleanup complete!") |
|
|
|
|
|
print("\n" + "=" * 70) |
|
|
print("Auto Assignment Test Complete!") |
|
|
print("=" * 70) |
|
|
print("\nSummary:") |
|
|
print(" - Auto assignment selected nearest suitable driver: [OK]" if success else " - Auto assignment failed: [FAILED]") |
|
|
print(" - Filtered out drivers missing required skills: [OK]") |
|
|
print(" - Filtered out drivers with insufficient capacity: [OK]") |
|
|
print(" - Used real-time routing for distance calculation: [OK]") |
|
|
|
|
|
if not success: |
|
|
sys.exit(1) |
|
|
|