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_object_books_status.py · 13.9 KB · 338 lines
| 1 | """Tests for object_books_status.py -- the readiness check that surfaces the |
| 2 | books spine's silent-skip posture (see that module's docstring, and |
| 3 | packages/app-payments/objects/system/books.py, packages/app-catalog/ |
| 4 | objects/system/stock_books.py, packages/app-banking/objects/action/ |
| 5 | resolve_bank_line.py for what actually skips). |
| 6 | |
| 7 | Fixture style borrowed from tests/test_books_spine.py (real schemas copied |
| 8 | from packages/*/schemas, hand-written minimal TSVs) but kept self-contained |
| 9 | here: this module is pure and read-only, so a test only needs the |
| 10 | collections + app_settings/fin_accounts rows that make a given check |
| 11 | "installed" or not -- no dispatcher, no object execution. |
| 12 | """ |
| 13 | |
| 14 | import pathlib |
| 15 | |
| 16 | import object_books_status |
| 17 | from conftest import schema_header, stage_collection |
| 18 | |
| 19 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 20 | PACKAGES = REPO_ROOT / "packages" |
| 21 | |
| 22 | CASH, AR, REV = "acct-cash", "acct-ar", "acct-rev" |
| 23 | INV, SHRINK = "acct-inventory", "acct-shrinkage" |
| 24 | FEES, INTEREST = "acct-fees", "acct-interest" |
| 25 | |
| 26 | |
| 27 | def _copy_schema(schema_dir, pkg, name): |
| 28 | (schema_dir / f"{name}.json").write_text( |
| 29 | (PACKAGES / pkg / "schemas" / f"{name}.json").read_text() |
| 30 | ) |
| 31 | |
| 32 | |
| 33 | def _write_tsv(data_dir, name, text): |
| 34 | d = data_dir / "collections" / name |
| 35 | d.mkdir(parents=True, exist_ok=True) |
| 36 | (d / "records.tsv").write_text(text) |
| 37 | |
| 38 | |
| 39 | def setup_env( |
| 40 | tmp_path, |
| 41 | *, |
| 42 | books=True, |
| 43 | app_settings=True, |
| 44 | payments=False, |
| 45 | inventory=False, |
| 46 | banking=False, |
| 47 | settings_rows="", |
| 48 | accounts_rows=(), |
| 49 | ): |
| 50 | """Build a data_dir with just enough schema + TSV to drive one check. |
| 51 | |
| 52 | books=False omits fin_journals/fin_journal_lines/fin_accounts entirely |
| 53 | (the "no accounting books installed at all" case). payments/inventory/ |
| 54 | banking each install just the one collection whose presence gates that |
| 55 | area (payments, stock_moves, bank_lines) -- no real payment/move/line |
| 56 | rows are needed since this module only checks account mapping, never |
| 57 | the operational data itself, so those three stay a deliberate one-column |
| 58 | placeholder rather than the real schema (see below). |
| 59 | |
| 60 | settings_rows is raw extra app_settings TSV row text (id\\tkey\\tvalue |
| 61 | \\tdescription\\n per row); accounts_rows is an iterable of |
| 62 | (id, name, account_type) tuples for fin_accounts. |
| 63 | """ |
| 64 | data_dir = tmp_path / "data" |
| 65 | |
| 66 | if books: |
| 67 | stage_collection(data_dir, "app-finance", "fin_journals") |
| 68 | stage_collection(data_dir, "app-finance", "fin_journal_lines") |
| 69 | # fin_accounts rows are pinned to the real schema's field order (via |
| 70 | # schema_header) rather than hand-typed, so a later schema edit |
| 71 | # can't silently shift account_type/owner_id into the wrong column. |
| 72 | account_fields = schema_header("app-finance", "fin_accounts").strip("\n").split("\t") |
| 73 | accounts_text = "" |
| 74 | for account_id, name, account_type in accounts_rows: |
| 75 | values = {"id": account_id, "name": name, "account_type": account_type, |
| 76 | "owner_id": "dan"} |
| 77 | accounts_text += "\t".join(values.get(f, "") for f in account_fields) + "\n" |
| 78 | stage_collection(data_dir, "app-finance", "fin_accounts", rows=accounts_text) |
| 79 | |
| 80 | if app_settings: |
| 81 | stage_collection(data_dir, "app-settings", "app_settings", rows=settings_rows) |
| 82 | |
| 83 | schema_dir = data_dir / "schemas" |
| 84 | schema_dir.mkdir(parents=True, exist_ok=True) |
| 85 | |
| 86 | if payments: |
| 87 | # Deliberately NOT the real payments schema shape: this module only |
| 88 | # checks whether the collection is installed, never reads a payment |
| 89 | # row, so a one-column placeholder proves the same "installed" gate |
| 90 | # without pretending to be real payment data. |
| 91 | _copy_schema(schema_dir, "app-payments", "payments") |
| 92 | _write_tsv(data_dir, "payments", "id\n") |
| 93 | |
| 94 | if inventory: |
| 95 | # Same placeholder posture as payments above, for stock_moves. |
| 96 | _copy_schema(schema_dir, "app-catalog", "stock_moves") |
| 97 | _write_tsv(data_dir, "stock_moves", "id\n") |
| 98 | |
| 99 | if banking: |
| 100 | # Same placeholder posture as payments above, for bank_lines. |
| 101 | _copy_schema(schema_dir, "app-banking", "bank_lines") |
| 102 | _write_tsv(data_dir, "bank_lines", "id\n") |
| 103 | |
| 104 | return data_dir |
| 105 | |
| 106 | |
| 107 | def _problem_ids(status): |
| 108 | return {p["id"] for p in status["problems"]} |
| 109 | |
| 110 | |
| 111 | def _check(status, check_id): |
| 112 | for c in status["checks"]: |
| 113 | if c["id"] == check_id: |
| 114 | return c |
| 115 | raise AssertionError(f"no check {check_id!r} among {[c['id'] for c in status['checks']]}") |
| 116 | |
| 117 | |
| 118 | # -- fully configured -> ready ------------------------------------------------ |
| 119 | |
| 120 | def test_fully_configured_cash_basis_is_ready(tmp_path): |
| 121 | settings = ( |
| 122 | "s0\tpayments.accounting_basis\tcash\t\n" |
| 123 | f"s1\tpayments.journal.cash_account\t{CASH}\t\n" |
| 124 | f"s2\tpayments.journal.revenue_account\t{REV}\t\n" |
| 125 | ) |
| 126 | data_dir = setup_env( |
| 127 | tmp_path, payments=True, settings_rows=settings, |
| 128 | accounts_rows=[(CASH, "Cash", "asset"), (REV, "Revenue", "income")], |
| 129 | ) |
| 130 | status = object_books_status.books_status(base_dir=data_dir) |
| 131 | assert status["ready"] is True |
| 132 | assert status["problems"] == [] |
| 133 | assert {c["id"] for c in status["checks"]} == { |
| 134 | "payments.cash_account", "payments.revenue_account", |
| 135 | } |
| 136 | assert all(c["status"] == "ok" for c in status["checks"]) |
| 137 | assert object_books_status.is_ready(base_dir=data_dir) is True |
| 138 | # No inventory/banking apps installed -> nothing reported for them. |
| 139 | assert not any(c["area"] in ("inventory", "banking") for c in status["checks"]) |
| 140 | |
| 141 | |
| 142 | # -- missing setting ----------------------------------------------------------- |
| 143 | |
| 144 | def test_missing_cash_account_is_one_named_problem(tmp_path): |
| 145 | settings = ( |
| 146 | "s0\tpayments.accounting_basis\tcash\t\n" |
| 147 | f"s2\tpayments.journal.revenue_account\t{REV}\t\n" |
| 148 | ) |
| 149 | data_dir = setup_env( |
| 150 | tmp_path, payments=True, settings_rows=settings, |
| 151 | accounts_rows=[(REV, "Revenue", "income")], |
| 152 | ) |
| 153 | status = object_books_status.books_status(base_dir=data_dir) |
| 154 | assert status["ready"] is False |
| 155 | assert _problem_ids(status) == {"payments.cash_account"} |
| 156 | problem = _check(status, "payments.cash_account") |
| 157 | assert problem["status"] == "missing" |
| 158 | assert problem["setting"] == "payments.journal.cash_account" |
| 159 | # impact must state the CONSEQUENCE, not just repeat "missing". |
| 160 | assert "never reach the ledger" in problem["impact"] |
| 161 | assert object_books_status.is_ready(base_dir=data_dir) is False |
| 162 | |
| 163 | |
| 164 | # -- dangling: configured but points nowhere ----------------------------------- |
| 165 | |
| 166 | def test_setting_pointing_at_nonexistent_account_is_dangling(tmp_path): |
| 167 | settings = ( |
| 168 | "s0\tpayments.accounting_basis\tcash\t\n" |
| 169 | "s1\tpayments.journal.cash_account\tacct-does-not-exist\t\n" |
| 170 | f"s2\tpayments.journal.revenue_account\t{REV}\t\n" |
| 171 | ) |
| 172 | data_dir = setup_env( |
| 173 | tmp_path, payments=True, settings_rows=settings, |
| 174 | accounts_rows=[(REV, "Revenue", "income")], |
| 175 | ) |
| 176 | status = object_books_status.books_status(base_dir=data_dir) |
| 177 | assert status["ready"] is False |
| 178 | problem = _check(status, "payments.cash_account") |
| 179 | assert problem["status"] == "dangling" |
| 180 | assert "acct-does-not-exist" in problem["detail"] |
| 181 | |
| 182 | |
| 183 | # -- accrual needs receivable; cash basis does not ----------------------------- |
| 184 | |
| 185 | def test_accrual_basis_requires_receivable_account(tmp_path): |
| 186 | settings = ( |
| 187 | "s0\tpayments.accounting_basis\taccrual\t\n" |
| 188 | f"s1\tpayments.journal.cash_account\t{CASH}\t\n" |
| 189 | f"s2\tpayments.journal.revenue_account\t{REV}\t\n" |
| 190 | ) |
| 191 | data_dir = setup_env( |
| 192 | tmp_path, payments=True, settings_rows=settings, |
| 193 | accounts_rows=[(CASH, "Cash", "asset"), (REV, "Revenue", "income")], |
| 194 | ) |
| 195 | status = object_books_status.books_status(base_dir=data_dir) |
| 196 | assert status["ready"] is False |
| 197 | assert _problem_ids(status) == {"payments.receivable_account"} |
| 198 | assert _check(status, "payments.receivable_account")["status"] == "missing" |
| 199 | |
| 200 | |
| 201 | def test_cash_basis_does_not_require_receivable_account(tmp_path): |
| 202 | settings = ( |
| 203 | "s0\tpayments.accounting_basis\tcash\t\n" |
| 204 | f"s1\tpayments.journal.cash_account\t{CASH}\t\n" |
| 205 | f"s2\tpayments.journal.revenue_account\t{REV}\t\n" |
| 206 | ) |
| 207 | data_dir = setup_env( |
| 208 | tmp_path, payments=True, settings_rows=settings, |
| 209 | accounts_rows=[(CASH, "Cash", "asset"), (REV, "Revenue", "income")], |
| 210 | ) |
| 211 | status = object_books_status.books_status(base_dir=data_dir) |
| 212 | assert status["ready"] is True |
| 213 | assert not any(c["id"] == "payments.receivable_account" for c in status["checks"]) |
| 214 | |
| 215 | |
| 216 | # -- areas not installed are not reported -------------------------------------- |
| 217 | |
| 218 | def test_uninstalled_areas_are_never_reported(tmp_path): |
| 219 | # Only banking is installed; payments and inventory are absent. Even |
| 220 | # though payments/inventory settings are entirely unconfigured, they |
| 221 | # must not show up -- system_books/system_stock_books can't even run. |
| 222 | settings = ( |
| 223 | f"s1\treconcile.journal.fees_account\t{FEES}\t\n" |
| 224 | f"s2\treconcile.journal.interest_account\t{INTEREST}\t\n" |
| 225 | ) |
| 226 | data_dir = setup_env( |
| 227 | tmp_path, banking=True, settings_rows=settings, |
| 228 | accounts_rows=[(FEES, "Bank Fees", "expense"), (INTEREST, "Interest Income", "income")], |
| 229 | ) |
| 230 | status = object_books_status.books_status(base_dir=data_dir) |
| 231 | assert {c["area"] for c in status["checks"]} == {"banking"} |
| 232 | assert status["ready"] is True |
| 233 | |
| 234 | |
| 235 | def test_inventory_area_checked_only_when_stock_moves_installed(tmp_path): |
| 236 | # inventory settings are missing, but stock_moves isn't installed -> |
| 237 | # no inventory problems should appear at all. |
| 238 | data_dir = setup_env(tmp_path, payments=False, inventory=False, banking=False) |
| 239 | status = object_books_status.books_status(base_dir=data_dir) |
| 240 | assert status["checks"] == [] |
| 241 | assert status["ready"] is True # nothing installed that could skip |
| 242 | |
| 243 | # Now install stock_moves with nothing configured: both required |
| 244 | # inventory accounts become named problems. |
| 245 | data_dir2 = setup_env(tmp_path / "with-inventory", inventory=True) |
| 246 | status2 = object_books_status.books_status(base_dir=data_dir2) |
| 247 | assert status2["ready"] is False |
| 248 | assert _problem_ids(status2) == { |
| 249 | "inventory.inventory_account", "inventory.shrinkage_account", |
| 250 | } |
| 251 | assert all(c["area"] == "inventory" for c in status2["problems"]) |
| 252 | |
| 253 | |
| 254 | def test_inventory_reason_override_dangling(tmp_path): |
| 255 | settings = ( |
| 256 | f"s1\tinventory.journal.inventory_account\t{INV}\t\n" |
| 257 | f"s2\tinventory.journal.shrinkage_account\t{SHRINK}\t\n" |
| 258 | "s3\tinventory.journal.theft_account\tacct-nowhere\t\n" |
| 259 | ) |
| 260 | data_dir = setup_env( |
| 261 | tmp_path, inventory=True, settings_rows=settings, |
| 262 | accounts_rows=[(INV, "Inventory", "asset"), (SHRINK, "Shrinkage", "expense")], |
| 263 | ) |
| 264 | status = object_books_status.books_status(base_dir=data_dir) |
| 265 | assert status["ready"] is False |
| 266 | assert _problem_ids(status) == {"inventory.theft_account"} |
| 267 | assert _check(status, "inventory.theft_account")["status"] == "dangling" |
| 268 | # The two required accounts are fine and not reported as problems. |
| 269 | assert _check(status, "inventory.inventory_account")["status"] == "ok" |
| 270 | assert _check(status, "inventory.shrinkage_account")["status"] == "ok" |
| 271 | |
| 272 | |
| 273 | def test_inventory_reason_override_unset_is_not_a_problem(tmp_path): |
| 274 | # No per-reason override configured at all -- falls back to |
| 275 | # shrinkage_account, so no inventory.<reason>_account check exists. |
| 276 | settings = ( |
| 277 | f"s1\tinventory.journal.inventory_account\t{INV}\t\n" |
| 278 | f"s2\tinventory.journal.shrinkage_account\t{SHRINK}\t\n" |
| 279 | ) |
| 280 | data_dir = setup_env( |
| 281 | tmp_path, inventory=True, settings_rows=settings, |
| 282 | accounts_rows=[(INV, "Inventory", "asset"), (SHRINK, "Shrinkage", "expense")], |
| 283 | ) |
| 284 | status = object_books_status.books_status(base_dir=data_dir) |
| 285 | assert status["ready"] is True |
| 286 | assert not any(c["id"].startswith("inventory.") and c["id"] not in |
| 287 | ("inventory.inventory_account", "inventory.shrinkage_account") |
| 288 | for c in status["checks"]) |
| 289 | |
| 290 | |
| 291 | def test_banking_fully_configured_is_ready(tmp_path): |
| 292 | settings = ( |
| 293 | f"s1\treconcile.journal.fees_account\t{FEES}\t\n" |
| 294 | f"s2\treconcile.journal.interest_account\t{INTEREST}\t\n" |
| 295 | ) |
| 296 | data_dir = setup_env( |
| 297 | tmp_path, banking=True, settings_rows=settings, |
| 298 | accounts_rows=[(FEES, "Bank Fees", "expense"), (INTEREST, "Interest Income", "income")], |
| 299 | ) |
| 300 | status = object_books_status.books_status(base_dir=data_dir) |
| 301 | assert status["ready"] is True |
| 302 | assert {c["id"] for c in status["checks"]} == { |
| 303 | "banking.fees_account", "banking.interest_account", |
| 304 | } |
| 305 | |
| 306 | |
| 307 | # -- no finance collections at all: clear summary, never a crash -------------- |
| 308 | |
| 309 | def test_no_finance_collections_at_all_is_not_ready(tmp_path): |
| 310 | data_dir = setup_env(tmp_path, books=False, app_settings=True, payments=True) |
| 311 | status = object_books_status.books_status(base_dir=data_dir) |
| 312 | assert status["ready"] is False |
| 313 | assert status["checks"] == [] |
| 314 | assert status["problems"] == [] |
| 315 | assert status["collections"]["fin_journals"] is False |
| 316 | assert "No accounting books are installed" in status["summary"] |
| 317 | assert object_books_status.is_ready(base_dir=data_dir) is False |
| 318 | |
| 319 | |
| 320 | def test_completely_empty_data_dir_never_raises(tmp_path): |
| 321 | # No schemas directory, no collections directory at all -- the |
| 322 | # bare-minimum "nothing has ever been installed here" server. |
| 323 | data_dir = tmp_path / "empty-data" |
| 324 | status = object_books_status.books_status(base_dir=data_dir) |
| 325 | assert status["ready"] is False |
| 326 | assert isinstance(status["summary"], str) and status["summary"] |
| 327 | assert object_books_status.is_ready(base_dir=data_dir) is False |
| 328 | |
| 329 | |
| 330 | def test_no_apps_installed_but_books_present_is_vacuously_ready(tmp_path): |
| 331 | # Books exist (fin_* collections + app_settings) but no payments/ |
| 332 | # inventory/banking app is installed: nothing can compose a journal, |
| 333 | # so there is nothing that could silently skip. |
| 334 | data_dir = setup_env(tmp_path) |
| 335 | status = object_books_status.books_status(base_dir=data_dir) |
| 336 | assert status["ready"] is True |
| 337 | assert status["checks"] == [] |
| 338 | assert status["problems"] == [] |