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_inventory_adjustments.py · 13.8 KB · 318 lines
| 1 | """Inventory adjustments (plan/inventory-adjustments-spec.md): the loss |
| 2 | taxonomy, the cost-stamping/gating hook, the shrinkage composer, and count |
| 3 | reconciliation. Losses are MOVEMENTS with a financial shadow -- never edits |
| 4 | to an on-hand number (docs/logic-decisions.md #3).""" |
| 5 | |
| 6 | import pathlib |
| 7 | from decimal import Decimal |
| 8 | |
| 9 | import object_execution |
| 10 | import object_records |
| 11 | import object_schemas |
| 12 | import object_stock |
| 13 | import python_object_runtime |
| 14 | from conftest import stage_collection |
| 15 | |
| 16 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 17 | PACKAGES = REPO_ROOT / "packages" |
| 18 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 19 | CATALOG_OBJECTS = PACKAGES / "app-catalog" / "objects" |
| 20 | |
| 21 | INV, SHRINK, THEFT = "acct-inventory", "acct-shrinkage", "acct-theft" |
| 22 | |
| 23 | |
| 24 | def setup_env(tmp_path, monkeypatch, *, settings=None): |
| 25 | data_dir = tmp_path / "data" |
| 26 | wanted = [("app-catalog", "products"), ("app-catalog", "locations"), |
| 27 | ("app-catalog", "stock_moves"), |
| 28 | ("app-finance", "fin_journals"), ("app-finance", "fin_journal_lines"), |
| 29 | ("app-finance", "fin_accounts")] |
| 30 | for pkg, name in wanted: |
| 31 | stage_collection(data_dir, pkg, name) |
| 32 | |
| 33 | if settings is None: |
| 34 | settings = (("inventory.journal.inventory_account", INV), |
| 35 | ("inventory.journal.shrinkage_account", SHRINK)) |
| 36 | rows = "" |
| 37 | for i, (k, v) in enumerate(settings): |
| 38 | rows += f"s{i}\t{k}\t{v}\t\n" |
| 39 | stage_collection(data_dir, "app-settings", "app_settings", rows=rows) |
| 40 | |
| 41 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 42 | for acct in (INV, SHRINK, THEFT): |
| 43 | object_records.create_collection_record( |
| 44 | "fin_accounts", |
| 45 | {"id": acct, "name": acct, "account_type": "asset", "owner_id": "dan"}, |
| 46 | base_dir=data_dir) |
| 47 | return data_dir |
| 48 | |
| 49 | |
| 50 | def make_product(data_dir, pid="prod1", cost="250"): |
| 51 | return object_records.create_collection_record( |
| 52 | "products", |
| 53 | {"id": pid, "name": f"Widget {pid}", "product_type": "physical", |
| 54 | "cost_cents": cost, "owner_id": "dan"}, |
| 55 | base_dir=data_dir) |
| 56 | |
| 57 | |
| 58 | def make_location(data_dir, lid="loc1"): |
| 59 | return object_records.create_collection_record( |
| 60 | "locations", |
| 61 | {"id": lid, "name": f"Shelf {lid}", "owner_id": "dan"}, |
| 62 | base_dir=data_dir) |
| 63 | |
| 64 | |
| 65 | def make_move(data_dir, mid, *, reason, qty="1", cost="250", |
| 66 | from_loc="", to_loc="", product="prod1"): |
| 67 | return object_records.create_collection_record( |
| 68 | "stock_moves", |
| 69 | {"id": mid, "product_id": product, "from_location_id": from_loc, |
| 70 | "to_location_id": to_loc, "quantity": qty, "unit_cost_cents": cost, |
| 71 | "reason": reason, "occurred_at": "2026-07-24", "owner_id": "dan"}, |
| 72 | base_dir=data_dir) |
| 73 | |
| 74 | |
| 75 | def run_hook(record, action="create"): |
| 76 | result = object_execution.execute_object( |
| 77 | RUNTIME, |
| 78 | object_execution.ObjectExecutionRequest( |
| 79 | "hook_stock_moves", method="BEFORE_WRITE", |
| 80 | payload={"action": action, "collection": "stock_moves", "record": record}), |
| 81 | roots=[CATALOG_OBJECTS]) |
| 82 | return result.result |
| 83 | |
| 84 | |
| 85 | def fire_stock_books(record_id): |
| 86 | # Mirror the REAL dispatcher payload: event name participle, action RAW. |
| 87 | result = object_execution.execute_object( |
| 88 | RUNTIME, |
| 89 | object_execution.ObjectExecutionRequest( |
| 90 | "system_stock_books", method="EVENT", |
| 91 | payload={"event": "stock_moves.record.created", |
| 92 | "collection": "stock_moves", "record_id": record_id, |
| 93 | "action": "create"}), |
| 94 | roots=[CATALOG_OBJECTS]) |
| 95 | return result.result |
| 96 | |
| 97 | |
| 98 | def apply_count(payload): |
| 99 | result = object_execution.execute_object( |
| 100 | RUNTIME, |
| 101 | object_execution.ObjectExecutionRequest( |
| 102 | "action_apply_count", method="POST", |
| 103 | payload={"_identity": {"user_id": "dan"}, **payload}), |
| 104 | roots=[CATALOG_OBJECTS]) |
| 105 | return result.result |
| 106 | |
| 107 | |
| 108 | def journals(data_dir): |
| 109 | return object_records.read_collection_records("fin_journals", base_dir=data_dir) |
| 110 | |
| 111 | |
| 112 | def lines_for(data_dir, journal_id): |
| 113 | return [l for l in object_records.read_collection_records( |
| 114 | "fin_journal_lines", base_dir=data_dir) |
| 115 | if l["journal_id"] == journal_id] |
| 116 | |
| 117 | |
| 118 | # --- 1. taxonomy ----------------------------------------------------------- |
| 119 | |
| 120 | def test_all_six_loss_reasons_are_valid_and_old_reasons_unaffected(tmp_path, monkeypatch): |
| 121 | data_dir = setup_env(tmp_path, monkeypatch) |
| 122 | make_product(data_dir) |
| 123 | make_location(data_dir) |
| 124 | for i, reason in enumerate(("waste", "breakage", "theft", "expiry", |
| 125 | "damage", "disaster")): |
| 126 | make_move(data_dir, f"m{i}", reason=reason, from_loc="loc1") |
| 127 | make_move(data_dir, "m-old", reason="transfer", from_loc="loc1", to_loc="loc1") |
| 128 | rows = object_records.read_collection_records("stock_moves", base_dir=data_dir) |
| 129 | assert len(rows) == 7 |
| 130 | |
| 131 | |
| 132 | def test_schema_declares_the_hook_and_reason_filter(tmp_path, monkeypatch): |
| 133 | data_dir = setup_env(tmp_path, monkeypatch) |
| 134 | schema = object_schemas.get_schema("stock_moves", base_dir=data_dir) |
| 135 | assert schema["hooks"]["before_write"] == "hook_stock_moves" |
| 136 | assert "reason" in schema["views"]["filter_fields"] |
| 137 | |
| 138 | |
| 139 | # --- 2/3. hook: stamps and gates ------------------------------------------- |
| 140 | |
| 141 | def test_hook_stamps_product_cost_when_absent_and_keeps_explicit_cost(tmp_path, monkeypatch): |
| 142 | data_dir = setup_env(tmp_path, monkeypatch) |
| 143 | make_product(data_dir, cost="250") |
| 144 | stamped = run_hook({"product_id": "prod1", "quantity": "3", |
| 145 | "reason": "waste", "from_location_id": "loc1"}) |
| 146 | assert stamped["record"]["unit_cost_cents"] == "250" |
| 147 | explicit = run_hook({"product_id": "prod1", "quantity": "3", "reason": "purchase", |
| 148 | "to_location_id": "loc1", "unit_cost_cents": "199"}) |
| 149 | assert explicit is None # no transform: the client's invoice cost is kept |
| 150 | |
| 151 | |
| 152 | def test_later_product_cost_edit_never_touches_the_stamped_move(tmp_path, monkeypatch): |
| 153 | data_dir = setup_env(tmp_path, monkeypatch) |
| 154 | make_product(data_dir, cost="250") |
| 155 | make_location(data_dir) |
| 156 | stamped = run_hook({"id": "mv-stamp", "product_id": "prod1", "quantity": "2", |
| 157 | "reason": "waste", "from_location_id": "loc1", |
| 158 | "occurred_at": "2026-07-24", "owner_id": "dan"}) |
| 159 | object_records.create_collection_record( |
| 160 | "stock_moves", stamped["record"], base_dir=data_dir) |
| 161 | object_records.update_collection_record( |
| 162 | "products", "prod1", {"cost_cents": "999"}, base_dir=data_dir) |
| 163 | move = object_records.get_collection_record("stock_moves", "mv-stamp", base_dir=data_dir) |
| 164 | assert move["unit_cost_cents"] == "250" |
| 165 | |
| 166 | |
| 167 | def test_hook_gates_quantity_and_loss_shape(tmp_path, monkeypatch): |
| 168 | setup_env(tmp_path, monkeypatch) |
| 169 | zero = run_hook({"product_id": "p", "quantity": "0", "reason": "waste", |
| 170 | "from_location_id": "loc1"}) |
| 171 | assert zero["status"] == 400 and "positive" in zero["error"] |
| 172 | negative = run_hook({"product_id": "p", "quantity": "-2", "reason": "sale"}) |
| 173 | assert negative["status"] == 400 |
| 174 | no_from = run_hook({"product_id": "p", "quantity": "1", "reason": "theft"}) |
| 175 | assert no_from["status"] == 400 and "from_location_id" in no_from["error"] |
| 176 | with_to = run_hook({"product_id": "p", "quantity": "1", "reason": "waste", |
| 177 | "from_location_id": "loc1", "to_location_id": "loc2"}) |
| 178 | assert with_to["status"] == 400 and "destination" in with_to["error"] |
| 179 | update = run_hook({"quantity": "0"}, action="update") |
| 180 | assert update is None # create-only |
| 181 | |
| 182 | |
| 183 | # --- 4. shrinkage composer --------------------------------------------------- |
| 184 | |
| 185 | def test_waste_composes_posted_shrinkage_journal_idempotently(tmp_path, monkeypatch): |
| 186 | data_dir = setup_env(tmp_path, monkeypatch) |
| 187 | make_product(data_dir) |
| 188 | make_location(data_dir) |
| 189 | make_move(data_dir, "mv1", reason="waste", qty="3", cost="250", from_loc="loc1") |
| 190 | result = fire_stock_books("mv1") |
| 191 | assert result["posted"] is True |
| 192 | journal = journals(data_dir)[0] |
| 193 | assert journal["status"] == "posted" |
| 194 | assert journal["generated_from"] == "stock_moves/mv1" |
| 195 | by_account = {l["account_id"]: l for l in lines_for(data_dir, result["journal_id"])} |
| 196 | assert by_account[SHRINK]["debit_cents"] == "750" |
| 197 | assert by_account[INV]["credit_cents"] == "750" |
| 198 | replay = fire_stock_books("mv1") |
| 199 | assert "already composed" in replay["skipped"] |
| 200 | assert len(journals(data_dir)) == 1 |
| 201 | |
| 202 | |
| 203 | def test_per_reason_account_override_routes_theft(tmp_path, monkeypatch): |
| 204 | data_dir = setup_env(tmp_path, monkeypatch, settings=( |
| 205 | ("inventory.journal.inventory_account", INV), |
| 206 | ("inventory.journal.shrinkage_account", SHRINK), |
| 207 | ("inventory.journal.theft_account", THEFT), |
| 208 | )) |
| 209 | make_product(data_dir) |
| 210 | make_location(data_dir) |
| 211 | make_move(data_dir, "mv-theft", reason="theft", qty="1", cost="1000", from_loc="loc1") |
| 212 | result = fire_stock_books("mv-theft") |
| 213 | by_account = {l["account_id"]: l for l in lines_for(data_dir, result["journal_id"])} |
| 214 | assert by_account[THEFT]["debit_cents"] == "1000" |
| 215 | assert by_account[INV]["credit_cents"] == "1000" |
| 216 | |
| 217 | |
| 218 | def test_unconfigured_accounts_skip_and_move_is_unaffected(tmp_path, monkeypatch): |
| 219 | data_dir = setup_env(tmp_path, monkeypatch, settings=()) |
| 220 | make_product(data_dir) |
| 221 | make_location(data_dir) |
| 222 | make_move(data_dir, "mv1", reason="waste", qty="3", from_loc="loc1") |
| 223 | result = fire_stock_books("mv1") |
| 224 | assert result["skipped"] == "accounts unconfigured" |
| 225 | assert journals(data_dir) == [] |
| 226 | assert object_records.get_collection_record("stock_moves", "mv1", base_dir=data_dir) |
| 227 | |
| 228 | |
| 229 | def test_a_transfer_shaped_adjustment_composes_nothing(tmp_path, monkeypatch): |
| 230 | """Both locations set: goods moved between shelves, no value changed.""" |
| 231 | data_dir = setup_env(tmp_path, monkeypatch) |
| 232 | make_product(data_dir) |
| 233 | make_location(data_dir) |
| 234 | make_move(data_dir, "mv-tr", reason="adjustment", qty="2", |
| 235 | from_loc="loc1", to_loc="loc1") |
| 236 | assert fire_stock_books("mv-tr")["skipped"] == "not a loss event" |
| 237 | assert journals(data_dir) == [] |
| 238 | |
| 239 | |
| 240 | def test_a_sale_needs_its_own_account_and_never_borrows_shrinkage( |
| 241 | tmp_path, monkeypatch): |
| 242 | """A sale IS a composable direction now (COGS-on-sale, see |
| 243 | test_cogs_on_sale.py) -- but this env configures only inventory and |
| 244 | shrinkage, and cost of sales must never fall back to the shrinkage |
| 245 | account the loss reasons default to. Goods sold are not goods lost, |
| 246 | and routing one into the other misstates the P&L while still |
| 247 | balancing, which is the failure mode nobody notices.""" |
| 248 | data_dir = setup_env(tmp_path, monkeypatch) |
| 249 | make_product(data_dir) |
| 250 | make_location(data_dir) |
| 251 | make_move(data_dir, "mv-sale", reason="sale", qty="2", from_loc="loc1") |
| 252 | assert fire_stock_books("mv-sale")["skipped"] == "accounts unconfigured" |
| 253 | assert journals(data_dir) == [] |
| 254 | |
| 255 | |
| 256 | # --- 5. count reconciliation ------------------------------------------------- |
| 257 | |
| 258 | def test_count_shortage_writes_move_out_and_shrinkage_journal(tmp_path, monkeypatch): |
| 259 | data_dir = setup_env(tmp_path, monkeypatch) |
| 260 | make_product(data_dir, cost="250") |
| 261 | make_location(data_dir) |
| 262 | make_move(data_dir, "mv-in", reason="purchase", qty="10", to_loc="loc1") |
| 263 | result = apply_count({"product_id": "prod1", "location_id": "loc1", |
| 264 | "counted_qty": "7"}) |
| 265 | assert result["status"] == 200 and result["direction"] == "shortage" |
| 266 | assert result["variance"] == "-3" |
| 267 | move = object_records.get_collection_record("stock_moves", result["move_id"], |
| 268 | base_dir=data_dir) |
| 269 | assert move["from_location_id"] == "loc1" and move["to_location_id"] == "" |
| 270 | assert move["quantity"] == "3" and move["reference"] == "count variance" |
| 271 | assert move["unit_cost_cents"] == "250" |
| 272 | by_account = {l["account_id"]: l |
| 273 | for l in lines_for(data_dir, result["journal"]["journal_id"])} |
| 274 | assert by_account[SHRINK]["debit_cents"] == "750" |
| 275 | assert object_stock.quantity_at_location( |
| 276 | "prod1", "loc1", base_dir=data_dir) == Decimal("7") |
| 277 | |
| 278 | |
| 279 | def test_count_overage_writes_move_in_and_mirror_journal(tmp_path, monkeypatch): |
| 280 | data_dir = setup_env(tmp_path, monkeypatch) |
| 281 | make_product(data_dir, cost="100") |
| 282 | make_location(data_dir) |
| 283 | make_move(data_dir, "mv-in", reason="purchase", qty="10", to_loc="loc1") |
| 284 | result = apply_count({"product_id": "prod1", "location_id": "loc1", |
| 285 | "counted_qty": "12"}) |
| 286 | assert result["direction"] == "overage" and result["variance"] == "2" |
| 287 | by_account = {l["account_id"]: l |
| 288 | for l in lines_for(data_dir, result["journal"]["journal_id"])} |
| 289 | assert by_account[INV]["debit_cents"] == "200" |
| 290 | assert by_account[SHRINK]["credit_cents"] == "200" |
| 291 | assert object_stock.quantity_at_location( |
| 292 | "prod1", "loc1", base_dir=data_dir) == Decimal("12") |
| 293 | |
| 294 | |
| 295 | def test_count_match_is_a_no_op(tmp_path, monkeypatch): |
| 296 | data_dir = setup_env(tmp_path, monkeypatch) |
| 297 | make_product(data_dir) |
| 298 | make_location(data_dir) |
| 299 | make_move(data_dir, "mv-in", reason="purchase", qty="5", to_loc="loc1") |
| 300 | result = apply_count({"product_id": "prod1", "location_id": "loc1", |
| 301 | "counted_qty": "5"}) |
| 302 | assert result["variance"] == "0" |
| 303 | moves = object_records.read_collection_records("stock_moves", base_dir=data_dir) |
| 304 | assert len(moves) == 1 # only the purchase |
| 305 | assert journals(data_dir) == [] |
| 306 | |
| 307 | |
| 308 | # --- 6. levels reflect losses ----------------------------------------------- |
| 309 | |
| 310 | def test_stock_levels_reflect_loss_moves(tmp_path, monkeypatch): |
| 311 | data_dir = setup_env(tmp_path, monkeypatch) |
| 312 | make_product(data_dir) |
| 313 | make_location(data_dir) |
| 314 | make_move(data_dir, "mv-in", reason="purchase", qty="10", to_loc="loc1") |
| 315 | make_move(data_dir, "mv-waste", reason="waste", qty="3", from_loc="loc1") |
| 316 | make_move(data_dir, "mv-theft", reason="theft", qty="1", from_loc="loc1") |
| 317 | assert object_stock.quantity_at_location( |
| 318 | "prod1", "loc1", base_dir=data_dir) == Decimal("6") |