askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_python_object_runtime.py · 5.0 KB · 149 lines
| 1 | import pytest |
| 2 | |
| 3 | import object_logs |
| 4 | import object_state |
| 5 | import python_object_runtime |
| 6 | |
| 7 | |
| 8 | def write_source(path, content): |
| 9 | path.parent.mkdir(parents=True, exist_ok=True) |
| 10 | path.write_text(content) |
| 11 | return path |
| 12 | |
| 13 | |
| 14 | def test_python_object_runtime_loads_and_executes_method(tmp_path): |
| 15 | source = write_source( |
| 16 | tmp_path / "objects" / "basics" / "counter.py", |
| 17 | "def GET(request):\n return {'count': int(request['count']) + 1}\n", |
| 18 | ) |
| 19 | runtime = python_object_runtime.PythonObjectRuntime() |
| 20 | |
| 21 | obj = runtime.load_object(source, object_id="basics_counter") |
| 22 | |
| 23 | assert obj.execute("GET", {"count": "1"}) == {"count": 2} |
| 24 | |
| 25 | |
| 26 | def test_python_object_runtime_normalizes_method_name(tmp_path): |
| 27 | source = write_source( |
| 28 | tmp_path / "objects" / "basics" / "counter.py", |
| 29 | "def GET(request):\n return {'ok': True}\n", |
| 30 | ) |
| 31 | runtime = python_object_runtime.PythonObjectRuntime() |
| 32 | |
| 33 | obj = runtime.load_object(source, object_id="basics_counter") |
| 34 | |
| 35 | assert obj.execute("get", {}) == {"ok": True} |
| 36 | |
| 37 | |
| 38 | def test_python_object_runtime_reports_missing_method(tmp_path): |
| 39 | source = write_source( |
| 40 | tmp_path / "objects" / "basics" / "counter.py", |
| 41 | "def POST(request):\n return {'ok': True}\n", |
| 42 | ) |
| 43 | runtime = python_object_runtime.PythonObjectRuntime() |
| 44 | |
| 45 | obj = runtime.load_object(source, object_id="basics_counter") |
| 46 | |
| 47 | with pytest.raises(python_object_runtime.MethodNotSupportedError) as exc: |
| 48 | obj.execute("GET", {}) |
| 49 | |
| 50 | assert "Method GET not supported by object basics_counter" in str(exc.value) |
| 51 | assert "Available methods: ['POST']" in str(exc.value) |
| 52 | |
| 53 | |
| 54 | def test_python_object_runtime_wraps_method_exception(tmp_path): |
| 55 | source = write_source( |
| 56 | tmp_path / "objects" / "basics" / "broken.py", |
| 57 | "def GET(request):\n raise RuntimeError('boom')\n", |
| 58 | ) |
| 59 | runtime = python_object_runtime.PythonObjectRuntime() |
| 60 | |
| 61 | obj = runtime.load_object(source, object_id="basics_broken") |
| 62 | |
| 63 | with pytest.raises(python_object_runtime.ObjectMethodExecutionError) as exc: |
| 64 | obj.execute("GET", {}) |
| 65 | |
| 66 | assert "GET failed for object basics_broken: RuntimeError: boom" in str(exc.value) |
| 67 | assert "Traceback" in str(exc.value) |
| 68 | |
| 69 | |
| 70 | def test_python_object_runtime_wraps_load_errors(tmp_path): |
| 71 | source = write_source( |
| 72 | tmp_path / "objects" / "basics" / "bad_syntax.py", |
| 73 | "def GET(request):\n return {\n", |
| 74 | ) |
| 75 | runtime = python_object_runtime.PythonObjectRuntime() |
| 76 | |
| 77 | with pytest.raises(python_object_runtime.ObjectLoadError) as exc: |
| 78 | runtime.load_object(source, object_id="basics_bad_syntax") |
| 79 | |
| 80 | assert "Failed to load object basics_bad_syntax" in str(exc.value) |
| 81 | assert "SyntaxError" in str(exc.value) |
| 82 | |
| 83 | |
| 84 | def test_python_object_runtime_loads_fresh_source_each_time(tmp_path): |
| 85 | source = write_source( |
| 86 | tmp_path / "objects" / "basics" / "counter.py", |
| 87 | "def GET(request):\n return {'version': 1}\n", |
| 88 | ) |
| 89 | runtime = python_object_runtime.PythonObjectRuntime() |
| 90 | |
| 91 | first = runtime.load_object(source, object_id="basics_counter") |
| 92 | source.write_text("def GET(request):\n return {'version': 2}\n") |
| 93 | second = runtime.load_object(source, object_id="basics_counter") |
| 94 | |
| 95 | assert first.execute("GET", {}) == {"version": 1} |
| 96 | assert second.execute("GET", {}) == {"version": 2} |
| 97 | |
| 98 | |
| 99 | def test_python_object_runtime_injects_state_manager(tmp_path): |
| 100 | source = write_source( |
| 101 | tmp_path / "objects" / "basics" / "counter.py", |
| 102 | "_state_manager = None\n" |
| 103 | "def GET(request):\n" |
| 104 | " count = _state_manager.get('count', 0) + 1\n" |
| 105 | " _state_manager.set('count', count)\n" |
| 106 | " return {'count': count}\n", |
| 107 | ) |
| 108 | runtime = python_object_runtime.PythonObjectRuntime(base_dir=tmp_path / "data") |
| 109 | |
| 110 | first = runtime.load_object(source, object_id="basics_counter") |
| 111 | assert first.execute("GET", {}) == {"count": 1} |
| 112 | |
| 113 | second = runtime.load_object(source, object_id="basics_counter") |
| 114 | assert second.execute("GET", {}) == {"count": 2} |
| 115 | assert object_state.get_object_state("basics_counter", base_dir=tmp_path / "data") == { |
| 116 | "count": 2 |
| 117 | } |
| 118 | |
| 119 | |
| 120 | def test_python_object_runtime_injects_logger(tmp_path): |
| 121 | source = write_source( |
| 122 | tmp_path / "objects" / "basics" / "counter.py", |
| 123 | "_logger = None\n" |
| 124 | "def GET(request):\n" |
| 125 | " _logger.info('Counter read', user_id=request.get('user_id'))\n" |
| 126 | " return {'ok': True}\n", |
| 127 | ) |
| 128 | runtime = python_object_runtime.PythonObjectRuntime(base_dir=tmp_path / "data") |
| 129 | |
| 130 | obj = runtime.load_object(source, object_id="basics_counter") |
| 131 | |
| 132 | assert obj.execute("GET", {"user_id": "user-1"}) == {"ok": True} |
| 133 | logs = object_logs.get_object_logs("basics_counter", base_dir=tmp_path / "data") |
| 134 | |
| 135 | assert logs == [ |
| 136 | { |
| 137 | "entry_id": logs[0]["entry_id"], |
| 138 | "correlation_id": "", |
| 139 | "timestamp": logs[0]["timestamp"], |
| 140 | "level": "INFO", |
| 141 | "message": "Counter read", |
| 142 | "method": "", |
| 143 | "status": "", |
| 144 | "duration_ms": "", |
| 145 | "error_type": "", |
| 146 | "error": "", |
| 147 | "user_id": "user-1", |
| 148 | } |
| 149 | ] |