Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_object_stock.py
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.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_object_stock.py · 10.6 KB · 245 lines
1"""Behavior tests for object_stock.py: the derived-levels fold over the
2immutable stock_moves append log (packages/app-catalog's STOCK slice).
3
4Mirrors tests/test_app_orders_totals.py's install-then-exercise shape:
5install app-catalog into an isolated data dir, write products/locations/
6stock_moves records directly via object_records, then call object_stock's
7functions and assert on the result. Structural/manifest/permission tests
8for the schemas live in tests/test_app_catalog_package.py.
9"""
10
11import os
12from decimal import Decimal
13from pathlib import Path
14
15import object_packages
16import object_records
17import object_stock
18
19PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages"
20
21
22def _install(tmp_path):
23 """Install app-catalog into an isolated data dir/object root and
24 return data_dir. Keeps DBBASIC_DATA_DIR in sync with tmp_path the same
25 way test_app_orders_totals.py's _install() does, since object_stock's
26 callers (objects/site/stock.py) resolve base_dir from that env var.
27 """
28 data_dir = tmp_path / "data"
29 object_root = tmp_path / "objects"
30 object_root.mkdir()
31 os.environ["DBBASIC_DATA_DIR"] = str(data_dir)
32
33 object_packages.install_package(
34 "app-catalog", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root]
35 )
36 return data_dir
37
38
39def _make_product(data_dir, **overrides):
40 record = {"id": "p1", "name": "Widget", "owner_id": "u1"}
41 record.update(overrides)
42 return object_records.create_collection_record("products", record, base_dir=data_dir, actor="test")
43
44
45def _make_location(data_dir, **overrides):
46 record = {"id": "loc1", "name": "Main Warehouse", "location_type": "warehouse", "owner_id": "u1"}
47 record.update(overrides)
48 return object_records.create_collection_record("locations", record, base_dir=data_dir, actor="test")
49
50
51def _make_move(data_dir, **overrides):
52 record = {"id": "m1", "product_id": "p1", "quantity": "1", "owner_id": "u1"}
53 record.update(overrides)
54 return object_records.create_collection_record("stock_moves", record, base_dir=data_dir, actor="test")
55
56
57# -- quantity_at_location ---------------------------------------------------
58
59def test_quantity_at_location_nets_moves_in_and_out(tmp_path):
60 data_dir = _install(tmp_path)
61 _make_product(data_dir)
62 _make_location(data_dir, id="wh", location_type="warehouse")
63 _make_move(data_dir, id="m1", to_location_id="wh", quantity="10", reason="purchase")
64 _make_move(data_dir, id="m2", from_location_id="wh", quantity="4", reason="sale")
65
66 qty = object_stock.quantity_at_location("p1", "wh", base_dir=data_dir, owner="u1")
67 assert qty == Decimal("6")
68 assert isinstance(qty, Decimal)
69
70
71def test_quantity_at_location_is_zero_for_a_never_touched_location(tmp_path):
72 data_dir = _install(tmp_path)
73 _make_product(data_dir)
74 _make_location(data_dir, id="wh")
75
76 assert object_stock.quantity_at_location("p1", "wh", base_dir=data_dir, owner="u1") == Decimal("0")
77
78
79def test_quantity_at_location_returns_zero_for_a_blank_location_id(tmp_path):
80 """Blank location ids are the source model's "external
81 origin/destination" sentinel -- they name no real location, so there
82 is nothing to hold a balance at.
83 """
84 data_dir = _install(tmp_path)
85 _make_product(data_dir)
86
87 assert object_stock.quantity_at_location("p1", "", base_dir=data_dir, owner="u1") == Decimal("0")
88
89
90def test_a_transfer_moves_quantity_between_two_real_locations(tmp_path):
91 data_dir = _install(tmp_path)
92 _make_product(data_dir)
93 _make_location(data_dir, id="wh_a", name="Warehouse A")
94 _make_location(data_dir, id="wh_b", name="Warehouse B")
95 _make_move(data_dir, id="m1", to_location_id="wh_a", quantity="10", reason="purchase")
96 _make_move(data_dir, id="m2", from_location_id="wh_a", to_location_id="wh_b", quantity="6", reason="transfer")
97
98 assert object_stock.quantity_at_location("p1", "wh_a", base_dir=data_dir, owner="u1") == Decimal("4")
99 assert object_stock.quantity_at_location("p1", "wh_b", base_dir=data_dir, owner="u1") == Decimal("6")
100
101
102# -- total_quantity: virtual-location exclusion ------------------------------
103
104def test_total_quantity_excludes_virtual_customer_and_supplier_locations(tmp_path):
105 """total_quantity is real on-hand only -- matches the source model's
106 own total_quantity semantics: a sale to a virtual customer location
107 reduces on-hand (it leaves the real warehouse) but the virtual
108 location's own positive balance is never counted as "on hand."
109 """
110 data_dir = _install(tmp_path)
111 _make_product(data_dir)
112 _make_location(data_dir, id="wh", location_type="warehouse")
113 _make_location(data_dir, id="cust", location_type="customer")
114 _make_location(data_dir, id="supp", location_type="supplier")
115
116 _make_move(data_dir, id="m1", from_location_id="supp", to_location_id="wh", quantity="20", reason="purchase")
117 _make_move(data_dir, id="m2", from_location_id="wh", to_location_id="cust", quantity="8", reason="sale")
118
119 # wh: +20 -8 = 12 (real, counted). cust: +8 (virtual, excluded).
120 # supp: -20 (virtual, excluded) -- would otherwise cancel wh's +20.
121 total = object_stock.total_quantity("p1", base_dir=data_dir, owner="u1")
122 assert total == Decimal("12")
123
124
125def test_total_quantity_excludes_plain_virtual_type_too(tmp_path):
126 data_dir = _install(tmp_path)
127 _make_product(data_dir)
128 _make_location(data_dir, id="wh", location_type="warehouse")
129 _make_location(data_dir, id="void", location_type="virtual")
130 _make_move(data_dir, id="m1", to_location_id="wh", quantity="5", reason="purchase")
131 _make_move(data_dir, id="m2", from_location_id="wh", to_location_id="void", quantity="2", reason="adjustment")
132
133 assert object_stock.total_quantity("p1", base_dir=data_dir, owner="u1") == Decimal("3")
134
135
136def test_virtual_location_ids_resolves_by_type(tmp_path):
137 data_dir = _install(tmp_path)
138 _make_location(data_dir, id="wh", location_type="warehouse")
139 _make_location(data_dir, id="cust", location_type="customer")
140 _make_location(data_dir, id="supp", location_type="supplier")
141 _make_location(data_dir, id="void", location_type="virtual")
142
143 virtual_ids = object_stock.virtual_location_ids(base_dir=data_dir, owner="u1")
144 assert virtual_ids == {"cust", "supp", "void"}
145
146
147# -- Decimal precision for fractional quantities -----------------------------
148
149def test_fractional_quantity_folds_exactly_via_decimal(tmp_path):
150 """2.5 - 0.7 must be exactly 1.8, not a binary-float artifact like
151 1.7999999999999998 -- Decimal arithmetic throughout, never a bare
152 float, same discipline app-orders' order_totals.py documents.
153 """
154 data_dir = _install(tmp_path)
155 _make_product(data_dir)
156 _make_location(data_dir, id="wh")
157 _make_move(data_dir, id="m1", to_location_id="wh", quantity="2.5", reason="purchase")
158 _make_move(data_dir, id="m2", from_location_id="wh", quantity="0.7", reason="sale")
159
160 qty = object_stock.quantity_at_location("p1", "wh", base_dir=data_dir, owner="u1")
161 assert qty == Decimal("1.8")
162 assert str(qty) == "1.8"
163
164
165# -- Oversold / negative levels: documented choice, no floor ----------------
166
167def test_overselling_a_location_goes_negative_rather_than_floored_at_zero(tmp_path):
168 """object_stock.py's documented choice (the source audit does not
169 spell out clamp-vs-negative behavior): no floor is applied anywhere
170 in the fold. Shipping out more than was ever received folds to a
171 visible negative Decimal, not zero -- an oversold location is a real
172 data-quality signal this module does not hide.
173 """
174 data_dir = _install(tmp_path)
175 _make_product(data_dir)
176 _make_location(data_dir, id="wh")
177 _make_move(data_dir, id="m1", to_location_id="wh", quantity="5", reason="purchase")
178 _make_move(data_dir, id="m2", from_location_id="wh", quantity="9", reason="sale")
179
180 qty = object_stock.quantity_at_location("p1", "wh", base_dir=data_dir, owner="u1")
181 assert qty == Decimal("-4")
182
183 total = object_stock.total_quantity("p1", base_dir=data_dir, owner="u1")
184 assert total == Decimal("-4")
185
186
187# -- Owner scoping ------------------------------------------------------------
188
189def test_moves_are_scoped_to_the_requesting_owner(tmp_path):
190 """A second owner's stock_moves rows must never leak into another
191 owner's derived levels, matching permissions/rules.json's row_filter
192 owner_id == $user_id on both locations and stock_moves.
193 """
194 data_dir = _install(tmp_path)
195 _make_product(data_dir, id="p1", owner_id="u1")
196 _make_location(data_dir, id="wh", owner_id="u1")
197 _make_move(data_dir, id="m1", to_location_id="wh", quantity="10", owner_id="u1")
198 # A different owner's move against the same product/location ids.
199 _make_move(data_dir, id="m2", to_location_id="wh", quantity="99", owner_id="u2")
200
201 assert object_stock.quantity_at_location("p1", "wh", base_dir=data_dir, owner="u1") == Decimal("10")
202 assert object_stock.quantity_at_location("p1", "wh", base_dir=data_dir, owner="u2") == Decimal("99")
203
204 unscoped = object_stock.quantity_at_location("p1", "wh", base_dir=data_dir, owner=None)
205 assert unscoped == Decimal("109")
206
207
208# -- stock_levels: the page-ready summary ------------------------------------
209
210def test_stock_levels_summary_shape(tmp_path):
211 data_dir = _install(tmp_path)
212 _make_product(data_dir, id="p1")
213 _make_location(data_dir, id="wh", location_type="warehouse")
214 _make_location(data_dir, id="cust", location_type="customer")
215 _make_move(data_dir, id="m1", to_location_id="wh", quantity="10", reason="purchase")
216 _make_move(data_dir, id="m2", from_location_id="wh", to_location_id="cust", quantity="3", reason="sale")
217
218 summary = object_stock.stock_levels(base_dir=data_dir, owner="u1")
219
220 levels_by_key = {(row["product_id"], row["location_id"]): row["quantity"] for row in summary["levels"]}
221 assert levels_by_key == {("p1", "wh"): "7", ("p1", "cust"): "3"}
222
223 totals_by_product = {row["product_id"]: row["quantity"] for row in summary["totals"]}
224 # cust is virtual -- excluded from the on-hand total.
225 assert totals_by_product == {"p1": "7"}
226
227
228def test_stock_levels_is_empty_when_no_moves_exist(tmp_path):
229 data_dir = _install(tmp_path)
230 _make_product(data_dir)
231
232 summary = object_stock.stock_levels(base_dir=data_dir, owner="u1")
233 assert summary == {"levels": [], "totals": []}
234
235
236def test_quantities_are_returned_as_exact_strings_not_floats(tmp_path):
237 data_dir = _install(tmp_path)
238 _make_product(data_dir)
239 _make_location(data_dir, id="wh")
240 _make_move(data_dir, id="m1", to_location_id="wh", quantity="2.5", reason="purchase")
241
242 summary = object_stock.stock_levels(base_dir=data_dir, owner="u1")
243 row = summary["levels"][0]
244 assert row["quantity"] == "2.5"
245 assert isinstance(row["quantity"], str)