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.
test_stock_losses_and_decimals.py · 7.9 KB · 137 lines
| 1 | """Stock by the litre: decimal quantities, evaporation and shrinkage, and a |
| 2 | count that says when a shortage is more than this product normally loses. |
| 3 | |
| 4 | Quantities were already `number` and folded with Decimal (object_stock), so |
| 5 | the question was not "add decimals" but "do they stay exact everywhere a |
| 6 | quantity is read": the level, the moving average a sale is costed at, the |
| 7 | variance a count computes and the journal it books. Each is checked here |
| 8 | through the real ASGI app against figures worked by hand in the comments. |
| 9 | |
| 10 | App-assets is deliberately NOT installed: the expected-loss flag must work |
| 11 | without it (the loss event it would open is a soft dependency). |
| 12 | """ |
| 13 | |
| 14 | from __future__ import annotations |
| 15 | |
| 16 | from decimal import Decimal |
| 17 | |
| 18 | import pytest |
| 19 | |
| 20 | import object_records |
| 21 | import object_stock |
| 22 | from scripts.try_package import Sandbox |
| 23 | |
| 24 | |
| 25 | class Shop: |
| 26 | def __init__(self, box): |
| 27 | self.box = box |
| 28 | self.acct = {} |
| 29 | for name, kind in (("Inventory", "asset"), ("Shrinkage", "expense"), ("Evaporation", "expense")): |
| 30 | self.acct[name] = self.create("fin_accounts", {"name": name, "account_type": kind}) |
| 31 | for key, name in (("inventory.journal.inventory_account", "Inventory"), |
| 32 | ("inventory.journal.shrinkage_account", "Shrinkage"), |
| 33 | ("inventory.journal.evaporation_account", "Evaporation")): |
| 34 | assert box.call("admin", "POST", "/collections/app_settings/records", |
| 35 | {"key": key, "value": self.acct[name]})[0] == 201 |
| 36 | |
| 37 | def create(self, collection, record, expect=201): |
| 38 | status, body = self.box.call("dan", "POST", f"/collections/{collection}/records", |
| 39 | {"owner_id": "dan", **record}) |
| 40 | assert status == expect, (collection, status, body) |
| 41 | return body["record"]["id"] if status == 201 else body |
| 42 | |
| 43 | def count(self, **body): |
| 44 | return self.box.call("dan", "POST", "/objects/action_apply_count", body) |
| 45 | |
| 46 | def journal_lines(self, marker): |
| 47 | data = self.box.data_dir |
| 48 | journal = next(j for j in object_records.read_collection_records("fin_journals", base_dir=data) |
| 49 | if j.get("generated_from") == marker) |
| 50 | names = {v: k for k, v in self.acct.items()} |
| 51 | return {(names[l["account_id"]], int(l["debit_cents"] or 0), int(l["credit_cents"] or 0)) |
| 52 | for l in object_records.read_collection_records("fin_journal_lines", base_dir=data) |
| 53 | if l["journal_id"] == journal["id"]} |
| 54 | |
| 55 | |
| 56 | @pytest.fixture |
| 57 | def shop(): |
| 58 | with Sandbox(["app-catalog", "app-finance", "app-settings"]) as box: |
| 59 | box.add_user("dan", roles=["owner"]) # the owner keeps the books (decision 5) |
| 60 | yield Shop(box) |
| 61 | |
| 62 | |
| 63 | def _cask(shop, expected_bps="250"): |
| 64 | product = shop.create("products", {"name": "Whisky (cask)", "unit": "L", "cost_cents": 1_100, |
| 65 | "expected_loss_bps": expected_bps}) |
| 66 | store = shop.create("locations", {"name": "Bonded store", "location_type": "warehouse"}) |
| 67 | return product, store |
| 68 | |
| 69 | |
| 70 | def test_decimal_quantities_keep_levels_average_cost_and_counts_exact(shop): |
| 71 | product, store = _cask(shop) |
| 72 | shop.create("stock_moves", {"product_id": product, "to_location_id": store, "quantity": "100", |
| 73 | "unit_cost_cents": 1_000, "reason": "purchase", "occurred_at": "2025-01-05"}) |
| 74 | shop.create("stock_moves", {"product_id": product, "to_location_id": store, "quantity": "50.5", |
| 75 | "unit_cost_cents": 1_300, "reason": "purchase", "occurred_at": "2025-02-05"}) |
| 76 | # Moving average: (100 x 1000 + 50.5 x 1300) / 150.5 = 165650 / 150.5 = 1100.66 -> 1101. |
| 77 | sale = shop.create("stock_moves", {"product_id": product, "from_location_id": store, "quantity": "10.25", |
| 78 | "reason": "sale", "occurred_at": "2025-03-01"}) |
| 79 | status, body = shop.box.call("dan", "GET", f"/collections/stock_moves/records/{sale}") |
| 80 | assert body["record"]["unit_cost_cents"] == "1101" |
| 81 | data = shop.box.data_dir |
| 82 | assert object_stock.total_quantity(product, base_dir=data, owner="dan") == Decimal("140.25") |
| 83 | assert object_stock.average_cost_for_product(product, base_dir=data, owner="dan") == 1101 |
| 84 | # Count 137.1 of a book 140.25: 3.15 short = 224.6 bps, inside the 250 expected. |
| 85 | status, body = shop.count(product_id=product, location_id=store, counted_qty="137.1", |
| 86 | counted_on="2025-06-30", reason="evaporation") |
| 87 | assert status == 200, body |
| 88 | assert (body["variance"], body["on_hand_before"]) == ("-3.15", "140.25") |
| 89 | assert body["expected_loss"] == {"expected_bps": 250, "actual_bps": 225, "above_expected": False} |
| 90 | # 3.15 L x 11.00 = 34.65: 3465 cents, to the evaporation account. |
| 91 | assert shop.journal_lines(f"stock_moves/{body['move_id']}") == {("Evaporation", 3_465, 0), |
| 92 | ("Inventory", 0, 3_465)} |
| 93 | status, move = shop.box.call("dan", "GET", f"/collections/stock_moves/records/{body['move_id']}") |
| 94 | assert move["record"]["reason"] == "evaporation" and move["record"]["quantity"] == "3.15" |
| 95 | assert object_stock.total_quantity(product, base_dir=data, owner="dan") == Decimal("137.1") |
| 96 | # A second count against the new state is an ordinary count: 137.1 -> 137.1 is no variance. |
| 97 | status, body = shop.count(product_id=product, location_id=store, counted_qty="137.10") |
| 98 | assert status == 200 and body["variance"] == "0" |
| 99 | |
| 100 | |
| 101 | def test_a_shortage_above_normal_is_flagged_even_without_app_assets(shop): |
| 102 | product, store = _cask(shop, expected_bps="200") |
| 103 | shop.create("stock_moves", {"product_id": product, "to_location_id": store, "quantity": "200.5", |
| 104 | "unit_cost_cents": 1_100, "reason": "purchase"}) |
| 105 | # 194.2 of 200.5: 6.3 short = 314.2 bps, above 200. |
| 106 | status, body = shop.count(product_id=product, location_id=store, counted_qty="194.2", reason="shrinkage") |
| 107 | assert status == 200 |
| 108 | assert body["expected_loss"]["above_expected"] is True and body["expected_loss"]["actual_bps"] == 314 |
| 109 | assert "not installed" in body["expected_loss"]["loss_event"]["skipped"] |
| 110 | # Shrinkage has no account of its own beyond the shrinkage account. |
| 111 | assert shop.journal_lines(f"stock_moves/{body['move_id']}") == {("Shrinkage", 6_930, 0), |
| 112 | ("Inventory", 0, 6_930)} |
| 113 | |
| 114 | |
| 115 | def test_a_product_with_no_expectation_flags_nothing_and_an_overage_is_no_loss(shop): |
| 116 | product, store = _cask(shop, expected_bps="") |
| 117 | shop.create("stock_moves", {"product_id": product, "to_location_id": store, "quantity": "10", |
| 118 | "reason": "purchase"}) |
| 119 | status, body = shop.count(product_id=product, location_id=store, counted_qty="9") |
| 120 | assert status == 200 and "expected_loss" not in body |
| 121 | status, body = shop.count(product_id=product, location_id=store, counted_qty="12", reason="evaporation") |
| 122 | assert status == 400 and "cannot add stock" in body["error"] |
| 123 | status, body = shop.count(product_id=product, location_id=store, counted_qty="8", reason="theft") |
| 124 | assert status == 400 |
| 125 | |
| 126 | |
| 127 | def test_evaporation_and_shrinkage_are_loss_moves_that_leave_a_shelf(shop): |
| 128 | product, store = _cask(shop) |
| 129 | shop.create("stock_moves", {"product_id": product, "to_location_id": store, "quantity": "10", |
| 130 | "reason": "purchase"}) |
| 131 | for reason in ("evaporation", "shrinkage"): |
| 132 | shop.create("stock_moves", {"product_id": product, "quantity": "0.5", "reason": reason}, expect=400) |
| 133 | shop.create("stock_moves", {"product_id": product, "from_location_id": store, "to_location_id": store, |
| 134 | "quantity": "0.5", "reason": reason}, expect=400) |
| 135 | shop.create("stock_moves", {"product_id": product, "from_location_id": store, "quantity": "0.25", |
| 136 | "reason": reason}) |
| 137 | assert object_stock.total_quantity(product, base_dir=shop.box.data_dir, owner="dan") == Decimal("9.50") |