Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/tests/test_settings_uniqueness.py
Install on DigitalOcean 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.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 18 minutes ago History
test_settings_uniqueness.py · 3.7 KB · 88 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 json
20import pathlib
21
22import pytest
23
24from conftest import stage_collection
25
26import object_records
27
28REPO_ROOT = pathlib.Path(__file__).resolve().parents[1]
29SCHEMA = REPO_ROOT / "packages" / "app-settings" / "schemas" / "app_settings.json"
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 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
45def 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
50def 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
66def 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
75def 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
81def 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"))