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_settings_uniqueness.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_settings_uniqueness.py · 4.9 KB · 121 lines
1"""A setting is configured once, or it is configured invisibly.
2
3app_settings is read by forty-odd objects, each through its own
4`_setting(base, key)` helper that scans the collection and returns the
5FIRST row whose key matches. Nothing stopped a second row with the same
6key from being written, and when one was, it did not conflict and did not
7warn -- it simply lost. The operator saw a saved setting that did nothing.
8
9This shipped a wrong journal on the live box, which is why the invariant
10moved from "every reader honours it" to "the write path enforces it":
11`inventory.journal.inventory_account` held a stale verification value, a
12correct one was added beside it, and the very next sale credited the
13stale account. The entry balanced. It was still wrong.
14
15Same shape as the provenance rule in hook_wallet_entries -- an invariant
16honoured by N callers is an invariant that holds until the N+1th.
17"""
18
19import pathlib
20
21from conftest import stage_collection
22
23import object_execution
24import object_records
25import python_object_runtime
26
27REPO_ROOT = pathlib.Path(__file__).resolve().parents[1]
28SETTINGS_OBJECTS = REPO_ROOT / "packages" / "app-settings" / "objects"
29RUNTIME = python_object_runtime.PythonObjectRuntime()
30
31
32def setup_env(tmp_path, monkeypatch):
33 data_dir = tmp_path / "data"
34 stage_collection(data_dir, "app-settings", "app_settings")
35 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir))
36 return data_dir
37
38
39def hook(record, action="create"):
40 return object_execution.execute_object(
41 RUNTIME,
42 object_execution.ObjectExecutionRequest(
43 "hook_app_settings", method="BEFORE_WRITE",
44 payload={"action": action, "collection": "app_settings",
45 "record": record}),
46 roots=[SETTINGS_OBJECTS]).result
47
48
49def existing(data_dir, record_id, key, value):
50 return object_records.create_collection_record(
51 "app_settings", {"id": record_id, "key": key, "value": value},
52 base_dir=data_dir)
53
54
55def test_a_first_setting_is_allowed(tmp_path, monkeypatch):
56 setup_env(tmp_path, monkeypatch)
57 assert hook({"id": "s1", "key": "a.b", "value": "x"}) is None
58
59
60def test_a_duplicate_key_is_refused_and_names_the_row_that_owns_it(
61 tmp_path, monkeypatch):
62 """THE bug, in the shape it actually took: a stale account setting and
63 a correct one beside it. The refusal must name the existing record,
64 because the operator's next question is always where it IS set."""
65 data_dir = setup_env(tmp_path, monkeypatch)
66 existing(data_dir, "old", "inventory.journal.inventory_account",
67 "acct-stale")
68
69 result = hook({"id": "new", "key": "inventory.journal.inventory_account",
70 "value": "acct-correct"})
71 assert result["status"] == 409
72 assert "old" in result["error"]
73 assert "acct-stale" in result["error"]
74 assert "silently ignored" in result["error"]
75
76
77def test_editing_the_row_that_owns_the_key_is_allowed(tmp_path, monkeypatch):
78 """Changing a setting's value is the normal case and must not be
79 mistaken for a collision with itself."""
80 data_dir = setup_env(tmp_path, monkeypatch)
81 existing(data_dir, "s1", "a.b", "x")
82 assert hook({"id": "s1", "key": "a.b", "value": "y"}, action="update") is None
83
84
85def test_a_different_key_is_never_blocked(tmp_path, monkeypatch):
86 data_dir = setup_env(tmp_path, monkeypatch)
87 existing(data_dir, "s1", "a.b", "x")
88 assert hook({"id": "s2", "key": "a.c", "value": "x"}) is None
89
90
91def test_whitespace_does_not_smuggle_a_second_row_past_the_gate(
92 tmp_path, monkeypatch):
93 """The readers strip; so must the gate, or " a.b" configures a key
94 that no reader will ever find while still shadowing nothing."""
95 data_dir = setup_env(tmp_path, monkeypatch)
96 existing(data_dir, "s1", "a.b", "x")
97 assert hook({"id": "s2", "key": " a.b ", "value": "y"})["status"] == 409
98
99
100def test_a_delete_is_not_this_hook_s_business(tmp_path, monkeypatch):
101 data_dir = setup_env(tmp_path, monkeypatch)
102 existing(data_dir, "s1", "a.b", "x")
103 assert hook({"id": "s1", "key": "a.b", "value": "x"}, action="delete") is None
104
105
106def test_the_schema_actually_declares_the_hook():
107 """The gate ships wired, not merely written.
108
109 Every other test here calls the hook directly, which proves its LOGIC
110 and proves nothing about whether the server ever invokes it. Hooks are
111 wired by a `hooks.before_write` declaration in the SCHEMA -- not by
112 naming convention -- so an object called hook_app_settings that no
113 schema points at is a silent no-op that passes its own unit tests.
114 That is exactly how this one first shipped: green tests, and the live
115 box happily accepted a duplicate key thirty seconds after deploy.
116 """
117 import json
118 schema = json.loads(
119 (REPO_ROOT / "packages" / "app-settings" / "schemas"
120 / "app_settings.json").read_text())
121 assert schema["hooks"]["before_write"] == "hook_app_settings"