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_computed_fields.py · 9.2 KB · 238 lines
| 1 | """Formula & rollup fields (plan/formula-rollup-spec.md): derived values, |
| 2 | materialized on write at the storage layer -- so EVERY writer (HTTP, daemon, |
| 3 | seeds, imports) keeps them consistent, and every surface (lists, tables, |
| 4 | detail, search, filters, realtime, backups) sees plain stored values. |
| 5 | |
| 6 | Formulas recompute on writes of the record itself; rollups recompute on |
| 7 | create/update/delete in the source collection (single-hop, guarded). A broken |
| 8 | formula yields "" and never fails the write -- derived values are |
| 9 | non-authoritative, the opposite posture of pre-write hooks. |
| 10 | """ |
| 11 | |
| 12 | import json |
| 13 | from pathlib import Path |
| 14 | |
| 15 | import pytest |
| 16 | |
| 17 | import object_computed |
| 18 | import object_records |
| 19 | |
| 20 | |
| 21 | def _setup(tmp_path, schemas: dict[str, dict], rows: dict[str, str]): |
| 22 | data_dir = tmp_path / "data" |
| 23 | schema_dir = data_dir / "schemas" |
| 24 | schema_dir.mkdir(parents=True, exist_ok=True) |
| 25 | for name, schema in schemas.items(): |
| 26 | (schema_dir / f"{name}.json").write_text(json.dumps(schema)) |
| 27 | for name, content in rows.items(): |
| 28 | collection_dir = data_dir / "collections" / name |
| 29 | collection_dir.mkdir(parents=True, exist_ok=True) |
| 30 | (collection_dir / "records.tsv").write_text(content) |
| 31 | return data_dir |
| 32 | |
| 33 | |
| 34 | CONTACTS = { |
| 35 | "name": "contacts", |
| 36 | "fields": [ |
| 37 | {"name": "id"}, |
| 38 | {"name": "first_name", "type": "text"}, |
| 39 | {"name": "last_name", "type": "text"}, |
| 40 | {"name": "full_name", "type": "computed", |
| 41 | "formula": 'first_name + " " + last_name'}, |
| 42 | ], |
| 43 | } |
| 44 | |
| 45 | INVOICES = { |
| 46 | "name": "invs", |
| 47 | "fields": [ |
| 48 | {"name": "id"}, |
| 49 | {"name": "customer", "type": "text"}, |
| 50 | {"name": "total_cents", "type": "computed", |
| 51 | "rollup": {"collection": "inv_lines", "fk_field": "invoice_id", |
| 52 | "op": "sum", "field": "amount_cents"}}, |
| 53 | {"name": "line_count", "type": "computed", |
| 54 | "rollup": {"collection": "inv_lines", "fk_field": "invoice_id", |
| 55 | "op": "count"}}, |
| 56 | {"name": "display", "type": "computed", |
| 57 | "formula": 'customer + ": " + total_cents'}, |
| 58 | ], |
| 59 | } |
| 60 | |
| 61 | INV_LINES = { |
| 62 | "name": "inv_lines", |
| 63 | "fields": [ |
| 64 | {"name": "id"}, |
| 65 | {"name": "invoice_id", "type": "text"}, |
| 66 | {"name": "amount_cents", "type": "integer"}, |
| 67 | {"name": "kind", "type": "text"}, |
| 68 | ], |
| 69 | } |
| 70 | |
| 71 | |
| 72 | # --------------------------------------------------------------------------- |
| 73 | # The expression language itself |
| 74 | # --------------------------------------------------------------------------- |
| 75 | |
| 76 | def test_formula_language_shapes(): |
| 77 | ev = object_computed.evaluate_formula |
| 78 | assert ev('first + " " + last', {"first": "Grace", "last": "Hopper"}) == "Grace Hopper" |
| 79 | assert ev("qty * price_cents", {"qty": "3", "price_cents": "250"}) == "750" |
| 80 | assert ev("(a + b) * 2", {"a": "1", "b": "2"}) == "6" |
| 81 | assert ev("a - b", {"a": "10", "b": "4"}) == "6" |
| 82 | assert ev("-a + 5", {"a": "2"}) == "3" |
| 83 | assert ev('"fixed"', {}) == "fixed" |
| 84 | # numeric-looking strings add numerically; anything else concatenates |
| 85 | assert ev("a + b", {"a": "1.5", "b": "2.5"}) == "4" |
| 86 | assert ev("a + b", {"a": "x", "b": "1"}) == "x1" |
| 87 | with pytest.raises(object_computed.FormulaError): |
| 88 | ev("a / b", {"a": "1", "b": "0"}) |
| 89 | with pytest.raises(object_computed.FormulaError): |
| 90 | ev("a * b", {"a": "text", "b": "2"}) |
| 91 | |
| 92 | |
| 93 | # --------------------------------------------------------------------------- |
| 94 | # Formulas at the storage layer |
| 95 | # --------------------------------------------------------------------------- |
| 96 | |
| 97 | def test_formula_computed_on_create_and_recomputed_on_update(tmp_path): |
| 98 | data_dir = _setup(tmp_path, {"contacts": CONTACTS}, |
| 99 | {"contacts": "id\tfirst_name\tlast_name\tfull_name\n"}) |
| 100 | stored = object_records.create_collection_record( |
| 101 | "contacts", {"id": "c1", "first_name": "Grace", "last_name": "Hopper"}, |
| 102 | base_dir=data_dir) |
| 103 | assert stored["full_name"] == "Grace Hopper" |
| 104 | |
| 105 | object_records.update_collection_record( |
| 106 | "contacts", "c1", {"last_name": "Murray"}, base_dir=data_dir) |
| 107 | row = object_records.get_collection_record("contacts", "c1", base_dir=data_dir) |
| 108 | assert row["full_name"] == "Grace Murray" |
| 109 | |
| 110 | |
| 111 | def test_broken_formula_yields_empty_and_write_succeeds(tmp_path): |
| 112 | schema = { |
| 113 | "name": "things", |
| 114 | "fields": [ |
| 115 | {"name": "id"}, |
| 116 | {"name": "a", "type": "text"}, |
| 117 | {"name": "bad", "type": "computed", "formula": "a / 0"}, |
| 118 | ], |
| 119 | } |
| 120 | data_dir = _setup(tmp_path, {"things": schema}, {"things": "id\ta\tbad\n"}) |
| 121 | stored = object_records.create_collection_record( |
| 122 | "things", {"id": "t1", "a": "5"}, base_dir=data_dir) |
| 123 | assert stored["bad"] == "" |
| 124 | |
| 125 | |
| 126 | def test_client_still_cannot_submit_a_computed_field(tmp_path): |
| 127 | data_dir = _setup(tmp_path, {"contacts": CONTACTS}, |
| 128 | {"contacts": "id\tfirst_name\tlast_name\tfull_name\n"}) |
| 129 | with pytest.raises(object_records.InvalidRecordPayloadError): |
| 130 | object_records.create_collection_record( |
| 131 | "contacts", |
| 132 | {"id": "c1", "first_name": "G", "last_name": "H", "full_name": "forged"}, |
| 133 | base_dir=data_dir) |
| 134 | |
| 135 | |
| 136 | # --------------------------------------------------------------------------- |
| 137 | # Rollups: child writes keep the parent current |
| 138 | # --------------------------------------------------------------------------- |
| 139 | |
| 140 | def _invoice_env(tmp_path): |
| 141 | return _setup( |
| 142 | tmp_path, |
| 143 | {"invs": INVOICES, "inv_lines": INV_LINES}, |
| 144 | { |
| 145 | "invs": "id\tcustomer\ttotal_cents\tline_count\tdisplay\ni1\tAcme\t\t\t\ni2\tBlue\t\t\t\n", |
| 146 | "inv_lines": "id\tinvoice_id\tamount_cents\tkind\n", |
| 147 | }, |
| 148 | ) |
| 149 | |
| 150 | |
| 151 | def _invoice(data_dir, invoice_id="i1"): |
| 152 | return object_records.get_collection_record("invs", invoice_id, base_dir=data_dir) |
| 153 | |
| 154 | |
| 155 | def test_rollup_updates_on_child_create_update_delete(tmp_path): |
| 156 | data_dir = _invoice_env(tmp_path) |
| 157 | object_records.create_collection_record( |
| 158 | "inv_lines", {"id": "l1", "invoice_id": "i1", "amount_cents": "1000"}, |
| 159 | base_dir=data_dir) |
| 160 | assert _invoice(data_dir)["total_cents"] == "1000" |
| 161 | assert _invoice(data_dir)["line_count"] == "1" |
| 162 | |
| 163 | object_records.create_collection_record( |
| 164 | "inv_lines", {"id": "l2", "invoice_id": "i1", "amount_cents": "250"}, |
| 165 | base_dir=data_dir) |
| 166 | assert _invoice(data_dir)["total_cents"] == "1250" |
| 167 | assert _invoice(data_dir)["line_count"] == "2" |
| 168 | |
| 169 | object_records.update_collection_record( |
| 170 | "inv_lines", "l2", {"amount_cents": "500"}, base_dir=data_dir) |
| 171 | assert _invoice(data_dir)["total_cents"] == "1500" |
| 172 | |
| 173 | object_records.delete_collection_record("inv_lines", "l1", base_dir=data_dir) |
| 174 | assert _invoice(data_dir)["total_cents"] == "500" |
| 175 | assert _invoice(data_dir)["line_count"] == "1" |
| 176 | |
| 177 | |
| 178 | def test_parent_formula_sees_fresh_rollup_value(tmp_path): |
| 179 | data_dir = _invoice_env(tmp_path) |
| 180 | object_records.create_collection_record( |
| 181 | "inv_lines", {"id": "l1", "invoice_id": "i1", "amount_cents": "700"}, |
| 182 | base_dir=data_dir) |
| 183 | assert _invoice(data_dir)["display"] == "Acme: 700" |
| 184 | |
| 185 | |
| 186 | def test_only_the_affected_parent_recomputes(tmp_path): |
| 187 | data_dir = _invoice_env(tmp_path) |
| 188 | object_records.create_collection_record( |
| 189 | "inv_lines", {"id": "l1", "invoice_id": "i2", "amount_cents": "42"}, |
| 190 | base_dir=data_dir) |
| 191 | assert _invoice(data_dir, "i2")["total_cents"] == "42" |
| 192 | # i1 untouched: still the header's empty value, no phantom recompute |
| 193 | assert _invoice(data_dir, "i1")["total_cents"] == "" |
| 194 | |
| 195 | |
| 196 | def test_rollup_where_filter_and_ops(tmp_path): |
| 197 | schema = dict(INVOICES) |
| 198 | schema["fields"] = list(INVOICES["fields"]) + [ |
| 199 | {"name": "fee_total", "type": "computed", |
| 200 | "rollup": {"collection": "inv_lines", "fk_field": "invoice_id", |
| 201 | "op": "sum", "field": "amount_cents", |
| 202 | "where": {"kind": "fee"}}}, |
| 203 | {"name": "biggest", "type": "computed", |
| 204 | "rollup": {"collection": "inv_lines", "fk_field": "invoice_id", |
| 205 | "op": "max", "field": "amount_cents"}}, |
| 206 | ] |
| 207 | data_dir = _setup( |
| 208 | tmp_path, |
| 209 | {"invs": schema, "inv_lines": INV_LINES}, |
| 210 | { |
| 211 | "invs": "id\tcustomer\ttotal_cents\tline_count\tdisplay\tfee_total\tbiggest\ni1\tAcme\t\t\t\t\t\n", |
| 212 | "inv_lines": "id\tinvoice_id\tamount_cents\tkind\n", |
| 213 | }, |
| 214 | ) |
| 215 | for line_id, cents, kind in (("l1", "100", "fee"), ("l2", "900", "work"), ("l3", "50", "fee")): |
| 216 | object_records.create_collection_record( |
| 217 | "inv_lines", |
| 218 | {"id": line_id, "invoice_id": "i1", "amount_cents": cents, "kind": kind}, |
| 219 | base_dir=data_dir) |
| 220 | row = _invoice(data_dir) |
| 221 | assert row["total_cents"] == "1050" |
| 222 | assert row["fee_total"] == "150" |
| 223 | assert row["biggest"] == "900" |
| 224 | |
| 225 | |
| 226 | def test_backfill_helper_recomputes_existing_rows(tmp_path): |
| 227 | data_dir = _setup( |
| 228 | tmp_path, |
| 229 | {"contacts": CONTACTS}, |
| 230 | {"contacts": "id\tfirst_name\tlast_name\tfull_name\nc1\tAda\tLovelace\t\nc2\tAlan\tTuring\tSTALE\n"}, |
| 231 | ) |
| 232 | changed = object_records.recompute_computed_fields("contacts", base_dir=data_dir) |
| 233 | assert changed == 2 |
| 234 | rows = {r["id"]: r for r in object_records.read_collection_records("contacts", base_dir=data_dir)} |
| 235 | assert rows["c1"]["full_name"] == "Ada Lovelace" |
| 236 | assert rows["c2"]["full_name"] == "Alan Turing" |
| 237 | # second run is a no-op |
| 238 | assert object_records.recompute_computed_fields("contacts", base_dir=data_dir) == 0 |