Files
zkxw/backend/tests/test_basic_experiment_application.py
T
cykjandCursor c22a0d1eb0 feat(market): 完善临床科研与委托加工上线能力
补齐项目组负责人审批、费用信息、合作客户、生产出库菜单、申请单号 LCKY 前缀及配套 SQL 迁移,并同步相关前后端与单测。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-30 18:51:37 +08:00

154 lines
5.3 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("LCKY"))
self.assertNotIn("JCSY", result)
self.assertEqual(result, "LCKY202606100001")
class TestBasicExperimentCreateValidation(unittest.TestCase):
def test_create_basic_experiment_requires_customer_fields(self):
db = MagicMock()
data = {
"bizType": "basic_experiment",
"researchCategory": "bio_tech_clinical",
"channelId": "ch-1",
"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))
def test_create_basic_experiment_requires_research_category(self):
db = MagicMock()
data = {
"bizType": "basic_experiment",
"channelId": "ch-1",
"projectTitle": "课题A",
"customerName": "客户A",
"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))
def test_create_basic_experiment_allows_missing_channel(self):
"""临床科研申请单不再要求渠道;缺项目负责人时仍会失败。"""
db = MagicMock()
data = {
"bizType": "basic_experiment",
"researchCategory": "bio_tech_clinical",
"projectTitle": "课题A",
"customerName": "客户A",
"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.assertNotIn("渠道", 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()