Files
ShiErFeng/shierfeng-fastapi-backend/tests/module_business/test_material_dates.py
T

61 lines
2.6 KiB
Python

"""物料日期是发货来源数据,不能由装填操作时间推算。"""
import unittest
from datetime import date
from pydantic import ValidationError
from module_business.entity.vo.operation_vo import FillingCreateModel, FillingModel
from module_business.service.filling_service import FillingService
from tests.module_business.test_filling_service import make_filling
def command_data(**changes):
return {
'qrToken': 'qr-material',
'warehouseId': 7,
'strainId': 11,
'batchNo': 'B-20261001',
'packageSpec': '标准包',
'initialPackages': 100,
'idempotencyKey': 'material-date-key',
**changes,
}
class TestMaterialDates(unittest.TestCase):
def test_new_filling_requires_source_dates(self):
with self.assertRaises(ValidationError):
FillingCreateModel(**command_data())
def test_dates_round_trip_without_time_or_timezone_conversion(self):
command = FillingCreateModel(**command_data(productionDate='2026-10-01', expiryDate='2027-03-31'))
self.assertEqual(command.production_date, date(2026, 10, 1))
self.assertEqual(command.model_dump(mode='json', by_alias=True)['expiryDate'], '2027-03-31')
def test_expiry_cannot_precede_production(self):
with self.assertRaises(ValidationError) as raised:
FillingCreateModel(**command_data(productionDate='2026-10-01', expiryDate='2026-09-30'))
self.assertIn('有效期', str(raised.exception))
def test_invalid_calendar_dates_are_rejected(self):
with self.assertRaises(ValidationError):
FillingCreateModel(**command_data(productionDate='2026-02-30', expiryDate='2027-03-31'))
def test_changing_either_source_date_changes_idempotency_content(self):
original = FillingCreateModel(**command_data(productionDate='2026-10-01', expiryDate='2027-03-31'))
expected = FillingService._build_request_hash(original)
for field, value in [('productionDate', '2026-10-02'), ('expiryDate', '2027-04-01')]:
with self.subTest(field=field):
changed = original.model_dump(by_alias=True)
changed[field] = value
self.assertNotEqual(expected, FillingService._build_request_hash(FillingCreateModel(**changed)))
def test_historical_missing_dates_remain_unknown(self):
record = make_filling()
record.production_date = None
record.expiry_date = None
result = FillingModel.model_validate(record).model_dump(mode='json', by_alias=True)
self.assertIsNone(result['productionDate'])
self.assertIsNone(result['expiryDate'])