askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
test_api.py · 6.9 KB · 257 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Test the Object Primitive System REST API |
| 4 | |
| 5 | Run this after starting the server with: python run_server.py |
| 6 | |
| 7 | Usage: |
| 8 | python test_api.py |
| 9 | """ |
| 10 | import requests |
| 11 | import json |
| 12 | |
| 13 | BASE_URL = "http://localhost:8001" |
| 14 | |
| 15 | |
| 16 | def test_list_objects(): |
| 17 | """Test listing all objects""" |
| 18 | print("\n" + "=" * 60) |
| 19 | print("TEST: List all objects") |
| 20 | print("=" * 60) |
| 21 | |
| 22 | response = requests.get(f"{BASE_URL}/objects") |
| 23 | print(f"Status: {response.status_code}") |
| 24 | print(f"Response:") |
| 25 | data = response.json() |
| 26 | print(json.dumps(data, indent=2)) |
| 27 | |
| 28 | assert response.status_code == 200 |
| 29 | assert data['status'] == 'ok' |
| 30 | assert 'objects' in data |
| 31 | assert 'count' in data |
| 32 | |
| 33 | print(f"\n✅ Found {data['count']} objects") |
| 34 | return data['objects'] |
| 35 | |
| 36 | |
| 37 | def test_execute_counter(): |
| 38 | """Test executing counter object""" |
| 39 | print("\n" + "=" * 60) |
| 40 | print("TEST: Execute counter object (GET)") |
| 41 | print("=" * 60) |
| 42 | |
| 43 | # Execute GET multiple times |
| 44 | for i in range(3): |
| 45 | response = requests.get(f"{BASE_URL}/objects/tutorial_03_counter") |
| 46 | print(f"\nRequest {i+1}:") |
| 47 | print(f"Status: {response.status_code}") |
| 48 | data = response.json() |
| 49 | print(f"Response: {json.dumps(data, indent=2)}") |
| 50 | |
| 51 | assert response.status_code == 200 |
| 52 | assert data['status'] == 'ok' |
| 53 | assert 'count' in data |
| 54 | |
| 55 | print(f"\n✅ Counter working correctly") |
| 56 | |
| 57 | |
| 58 | def test_get_metadata(): |
| 59 | """Test getting object metadata""" |
| 60 | print("\n" + "=" * 60) |
| 61 | print("TEST: Get object metadata") |
| 62 | print("=" * 60) |
| 63 | |
| 64 | response = requests.get(f"{BASE_URL}/objects/tutorial_03_counter?metadata=true") |
| 65 | print(f"Status: {response.status_code}") |
| 66 | data = response.json() |
| 67 | print(f"Response:") |
| 68 | print(json.dumps(data, indent=2)) |
| 69 | |
| 70 | assert response.status_code == 200 |
| 71 | assert data['status'] == 'ok' |
| 72 | assert 'metadata' in data |
| 73 | |
| 74 | metadata = data['metadata'] |
| 75 | assert 'name' in metadata |
| 76 | assert 'version' in metadata |
| 77 | assert 'log_count' in metadata |
| 78 | |
| 79 | print(f"\n✅ Metadata retrieved: {metadata['name']} v{metadata['version']}") |
| 80 | |
| 81 | |
| 82 | def test_get_logs(): |
| 83 | """Test getting object logs""" |
| 84 | print("\n" + "=" * 60) |
| 85 | print("TEST: Get object logs") |
| 86 | print("=" * 60) |
| 87 | |
| 88 | response = requests.get(f"{BASE_URL}/objects/tutorial_03_counter?logs=true&limit=10") |
| 89 | print(f"Status: {response.status_code}") |
| 90 | data = response.json() |
| 91 | print(f"Response:") |
| 92 | print(json.dumps(data, indent=2)) |
| 93 | |
| 94 | assert response.status_code == 200 |
| 95 | assert data['status'] == 'ok' |
| 96 | assert 'logs' in data |
| 97 | |
| 98 | print(f"\n✅ Retrieved {data['count']} log entries") |
| 99 | |
| 100 | |
| 101 | def test_get_source(): |
| 102 | """Test getting object source code""" |
| 103 | print("\n" + "=" * 60) |
| 104 | print("TEST: Get object source code") |
| 105 | print("=" * 60) |
| 106 | |
| 107 | response = requests.get(f"{BASE_URL}/objects/tutorial_03_counter?source=true") |
| 108 | print(f"Status: {response.status_code}") |
| 109 | data = response.json() |
| 110 | |
| 111 | assert response.status_code == 200 |
| 112 | assert data['status'] == 'ok' |
| 113 | assert 'source' in data |
| 114 | |
| 115 | source_lines = data['source'].split('\n') |
| 116 | print(f"Source code preview (first 10 lines):") |
| 117 | for i, line in enumerate(source_lines[:10], 1): |
| 118 | print(f" {i:3d}: {line}") |
| 119 | |
| 120 | if len(source_lines) > 10: |
| 121 | print(f" ... ({len(source_lines) - 10} more lines)") |
| 122 | |
| 123 | print(f"\n✅ Retrieved {len(source_lines)} lines of source code") |
| 124 | |
| 125 | |
| 126 | def test_post_reset_counter(): |
| 127 | """Test POST request to reset counter""" |
| 128 | print("\n" + "=" * 60) |
| 129 | print("TEST: Reset counter (POST)") |
| 130 | print("=" * 60) |
| 131 | |
| 132 | response = requests.post( |
| 133 | f"{BASE_URL}/objects/tutorial_03_counter", |
| 134 | json={"user_id": "test_user"}, |
| 135 | ) |
| 136 | print(f"Status: {response.status_code}") |
| 137 | data = response.json() |
| 138 | print(f"Response:") |
| 139 | print(json.dumps(data, indent=2)) |
| 140 | |
| 141 | assert response.status_code == 200 |
| 142 | assert data['status'] == 'ok' |
| 143 | assert data['count'] == 0 |
| 144 | |
| 145 | print(f"\n✅ Counter reset successfully") |
| 146 | |
| 147 | |
| 148 | def test_calculator(): |
| 149 | """Test calculator object with various operations""" |
| 150 | print("\n" + "=" * 60) |
| 151 | print("TEST: Calculator operations") |
| 152 | print("=" * 60) |
| 153 | |
| 154 | operations = [ |
| 155 | {'a': 10, 'b': 5, 'operation': 'add'}, |
| 156 | {'a': 10, 'b': 5, 'operation': 'subtract'}, |
| 157 | {'a': 10, 'b': 5, 'operation': 'multiply'}, |
| 158 | {'a': 10, 'b': 5, 'operation': 'divide'}, |
| 159 | ] |
| 160 | |
| 161 | for op in operations: |
| 162 | response = requests.get( |
| 163 | f"{BASE_URL}/objects/tutorial_04_calculator", |
| 164 | params=op, |
| 165 | ) |
| 166 | print(f"\nOperation: {op['a']} {op['operation']} {op['b']}") |
| 167 | print(f"Status: {response.status_code}") |
| 168 | data = response.json() |
| 169 | print(f"Result: {data.get('result')}") |
| 170 | print(f"Operation string: {data.get('operation')}") |
| 171 | |
| 172 | assert response.status_code == 200 |
| 173 | assert data['status'] == 'ok' |
| 174 | assert 'result' in data |
| 175 | |
| 176 | print(f"\n✅ Calculator working correctly") |
| 177 | |
| 178 | |
| 179 | def test_error_handling(): |
| 180 | """Test error handling""" |
| 181 | print("\n" + "=" * 60) |
| 182 | print("TEST: Error handling") |
| 183 | print("=" * 60) |
| 184 | |
| 185 | # Test 404 - object not found |
| 186 | response = requests.get(f"{BASE_URL}/objects/nonexistent_object") |
| 187 | print(f"\n404 Test:") |
| 188 | print(f"Status: {response.status_code}") |
| 189 | data = response.json() |
| 190 | print(f"Response: {json.dumps(data, indent=2)}") |
| 191 | |
| 192 | assert response.status_code == 404 |
| 193 | assert 'error' in data |
| 194 | |
| 195 | # Test validation error in calculator |
| 196 | response = requests.get(f"{BASE_URL}/objects/tutorial_04_calculator") |
| 197 | print(f"\nValidation Error Test:") |
| 198 | print(f"Status: {response.status_code}") |
| 199 | data = response.json() |
| 200 | print(f"Response: {json.dumps(data, indent=2)}") |
| 201 | |
| 202 | assert response.status_code == 200 |
| 203 | assert data['status'] == 'error' # Application-level error |
| 204 | |
| 205 | print(f"\n✅ Error handling working correctly") |
| 206 | |
| 207 | |
| 208 | def main(): |
| 209 | """Run all tests""" |
| 210 | print("\n") |
| 211 | print("╔" + "=" * 58 + "╗") |
| 212 | print("║" + " " * 10 + "Object Primitive System REST API Tests" + " " * 9 + "║") |
| 213 | print("╚" + "=" * 58 + "╝") |
| 214 | |
| 215 | try: |
| 216 | # Test listing objects |
| 217 | objects = test_list_objects() |
| 218 | |
| 219 | # Test counter operations |
| 220 | test_execute_counter() |
| 221 | test_get_metadata() |
| 222 | test_get_logs() |
| 223 | test_get_source() |
| 224 | test_post_reset_counter() |
| 225 | |
| 226 | # Test calculator |
| 227 | test_calculator() |
| 228 | |
| 229 | # Test error handling |
| 230 | test_error_handling() |
| 231 | |
| 232 | print("\n" + "=" * 60) |
| 233 | print("ALL TESTS PASSED! ✅") |
| 234 | print("=" * 60) |
| 235 | print() |
| 236 | |
| 237 | except requests.exceptions.ConnectionError: |
| 238 | print("\n❌ ERROR: Could not connect to server") |
| 239 | print("Make sure the server is running: python run_server.py") |
| 240 | print() |
| 241 | exit(1) |
| 242 | |
| 243 | except AssertionError as e: |
| 244 | print(f"\n❌ TEST FAILED: {e}") |
| 245 | print() |
| 246 | exit(1) |
| 247 | |
| 248 | except Exception as e: |
| 249 | print(f"\n❌ UNEXPECTED ERROR: {e}") |
| 250 | import traceback |
| 251 | traceback.print_exc() |
| 252 | print() |
| 253 | exit(1) |
| 254 | |
| 255 | |
| 256 | if __name__ == "__main__": |
| 257 | main() |