Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_object_execution.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_object_execution.py · 7.2 KB · 228 lines
1from pathlib import Path
2
3import pytest
4
5import object_correlation
6import object_execution
7
8
9class FakeObject:
10 def __init__(self, output=None):
11 self.output = {"status": "ok"} if output is None else output
12 self.calls = []
13
14 def execute(self, method, payload):
15 self.calls.append((method, payload))
16 return self.output
17
18
19class FailingObject(FakeObject):
20 def execute(self, method, payload):
21 self.calls.append((method, payload))
22 raise RuntimeError("target failed")
23
24
25class FakeRuntime:
26 def __init__(self, obj=None):
27 self.obj = FakeObject() if obj is None else obj
28 self.loaded = []
29
30 def load_object(self, path, object_id=None):
31 self.loaded.append((Path(path), object_id))
32 return self.obj
33
34
35def write_object(path: Path):
36 path.parent.mkdir(parents=True, exist_ok=True)
37 path.write_text("def POST(request):\n return {'status': 'ok'}\n")
38 return path
39
40
41def test_execute_object_success_returns_structured_result(tmp_path):
42 root = tmp_path / "objects"
43 source = write_object(root / "basics" / "counter.py")
44 obj = FakeObject(output={"count": 1})
45 runtime = FakeRuntime(obj)
46
47 request = object_execution.ObjectExecutionRequest(
48 object_id="basics_counter",
49 method="post",
50 payload={"step": 1},
51 )
52 result = object_execution.execute_object(runtime, request, roots=[root])
53
54 assert result.ok
55 assert result.object_id == "basics_counter"
56 assert result.method == "POST"
57 assert result.path == source
58 assert result.result == {"count": 1}
59 assert result.error is None
60 assert result.started_at.endswith("Z")
61 assert result.finished_at.endswith("Z")
62 assert result.duration_ms >= 0
63 assert runtime.loaded == [(source, "basics_counter")]
64 assert obj.calls == [("POST", {"step": 1})]
65
66
67def test_execute_object_serializes_result_to_dict(tmp_path):
68 root = tmp_path / "objects"
69 source = write_object(root / "basics" / "counter.py")
70 runtime = FakeRuntime(FakeObject(output={"count": 1}))
71
72 result = object_execution.execute_object(
73 runtime,
74 object_execution.ObjectExecutionRequest("basics_counter"),
75 roots=[root],
76 )
77
78 assert result.to_dict()["path"] == str(source)
79 assert result.to_dict()["ok"] is True
80 assert result.to_dict()["result"] == {"count": 1}
81 assert result.to_dict()["error"] is None
82
83
84def test_execute_object_preserves_correlation_id(tmp_path):
85 root = tmp_path / "objects"
86 source = write_object(root / "basics" / "counter.py")
87 runtime = FakeRuntime(FakeObject(output={"count": 1}))
88 correlation_id = "123e4567-e89b-42d3-a456-426614174000"
89
90 result = object_execution.execute_object(
91 runtime,
92 object_execution.ObjectExecutionRequest(
93 "basics_counter",
94 correlation_id=correlation_id,
95 ),
96 roots=[root],
97 )
98
99 assert result.ok
100 assert result.path == source
101 assert result.correlation_id == correlation_id
102 assert result.to_dict()["correlation_id"] == correlation_id
103 assert object_correlation.current_correlation_id() is None
104
105
106def test_execute_object_missing_source_returns_error_without_loading(tmp_path):
107 root = tmp_path / "objects"
108 runtime = FakeRuntime()
109
110 result = object_execution.execute_object(
111 runtime,
112 object_execution.ObjectExecutionRequest("missing_object"),
113 roots=[root],
114 )
115
116 assert not result.ok
117 assert result.result is None
118 assert result.path is None
119 assert result.error is not None
120 assert result.error.type == "ObjectNotFoundError"
121 assert result.error.message == "Object not found: missing_object"
122 assert result.error.traceback == ""
123 assert runtime.loaded == []
124
125
126def test_execute_object_exception_captures_error_and_traceback(tmp_path):
127 root = tmp_path / "objects"
128 source = write_object(root / "basics" / "counter.py")
129 runtime = FakeRuntime(FailingObject())
130
131 result = object_execution.execute_object(
132 runtime,
133 object_execution.ObjectExecutionRequest("basics_counter", method="GET"),
134 roots=[root],
135 )
136
137 assert not result.ok
138 assert result.path == source
139 assert result.error is not None
140 assert result.error.type == "RuntimeError"
141 assert result.error.message == "target failed"
142 assert "RuntimeError: target failed" in result.error.traceback
143 assert "test_object_execution.py" in result.error.traceback
144
145
146def test_execute_object_can_raise_failure_for_existing_daemon_style_calls(tmp_path):
147 root = tmp_path / "objects"
148 write_object(root / "basics" / "counter.py")
149 runtime = FakeRuntime(FailingObject())
150
151 with pytest.raises(object_execution.ObjectExecutionFailure) as exc:
152 object_execution.execute_object(
153 runtime,
154 object_execution.ObjectExecutionRequest("basics_counter"),
155 roots=[root],
156 raise_on_error=True,
157 )
158
159 assert str(exc.value) == "target failed"
160 assert exc.value.execution_result.error is not None
161 assert exc.value.execution_result.error.type == "RuntimeError"
162
163
164def test_execution_request_defaults_to_get_with_empty_payload(tmp_path):
165 root = tmp_path / "objects"
166 write_object(root / "basics" / "counter.py")
167 obj = FakeObject()
168 runtime = FakeRuntime(obj)
169
170 result = object_execution.execute_object(
171 runtime,
172 object_execution.ObjectExecutionRequest("basics_counter", method=""),
173 roots=[root],
174 )
175
176 assert result.ok
177 assert obj.calls == [("GET", {})]
178
179
180def test_execute_object_can_use_explicit_path(tmp_path):
181 source = write_object(tmp_path / "detached" / "counter.py")
182 runtime = FakeRuntime()
183
184 result = object_execution.execute_object(
185 runtime,
186 object_execution.ObjectExecutionRequest("detached_counter", path=source),
187 roots=[tmp_path / "objects"],
188 )
189
190 assert result.ok
191 assert result.path == source
192 assert runtime.loaded == [(source, "detached_counter")]
193
194
195def test_execute_python_object_subprocess_returns_success(tmp_path):
196 root = tmp_path / "objects"
197 source = root / "basics" / "worker.py"
198 source.parent.mkdir(parents=True, exist_ok=True)
199 source.write_text("def GET(request):\n return {'value': request['value']}\n")
200
201 result = object_execution.execute_python_object_subprocess(
202 object_execution.ObjectExecutionRequest("basics_worker", payload={"value": 7}),
203 roots=[root],
204 timeout_seconds=2,
205 )
206
207 assert result.ok
208 assert result.path == source
209 assert result.result == {"value": 7}
210
211
212def test_execute_python_object_subprocess_returns_timeout(tmp_path):
213 root = tmp_path / "objects"
214 source = root / "basics" / "slow.py"
215 source.parent.mkdir(parents=True, exist_ok=True)
216 source.write_text("import time\n\ndef GET(request):\n time.sleep(5)\n return {'ok': True}\n")
217
218 result = object_execution.execute_python_object_subprocess(
219 object_execution.ObjectExecutionRequest("basics_slow"),
220 roots=[root],
221 timeout_seconds=0.5,
222 )
223
224 assert not result.ok
225 assert result.path == source
226 assert result.error is not None
227 assert result.error.type == object_execution.TIMEOUT_ERROR_TYPE
228 assert result.error.message == "GET timed out for object basics_slow after 0.5 seconds"