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_change_dispatch.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_change_dispatch.py · 8.6 KB · 214 lines
1"""object_change_dispatch: the record-change-log dispatch pass that closes
2docs/logic-decisions.md #9 -- a storage-level write (a runner, an action)
3bypasses object_server's synchronous HTTP dispatch entirely, so its HANDLES
4reactions only ever fire once this pass polls the change log and finds the
5resulting entry.
6
7Fixture style follows tests/test_scheduler_admin.py and
8tests/test_books_spine.py: a tmp objects dir (DBBASIC_OBJECTS_DIR) holding
9handler sources, a tmp data dir (DBBASIC_DATA_DIR) holding a pre-created
10collection, writes made directly through object_records (the storage-level
11path this whole module exists to cover -- never through object_server).
12"""
13
14import json
15
16import object_change_dispatch
17import object_records
18
19HANDLER_SOURCE = '''
20import json
21import os
22
23HANDLES = ["widgets.record.created", "widgets.record.updated"]
24
25
26def EVENT(request):
27 log_path = os.environ["WIDGET_HANDLER_LOG"]
28 with open(log_path, "a", encoding="utf-8") as handle:
29 handle.write(json.dumps(request) + "\\n")
30 return {"ok": True}
31'''
32
33FAILING_HANDLER_SOURCE = '''
34HANDLES = ["widgets.record.created"]
35
36
37def EVENT(request):
38 raise RuntimeError("handler blew up")
39'''
40
41ZERO_RESULT = {"processed": 0, "dispatched": 0, "skipped": 0, "errors": 0, "results": []}
42
43
44def setup_env(tmp_path, monkeypatch, *, with_handler=True, with_failing_handler=False, enabled=True):
45 objects_dir = tmp_path / "objects"
46 (objects_dir / "system").mkdir(parents=True)
47 if with_handler:
48 (objects_dir / "system" / "widget_handler.py").write_text(HANDLER_SOURCE)
49 if with_failing_handler:
50 (objects_dir / "system" / "widget_failer.py").write_text(FAILING_HANDLER_SOURCE)
51
52 data_dir = tmp_path / "data"
53 coll_dir = data_dir / "collections" / "widgets"
54 coll_dir.mkdir(parents=True)
55 (coll_dir / "records.tsv").write_text("id\tname\n")
56
57 log_path = tmp_path / "handler_log.jsonl"
58 monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(objects_dir))
59 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir))
60 monkeypatch.setenv("WIDGET_HANDLER_LOG", str(log_path))
61 if enabled:
62 monkeypatch.setenv(object_change_dispatch.ENABLE_ENV, "1")
63 else:
64 monkeypatch.delenv(object_change_dispatch.ENABLE_ENV, raising=False)
65 return objects_dir, data_dir, log_path
66
67
68def create_widget(data_dir, record_id="w1", name="Widget One", actor="test_runner"):
69 return object_records.create_collection_record(
70 "widgets", {"id": record_id, "name": name}, base_dir=data_dir, actor=actor
71 )
72
73
74# --- core behavior: a storage-level write reaches its handler -----------------
75
76def test_storage_write_reaches_handler_on_next_pass(tmp_path, monkeypatch):
77 _, data_dir, log_path = setup_env(tmp_path, monkeypatch)
78
79 # First pass just stamps the cursor -- nothing to dispatch yet.
80 first = object_change_dispatch.dispatch_pending(base_dir=data_dir)
81 assert first == ZERO_RESULT
82
83 # A storage-level write -- object_records directly, never object_server --
84 # is exactly the write this module exists to cover.
85 create_widget(data_dir)
86
87 second = object_change_dispatch.dispatch_pending(base_dir=data_dir)
88 assert second["processed"] == 1
89 assert second["dispatched"] == 1
90 assert second["errors"] == 0
91 assert second["skipped"] == 0
92 assert len(second["results"]) == 1
93
94 entry = second["results"][0]
95 assert entry["ok"] is True
96 assert entry["handler_id"] == "system_widget_handler"
97 assert entry["collection"] == "widgets"
98 assert entry["record_id"] == "w1"
99
100 assert log_path.exists()
101 logged_lines = log_path.read_text(encoding="utf-8").strip().splitlines()
102 assert len(logged_lines) == 1
103
104
105def test_raw_verb_payload_shape_matches_http_dispatcher(tmp_path, monkeypatch):
106 """The dispatcher's payload carries the RAW verb ("create") in `action`
107 while `event` uses the past-participle ("...record.created") -- the
108 exact mismatch a handler once got wrong and silently skipped every
109 entry in production (see packages/app-payments/objects/system/books.py
110 EVENT()'s comment). This pass must build the identical shape."""
111 _, data_dir, log_path = setup_env(tmp_path, monkeypatch)
112 object_change_dispatch.dispatch_pending(base_dir=data_dir)
113 create_widget(data_dir)
114 result = object_change_dispatch.dispatch_pending(base_dir=data_dir)
115 assert result["dispatched"] == 1
116
117 payload = json.loads(log_path.read_text(encoding="utf-8").strip().splitlines()[0])
118 assert payload == {
119 "event": "widgets.record.created",
120 "collection": "widgets",
121 "record_id": "w1",
122 "action": "create",
123 }
124
125
126# --- cursor / idempotency -------------------------------------------------
127
128def test_cursor_advances_so_second_pass_does_not_redispatch(tmp_path, monkeypatch):
129 _, data_dir, log_path = setup_env(tmp_path, monkeypatch)
130 object_change_dispatch.dispatch_pending(base_dir=data_dir) # stamp cursor
131 create_widget(data_dir)
132
133 first = object_change_dispatch.dispatch_pending(base_dir=data_dir)
134 assert first["dispatched"] == 1
135
136 second = object_change_dispatch.dispatch_pending(base_dir=data_dir)
137 assert second == ZERO_RESULT
138
139 # The handler log should still show exactly one invocation.
140 assert len(log_path.read_text(encoding="utf-8").strip().splitlines()) == 1
141
142
143def test_first_run_does_not_replay_history(tmp_path, monkeypatch):
144 """A write that happened before this pass ever ran (or before the flag
145 was ever turned on) must not flood every handler the first time the
146 daemon polls -- same posture as process_notifications' cursor."""
147 _, data_dir, log_path = setup_env(tmp_path, monkeypatch)
148 create_widget(data_dir) # pre-existing history, cursor does not exist yet
149
150 first = object_change_dispatch.dispatch_pending(base_dir=data_dir)
151 assert first == ZERO_RESULT
152 assert not log_path.exists()
153
154 second = object_change_dispatch.dispatch_pending(base_dir=data_dir)
155 assert second == ZERO_RESULT
156
157
158# --- isolation --------------------------------------------------------------
159
160def test_one_raising_handler_does_not_stop_the_others(tmp_path, monkeypatch):
161 _, data_dir, log_path = setup_env(tmp_path, monkeypatch, with_failing_handler=True)
162 object_change_dispatch.dispatch_pending(base_dir=data_dir)
163 create_widget(data_dir)
164
165 result = object_change_dispatch.dispatch_pending(base_dir=data_dir)
166 assert result["processed"] == 1
167 assert result["dispatched"] == 2 # both handlers attempted
168 assert result["errors"] == 1
169
170 by_handler = {r["handler_id"]: r for r in result["results"]}
171 assert by_handler["system_widget_failer"]["ok"] is False
172 assert "handler blew up" in (by_handler["system_widget_failer"]["error"] or "")
173 assert by_handler["system_widget_handler"]["ok"] is True
174
175 # The good handler still ran despite its sibling raising.
176 assert len(log_path.read_text(encoding="utf-8").strip().splitlines()) == 1
177
178
179# --- graceful skips ----------------------------------------------------------
180
181def test_no_handler_objects_is_a_graceful_skip(tmp_path, monkeypatch):
182 _, data_dir, _log_path = setup_env(tmp_path, monkeypatch, with_handler=False)
183 create_widget(data_dir)
184 result = object_change_dispatch.dispatch_pending(base_dir=data_dir)
185 assert result == ZERO_RESULT
186
187
188def test_no_change_log_is_a_graceful_skip(tmp_path, monkeypatch):
189 # A handler is installed (so "widgets" is a watched collection) but no
190 # write has ever happened -- record_changes/widgets/changes.jsonl does
191 # not exist on disk at all.
192 _, data_dir, _log_path = setup_env(tmp_path, monkeypatch)
193 object_change_dispatch.dispatch_pending(base_dir=data_dir) # stamp cursor
194 result = object_change_dispatch.dispatch_pending(base_dir=data_dir)
195 assert result == ZERO_RESULT
196
197
198def test_disabled_by_default_unless_flag_set(tmp_path, monkeypatch):
199 _, data_dir, log_path = setup_env(tmp_path, monkeypatch, enabled=False)
200 create_widget(data_dir)
201 result = object_change_dispatch.dispatch_pending(base_dir=data_dir)
202 assert result == ZERO_RESULT
203 assert not log_path.exists()
204 # Disabled means inert -- it must not even stamp a cursor.
205 assert not (data_dir / object_change_dispatch.CURSOR_FILE_NAME).exists()
206
207
208def test_change_dispatch_enabled_reads_the_env_flag(tmp_path, monkeypatch):
209 monkeypatch.delenv(object_change_dispatch.ENABLE_ENV, raising=False)
210 assert object_change_dispatch.change_dispatch_enabled(base_dir=tmp_path) is False
211 monkeypatch.setenv(object_change_dispatch.ENABLE_ENV, "1")
212 assert object_change_dispatch.change_dispatch_enabled(base_dir=tmp_path) is True
213 monkeypatch.setenv(object_change_dispatch.ENABLE_ENV, "off")
214 assert object_change_dispatch.change_dispatch_enabled(base_dir=tmp_path) is False