111 lines
3.8 KiB
Python
111 lines
3.8 KiB
Python
"""基础实验申请单与售卖计划批量创建"""
|
|
import unittest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from app.crud import pilot_application as crud
|
|
|
|
|
|
class TestBasicExperimentApplicationNo(unittest.TestCase):
|
|
@patch("app.crud.pilot_application.datetime")
|
|
def test_generate_application_no_basic_experiment_prefix(self, mock_dt):
|
|
mock_dt.now.return_value.strftime.return_value = "20260610"
|
|
db = MagicMock()
|
|
mock_query = MagicMock()
|
|
db.query.return_value = mock_query
|
|
mock_query.filter.return_value = mock_query
|
|
mock_query.order_by.return_value = mock_query
|
|
mock_query.first.return_value = None
|
|
|
|
result = crud._generate_application_no(db, "zkxw", biz_type="basic_experiment")
|
|
|
|
self.assertTrue(result.startswith("JCSY"))
|
|
self.assertNotIn("JCSQ", result)
|
|
self.assertEqual(result, "JCSY202606100001")
|
|
|
|
|
|
class TestBasicExperimentCreateValidation(unittest.TestCase):
|
|
def test_create_basic_experiment_requires_customer_fields(self):
|
|
db = MagicMock()
|
|
data = {
|
|
"bizType": "basic_experiment",
|
|
"projectTitle": "课题A",
|
|
"customerName": "",
|
|
"customerContact": "张三",
|
|
"customerPhone": "13800000000",
|
|
}
|
|
with self.assertRaises(ValueError) as ctx:
|
|
crud.create_application(
|
|
db,
|
|
data=data,
|
|
tenant_no="zkxw",
|
|
user_id="user-1",
|
|
dept_id="dept-1",
|
|
)
|
|
self.assertIn("客户名称", str(ctx.exception))
|
|
|
|
|
|
class TestBasicExperimentSaleSchedules(unittest.TestCase):
|
|
@patch("app.crud.pilot_application.create_schedule")
|
|
@patch("app.crud.pilot_application._get_or_create_infusion_plan")
|
|
def test_two_product_lines_create_two_schedules(self, mock_get_plan, mock_create_schedule):
|
|
db = MagicMock()
|
|
app = MagicMock()
|
|
app.biz_type = "basic_experiment"
|
|
plan = MagicMock()
|
|
plan.id = "plan-1"
|
|
|
|
mock_query = MagicMock()
|
|
db.query.return_value = mock_query
|
|
mock_query.filter.return_value = mock_query
|
|
mock_query.first.return_value = app
|
|
mock_query.scalar.return_value = 2
|
|
mock_get_plan.return_value = plan
|
|
mock_create_schedule.side_effect = [{"id": "sched-1"}, {"id": "sched-2"}]
|
|
|
|
product_lines = [
|
|
{
|
|
"cellTypeName": "NK",
|
|
"specText": "1e8",
|
|
"pickupTime": "2026-06-15",
|
|
"amount": 1000,
|
|
"priceId": "p1",
|
|
"projectCode": "C01",
|
|
"projectName": "产品1",
|
|
"agreementPrice": 1000,
|
|
"actualPrice": 900,
|
|
"quantity": 1,
|
|
},
|
|
{
|
|
"cellTypeName": "T",
|
|
"specText": "5e7",
|
|
"pickupTime": "2026-06-20",
|
|
"amount": 2000,
|
|
"priceId": "p2",
|
|
"projectCode": "C02",
|
|
"projectName": "产品2",
|
|
"agreementPrice": 2000,
|
|
"actualPrice": 1800,
|
|
"quantity": 2,
|
|
},
|
|
]
|
|
|
|
results = crud.create_basic_experiment_sale_schedules(
|
|
db,
|
|
application_id="app-1",
|
|
product_lines=product_lines,
|
|
tenant_no="zkxw",
|
|
user_id="user-1",
|
|
dept_id="dept-1",
|
|
)
|
|
|
|
self.assertEqual(len(results), 2)
|
|
self.assertEqual(mock_create_schedule.call_count, 2)
|
|
first_call = mock_create_schedule.call_args_list[0]
|
|
self.assertEqual(first_call.kwargs.get("commit"), False)
|
|
self.assertEqual(len(first_call.args[2].get("cellProducts", [])), 1)
|
|
db.commit.assert_called_once()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|