33 lines
1.7 KiB
Python
33 lines
1.7 KiB
Python
import json
|
|
import unittest
|
|
from unittest.mock import patch
|
|
from routes import purchase
|
|
|
|
|
|
class DomesticPrePackagingSnapshotTest(unittest.TestCase):
|
|
def test_reads_exact_order_snapshot_including_zero(self):
|
|
items = [{'material_code': 'M1', 'pre_pack_qty': 0},
|
|
{'material_code': 'M2', 'pre_pack_qty': 120},
|
|
{'material_code': 'M3'}]
|
|
with patch.object(purchase, 'query', side_effect=[
|
|
[{'货品编号': 'SKU', '计划下单时间': '2026-10-22 09:30:00'}],
|
|
[{'bom_items': json.dumps(items)}]
|
|
]) as query:
|
|
self.assertEqual(purchase._domestic_order_pre_packaging(23), {'M1': 0, 'M2': 120})
|
|
self.assertEqual(query.call_args_list[0].args[1], (23,))
|
|
self.assertEqual(query.call_args_list[1].args[1], ('SKU', '2026-10-22 09:30:00'))
|
|
self.assertIn('ordered_at=%s', query.call_args_list[1].args[0])
|
|
|
|
def test_missing_or_ambiguous_snapshot_is_not_replaced_with_current_balance(self):
|
|
for snapshots in ([], [{'bom_items': '[]'}, {'bom_items': '[]'}],
|
|
[{'bom_items': 'invalid'}], [{'bom_items': None}]):
|
|
with self.subTest(snapshots=snapshots), patch.object(purchase, 'query', side_effect=[
|
|
[{'货品编号': 'SKU', '计划下单时间': '2026-10-22 09:30:00'}], snapshots
|
|
]):
|
|
self.assertEqual(purchase._domestic_order_pre_packaging(23), {})
|
|
|
|
def test_manual_order_without_confirmation_time_has_no_snapshot(self):
|
|
with patch.object(purchase, 'query', return_value=[{'货品编号': 'SKU', '计划下单时间': None}]) as query:
|
|
self.assertEqual(purchase._domestic_order_pre_packaging(23), {})
|
|
self.assertEqual(query.call_count, 1)
|