-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_backend.py
More file actions
96 lines (81 loc) · 3.09 KB
/
test_backend.py
File metadata and controls
96 lines (81 loc) · 3.09 KB
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
#!/usr/bin/env python3
"""
Test script for SpurHacked backend API
"""
import requests
import json
import time
def test_backend():
base_url = "http://localhost:5001"
print("🧪 Testing SpurHacked Backend API")
print("=" * 40)
# Test 1: Health check
print("\n1. Testing health endpoint...")
try:
response = requests.get(f"{base_url}/health")
if response.status_code == 200:
data = response.json()
print(f"✅ Health check passed: {data}")
else:
print(f"❌ Health check failed: {response.status_code}")
return False
except requests.exceptions.ConnectionError:
print("❌ Cannot connect to backend. Make sure the server is running on localhost:5001")
return False
# Test 2: Translation endpoint
print("\n2. Testing translation endpoint...")
test_data = {
"text": "Hello world, this is a beautiful morning.",
"level": "beginner",
"targetLanguage": "fr"
}
try:
response = requests.post(
f"{base_url}/translate",
headers={"Content-Type": "application/json"},
data=json.dumps(test_data)
)
if response.status_code == 200:
data = response.json()
print(f"✅ Translation successful!")
print(f" Found {data.get('total_words_found', 0)} words")
print(f" Translated {data.get('translations_provided', 0)} words")
translations = data.get('translations', [])
if translations:
print(" Sample translations:")
for t in translations[:3]: # Show first 3 translations
print(f" {t['original']} → {t['translated']} ({t['meaning']})")
else:
print(f"❌ Translation failed: {response.status_code}")
print(f" Response: {response.text}")
return False
except requests.exceptions.RequestException as e:
print(f"❌ Translation request failed: {e}")
return False
# Test 3: Different language
print("\n3. Testing Spanish translation...")
test_data_es = {
"text": "Good morning, thank you very much.",
"level": "beginner",
"targetLanguage": "es"
}
try:
response = requests.post(
f"{base_url}/translate",
headers={"Content-Type": "application/json"},
data=json.dumps(test_data_es)
)
if response.status_code == 200:
data = response.json()
print(f"✅ Spanish translation successful!")
print(f" Translated {data.get('translations_provided', 0)} words")
else:
print(f"❌ Spanish translation failed: {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"❌ Spanish translation request failed: {e}")
print("\n🎉 Backend tests completed!")
return True
if __name__ == "__main__":
# Wait a moment for server to start if needed
time.sleep(2)
test_backend()