askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
test_object_runtime.py · 10.5 KB · 331 lines
| 1 | """ |
| 2 | Unit tests for Object Runtime |
| 3 | |
| 4 | Test-Driven Development: These tests are written FIRST, before implementation. |
| 5 | They define the expected behavior of the integrated Object Runtime. |
| 6 | |
| 7 | Object Runtime: |
| 8 | - Loads endpoints |
| 9 | - Injects logger and state manager |
| 10 | - Executes methods |
| 11 | - Logs all operations |
| 12 | - Versions code changes |
| 13 | - Provides introspection |
| 14 | """ |
| 15 | |
| 16 | import pytest |
| 17 | import tempfile |
| 18 | import shutil |
| 19 | from pathlib import Path |
| 20 | |
| 21 | |
| 22 | class TestObjectRuntimeBasics: |
| 23 | """Test basic object runtime functionality""" |
| 24 | |
| 25 | def setup_method(self): |
| 26 | """Create temporary directory for each test""" |
| 27 | self.temp_dir = Path(tempfile.mkdtemp()) |
| 28 | |
| 29 | def teardown_method(self): |
| 30 | """Clean up temporary directory""" |
| 31 | if self.temp_dir.exists(): |
| 32 | shutil.rmtree(self.temp_dir) |
| 33 | |
| 34 | def test_load_object(self): |
| 35 | """Should load an endpoint as an object""" |
| 36 | from dbbasic_object_core.runtime.object_runtime import ObjectRuntime |
| 37 | |
| 38 | runtime = ObjectRuntime(base_dir=self.temp_dir) |
| 39 | |
| 40 | # Load the hello endpoint |
| 41 | obj = runtime.load_object('tests/fixtures/endpoints/hello.py') |
| 42 | |
| 43 | assert obj is not None |
| 44 | assert obj.object_id == 'hello' |
| 45 | |
| 46 | def test_execute_object_method(self): |
| 47 | """Should execute object methods""" |
| 48 | from dbbasic_object_core.runtime.object_runtime import ObjectRuntime |
| 49 | |
| 50 | runtime = ObjectRuntime(base_dir=self.temp_dir) |
| 51 | obj = runtime.load_object('tests/fixtures/endpoints/hello.py') |
| 52 | |
| 53 | # Execute GET method |
| 54 | result = obj.execute('GET', {}) |
| 55 | |
| 56 | assert result['status'] == 'ok' |
| 57 | assert 'Hello' in result['message'] |
| 58 | |
| 59 | def test_object_logs_to_itself(self): |
| 60 | """Should automatically log all method executions""" |
| 61 | from dbbasic_object_core.runtime.object_runtime import ObjectRuntime |
| 62 | |
| 63 | runtime = ObjectRuntime(base_dir=self.temp_dir) |
| 64 | obj = runtime.load_object('tests/fixtures/endpoints/hello.py') |
| 65 | |
| 66 | # Execute method |
| 67 | obj.execute('GET', {'user_id': 'user-123'}) |
| 68 | |
| 69 | # Check logs |
| 70 | logs = obj.get_logs() |
| 71 | assert len(logs) >= 1 |
| 72 | |
| 73 | # Should have logged the execution (check INFO level logs) |
| 74 | info_logs = [log for log in logs if log['level'] == 'INFO'] |
| 75 | assert len(info_logs) >= 1 |
| 76 | |
| 77 | execution_log = info_logs[0] |
| 78 | assert execution_log['method'] == 'GET' |
| 79 | assert execution_log['user_id'] == 'user-123' |
| 80 | |
| 81 | def test_object_has_state(self): |
| 82 | """Should provide state management to objects""" |
| 83 | from dbbasic_object_core.runtime.object_runtime import ObjectRuntime |
| 84 | |
| 85 | runtime = ObjectRuntime(base_dir=self.temp_dir) |
| 86 | |
| 87 | # Load counter endpoint (uses state) |
| 88 | obj = runtime.load_object('examples/basics/counter.py') |
| 89 | |
| 90 | # Execute GET (increments counter) |
| 91 | result1 = obj.execute('GET', {}) |
| 92 | result2 = obj.execute('GET', {}) |
| 93 | result3 = obj.execute('GET', {}) |
| 94 | |
| 95 | # Counter should increment |
| 96 | assert result1['count'] == 1 |
| 97 | assert result2['count'] == 2 |
| 98 | assert result3['count'] == 3 |
| 99 | |
| 100 | def test_object_state_persists(self): |
| 101 | """Should persist state across reloads""" |
| 102 | from dbbasic_object_core.runtime.object_runtime import ObjectRuntime |
| 103 | |
| 104 | runtime = ObjectRuntime(base_dir=self.temp_dir) |
| 105 | obj = runtime.load_object('examples/basics/counter.py') |
| 106 | |
| 107 | # Increment counter |
| 108 | obj.execute('GET', {}) |
| 109 | obj.execute('GET', {}) |
| 110 | |
| 111 | # Reload object (simulate restart) |
| 112 | obj2 = runtime.load_object('examples/basics/counter.py') |
| 113 | result = obj2.execute('GET', {}) |
| 114 | |
| 115 | # Should continue from where we left off |
| 116 | assert result['count'] == 3 |
| 117 | |
| 118 | |
| 119 | class TestObjectVersioning: |
| 120 | """Test automatic versioning of object code""" |
| 121 | |
| 122 | def setup_method(self): |
| 123 | """Create temporary directory for each test""" |
| 124 | self.temp_dir = Path(tempfile.mkdtemp()) |
| 125 | |
| 126 | def teardown_method(self): |
| 127 | """Clean up temporary directory""" |
| 128 | if self.temp_dir.exists(): |
| 129 | shutil.rmtree(self.temp_dir) |
| 130 | |
| 131 | def test_save_version_on_modification(self): |
| 132 | """Should automatically version code when modified""" |
| 133 | from dbbasic_object_core.runtime.object_runtime import ObjectRuntime |
| 134 | import shutil |
| 135 | |
| 136 | # Copy hello.py to temp dir |
| 137 | hello_src = Path('tests/fixtures/endpoints/hello.py') |
| 138 | hello_temp = self.temp_dir / 'hello.py' |
| 139 | shutil.copy(hello_src, hello_temp) |
| 140 | |
| 141 | runtime = ObjectRuntime(base_dir=self.temp_dir) |
| 142 | obj = runtime.load_object(str(hello_temp)) |
| 143 | |
| 144 | # Modify the object's code |
| 145 | new_code = """ |
| 146 | def GET(request): |
| 147 | return {'status': 'ok', 'message': 'Modified!'} |
| 148 | |
| 149 | __endpoint__ = {'name': 'hello', 'version': '2.0.0'} |
| 150 | """ |
| 151 | obj.update_code(new_code, author='test_user', message='Updated hello') |
| 152 | |
| 153 | # Should have created a version |
| 154 | history = obj.get_version_history() |
| 155 | assert len(history) >= 1 |
| 156 | |
| 157 | def test_rollback_to_previous_version(self): |
| 158 | """Should be able to rollback to previous version""" |
| 159 | from dbbasic_object_core.runtime.object_runtime import ObjectRuntime |
| 160 | import shutil as sh |
| 161 | |
| 162 | # Copy hello.py to temp dir |
| 163 | hello_src = Path('tests/fixtures/endpoints/hello.py') |
| 164 | hello_temp = self.temp_dir / 'hello.py' |
| 165 | sh.copy(hello_src, hello_temp) |
| 166 | |
| 167 | runtime = ObjectRuntime(base_dir=self.temp_dir) |
| 168 | obj = runtime.load_object(str(hello_temp)) |
| 169 | |
| 170 | # Execute original |
| 171 | result1 = obj.execute('GET', {}) |
| 172 | original_message = result1['message'] |
| 173 | |
| 174 | # Modify code |
| 175 | new_code = """ |
| 176 | def GET(request): |
| 177 | return {'status': 'ok', 'message': 'MODIFIED VERSION'} |
| 178 | |
| 179 | __endpoint__ = {'name': 'hello', 'version': '2.0.0'} |
| 180 | """ |
| 181 | obj.update_code(new_code, author='test', message='Modification') |
| 182 | |
| 183 | # Verify modification worked |
| 184 | result2 = obj.execute('GET', {}) |
| 185 | assert result2['message'] == 'MODIFIED VERSION' |
| 186 | |
| 187 | # Rollback to version 1 |
| 188 | obj.rollback_to_version(1, author='test', message='Rollback') |
| 189 | |
| 190 | # Should execute original code |
| 191 | result3 = obj.execute('GET', {}) |
| 192 | assert result3['message'] == original_message |
| 193 | |
| 194 | |
| 195 | class TestObjectIntrospection: |
| 196 | """Test object introspection capabilities""" |
| 197 | |
| 198 | def setup_method(self): |
| 199 | """Create temporary directory for each test""" |
| 200 | self.temp_dir = Path(tempfile.mkdtemp()) |
| 201 | |
| 202 | def teardown_method(self): |
| 203 | """Clean up temporary directory""" |
| 204 | if self.temp_dir.exists(): |
| 205 | shutil.rmtree(self.temp_dir) |
| 206 | |
| 207 | def test_get_object_metadata(self): |
| 208 | """Should provide object metadata""" |
| 209 | from dbbasic_object_core.runtime.object_runtime import ObjectRuntime |
| 210 | |
| 211 | runtime = ObjectRuntime(base_dir=self.temp_dir) |
| 212 | obj = runtime.load_object('tests/fixtures/endpoints/hello.py') |
| 213 | |
| 214 | metadata = obj.get_metadata() |
| 215 | |
| 216 | assert metadata['name'] == 'hello' |
| 217 | assert 'description' in metadata |
| 218 | assert 'version' in metadata |
| 219 | |
| 220 | def test_get_object_source_code(self): |
| 221 | """Should return object's source code""" |
| 222 | from dbbasic_object_core.runtime.object_runtime import ObjectRuntime |
| 223 | |
| 224 | runtime = ObjectRuntime(base_dir=self.temp_dir) |
| 225 | obj = runtime.load_object('tests/fixtures/endpoints/hello.py') |
| 226 | |
| 227 | source = obj.get_source_code() |
| 228 | |
| 229 | assert 'def GET(request):' in source |
| 230 | assert '__endpoint__' in source |
| 231 | |
| 232 | def test_get_object_logs(self): |
| 233 | """Should return object's logs""" |
| 234 | from dbbasic_object_core.runtime.object_runtime import ObjectRuntime |
| 235 | |
| 236 | runtime = ObjectRuntime(base_dir=self.temp_dir) |
| 237 | obj = runtime.load_object('tests/fixtures/endpoints/hello.py') |
| 238 | |
| 239 | # Execute to generate logs |
| 240 | obj.execute('GET', {'user_id': 'user-123'}) |
| 241 | obj.execute('POST', {'name': 'Alice'}) |
| 242 | |
| 243 | logs = obj.get_logs() |
| 244 | |
| 245 | assert len(logs) >= 2 |
| 246 | assert any(log['method'] == 'GET' for log in logs) |
| 247 | assert any(log['method'] == 'POST' for log in logs) |
| 248 | |
| 249 | def test_get_object_state(self): |
| 250 | """Should return object's current state""" |
| 251 | from dbbasic_object_core.runtime.object_runtime import ObjectRuntime |
| 252 | |
| 253 | runtime = ObjectRuntime(base_dir=self.temp_dir) |
| 254 | obj = runtime.load_object('examples/basics/counter.py') |
| 255 | |
| 256 | # Increment counter |
| 257 | obj.execute('GET', {}) |
| 258 | obj.execute('GET', {}) |
| 259 | |
| 260 | state = obj.get_state() |
| 261 | |
| 262 | assert 'count' in state |
| 263 | assert state['count'] == 2 |
| 264 | |
| 265 | |
| 266 | class TestObjectIsolation: |
| 267 | """Test that objects are properly isolated""" |
| 268 | |
| 269 | def setup_method(self): |
| 270 | """Create temporary directory for each test""" |
| 271 | self.temp_dir = Path(tempfile.mkdtemp()) |
| 272 | |
| 273 | def teardown_method(self): |
| 274 | """Clean up temporary directory""" |
| 275 | if self.temp_dir.exists(): |
| 276 | shutil.rmtree(self.temp_dir) |
| 277 | |
| 278 | def test_objects_have_separate_logs(self): |
| 279 | """Should keep logs separate for different objects""" |
| 280 | from dbbasic_object_core.runtime.object_runtime import ObjectRuntime |
| 281 | |
| 282 | runtime = ObjectRuntime(base_dir=self.temp_dir) |
| 283 | |
| 284 | obj1 = runtime.load_object('tests/fixtures/endpoints/hello.py') |
| 285 | obj2 = runtime.load_object('tests/fixtures/endpoints/calculator.py') |
| 286 | |
| 287 | # Execute both |
| 288 | obj1.execute('GET', {}) |
| 289 | obj2.execute('POST', {'operation': 'add', 'a': 1, 'b': 2}) |
| 290 | |
| 291 | # Logs should be separate |
| 292 | logs1 = obj1.get_logs() |
| 293 | logs2 = obj2.get_logs() |
| 294 | |
| 295 | assert len(logs1) >= 1 |
| 296 | assert len(logs2) >= 1 |
| 297 | assert logs1[0] != logs2[0] # Different logs |
| 298 | |
| 299 | def test_objects_have_separate_state(self): |
| 300 | """Should keep state separate for different counter instances""" |
| 301 | from dbbasic_object_core.runtime.object_runtime import ObjectRuntime |
| 302 | |
| 303 | runtime = ObjectRuntime(base_dir=self.temp_dir) |
| 304 | |
| 305 | # Load counter twice (simulate two different counter endpoints) |
| 306 | # We'll need to copy the file with different names |
| 307 | import shutil as sh |
| 308 | counter_src = Path('examples/basics/counter.py') |
| 309 | counter1 = self.temp_dir / 'counter1.py' |
| 310 | counter2 = self.temp_dir / 'counter2.py' |
| 311 | |
| 312 | sh.copy(counter_src, counter1) |
| 313 | sh.copy(counter_src, counter2) |
| 314 | |
| 315 | obj1 = runtime.load_object(str(counter1)) |
| 316 | obj2 = runtime.load_object(str(counter2)) |
| 317 | |
| 318 | # Increment obj1 three times |
| 319 | obj1.execute('GET', {}) |
| 320 | obj1.execute('GET', {}) |
| 321 | obj1.execute('GET', {}) |
| 322 | |
| 323 | # Increment obj2 once |
| 324 | obj2.execute('GET', {}) |
| 325 | |
| 326 | # Should have different counts |
| 327 | state1 = obj1.get_state() |
| 328 | state2 = obj2.get_state() |
| 329 | |
| 330 | assert state1['count'] == 3 |
| 331 | assert state2['count'] == 1 |