linebot_pydantic_fastapi / test_order_queries.py
mickeywu520's picture
修正訂單查詢問題
68c8519
"""
測試訂單查詢功能 - 基於實際銷售訂單資料
"""
def test_order_number_extraction():
"""測試訂單編號提取功能"""
def extract_order_number_simple(message: str) -> str:
"""簡化版訂單編號提取"""
import re
# 尋找 SO- 格式的訂單編號
so_pattern = r'SO-\d{8}-\d{3}'
match = re.search(so_pattern, message.upper())
if match:
return match.group()
# 尋找日期格式的編號 (20250706-001)
date_pattern = r'\d{8}-\d{3}'
match = re.search(date_pattern, message)
if match:
return f"SO-{match.group()}"
# 尋找純數字編號
number_pattern = r'\d{6,}'
match = re.search(number_pattern, message)
if match:
return match.group()
return None
print("🔍 測試訂單編號提取")
print("=" * 50)
test_queries = [
"查詢訂單 SO-20250706-001",
"我的訂單 SO-20250708-001 狀態如何?",
"20250706-001 這個訂單",
"訂單編號 20250708-001",
"SO20250706001", # 沒有連字符
"查詢我的訂單", # 沒有編號
]
for query in test_queries:
order_number = extract_order_number_simple(query)
print(f"'{query}' → 訂單編號: {order_number}")
def test_order_status_extraction():
"""測試訂單狀態提取功能"""
def extract_order_status_simple(message: str) -> str:
"""簡化版訂單狀態提取"""
message_lower = message.lower()
# 中文狀態關鍵字
status_keywords = {
'已交付': 'DELIVERED',
'已出貨': 'SHIPPED',
'出貨': 'SHIPPED',
'交付': 'DELIVERED',
'處理中': 'PROCESSING',
'已取消': 'CANCELLED',
'取消': 'CANCELLED',
'待處理': 'PENDING',
'已確認': 'CONFIRMED',
'已完成': 'COMPLETED'
}
for chinese, english in status_keywords.items():
if chinese in message_lower:
return english
# 英文狀態關鍵字
english_keywords = ['delivered', 'shipped', 'processing', 'cancelled', 'pending', 'confirmed', 'completed']
for keyword in english_keywords:
if keyword in message_lower:
return keyword.upper()
return None
print("\n📊 測試訂單狀態提取")
print("=" * 50)
test_queries = [
"查詢已交付的訂單",
"已出貨訂單有哪些?",
"處理中的訂單",
"DELIVERED 狀態的訂單",
"shipped orders",
"我的訂單狀態", # 沒有狀態
]
for query in test_queries:
status = extract_order_status_simple(query)
print(f"'{query}' → 狀態: {status}")
def test_order_query_simulation():
"""模擬訂單查詢功能"""
# 模擬的銷售訂單資料(來自您的 Supabase)
orders = [
{
"id": 1,
"so_number": "SO-20250706-001",
"sales_date": "2025-07-06",
"customer_id": 1,
"salesperson_id": 1,
"payment_term": "MONTHLY",
"status": "DELIVERED",
"subtotal": 0,
"tax_amount": 0,
"discount_amount": 0,
"total_amount": 0,
"created_at": "2025-07-06 14:53:23",
"updated_at": "2025-07-06 14:55:18"
},
{
"id": 2,
"so_number": "SO-20250708-001",
"sales_date": "2025-07-08",
"customer_id": 1,
"salesperson_id": 1,
"payment_term": "CASH",
"status": "SHIPPED",
"subtotal": 0,
"tax_amount": 0,
"discount_amount": 0,
"total_amount": 0,
"created_at": "2025-07-08 06:24:34",
"updated_at": "2025-07-10 07:08:12"
}
]
def search_orders_simulation(order_number=None, status=None):
"""模擬訂單搜尋"""
results = []
for order in orders:
match = True
# 訂單編號篩選
if order_number:
if order_number not in order["so_number"]:
match = False
# 狀態篩選
if status:
if status.upper() != order["status"]:
match = False
if match:
results.append(order)
return results
def get_status_display(status):
"""狀態中文顯示"""
status_mapping = {
'DELIVERED': '已交付',
'SHIPPED': '已出貨',
'PROCESSING': '處理中',
'CANCELLED': '已取消',
'PENDING': '待處理'
}
return status_mapping.get(status, status)
print("\n🛍️ 測試訂單查詢模擬")
print("=" * 50)
test_scenarios = [
{"query": "查詢訂單 SO-20250706-001", "order_number": "SO-20250706-001", "status": None},
{"query": "已交付的訂單", "order_number": None, "status": "DELIVERED"},
{"query": "已出貨訂單", "order_number": None, "status": "SHIPPED"},
{"query": "我的所有訂單", "order_number": None, "status": None},
{"query": "SO-20250708-001 狀態", "order_number": "SO-20250708-001", "status": None},
]
for scenario in test_scenarios:
print(f"\n查詢: '{scenario['query']}'")
results = search_orders_simulation(
order_number=scenario["order_number"],
status=scenario["status"]
)
print(f"找到 {len(results)} 個訂單:")
for order in results:
status_display = get_status_display(order["status"])
print(f" ✅ {order['so_number']} - {status_display} - {order['sales_date']} - 付款: {order['payment_term']}")
def test_natural_language_processing():
"""測試自然語言處理"""
print("\n🤖 測試自然語言訂單查詢")
print("=" * 50)
def extract_order_number_simple(message: str) -> str:
import re
so_pattern = r'SO-\d{8}-\d{3}'
match = re.search(so_pattern, message.upper())
if match:
return match.group()
date_pattern = r'\d{8}-\d{3}'
match = re.search(date_pattern, message)
if match:
return f"SO-{match.group()}"
return None
def extract_order_status_simple(message: str) -> str:
message_lower = message.lower()
status_keywords = {
'已交付': 'DELIVERED',
'已出貨': 'SHIPPED',
'出貨': 'SHIPPED',
'交付': 'DELIVERED',
'處理中': 'PROCESSING'
}
for chinese, english in status_keywords.items():
if chinese in message_lower:
return english
return None
natural_queries = [
"請問我的訂單 SO-20250706-001 現在是什麼狀態?",
"查詢已交付的所有訂單",
"20250708-001 這個訂單出貨了嗎?",
"我有哪些已出貨的訂單?",
"訂單狀態查詢"
]
for query in natural_queries:
print(f"\n自然語言查詢: '{query}'")
order_number = extract_order_number_simple(query)
status = extract_order_status_simple(query)
print(f" 提取的訂單編號: {order_number}")
print(f" 提取的狀態: {status}")
# 判斷查詢類型
if order_number and status:
query_type = "特定訂單狀態查詢"
elif order_number:
query_type = "特定訂單查詢"
elif status:
query_type = "狀態篩選查詢"
else:
query_type = "一般訂單查詢"
print(f" 查詢類型: {query_type}")
def main():
"""主函數"""
print("🚀 開始訂單查詢功能測試\n")
test_order_number_extraction()
test_order_status_extraction()
test_order_query_simulation()
test_natural_language_processing()
print("\n" + "=" * 50)
print("✅ 測試完成!")
print("\n💡 分析結果:")
print("1. 訂單編號提取邏輯能正確識別 SO- 格式")
print("2. 狀態提取支援中文和英文")
print("3. 查詢模擬能正確篩選訂單")
print("4. 自然語言處理能提取關鍵資訊")
if __name__ == "__main__":
main()