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_settings_uniqueness.py · 4.9 KB · 121 lines
| 1 | """A setting is configured once, or it is configured invisibly. |
| 2 | |
| 3 | app_settings is read by forty-odd objects, each through its own |
| 4 | `_setting(base, key)` helper that scans the collection and returns the |
| 5 | FIRST row whose key matches. Nothing stopped a second row with the same |
| 6 | key from being written, and when one was, it did not conflict and did not |
| 7 | warn -- it simply lost. The operator saw a saved setting that did nothing. |
| 8 | |
| 9 | This shipped a wrong journal on the live box, which is why the invariant |
| 10 | moved from "every reader honours it" to "the write path enforces it": |
| 11 | `inventory.journal.inventory_account` held a stale verification value, a |
| 12 | correct one was added beside it, and the very next sale credited the |
| 13 | stale account. The entry balanced. It was still wrong. |
| 14 | |
| 15 | Same shape as the provenance rule in hook_wallet_entries -- an invariant |
| 16 | honoured by N callers is an invariant that holds until the N+1th. |
| 17 | """ |
| 18 | |
| 19 | import pathlib |
| 20 | |
| 21 | from conftest import stage_collection |
| 22 | |
| 23 | import object_execution |
| 24 | import object_records |
| 25 | import python_object_runtime |
| 26 | |
| 27 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 28 | SETTINGS_OBJECTS = REPO_ROOT / "packages" / "app-settings" / "objects" |
| 29 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 30 | |
| 31 | |
| 32 | def 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 | |
| 39 | def 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 | |
| 49 | def 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 | |
| 55 | def 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 | |
| 60 | def 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 | |
| 77 | def 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 | |
| 85 | def 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 | |
| 91 | def 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 | |
| 100 | def 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 | |
| 106 | def 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" |