Files
zkxw/backend/tests/test_sample_freight.py
T

110 lines
3.4 KiB
Python

"""样本运费:运输方式字典解析与关联规则"""
import unittest
from decimal import Decimal
from unittest.mock import MagicMock
from app.services.sample_transport_mode import resolve_transport_mode_value
class TestResolveTransportMode(unittest.TestCase):
def test_empty_raises(self):
db = MagicMock()
with self.assertRaises(ValueError):
resolve_transport_mode_value(db, tenant_no="zkxw", raw="", user_id=None)
def test_hit_by_value(self):
db = MagicMock()
type_exists = MagicMock()
row = MagicMock(dict_value="sfkd", dict_label="顺丰快递", del_flag=0)
db.query.return_value.filter.return_value.first.side_effect = [type_exists, row]
self.assertEqual(
resolve_transport_mode_value(db, tenant_no="zkxw", raw="sfkd", user_id=None),
"sfkd",
)
def test_hit_by_label_then_create_path_not_called(self):
db = MagicMock()
type_exists = MagicMock()
miss = None
hit = MagicMock(dict_value="zxc", dict_label="专线车", del_flag=0)
db.query.return_value.filter.return_value.first.side_effect = [
type_exists,
miss,
hit,
]
self.assertEqual(
resolve_transport_mode_value(db, tenant_no="zkxw", raw="专线车", user_id=None),
"zxc",
)
db.add.assert_not_called()
class TestFreightAmountValidation(unittest.TestCase):
def test_negative_rejected(self):
from app.crud.sample_freight import _parse_amount
with self.assertRaises(ValueError):
_parse_amount(-1)
def test_zero_ok(self):
from app.crud.sample_freight import _parse_amount
self.assertEqual(_parse_amount(0), Decimal("0"))
def test_empty_rejected(self):
from app.crud.sample_freight import _parse_amount
with self.assertRaises(ValueError):
_parse_amount(None)
with self.assertRaises(ValueError):
_parse_amount("")
class TestFreightListHelpers(unittest.TestCase):
def test_item_dict_has_enrichment_keys(self):
from app.crud.sample_freight import _item_to_dict_enriched
class I:
id = "i1"
freight_id = "f1"
sample_source = "cell_storage"
sample_id = "s1"
sample_code = "CODE1"
protocol_id = "p1"
d = _item_to_dict_enriched(
I(),
protocol_no="CP001",
sample_type_name="脐带血",
)
self.assertEqual(d["protocolNo"], "CP001")
self.assertEqual(d["sampleTypeName"], "脐带血")
self.assertEqual(d["sampleSource"], "cell_storage")
class TestFreightListHelpers(unittest.TestCase):
def test_item_dict_has_enrichment_keys(self):
from app.crud.sample_freight import _item_to_dict_enriched
class I:
id = "i1"
freight_id = "f1"
sample_source = "cell_storage"
sample_id = "s1"
sample_code = "CODE1"
protocol_id = "p1"
d = _item_to_dict_enriched(
I(),
protocol_no="CP001",
sample_type_name="脐带血",
)
self.assertEqual(d["protocolNo"], "CP001")
self.assertEqual(d["sampleTypeName"], "脐带血")
self.assertEqual(d["sampleSource"], "cell_storage")
if __name__ == "__main__":
unittest.main()