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_rollup_broken_schema.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, 19 minutes ago History
test_rollup_broken_schema.py · 6.5 KB · 142 lines
1"""A schema file that does not load must not turn a write that landed into a refusal.
2
3Every create, update and delete asks which schemas roll up the collection it
4wrote, and that question is asked after the row is on disk. It used to raise
5the broken file's error (invalid JSON, a file named outside the schema-name
6rules), so the caller was told 400 for a write that had happened: a retry
7duplicated it, an action compensated for it, and the parent's rollup stayed
8stale. A reported failure must mean nothing was written.
9
10The lookup is now as best-effort as the refresh it feeds: a schema that does
11not load is skipped, as the check-lock plan already skips it. Its own
12declarations cannot be honoured while it does not load (writes to its own
13collection are refused before anything is written, because its schema is
14what they validate against). The parents that still load are refreshed, and
15one a broken file declared is brought current by the next write to one of
16its children once the file is fixed, or by recompute_computed_fields.
17"""
18
19import json
20from pathlib import Path
21
22import pytest
23
24import object_records
25import object_server
26from test_object_server import TEST_ADMIN_TOKEN, enable_admin_token, request
27
28AUTH = [("authorization", f"Token {TEST_ADMIN_TOKEN}")]
29
30
31def stage(data_dir: Path) -> Path:
32 schemas = data_dir / "schemas"
33 schemas.mkdir(parents=True)
34 rollup = {"collection": "order_lines", "fk_field": "order_id", "op": "sum", "field": "cents"}
35 (schemas / "orders.json").write_text(json.dumps({"name": "orders", "fields": [
36 {"name": "id"}, {"name": "lines_total", "type": "computed", "rollup": rollup}]}))
37 (schemas / "order_lines.json").write_text(json.dumps({"name": "order_lines", "fields": [
38 {"name": "id"}, {"name": "order_id"}, {"name": "cents", "type": "integer"}]}))
39 object_records.create_collection_record("orders", {"id": "o1"}, base_dir=data_dir)
40 return schemas
41
42
43def total(data_dir: Path, order_id: str = "o1") -> str:
44 return object_records.get_collection_record("orders", order_id, base_dir=data_dir)["lines_total"]
45
46
47def line_ids(data_dir: Path) -> list[str]:
48 return [row["id"] for row in object_records.read_collection_records("order_lines", base_dir=data_dir)]
49
50
51BROKEN = {
52 "invalid JSON": ("scratch_things.json", "{ this is not json"),
53 "a name outside the schema-name rules": ("bad-name.json", json.dumps({"fields": [{"name": "id"}]})),
54}
55
56
57@pytest.mark.parametrize("broken", sorted(BROKEN))
58def test_create_update_and_delete_succeed_and_refresh_the_parents_that_load(tmp_path, broken):
59 data_dir = tmp_path / "data"
60 schemas = stage(data_dir)
61 name, text = BROKEN[broken]
62 (schemas / name).write_text(text)
63
64 created = object_records.create_collection_record(
65 "order_lines", {"id": "l1", "order_id": "o1", "cents": "500"}, base_dir=data_dir, actor="t")
66 assert created["id"] == "l1"
67 assert line_ids(data_dir) == ["l1"]
68 assert total(data_dir) == "500"
69
70 object_records.update_collection_record("order_lines", "l1", {"cents": "700"},
71 base_dir=data_dir, actor="t")
72 assert total(data_dir) == "700"
73
74 object_records.delete_collection_record("order_lines", "l1", base_dir=data_dir, actor="t")
75 assert line_ids(data_dir) == []
76 assert total(data_dir) == "0"
77
78
79def test_a_write_to_the_broken_schemas_own_collection_is_still_refused_with_nothing_written(tmp_path):
80 data_dir = tmp_path / "data"
81 schemas = stage(data_dir)
82 (schemas / "order_lines.json").write_text("{ this is not json")
83 with pytest.raises(ValueError, match="invalid JSON"):
84 object_records.create_collection_record(
85 "order_lines", {"id": "l1", "order_id": "o1", "cents": "500"}, base_dir=data_dir)
86 records = data_dir / "collections" / "order_lines" / "records.tsv"
87 assert not records.exists() or "l1" not in records.read_text()
88
89
90def test_a_parent_the_broken_file_declares_heals_once_the_file_is_fixed(tmp_path):
91 """While orders.json does not load nobody can know it rolls up
92 order_lines; the write lands and orders is left as it was. Fixing the
93 file and writing one more child brings the parent current from every
94 child, and recompute_computed_fields does the same without a write."""
95 data_dir = tmp_path / "data"
96 schemas = stage(data_dir)
97 good = (schemas / "orders.json").read_text()
98 (schemas / "orders.json").write_text("{ this is not json")
99
100 object_records.create_collection_record(
101 "order_lines", {"id": "l1", "order_id": "o1", "cents": "500"}, base_dir=data_dir)
102 object_records.create_collection_record(
103 "order_lines", {"id": "l2", "order_id": "o1", "cents": "200"}, base_dir=data_dir)
104 assert line_ids(data_dir) == ["l1", "l2"]
105
106 (schemas / "orders.json").write_text(good)
107 assert total(data_dir) == "0", "stale: written while its declaration could not be read"
108 assert object_records.recompute_computed_fields("orders", base_dir=data_dir) == 1
109 assert total(data_dir) == "700"
110
111 (schemas / "orders.json").write_text("{ this is not json")
112 object_records.create_collection_record(
113 "order_lines", {"id": "l3", "order_id": "o1", "cents": "50"}, base_dir=data_dir)
114 (schemas / "orders.json").write_text(good)
115 assert total(data_dir) == "700"
116 object_records.update_collection_record("order_lines", "l3", {"cents": "60"}, base_dir=data_dir)
117 assert total(data_dir) == "760", "the next write to a child recomputes from every child"
118
119
120def test_over_http_the_write_is_answered_as_written_once(tmp_path, monkeypatch):
121 data_dir = tmp_path / "data"
122 schemas = stage(data_dir)
123 monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir))
124 enable_admin_token(monkeypatch)
125 (schemas / "scratch_things.json").write_text("{ this is not json")
126
127 status, _, payload = request("/collections/order_lines/records", method="POST", headers=AUTH,
128 body=json.dumps({"id": "l1", "order_id": "o1", "cents": 500}).encode())
129 assert status == 201, payload
130 assert payload["record"]["id"] == "l1"
131 assert line_ids(data_dir) == ["l1"]
132 assert total(data_dir) == "500"
133
134 status, _, payload = request("/collections/order_lines/records/l1", method="PUT", headers=AUTH,
135 body=json.dumps({"cents": 900}).encode())
136 assert status == 200, payload
137 assert total(data_dir) == "900"
138
139 status, _, payload = request("/collections/order_lines/records/l1", method="DELETE", headers=AUTH)
140 assert status == 200, payload
141 assert line_ids(data_dir) == []
142 assert total(data_dir) == "0"