forked from erp-dev/erp
feat: upload api
This commit is contained in:
@@ -418,20 +418,23 @@ class PrintingJobAPITestCase(TestCase):
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn('quantity', response.data)
|
||||
|
||||
def test_validate_pieces_positive(self):
|
||||
"""测试件数必须大于0"""
|
||||
def test_validate_pieces_can_be_zero(self):
|
||||
"""测试件数可以为0或null"""
|
||||
data = {
|
||||
'printing_order': self.printing_order.id,
|
||||
'product': self.product.id,
|
||||
'quantity': 100,
|
||||
'unit': '米',
|
||||
'size': '50*60',
|
||||
'pieces': 0 # 无效件数
|
||||
'pieces': 0 # 允许为0
|
||||
}
|
||||
|
||||
response = self.client.post('/api/v1/printing-jobs/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn('pieces', response.data)
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
|
||||
# 验证创建成功
|
||||
job = printing_models.PrintingJob.objects.get(id=response.data['id'])
|
||||
self.assertEqual(job.pieces, 0)
|
||||
|
||||
def test_create_job_with_business_object(self):
|
||||
"""测试创建任务自动创建 BusinessObject"""
|
||||
@@ -583,6 +586,153 @@ class PrintingJobAPITestCase(TestCase):
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn('没有关联的流程实例', response.data['detail'])
|
||||
|
||||
def test_advance_with_parameters(self):
|
||||
"""测试推进到下一个状态 - 带参数"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
# 推进到下一个状态,带参数
|
||||
data = {
|
||||
'parameters': {
|
||||
'temperature': '25.5',
|
||||
'operator': '张三',
|
||||
'humidity': '60%'
|
||||
}
|
||||
}
|
||||
response = self.client.post(
|
||||
f'/api/v1/printing-jobs/{job.id}/advance-to-next-state/',
|
||||
data,
|
||||
format='json'
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertIn('detail', response.data)
|
||||
self.assertIn('data', response.data)
|
||||
|
||||
# 验证参数已保存
|
||||
job.refresh_from_db()
|
||||
state_log = business_object.state_logs.filter(state=self.state1, is_cancelled=False).first()
|
||||
self.assertIsNotNone(state_log)
|
||||
|
||||
# 验证参数记录
|
||||
param_record = state_log.parameter_records.first()
|
||||
self.assertIsNotNone(param_record)
|
||||
self.assertEqual(param_record.parameters['temperature'], '25.5')
|
||||
self.assertEqual(param_record.parameters['operator'], '张三')
|
||||
self.assertEqual(param_record.parameters['humidity'], '60%')
|
||||
|
||||
def test_advance_with_required_parameters_missing(self):
|
||||
"""测试推进到下一个状态 - 缺少必填参数"""
|
||||
# 创建带必填参数的状态
|
||||
param1 = stateflow_models.StateParameter.objects.create(
|
||||
key='temperature',
|
||||
is_required=True,
|
||||
description='温度'
|
||||
)
|
||||
param2 = stateflow_models.StateParameter.objects.create(
|
||||
key='operator',
|
||||
is_required=True,
|
||||
description='操作员'
|
||||
)
|
||||
|
||||
state_with_params = stateflow_models.State.objects.create(name='带参数状态')
|
||||
state_with_params.parameters.add(param1, param2)
|
||||
|
||||
# 创建新流程,第一个状态是带必填参数的
|
||||
process_with_params = stateflow_models.Process.objects.create(name='带参数流程')
|
||||
process_with_params.replace_nodes([state_with_params, self.state2])
|
||||
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=process_with_params,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
# 尝试推进但不提供必填参数
|
||||
response = self.client.post(f'/api/v1/printing-jobs/{job.id}/advance-to-next-state/')
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn('缺失必填参数', response.data['detail'])
|
||||
self.assertIn('temperature', response.data['detail'])
|
||||
self.assertIn('operator', response.data['detail'])
|
||||
|
||||
def test_advance_with_partial_required_parameters(self):
|
||||
"""测试推进到下一个状态 - 只提供部分必填参数"""
|
||||
# 创建带必填参数的状态
|
||||
param1 = stateflow_models.StateParameter.objects.create(
|
||||
key='temperature',
|
||||
is_required=True,
|
||||
description='温度'
|
||||
)
|
||||
param2 = stateflow_models.StateParameter.objects.create(
|
||||
key='operator',
|
||||
is_required=True,
|
||||
description='操作员'
|
||||
)
|
||||
|
||||
state_with_params = stateflow_models.State.objects.create(name='带参数状态')
|
||||
state_with_params.parameters.add(param1, param2)
|
||||
|
||||
# 创建新流程
|
||||
process_with_params = stateflow_models.Process.objects.create(name='带参数流程')
|
||||
process_with_params.replace_nodes([state_with_params, self.state2])
|
||||
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
product=self.product,
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
)
|
||||
|
||||
# 创建 BusinessObject
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=process_with_params,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
# 只提供一个必填参数
|
||||
data = {
|
||||
'parameters': {
|
||||
'temperature': '25.5'
|
||||
}
|
||||
}
|
||||
response = self.client.post(
|
||||
f'/api/v1/printing-jobs/{job.id}/advance-to-next-state/',
|
||||
data,
|
||||
format='json'
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn('缺失必填参数', response.data['detail'])
|
||||
self.assertIn('operator', response.data['detail'])
|
||||
self.assertNotIn('temperature', response.data['detail']) # temperature 已提供
|
||||
|
||||
def test_advance_through_all_states(self):
|
||||
"""测试推进到完成所有状态"""
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
|
||||
Reference in New Issue
Block a user