-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
56 lines (48 loc) · 1.77 KB
/
test_api.py
File metadata and controls
56 lines (48 loc) · 1.77 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
#!/usr/bin/env python3
"""Simple API test script"""
import subprocess
import json
def run_curl(command):
"""Run curl command and return result"""
try:
result = subprocess.run(command, shell=True, capture_output=True, text=True)
return result.stdout.strip()
except Exception as e:
return f"Error: {e}"
print("=== E-Commerce Recommendation Engine API Test ===\n")
# Test 1: Record interaction
print("1. Recording interaction...")
cmd1 = '''curl -s -X POST "http://127.0.0.1:8000/api/interaction/" -H "Content-Type: application/json" -d '{"user_id": 1, "product_id": 25, "interaction_type": "like"}\''''
result1 = run_curl(cmd1)
print(f"Result: {result1}\n")
# Test 2: Get recommendations
print("2. Getting recommendations...")
cmd2 = 'curl -s "http://127.0.0.1:8000/api/recommendations/1/?count=3"'
result2 = run_curl(cmd2)
try:
data = json.loads(result2)
print(f"User {data['user_id']} recommendations:")
for rec in data['recommendations']:
print(f" - {rec['product_name']} (Score: {rec['confidence_score']:.3f})")
except:
print(f"Raw result: {result2}")
print()
# Test 3: Search products
print("3. Searching products...")
cmd3 = 'curl -s "http://127.0.0.1:8000/api/search/?q=home&user_id=1"'
result3 = run_curl(cmd3)
try:
data = json.loads(result3)
print(f"Found {data['count']} products for 'home'")
for product in data['products'][:3]:
score = product.get('personalization_score', 'N/A')
print(f" - {product['name']} (${product['price']}) - Score: {score}")
except:
print(f"Raw result: {result3}")
print()
# Test 4: Train models
print("4. Training models...")
cmd4 = 'curl -s -X POST "http://127.0.0.1:8000/api/train/"'
result4 = run_curl(cmd4)
print(f"Result: {result4}")
print("\n=== API Test Complete ===")