-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
158 lines (121 loc) · 4.75 KB
/
test_api.py
File metadata and controls
158 lines (121 loc) · 4.75 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
156
157
#!/usr/bin/env python3
"""
Simple test script for the LearningSteps API
Tests all available endpoints
"""
import requests
import json
from datetime import datetime
# API base URL
BASE_URL = "http://localhost:8000"
def print_section(title):
"""Print a formatted section header"""
print("\n" + "="*60)
print(f" {title}")
print("="*60)
def print_response(response):
"""Pretty print API response"""
print(f"Status Code: {response.status_code}")
try:
print(f"Response: {json.dumps(response.json(), indent=2, default=str)}")
except:
print(f"Response: {response.text}")
def test_create_entry():
"""Test POST /entries - Create a new journal entry"""
print_section("TEST 1: Create a New Entry")
entry_data = {
"work": "Learned FastAPI basics and tested API endpoints",
"struggle": "Understanding async/await patterns in Python",
"intention": "Build a complete test suite for the API"
}
response = requests.post(f"{BASE_URL}/entries", json=entry_data)
print_response(response)
if response.status_code == 200:
return response.json().get("entry", {}).get("id")
return None
def test_get_all_entries():
"""Test GET /entries - Get all journal entries"""
print_section("TEST 2: Get All Entries")
response = requests.get(f"{BASE_URL}/entries")
print_response(response)
if response.status_code == 200:
entries = response.json().get("entries", [])
if entries:
return entries[0].get("id")
return None
def test_get_single_entry(entry_id):
"""Test GET /entries/{entry_id} - Get a single entry"""
print_section("TEST 3: Get Single Entry")
if not entry_id:
print("⚠️ No entry ID available, skipping test")
return
response = requests.get(f"{BASE_URL}/entries/{entry_id}")
print_response(response)
def test_update_entry(entry_id):
"""Test PATCH /entries/{entry_id} - Update an entry"""
print_section("TEST 4: Update Entry")
if not entry_id:
print("⚠️ No entry ID available, skipping test")
return
update_data = {
"work": "Updated: Completed API testing script"
}
response = requests.patch(f"{BASE_URL}/entries/{entry_id}", json=update_data)
print_response(response)
def test_delete_single_entry(entry_id):
"""Test DELETE /entries/{entry_id} - Delete a specific entry"""
print_section("TEST 5: Delete Single Entry")
if not entry_id:
print("⚠️ No entry ID available, skipping test")
return
response = requests.delete(f"{BASE_URL}/entries/{entry_id}")
print_response(response)
def test_delete_all_entries():
"""Test DELETE /entries - Delete all entries"""
print_section("TEST 6: Delete All Entries")
response = requests.delete(f"{BASE_URL}/entries")
print_response(response)
def test_api_health():
"""Check if API is accessible"""
print_section("API Health Check")
try:
response = requests.get(f"{BASE_URL}/docs")
if response.status_code == 200:
print("✅ API is running and accessible")
return True
else:
print(f"⚠️ API returned status code: {response.status_code}")
return False
except requests.exceptions.ConnectionError:
print("❌ Cannot connect to API. Make sure it's running on http://localhost:8000")
return False
def main():
"""Run all API tests"""
print("\n🚀 Starting LearningSteps API Tests")
print(f"Testing API at: {BASE_URL}")
print(f"Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
# Check if API is running
if not test_api_health():
print("\n❌ Tests aborted: API is not accessible")
print("💡 Tip: Run './start.sh' to start the API")
return
# Test creating an entry
created_entry_id = test_create_entry()
# Test getting all entries
entry_id = test_get_all_entries()
# Use the created entry ID or the first available entry ID
test_id = created_entry_id or entry_id
# Test getting a single entry
test_get_single_entry(test_id)
# Test updating an entry
test_update_entry(test_id)
# Test deleting a single entry (creates a new one first to avoid deleting all data)
temp_entry_id = test_create_entry()
test_delete_single_entry(temp_entry_id)
# Uncomment below to test delete all entries (warning: deletes all data!)
# print("\n⚠️ Warning: The next test will delete ALL entries from the database")
# test_delete_all_entries()
print_section("✅ Tests Complete!")
print("💡 Tip: Visit http://localhost:8000/docs to explore the API interactively")
if __name__ == "__main__":
main()