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.
test_settings_uniqueness.py · 3.7 KB · 88 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 json |
| 20 | import pathlib |
| 21 | |
| 22 | import pytest |
| 23 | |
| 24 | from conftest import stage_collection |
| 25 | |
| 26 | import object_records |
| 27 | |
| 28 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 29 | SCHEMA = REPO_ROOT / "packages" / "app-settings" / "schemas" / "app_settings.json" |
| 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 setting(data_dir, record_id, key, value): |
| 40 | return object_records.create_collection_record( |
| 41 | "app_settings", {"id": record_id, "key": key, "value": value}, |
| 42 | base_dir=data_dir) |
| 43 | |
| 44 | |
| 45 | def test_a_first_setting_is_allowed(tmp_path, monkeypatch): |
| 46 | data_dir = setup_env(tmp_path, monkeypatch) |
| 47 | assert setting(data_dir, "s1", "a.b", "x")["key"] == "a.b" |
| 48 | |
| 49 | |
| 50 | def test_a_duplicate_key_is_refused_inside_the_write_lock(tmp_path, monkeypatch): |
| 51 | """THE bug, in the shape it actually took: a stale account setting and |
| 52 | a correct one beside it. `unique` is the whole guard now (the hook that |
| 53 | also refused it ran before the lock and is gone), so it refuses for |
| 54 | every writer, and the sentence says the second row would be ignored.""" |
| 55 | data_dir = setup_env(tmp_path, monkeypatch) |
| 56 | setting(data_dir, "old", "inventory.journal.inventory_account", "acct-stale") |
| 57 | |
| 58 | with pytest.raises(object_records.UniqueConstraintError) as refused: |
| 59 | setting(data_dir, "new", "inventory.journal.inventory_account", "acct-correct") |
| 60 | assert refused.value.existing_id == "old" |
| 61 | assert "silently ignored" in str(refused.value) |
| 62 | assert [row["id"] for row in object_records.read_collection_records( |
| 63 | "app_settings", base_dir=data_dir)] == ["old"] |
| 64 | |
| 65 | |
| 66 | def test_editing_the_row_that_owns_the_key_is_allowed(tmp_path, monkeypatch): |
| 67 | """Changing a setting's value is the normal case and must not be |
| 68 | mistaken for a collision with itself.""" |
| 69 | data_dir = setup_env(tmp_path, monkeypatch) |
| 70 | setting(data_dir, "s1", "a.b", "x") |
| 71 | object_records.update_collection_record("app_settings", "s1", {"value": "y"}, |
| 72 | base_dir=data_dir) |
| 73 | |
| 74 | |
| 75 | def test_a_different_key_is_never_blocked(tmp_path, monkeypatch): |
| 76 | data_dir = setup_env(tmp_path, monkeypatch) |
| 77 | setting(data_dir, "s1", "a.b", "x") |
| 78 | setting(data_dir, "s2", "a.c", "x") |
| 79 | |
| 80 | |
| 81 | def test_the_schema_declares_the_key_unique_and_no_longer_a_hook(): |
| 82 | """The guard is a declaration, not a hook wired by name. A pre-write hook |
| 83 | reads before the lock, so it could only ever be advisory here.""" |
| 84 | schema = json.loads(SCHEMA.read_text()) |
| 85 | assert [(rule["fields"], rule.get("scope", [])) for rule in schema["unique"]] == [(["key"], [])] |
| 86 | assert "hooks" not in schema |
| 87 | # No hook source ships; a stale __pycache__ left by an older checkout is not one. |
| 88 | assert not list((REPO_ROOT / "packages" / "app-settings" / "objects").glob("hook/*.py")) |