Spaces:
Paused
Paused
| import os | |
| import json | |
| import requests | |
| from log import log | |
| BASE_URL = os.environ.get("BASE_URL", "http://localhost:7860") | |
| SERVICE_CONFIG_PATH = os.environ.get("SERVICE_CONFIG_PATH", "service_config.json") | |
| with open(SERVICE_CONFIG_PATH, "r", encoding="utf-8") as f: | |
| service_config = json.load(f) | |
| fallback_answers = service_config["projects"]["project1"]["llm"]["fallback_answers"] | |
| currency_options = service_config["config"]["data_formats"]["currency_format"]["valid_options"] | |
| city_options = service_config["config"]["data_formats"]["city_format"]["valid_options"] | |
| test_results = [] | |
| def assert_test(name, actual, expected_substring, explanation=None): | |
| if explanation: | |
| log(f"🧪 TEST: {name} → {explanation}") | |
| actual_str = str(actual) | |
| passed = expected_substring in actual_str | |
| if passed: | |
| log(f"[TEST] {name:<45} ✅") | |
| test_results.append((name, True)) | |
| else: | |
| log(f"[TEST] {name:<45} ❌ — Beklenen: {expected_substring}, Gelen: {actual_str[:100]}...") | |
| test_results.append((name, False)) | |
| def summarize_tests(): | |
| total = len(test_results) | |
| success = sum(1 for _, ok in test_results if ok) | |
| fail = total - success | |
| log("🧾 TEST SONUCU ÖZETİ") | |
| log(f"🔢 Toplam Test : {total}") | |
| log(f"✅ Başarılı : {success}") | |
| log(f"❌ Başarısız : {fail}") | |
| def run_all_tests(): | |
| try: | |
| log("🚀 Test süreci başlatıldı.") | |
| response = requests.post(f"{BASE_URL}/start_chat?project_name=project1") | |
| if response.status_code != 200: | |
| raise Exception(f"Start chat başarısız: {response.status_code}, {response.text}") | |
| session_id = response.json().get("session_id") | |
| if not session_id: | |
| raise Exception("Session ID alınamadı.") | |
| headers = {"X-Session-ID": session_id} | |
| # === Test 1: LLM fallback | |
| r = requests.post(f"{BASE_URL}/chat", json={"user_input": "Mars'a bilet alabilir miyim?"}, headers=headers) | |
| assert_test("LLM fallback", r.json(), "") | |
| # === Test 2: Döviz kuru — başarılı | |
| valid_currency = currency_options[0] | |
| r = requests.post(f"{BASE_URL}/chat", json={"user_input": f"{valid_currency} kuru nedir"}, headers=headers) | |
| assert_test("Parametre tamamlandı — dolar", r.json(), f"{valid_currency} kuru şu an") | |
| # === Test 3: Döviz kuru — geçersiz parametre | |
| r = requests.post(f"{BASE_URL}/chat", json={"user_input": "yenidolar kuru nedir"}, headers=headers) | |
| assert_test("Geçersiz parametre — currency", r.json(), "Geçerli bir döviz cinsi") | |
| # === Test 4: Yol durumu — eksik parametreleri isteme | |
| r = requests.post(f"{BASE_URL}/chat", json={"user_input": "yol durumu"}, headers=headers) | |
| assert_test("Eksik parametre — from_location & to_location", r.json(), "Lütfen şu bilgileri sırayla belirtir misiniz") | |
| # === Test 4b: Yol durumu — eksik parametreleri tamamlama ve doğrulama | |
| r = requests.post(f"{BASE_URL}/chat", json={"user_input": f"{city_options[0]} {city_options[1]}"}, headers=headers) | |
| assert_test("Parametre tamamlandı — yol durumu", r.json(), "trafik açık") | |
| # === Test 5: Hava durumu — eksik parametre isteme | |
| r = requests.post(f"{BASE_URL}/chat", json={"user_input": "hava durumu"}, headers=headers) | |
| assert_test("Eksik parametre — city", r.json(), "Lütfen şu bilgileri sırayla belirtir misiniz") | |
| # === Test 5b: Hava durumu — parametre tamamlama ve doğrulama | |
| r = requests.post(f"{BASE_URL}/chat", json={"user_input": f"{city_options[0]}"}, headers=headers) | |
| assert_test("Parametre tamamlandı — hava durumu", r.json(), f"{city_options[0]} için hava güneşli") | |
| summarize_tests() | |
| except Exception as e: | |
| log(f"❌ run_all_tests sırasında hata oluştu: {e}") | |