-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
155 lines (126 loc) · 5.57 KB
/
Copy pathtest.py
File metadata and controls
155 lines (126 loc) · 5.57 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import requests
import json
import time
API_URL = "http://127.0.0.1:8000"
def test_health_endpoint():
"""Test the health check endpoint"""
print("Testing health endpoint...")
try:
response = requests.get(f"{API_URL}/")
print(f"Health check - Status Code: {response.status_code}")
print(f"Response: {response.json()}")
return response.status_code == 200
except Exception as e:
print(f"Health check failed: {e}")
return False
def test_cache_stats():
"""Test the cache stats endpoint"""
print("\nTesting cache stats endpoint...")
try:
response = requests.get(f"{API_URL}/cache-stats")
print(f"Cache stats - Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
return response.status_code == 200
except Exception as e:
print(f"Cache stats test failed: {e}")
return False
def test_analyze_endpoint():
"""Test the analyze endpoint"""
print("\nTesting analyze endpoint...")
sample_text = """
When I walk through a quiet forest trail, I often find myself reflecting on the nature of existence,
the fragility of time, and the small joys we often overlook. My writing tends to wander, much like my thoughts.
I enjoy exploring philosophical concepts and finding meaning in everyday experiences. Sometimes I prefer
solitude to gather my thoughts, but I also appreciate deep conversations with close friends about life's mysteries.
"""
payload = {
"text": sample_text
}
headers = {
"Content-Type": "application/json"
}
try:
response = requests.post(f"{API_URL}/analyze", headers=headers, json=payload)
print(f"Analyze - Status Code: {response.status_code}")
if response.status_code == 200:
print("Response JSON:")
print(json.dumps(response.json(), indent=2))
else:
print(f"Error Response: {response.text}")
return response.status_code == 200
except Exception as e:
print(f"Analyze test failed: {e}")
return False
def test_empty_text():
"""Test with empty text"""
print("\nTesting with empty text...")
payload = {"text": ""}
headers = {"Content-Type": "application/json"}
try:
response = requests.post(f"{API_URL}/analyze", headers=headers, json=payload)
print(f"Empty text test - Status Code: {response.status_code}")
print(f"Response: {response.text}")
return True
except Exception as e:
print(f"Empty text test failed: {e}")
return False
def test_multiple_requests():
"""Test multiple requests to verify caching"""
print("\nTesting multiple requests for caching...")
test_texts = [
"I love meeting new people and going to parties. I'm always energetic and optimistic!",
"I prefer quiet evenings at home with a good book. Deep thinking and reflection are important to me.",
"I'm very organized and always plan everything in advance. I never miss deadlines."
]
headers = {"Content-Type": "application/json"}
success_count = 0
for i, text in enumerate(test_texts, 1):
print(f"\n Test {i}/3:")
payload = {"text": text}
try:
response = requests.post(f"{API_URL}/analyze", headers=headers, json=payload)
print(f" Status Code: {response.status_code}")
if response.status_code == 200:
success_count += 1
result = response.json()
if result.get("response"):
mbti = result["response"].get("mbti_type", "Unknown")
print(f" MBTI: {mbti}")
else:
print(f" Error: {response.text}")
# Small delay between requests
time.sleep(1)
except Exception as e:
print(f" Request failed: {e}")
return success_count == len(test_texts)
if __name__ == "__main__":
print("Starting API tests with caching...")
print("Make sure the API server is running on http://127.0.0.1:8000")
print("=" * 60)
# Run tests
health_ok = test_health_endpoint()
cache_stats_ok = test_cache_stats()
analyze_ok = test_analyze_endpoint()
empty_ok = test_empty_text()
multiple_ok = test_multiple_requests()
# Final cache stats
print("\n" + "=" * 60)
print("Final cache statistics:")
try:
response = requests.get(f"{API_URL}/cache-stats")
if response.status_code == 200:
stats = response.json()
print(f"Total cached files: {stats.get('total_files', 0)}")
print(f"File types: {stats.get('file_types', {})}")
print(f"Cache directory: {stats.get('cache_directory', 'Unknown')}")
except Exception as e:
print(f"Failed to get final cache stats: {e}")
print("\n" + "=" * 60)
print("Test Results:")
print(f"Health endpoint: {'✓ PASS' if health_ok else '✗ FAIL'}")
print(f"Cache stats endpoint: {'✓ PASS' if cache_stats_ok else '✗ FAIL'}")
print(f"Analyze endpoint: {'✓ PASS' if analyze_ok else '✗ FAIL'}")
print(f"Empty text handling: {'✓ PASS' if empty_ok else '✗ FAIL'}")
print(f"Multiple requests: {'✓ PASS' if multiple_ok else '✗ FAIL'}")
print(f"\nOverall: {'✓ ALL TESTS PASSED' if all([health_ok, cache_stats_ok, analyze_ok, empty_ok, multiple_ok]) else '✗ SOME TESTS FAILED'}")
print("\nCheck the 'cache' directory for logged JSON files!")