askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
test_endpoint_loader.py · 9.9 KB · 275 lines
| 1 | """ |
| 2 | Unit tests for endpoint_loader |
| 3 | |
| 4 | Test-Driven Development: These tests are written FIRST, before implementation. |
| 5 | They define the expected behavior of the endpoint loader. |
| 6 | """ |
| 7 | |
| 8 | import pytest |
| 9 | import os |
| 10 | from pathlib import Path |
| 11 | |
| 12 | |
| 13 | class TestEndpointLoaderBasics: |
| 14 | """Test basic endpoint loading functionality""" |
| 15 | |
| 16 | def test_import_endpoint_module(self): |
| 17 | """Should import an endpoint file as a Python module""" |
| 18 | from dbbasic_object_core.core.endpoint_loader import load_endpoint |
| 19 | |
| 20 | # Load the hello endpoint fixture |
| 21 | endpoint_path = Path(__file__).parent.parent / 'fixtures' / 'endpoints' / 'hello.py' |
| 22 | endpoint = load_endpoint(endpoint_path) |
| 23 | |
| 24 | assert endpoint is not None |
| 25 | assert hasattr(endpoint, 'GET') |
| 26 | assert hasattr(endpoint, 'POST') |
| 27 | # hello.py only has GET and POST, not PUT/DELETE |
| 28 | |
| 29 | def test_load_endpoint_by_string_path(self): |
| 30 | """Should accept path as string""" |
| 31 | from dbbasic_object_core.core.endpoint_loader import load_endpoint |
| 32 | |
| 33 | endpoint_path = str(Path(__file__).parent.parent / 'fixtures' / 'endpoints' / 'hello.py') |
| 34 | endpoint = load_endpoint(endpoint_path) |
| 35 | |
| 36 | assert endpoint is not None |
| 37 | |
| 38 | def test_load_nonexistent_endpoint_raises_error(self): |
| 39 | """Should raise error when endpoint file doesn't exist""" |
| 40 | from dbbasic_object_core.core.endpoint_loader import load_endpoint, EndpointNotFoundError |
| 41 | |
| 42 | with pytest.raises(EndpointNotFoundError): |
| 43 | load_endpoint('/nonexistent/path/to/endpoint.py') |
| 44 | |
| 45 | def test_load_invalid_python_raises_error(self): |
| 46 | """Should raise error when endpoint file has syntax errors""" |
| 47 | from dbbasic_object_core.core.endpoint_loader import load_endpoint, EndpointLoadError |
| 48 | |
| 49 | # Create a temporary file with invalid Python |
| 50 | import tempfile |
| 51 | with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: |
| 52 | f.write('def invalid syntax here\n') |
| 53 | temp_path = f.name |
| 54 | |
| 55 | try: |
| 56 | with pytest.raises(EndpointLoadError): |
| 57 | load_endpoint(temp_path) |
| 58 | finally: |
| 59 | os.unlink(temp_path) |
| 60 | |
| 61 | |
| 62 | class TestEndpointExecution: |
| 63 | """Test executing endpoint methods""" |
| 64 | |
| 65 | def test_execute_get_method(self): |
| 66 | """Should execute GET method and return result""" |
| 67 | from dbbasic_object_core.core.endpoint_loader import load_endpoint, execute_endpoint |
| 68 | |
| 69 | endpoint_path = Path(__file__).parent.parent / 'fixtures' / 'endpoints' / 'hello.py' |
| 70 | endpoint = load_endpoint(endpoint_path) |
| 71 | |
| 72 | result = execute_endpoint(endpoint, 'GET', {}) |
| 73 | |
| 74 | assert result['status'] == 'ok' |
| 75 | assert result['message'] == 'Hello, World!' |
| 76 | assert result['method'] == 'GET' |
| 77 | |
| 78 | def test_execute_post_method(self): |
| 79 | """Should execute POST method with data""" |
| 80 | from dbbasic_object_core.core.endpoint_loader import load_endpoint, execute_endpoint |
| 81 | |
| 82 | endpoint_path = Path(__file__).parent.parent / 'fixtures' / 'endpoints' / 'hello.py' |
| 83 | endpoint = load_endpoint(endpoint_path) |
| 84 | |
| 85 | result = execute_endpoint(endpoint, 'POST', {'name': 'Claude'}) |
| 86 | |
| 87 | assert result['status'] == 'ok' |
| 88 | assert result['message'] == 'Hello, Claude!' |
| 89 | assert result['method'] == 'POST' |
| 90 | |
| 91 | def test_execute_unsupported_method_raises_error(self): |
| 92 | """Should raise error for unsupported HTTP methods""" |
| 93 | from dbbasic_object_core.core.endpoint_loader import load_endpoint, execute_endpoint, MethodNotSupportedError |
| 94 | |
| 95 | endpoint_path = Path(__file__).parent.parent / 'fixtures' / 'endpoints' / 'hello.py' |
| 96 | endpoint = load_endpoint(endpoint_path) |
| 97 | |
| 98 | with pytest.raises(MethodNotSupportedError): |
| 99 | execute_endpoint(endpoint, 'PATCH', {}) |
| 100 | |
| 101 | def test_execute_method_that_raises_exception(self): |
| 102 | """Should catch and wrap exceptions from endpoint code""" |
| 103 | from dbbasic_object_core.core.endpoint_loader import load_endpoint, execute_endpoint, EndpointExecutionError |
| 104 | |
| 105 | # Create a temporary endpoint that raises an error |
| 106 | import tempfile |
| 107 | with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: |
| 108 | f.write(''' |
| 109 | def GET(request): |
| 110 | raise ValueError("Something went wrong") |
| 111 | ''') |
| 112 | temp_path = f.name |
| 113 | |
| 114 | try: |
| 115 | endpoint = load_endpoint(temp_path) |
| 116 | with pytest.raises(EndpointExecutionError) as exc_info: |
| 117 | execute_endpoint(endpoint, 'GET', {}) |
| 118 | |
| 119 | # Should include the original exception |
| 120 | assert 'ValueError' in str(exc_info.value) |
| 121 | assert 'Something went wrong' in str(exc_info.value) |
| 122 | finally: |
| 123 | os.unlink(temp_path) |
| 124 | |
| 125 | |
| 126 | class TestEndpointMetadata: |
| 127 | """Test reading endpoint metadata""" |
| 128 | |
| 129 | def test_read_endpoint_metadata(self): |
| 130 | """Should read __endpoint__ metadata from endpoint""" |
| 131 | from dbbasic_object_core.core.endpoint_loader import load_endpoint, get_endpoint_metadata |
| 132 | |
| 133 | endpoint_path = Path(__file__).parent.parent / 'fixtures' / 'endpoints' / 'hello.py' |
| 134 | endpoint = load_endpoint(endpoint_path) |
| 135 | |
| 136 | metadata = get_endpoint_metadata(endpoint) |
| 137 | |
| 138 | assert metadata['name'] == 'hello' |
| 139 | assert metadata['description'] == 'Simple hello world endpoint for testing' |
| 140 | assert metadata['version'] == '1.0.0' |
| 141 | assert metadata['author'] == 'test' |
| 142 | |
| 143 | def test_endpoint_without_metadata_returns_defaults(self): |
| 144 | """Should return default metadata if __endpoint__ not present""" |
| 145 | from dbbasic_object_core.core.endpoint_loader import load_endpoint, get_endpoint_metadata |
| 146 | |
| 147 | # Create a temporary endpoint without metadata |
| 148 | import tempfile |
| 149 | with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: |
| 150 | f.write('def GET(request): return {}') |
| 151 | temp_path = f.name |
| 152 | |
| 153 | try: |
| 154 | endpoint = load_endpoint(temp_path) |
| 155 | metadata = get_endpoint_metadata(endpoint) |
| 156 | |
| 157 | assert 'name' in metadata |
| 158 | assert 'version' in metadata |
| 159 | finally: |
| 160 | os.unlink(temp_path) |
| 161 | |
| 162 | |
| 163 | class TestEndpointValidation: |
| 164 | """Test validation of calculator endpoint""" |
| 165 | |
| 166 | def test_calculator_add_operation(self): |
| 167 | """Should perform addition correctly""" |
| 168 | from dbbasic_object_core.core.endpoint_loader import load_endpoint, execute_endpoint |
| 169 | |
| 170 | endpoint_path = Path(__file__).parent.parent / 'fixtures' / 'endpoints' / 'calculator.py' |
| 171 | endpoint = load_endpoint(endpoint_path) |
| 172 | |
| 173 | result = execute_endpoint(endpoint, 'POST', { |
| 174 | 'operation': 'add', |
| 175 | 'a': 5, |
| 176 | 'b': 3, |
| 177 | }) |
| 178 | |
| 179 | assert result['status'] == 'ok' |
| 180 | assert result['result'] == 8 |
| 181 | |
| 182 | def test_calculator_division_by_zero(self): |
| 183 | """Should handle division by zero gracefully""" |
| 184 | from dbbasic_object_core.core.endpoint_loader import load_endpoint, execute_endpoint |
| 185 | |
| 186 | endpoint_path = Path(__file__).parent.parent / 'fixtures' / 'endpoints' / 'calculator.py' |
| 187 | endpoint = load_endpoint(endpoint_path) |
| 188 | |
| 189 | result = execute_endpoint(endpoint, 'POST', { |
| 190 | 'operation': 'divide', |
| 191 | 'a': 10, |
| 192 | 'b': 0, |
| 193 | }) |
| 194 | |
| 195 | assert result['status'] == 'error' |
| 196 | assert 'Division by zero' in result['error'] |
| 197 | |
| 198 | def test_calculator_invalid_operation(self): |
| 199 | """Should reject invalid operations""" |
| 200 | from dbbasic_object_core.core.endpoint_loader import load_endpoint, execute_endpoint |
| 201 | |
| 202 | endpoint_path = Path(__file__).parent.parent / 'fixtures' / 'endpoints' / 'calculator.py' |
| 203 | endpoint = load_endpoint(endpoint_path) |
| 204 | |
| 205 | result = execute_endpoint(endpoint, 'POST', { |
| 206 | 'operation': 'power', |
| 207 | 'a': 2, |
| 208 | 'b': 3, |
| 209 | }) |
| 210 | |
| 211 | assert result['status'] == 'error' |
| 212 | assert 'Invalid operation' in result['error'] |
| 213 | |
| 214 | def test_calculator_missing_parameters(self): |
| 215 | """Should reject missing parameters""" |
| 216 | from dbbasic_object_core.core.endpoint_loader import load_endpoint, execute_endpoint |
| 217 | |
| 218 | endpoint_path = Path(__file__).parent.parent / 'fixtures' / 'endpoints' / 'calculator.py' |
| 219 | endpoint = load_endpoint(endpoint_path) |
| 220 | |
| 221 | result = execute_endpoint(endpoint, 'POST', { |
| 222 | 'operation': 'add', |
| 223 | 'a': 5, |
| 224 | }) |
| 225 | |
| 226 | assert result['status'] == 'error' |
| 227 | assert 'Missing parameters' in result['error'] |
| 228 | |
| 229 | |
| 230 | class TestEndpointCaching: |
| 231 | """Test caching of loaded endpoints""" |
| 232 | |
| 233 | def test_cache_loaded_endpoints(self): |
| 234 | """Should cache endpoints to avoid reloading""" |
| 235 | from dbbasic_object_core.core.endpoint_loader import load_endpoint, clear_cache, get_cache_stats |
| 236 | |
| 237 | clear_cache() |
| 238 | |
| 239 | endpoint_path = Path(__file__).parent.parent / 'fixtures' / 'endpoints' / 'hello.py' |
| 240 | |
| 241 | # First load |
| 242 | endpoint1 = load_endpoint(endpoint_path) |
| 243 | stats1 = get_cache_stats() |
| 244 | |
| 245 | # Second load (should hit cache) |
| 246 | endpoint2 = load_endpoint(endpoint_path) |
| 247 | stats2 = get_cache_stats() |
| 248 | |
| 249 | # Should be the same object (from cache) |
| 250 | assert endpoint1 is endpoint2 |
| 251 | assert stats2['hits'] > stats1['hits'] |
| 252 | |
| 253 | def test_clear_cache(self): |
| 254 | """Should clear the endpoint cache""" |
| 255 | from dbbasic_object_core.core.endpoint_loader import load_endpoint, clear_cache, get_cache_stats |
| 256 | |
| 257 | endpoint_path = Path(__file__).parent.parent / 'fixtures' / 'endpoints' / 'hello.py' |
| 258 | load_endpoint(endpoint_path) |
| 259 | |
| 260 | clear_cache() |
| 261 | stats = get_cache_stats() |
| 262 | |
| 263 | assert stats['size'] == 0 |
| 264 | |
| 265 | def test_reload_endpoint_bypasses_cache(self): |
| 266 | """Should reload endpoint when reload=True""" |
| 267 | from dbbasic_object_core.core.endpoint_loader import load_endpoint |
| 268 | |
| 269 | endpoint_path = Path(__file__).parent.parent / 'fixtures' / 'endpoints' / 'hello.py' |
| 270 | |
| 271 | endpoint1 = load_endpoint(endpoint_path) |
| 272 | endpoint2 = load_endpoint(endpoint_path, reload=True) |
| 273 | |
| 274 | # Should be different objects (reloaded) |
| 275 | assert endpoint1 is not endpoint2 |