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_events.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_events.py · 14.9 KB · 417 lines
1import json
2from uuid import UUID
3
4import pytest
5
6import object_events
7import object_state
8
9
10def test_publish_event_writes_daemon_compatible_state_and_lists_newest_first(
11 tmp_path,
12 monkeypatch,
13):
14 data_dir = tmp_path / "data"
15 timestamps = iter([100, 100, 101, 101])
16 monkeypatch.setattr(object_events.time, "time", lambda: next(timestamps))
17
18 first = object_events.publish_event(
19 "collection.record.created",
20 payload={"id": "c1", "nested": {"ok": True}},
21 source="records",
22 actor="admin",
23 base_dir=data_dir,
24 )
25 second = object_events.publish_event(
26 "collection.record.updated",
27 payload={"id": "c1"},
28 source="records",
29 actor="admin",
30 base_dir=data_dir,
31 )
32
33 state_file = data_dir / "state" / "events" / "state.tsv"
34 rows = state_file.read_text().splitlines()
35
36 assert len(rows) == 2
37 assert all(row.startswith("event_") for row in rows)
38 assert UUID(first["id"]).version == 4
39 assert UUID(second["id"]).version == 4
40 assert json.loads(rows[0].split("\t")[1]) == first
41 assert json.loads(rows[1].split("\t")[1]) == second
42
43 payload = object_events.list_events(base_dir=data_dir)
44
45 assert payload["count"] == 2
46 assert payload["total"] == 2
47 assert [event["event_type"] for event in payload["events"]] == [
48 "collection.record.updated",
49 "collection.record.created",
50 ]
51
52
53def test_list_events_filters_event_type_since_and_paginates(tmp_path, monkeypatch):
54 data_dir = tmp_path / "data"
55 timestamps = iter([100, 100, 200, 200, 300, 300])
56 monkeypatch.setattr(object_events.time, "time", lambda: next(timestamps))
57
58 object_events.publish_event("invoice.created", payload={"id": "i1"}, base_dir=data_dir)
59 object_events.publish_event("invoice.paid", payload={"id": "i1"}, base_dir=data_dir)
60 object_events.publish_event("invoice.created", payload={"id": "i2"}, base_dir=data_dir)
61
62 payload = object_events.list_events(
63 event_type="invoice.created",
64 since=100,
65 base_dir=data_dir,
66 limit=1,
67 offset=1,
68 )
69
70 assert payload["event_type"] == "invoice.created"
71 assert payload["since"] == 100
72 assert payload["count"] == 1
73 assert payload["total"] == 2
74 assert payload["has_more"] is False
75 assert payload["events"][0]["payload"] == {"id": "i1"}
76
77
78def test_subscribe_list_and_delete_subscription(tmp_path, monkeypatch):
79 data_dir = tmp_path / "data"
80 timestamps = iter([100, 100, 101])
81 monkeypatch.setattr(object_events.time, "time", lambda: next(timestamps))
82
83 subscription = object_events.subscribe_event(
84 "collection.record.updated",
85 subscriber_id="scroll",
86 callback_url="https://example.com/hooks/dbbasic",
87 actor="admin",
88 base_dir=data_dir,
89 )
90
91 state_file = data_dir / "state" / "events" / "state.tsv"
92 assert state_file.read_text().split("\t", 1)[0] == "sub_collection.record.updated_scroll"
93 assert subscription["last_event_id"] is None
94 assert subscription["delivery"]["status"] == "idle"
95 assert subscription["delivery"]["attempts"] == 0
96
97 payload = object_events.list_subscriptions(
98 event_type="collection.record.updated",
99 base_dir=data_dir,
100 )
101
102 assert payload["count"] == 1
103 assert payload["subscriptions"] == [subscription]
104
105 deleted = object_events.delete_subscription(
106 "collection.record.updated",
107 "scroll",
108 base_dir=data_dir,
109 )
110
111 assert deleted == subscription
112 assert object_events.list_subscriptions(base_dir=data_dir)["subscriptions"] == []
113
114
115def test_subscribe_event_generates_uuid_id_when_missing(tmp_path):
116 subscription = object_events.subscribe_event(
117 "collection.record.updated",
118 callback_url="https://example.com/hooks/dbbasic",
119 base_dir=tmp_path / "data",
120 )
121
122 parsed = UUID(subscription["id"])
123 assert parsed.version == 4
124
125
126def test_record_subscription_delivery_tracks_success_and_failure(tmp_path, monkeypatch):
127 data_dir = tmp_path / "data"
128 subscription = object_events.subscribe_event(
129 "collection.record.updated",
130 subscriber_id="scroll",
131 callback_url="https://example.com/hooks/dbbasic",
132 base_dir=data_dir,
133 )
134 event = {"id": "evt_001", "event_type": "collection.record.updated"}
135
136 failed = object_events.record_subscription_delivery(
137 subscription,
138 event,
139 success=False,
140 status_code=500,
141 error="callback failed",
142 now=100,
143 )
144
145 assert failed["last_event_id"] is None
146 assert failed["delivery"]["status"] == "failed"
147 assert failed["delivery"]["attempts"] == 1
148 assert failed["delivery"]["failures"] == 1
149 assert failed["delivery"]["last_failure_event_id"] == "evt_001"
150 assert failed["delivery"]["last_error"] == "callback failed"
151
152 succeeded = object_events.record_subscription_delivery(
153 failed,
154 event,
155 success=True,
156 status_code=204,
157 now=101,
158 )
159
160 assert succeeded["last_event_id"] == "evt_001"
161 assert succeeded["delivery"]["status"] == "ok"
162 assert succeeded["delivery"]["attempts"] == 2
163 assert succeeded["delivery"]["successes"] == 1
164 assert succeeded["delivery"]["last_success_event_id"] == "evt_001"
165 assert succeeded["delivery"]["last_status_code"] == 204
166 assert succeeded["delivery"]["last_error"] is None
167
168
169def test_list_event_deliveries_summarizes_pending_and_failed_without_payloads(tmp_path, monkeypatch):
170 data_dir = tmp_path / "data"
171 timestamps = iter(range(100, 120))
172 monkeypatch.setattr(object_events.time, "time", lambda: next(timestamps))
173 first_event = object_events.publish_event(
174 "collection.record.updated",
175 payload={"secret": "do-not-list"},
176 base_dir=data_dir,
177 )
178 second_event = object_events.publish_event(
179 "collection.record.updated",
180 payload={"secret": "also-hidden"},
181 base_dir=data_dir,
182 )
183 subscription = object_events.subscribe_event(
184 "collection.record.updated",
185 subscriber_id="scroll",
186 callback_url="https://example.com/hooks/dbbasic",
187 base_dir=data_dir,
188 )
189 subscription = object_events.record_subscription_delivery(
190 subscription,
191 first_event,
192 success=False,
193 status_code=500,
194 error="callback down",
195 now=100,
196 )
197 manager = object_state.ObjectStateManager(object_events.EVENTS_OBJECT_ID, base_dir=data_dir)
198 manager.set("sub_collection.record.updated_scroll", json.dumps(subscription))
199
200 payload = object_events.list_event_deliveries(
201 event_type="collection.record.updated",
202 delivery_status="failed",
203 pending=True,
204 base_dir=data_dir,
205 )
206
207 assert payload["count"] == 1
208 delivery = payload["deliveries"][0]
209 assert delivery["subscriber_id"] == "scroll"
210 assert delivery["callback_url_present"] is True
211 assert "callback_url" not in delivery
212 assert delivery["pending"] is True
213 assert delivery["pending_count"] == 2
214 assert delivery["next_pending_event"]["id"] == first_event["id"]
215 assert delivery["latest_pending_event"]["id"] == second_event["id"]
216 assert "payload" not in delivery["next_pending_event"]
217 assert delivery["delivery"]["status"] == "failed"
218 assert delivery["delivery"]["last_error"] == "callback down"
219
220
221def test_list_event_deliveries_can_include_callback_and_limited_event_summaries(tmp_path, monkeypatch):
222 data_dir = tmp_path / "data"
223 timestamps = iter(range(100, 120))
224 monkeypatch.setattr(object_events.time, "time", lambda: next(timestamps))
225 first_event = object_events.publish_event("invoice.created", payload={"id": "i1"}, base_dir=data_dir)
226 object_events.publish_event("invoice.created", payload={"id": "i2"}, base_dir=data_dir)
227 subscription = object_events.subscribe_event(
228 "invoice.created",
229 subscriber_id="billing",
230 callback_url="https://example.com/hooks/billing",
231 base_dir=data_dir,
232 )
233 subscription = object_events.record_subscription_delivery(
234 subscription,
235 first_event,
236 success=True,
237 status_code=204,
238 now=100,
239 )
240 manager = object_state.ObjectStateManager(object_events.EVENTS_OBJECT_ID, base_dir=data_dir)
241 manager.set("sub_invoice.created_billing", json.dumps(subscription))
242
243 payload = object_events.list_event_deliveries(
244 event_type="invoice.created",
245 pending=True,
246 include_callback_url=True,
247 include_events=True,
248 event_limit=1,
249 base_dir=data_dir,
250 )
251
252 assert payload["count"] == 1
253 delivery = payload["deliveries"][0]
254 assert delivery["callback_url"] == "https://example.com/hooks/billing"
255 assert delivery["pending_count"] == 1
256 assert len(delivery["pending_events"]) == 1
257 assert "payload" not in delivery["pending_events"][0]
258 assert delivery["pending_events"][0]["id"] != first_event["id"]
259
260
261def test_prune_events_removes_old_overflow_and_corrupt_rows_but_keeps_cursor(
262 tmp_path,
263 monkeypatch,
264):
265 data_dir = tmp_path / "data"
266 timestamps = iter([100, 100, 200, 200, 300, 300, 400, 400, 500, 500, 600])
267 monkeypatch.setattr(object_events.time, "time", lambda: next(timestamps))
268
269 protected = object_events.publish_event("invoice.created", payload={"id": "old"}, base_dir=data_dir)
270 old_unprotected = object_events.publish_event(
271 "invoice.created",
272 payload={"id": "old_unprotected"},
273 base_dir=data_dir,
274 )
275 newest = object_events.publish_event("invoice.created", payload={"id": "newest"}, base_dir=data_dir)
276 subscription = object_events.subscribe_event(
277 "invoice.created",
278 subscriber_id="scroll",
279 callback_url="https://example.com/hooks/dbbasic",
280 base_dir=data_dir,
281 )
282
283 manager = object_state.ObjectStateManager(object_events.EVENTS_OBJECT_ID, base_dir=data_dir)
284 subscription["last_event_id"] = protected["id"]
285 manager.set("sub_invoice.created_scroll", json.dumps(subscription))
286 manager.set("event_bad", "not-json")
287
288 result = object_events.prune_events(
289 base_dir=data_dir,
290 keep_count=1,
291 keep_seconds=150,
292 now=400,
293 )
294
295 assert result == {
296 "deleted": 2,
297 "kept": 2,
298 "scanned": 4,
299 "protected": 1,
300 "corrupt_deleted": 1,
301 "keep_count": 1,
302 "keep_seconds": 150,
303 }
304
305 events = object_events.list_events(base_dir=data_dir)["events"]
306 assert [event["id"] for event in events] == [newest["id"], protected["id"]]
307 assert old_unprotected["id"] not in {event["id"] for event in events}
308 assert object_events.list_subscriptions(base_dir=data_dir)["subscriptions"][0]["last_event_id"] == protected["id"]
309
310
311def test_prune_events_keeps_failed_delivery_attempt(tmp_path):
312 data_dir = tmp_path / "data"
313
314 failed_event = object_events.publish_event("invoice.created", payload={"id": "failed"}, base_dir=data_dir)
315 old_unprotected = object_events.publish_event("invoice.created", payload={"id": "old"}, base_dir=data_dir)
316 subscription = object_events.subscribe_event(
317 "invoice.created",
318 subscriber_id="scroll",
319 callback_url="https://example.com/hooks/dbbasic",
320 base_dir=data_dir,
321 )
322 subscription = object_events.record_subscription_delivery(
323 subscription,
324 failed_event,
325 success=False,
326 error="callback down",
327 now=102,
328 )
329 manager = object_state.ObjectStateManager(object_events.EVENTS_OBJECT_ID, base_dir=data_dir)
330 manager.set("sub_invoice.created_scroll", json.dumps(subscription))
331
332 result = object_events.prune_events(
333 base_dir=data_dir,
334 keep_count=None,
335 keep_seconds=1,
336 now=max(failed_event["timestamp"], old_unprotected["timestamp"]) + 10,
337 )
338
339 assert result["protected"] == 1
340 events = object_events.list_events(base_dir=data_dir)["events"]
341 assert [event["id"] for event in events] == [failed_event["id"]]
342 assert old_unprotected["id"] not in {event["id"] for event in events}
343
344
345def test_publish_event_can_prune_with_retention_options(tmp_path, monkeypatch):
346 data_dir = tmp_path / "data"
347 timestamps = iter([100, 100, 200, 200, 300, 300])
348 monkeypatch.setattr(object_events.time, "time", lambda: next(timestamps))
349
350 first = object_events.publish_event("invoice.created", payload={"id": "i1"}, base_dir=data_dir)
351 second = object_events.publish_event(
352 "invoice.created",
353 payload={"id": "i2"},
354 base_dir=data_dir,
355 keep_count=1,
356 keep_seconds=0,
357 )
358
359 events = object_events.list_events(base_dir=data_dir)["events"]
360
361 assert [event["id"] for event in events] == [second["id"]]
362 assert first["id"] not in {event["id"] for event in events}
363
364
365def test_event_helpers_reject_unsafe_inputs(tmp_path):
366 data_dir = tmp_path / "data"
367
368 with pytest.raises(object_events.InvalidEventTypeError):
369 object_events.publish_event("bad event", base_dir=data_dir)
370
371 with pytest.raises(object_events.InvalidEventTypeError):
372 object_events.publish_event(7, base_dir=data_dir)
373
374 with pytest.raises(object_events.InvalidSubscriberIdError):
375 object_events.subscribe_event("invoice.created", subscriber_id="bad/id", base_dir=data_dir)
376
377 with pytest.raises(object_events.InvalidSubscriberIdError):
378 object_events.subscribe_event("invoice.created", subscriber_id=7, base_dir=data_dir)
379
380 with pytest.raises(ValueError, match="callback_url"):
381 object_events.subscribe_event(
382 "invoice.created",
383 callback_url="ftp://example.com/hook",
384 base_dir=data_dir,
385 )
386
387 with pytest.raises(ValueError, match="limit must be at most"):
388 object_events.list_events(base_dir=data_dir, limit=1001)
389
390 with pytest.raises(ValueError, match="keep_count must be at least"):
391 object_events.prune_events(base_dir=data_dir, keep_count=-1)
392
393 with pytest.raises(ValueError, match="keep_seconds must be at least"):
394 object_events.prune_events(base_dir=data_dir, keep_seconds=-1)
395
396
397def test_event_helpers_ignore_corrupt_state_rows(tmp_path):
398 data_dir = tmp_path / "data"
399 state_file = data_dir / "state" / "events" / "state.tsv"
400 state_file.parent.mkdir(parents=True)
401 state_file.write_text(
402 'event_1_evt_ok\t{"id":"evt_ok","event_type":"invoice.created","timestamp":1}\n'
403 'event_2_evt_time\t{"id":"evt_time","event_type":"invoice.created","timestamp":"bad"}\n'
404 "event_2_evt_bad\tnot-json\n"
405 'sub_invoice.created_scroll\t{"id":"scroll","event_type":"invoice.created","created_at":1}\n'
406 )
407
408 assert object_events.list_events(base_dir=data_dir, since=1)["events"] == [
409 {"id": "evt_ok", "event_type": "invoice.created", "timestamp": 1}
410 ]
411 subscriptions = object_events.list_subscriptions(base_dir=data_dir)["subscriptions"]
412 assert len(subscriptions) == 1
413 assert subscriptions[0]["id"] == "scroll"
414 assert subscriptions[0]["event_type"] == "invoice.created"
415 assert subscriptions[0]["created_at"] == 1
416 assert subscriptions[0]["last_event_id"] is None
417 assert subscriptions[0]["delivery"]["status"] == "idle"