Integra inventario WMS-SAM nella versione monolista con test e guida deploy
This commit is contained in:
202
tests/test_inventory.py
Normal file
202
tests/test_inventory.py
Normal file
@@ -0,0 +1,202 @@
|
||||
"""Domain and XLSX regression tests: no live stock is modified."""
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from inventory_service import (DETAIL_KEYS, SUMMARY_KEYS, build_report, export_inventory,
|
||||
inventory_cutoff, load_inventory)
|
||||
from locale_text import text
|
||||
|
||||
DAY = date(2025, 12, 31)
|
||||
HISTORY = dict(Kind="META", Component="HIST_MIN", SourceDate=datetime(2022, 1, 1))
|
||||
|
||||
|
||||
def stock(**changes):
|
||||
r = dict(Kind="ROW", Component="A", Product="P1", Description="Product",
|
||||
Unit="NR", ContainerId=1, Lot="L1", Serial="600001/000001",
|
||||
Pallet="600001", Cell=1101, Location="1A.1.a", Balance=Decimal(1),
|
||||
Quantity=Decimal(20), SamQuantity=Decimal(20), Locations=1,
|
||||
NegativeBalances=0, Shipped=0, AssignmentIssue=0,
|
||||
MovementIssues=0, OtherReceipts=0)
|
||||
r.update(changes)
|
||||
return r
|
||||
|
||||
|
||||
def report(*records):
|
||||
return build_report(DAY, [HISTORY, *records])
|
||||
|
||||
|
||||
def packing(**changes):
|
||||
return stock(Component="B", ContainerId=2, Pallet="600002", Serial="600002/000002",
|
||||
Cell=None, Location=None, Balance=None, Locations=0, **changes)
|
||||
|
||||
|
||||
def loose(**changes):
|
||||
return stock(Component="C", ContainerId=3, Pallet="000000", Serial="000000/000003",
|
||||
Cell=None, Location=None, Balance=None, Locations=0, **changes)
|
||||
|
||||
|
||||
class InventoryTests(unittest.TestCase):
|
||||
def test_year_end_exclusive_cutoff(self):
|
||||
self.assertEqual(inventory_cutoff("31/12/2025"), (DAY, datetime(2026, 1, 1), 46023))
|
||||
|
||||
def test_dates_invalid_future_and_leap(self):
|
||||
for value in ("31/02/2025", "2025-12-31", "01/01/3000", "31/12/2009"):
|
||||
with self.assertRaises(ValueError):
|
||||
inventory_cutoff(value)
|
||||
self.assertEqual(inventory_cutoff("29/02/2024")[1], datetime(2024, 3, 1))
|
||||
|
||||
def test_three_components_and_sign(self):
|
||||
r = report(stock(), packing(Quantity=10, SamQuantity=10), loose(Quantity=5, SamQuantity=5))
|
||||
row = r.rows[0]
|
||||
self.assertEqual([row[k] for k in ("WMS", "Packing", "Loose", "Reconstructed", "SAM", "Difference")],
|
||||
[20, 10, 5, 35, 35, 0])
|
||||
self.assertEqual(row["InitialDifference"], 15)
|
||||
self.assertEqual(row["LooseContainers"], 1)
|
||||
|
||||
def test_non_shelved_is_a_not_b(self):
|
||||
row = report(stock(Cell=1000)).rows[0]
|
||||
self.assertEqual((row["NonShelved"], row["Packing"], row["UDCs"]), (20, 0, 1))
|
||||
|
||||
def test_multilot_counts_udc_once_and_keeps_lots(self):
|
||||
r = report(stock(), stock(ContainerId=4, Lot="L2", Serial="600001/000004"))
|
||||
self.assertEqual((r.rows[0]["UDCs"], r.rows[0]["WMS"]), (1, 40))
|
||||
self.assertEqual(len(r.lots), 2)
|
||||
|
||||
def test_multi_product_udc(self):
|
||||
r = report(stock(), stock(ContainerId=4, Product="P2"))
|
||||
self.assertEqual([row["UDCs"] for row in r.rows], [1, 1])
|
||||
|
||||
def test_lot_offsets_do_not_look_like_a_match(self):
|
||||
row = report(stock(SamQuantity=10), stock(ContainerId=2, Lot="L2", SamQuantity=30)).rows[0]
|
||||
self.assertEqual(row["Difference"], 0)
|
||||
self.assertIn("lot_offsets", row["Issues"])
|
||||
|
||||
def test_multilocation_does_not_double_sam(self):
|
||||
r = report(stock(Locations=2), stock(Locations=2, Cell=8004))
|
||||
self.assertEqual((r.rows[0]["SAM"], r.rows[0]["UDCs"]), (20, 1))
|
||||
self.assertIsNone(r.rows[0]["WMS"])
|
||||
self.assertIn("locations", r.rows[0]["Issues"])
|
||||
|
||||
def test_duplicate_incompatible_data_invalidates(self):
|
||||
row = report(stock(), stock(Quantity=50)).rows[0]
|
||||
self.assertIn("duplicate", row["Issues"])
|
||||
self.assertIsNone(row["SAM"])
|
||||
|
||||
def test_missing_content_is_not_zero(self):
|
||||
row = report(stock(Quantity=None)).rows[0]
|
||||
self.assertIsNone(row["WMS"])
|
||||
self.assertIsNone(row["Difference"])
|
||||
self.assertEqual(row["SAM"], 20)
|
||||
|
||||
def test_shipped_residual_is_not_packaging(self):
|
||||
row = report(
|
||||
stock(), stock(Component="X", ContainerId=9, Pallet="600009",
|
||||
Cell=None, Locations=1, Shipped=1, Quantity=5, SamQuantity=5)).rows[0]
|
||||
self.assertEqual((row["Packing"], row["SAM"], row["Difference"]), (0, 25, 5))
|
||||
self.assertIn("shipped", row["Issues"])
|
||||
|
||||
def test_sam_zero_does_not_hide_wms_presence(self):
|
||||
row = report(stock(SamQuantity=0)).rows[0]
|
||||
self.assertEqual((row["WMS"], row["Difference"]), (20, -20))
|
||||
|
||||
def test_partial_container_uses_actual_quantity(self):
|
||||
row = report(loose(Quantity=Decimal("3.5"), SamQuantity=Decimal("3.5"))).rows[0]
|
||||
self.assertEqual(row["Loose"], Decimal("3.5"))
|
||||
|
||||
def test_history_missing_or_ambiguous_is_not_certified(self):
|
||||
for r in (build_report(DAY, [stock()]), report(stock(AssignmentIssue=1)),
|
||||
build_report(date(2020, 1, 1), [HISTORY, stock()])):
|
||||
self.assertIn("history", r.rows[0]["Issues"])
|
||||
self.assertIsNone(r.rows[0]["Difference"])
|
||||
|
||||
def test_movements_missing_invalidates_sam(self):
|
||||
row = report(stock(MovementIssues=1)).rows[0]
|
||||
self.assertIsNone(row["SAM"])
|
||||
|
||||
def test_negative_balance_flagged(self):
|
||||
row = report(loose(Quantity=-5, SamQuantity=-5)).rows[0]
|
||||
self.assertIn("negative", row["Issues"])
|
||||
self.assertIsNone(row["Difference"])
|
||||
|
||||
def test_other_receipt_requires_validation(self):
|
||||
row = report(stock(OtherReceipts=1)).rows[0]
|
||||
self.assertIsNone(row["WMS"])
|
||||
|
||||
def test_unknown_unit(self):
|
||||
row = report(stock(Unit="KG")).rows[0]
|
||||
self.assertIn("unit", row["Issues"])
|
||||
self.assertIsNone(row["SAM"])
|
||||
|
||||
def test_undated_wms(self):
|
||||
row = report(stock(), dict(Kind="META", Component="UNDATED", Quantity=1)).rows[0]
|
||||
self.assertIsNone(row["Difference"])
|
||||
|
||||
def test_missing_label_remains_visible(self):
|
||||
row = report(stock(Product=None, ContainerId=None, Quantity=None)).rows[0]
|
||||
self.assertIn("600001", row["Product"])
|
||||
self.assertIsNone(row["WMS"])
|
||||
|
||||
def test_empty_report(self):
|
||||
r = report()
|
||||
self.assertEqual((r.rows, r.lots, r.details), ([], [], []))
|
||||
|
||||
def test_export_roundtrip(self):
|
||||
from openpyxl import load_workbook
|
||||
r = report(stock(Product="=CODE"), stock(Product="P2", ContainerId=2, Quantity=None))
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
path = str(Path(folder) / "inventory.xlsx")
|
||||
export_inventory(r, path, translate=text)
|
||||
wb = load_workbook(path)
|
||||
ws = wb.active
|
||||
self.assertEqual(len(wb.worksheets), 3)
|
||||
self.assertEqual(ws["B1"].value.date(), DAY)
|
||||
self.assertEqual((ws["A7"].value, ws["A7"].data_type), ("=CODE", "s"))
|
||||
diff_col = SUMMARY_KEYS.index("Difference") + 1
|
||||
self.assertEqual(ws.cell(7, diff_col).value, 0)
|
||||
self.assertIsNone(ws.cell(8, SUMMARY_KEYS.index("WMS")+1).value)
|
||||
self.assertEqual(ws.freeze_panes, "A7")
|
||||
self.assertEqual(wb.worksheets[2].cell(2, DETAIL_KEYS.index("Serial")+1).value, "600001/000001")
|
||||
wb.close()
|
||||
|
||||
def test_translations_complete(self):
|
||||
catalog = json.loads(Path("locale.json").read_text(encoding="utf-8"))
|
||||
for language in ("IT", "ENG"):
|
||||
for key in SUMMARY_KEYS + DETAIL_KEYS:
|
||||
self.assertIn("inventory.col." + key, catalog[language])
|
||||
for component in "ABCX":
|
||||
self.assertIn("inventory.component." + component, catalog[language])
|
||||
self.assertEqual({k for k in catalog["IT"] if k.startswith("inventory.")},
|
||||
{k for k in catalog["ENG"] if k.startswith("inventory.")})
|
||||
|
||||
def test_sqlalchemy_has_only_two_actual_bind_markers(self):
|
||||
from sqlalchemy import text as sql_text
|
||||
from sqlalchemy.dialects.mssql.pyodbc import dialect
|
||||
from inventory_service import SQL_PATH
|
||||
compiled = sql_text(SQL_PATH.read_text(encoding="utf-8")).compile(dialect=dialect(paramstyle="qmark"))
|
||||
self.assertEqual(compiled.positiontup, ["cutoff", "sam_cutoff"])
|
||||
|
||||
|
||||
class InventoryAsyncTests(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_bound_cutoffs_without_commit(self):
|
||||
db = type("DB", (), {"query_json": AsyncMock(return_value={"rows": []})})()
|
||||
r = await load_inventory(db, "31/12/2025")
|
||||
args, kwargs = db.query_json.call_args
|
||||
self.assertEqual(args[1]["cutoff"], datetime(2026, 1, 1))
|
||||
self.assertEqual(args[1]["sam_cutoff"], 46023)
|
||||
self.assertNotIn("commit", kwargs)
|
||||
self.assertEqual(r.selected_date, DAY)
|
||||
|
||||
async def test_invalid_date_never_queries(self):
|
||||
db = type("DB", (), {"query_json": AsyncMock()})()
|
||||
with self.assertRaises(ValueError):
|
||||
await load_inventory(db, "31/02/2025")
|
||||
db.query_json.assert_not_called()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user