forked from erp-dev/erp
feat: printing module
This commit is contained in:
@@ -2,12 +2,14 @@
|
||||
PrintingOrder API 测试
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from django.conf import settings
|
||||
from rest_framework.test import APIClient
|
||||
from rest_framework import status
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Permission
|
||||
from basic_info import models as basic_models
|
||||
from printing import models as printing_models
|
||||
from stateflow import models as stateflow_models
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
@@ -49,6 +51,17 @@ class PrintingOrderAPITestCase(TestCase):
|
||||
area='测试地区'
|
||||
)
|
||||
|
||||
# 创建流程
|
||||
self.state1 = stateflow_models.State.objects.create(name='待印染')
|
||||
self.state2 = stateflow_models.State.objects.create(name='印染中')
|
||||
self.state3 = stateflow_models.State.objects.create(name='已完成')
|
||||
|
||||
self.process = stateflow_models.Process.objects.create(name='印染流程')
|
||||
self.process.replace_nodes([self.state1, self.state2, self.state3])
|
||||
|
||||
# 设置默认流程
|
||||
settings.PRINTING_DEFAULT_PROCESS_ID = self.process.id
|
||||
|
||||
# 认证用户
|
||||
self.client.force_authenticate(user=self.user)
|
||||
|
||||
@@ -398,3 +411,132 @@ class PrintingOrderAPITestCase(TestCase):
|
||||
self.assertTrue(len(order.human_id) >= 14)
|
||||
today = date.today().strftime('%Y%m%d')
|
||||
self.assertTrue(order.human_id.startswith(today))
|
||||
|
||||
def test_create_order_with_default_process(self):
|
||||
"""测试创建订单使用默认流程"""
|
||||
data = {
|
||||
'customer': self.customer.id,
|
||||
'fabric': '测试布料',
|
||||
'width': '150cm',
|
||||
}
|
||||
|
||||
response = self.client.post('/api/v1/printing-orders/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
|
||||
# 验证使用了默认流程
|
||||
order = printing_models.PrintingOrder.objects.get(id=response.data['id'])
|
||||
self.assertEqual(order.process.id, self.process.id)
|
||||
|
||||
def test_create_order_with_custom_process(self):
|
||||
"""测试创建订单指定自定义流程"""
|
||||
custom_process = stateflow_models.Process.objects.create(name='自定义流程')
|
||||
|
||||
data = {
|
||||
'customer': self.customer.id,
|
||||
'fabric': '测试布料',
|
||||
'width': '150cm',
|
||||
'process': custom_process.id,
|
||||
}
|
||||
|
||||
response = self.client.post('/api/v1/printing-orders/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
|
||||
order = printing_models.PrintingOrder.objects.get(id=response.data['id'])
|
||||
self.assertEqual(order.process.id, custom_process.id)
|
||||
|
||||
def test_order_progress_in_list(self):
|
||||
"""测试订单列表包含进度字段"""
|
||||
order = printing_models.PrintingOrder.objects.create(
|
||||
customer=self.customer,
|
||||
fabric='测试布料',
|
||||
width='150cm',
|
||||
process=self.process,
|
||||
)
|
||||
|
||||
response = self.client.get('/api/v1/printing-orders/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertIn('progress', response.data[0])
|
||||
self.assertEqual(response.data[0]['progress'], 0) # 没有任务时为0
|
||||
|
||||
def test_update_process_when_no_jobs(self):
|
||||
"""测试没有任务时可以修改流程"""
|
||||
order = printing_models.PrintingOrder.objects.create(
|
||||
customer=self.customer,
|
||||
fabric='测试布料',
|
||||
width='150cm',
|
||||
process=self.process,
|
||||
)
|
||||
|
||||
new_process = stateflow_models.Process.objects.create(name='新流程')
|
||||
|
||||
data = {
|
||||
'process': new_process.id,
|
||||
}
|
||||
|
||||
response = self.client.patch(
|
||||
f'/api/v1/printing-orders/{order.id}/',
|
||||
data,
|
||||
format='json'
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
order.refresh_from_db()
|
||||
self.assertEqual(order.process.id, new_process.id)
|
||||
|
||||
def test_cannot_update_process_when_job_started(self):
|
||||
"""测试有已开始的任务时不能修改流程"""
|
||||
order = printing_models.PrintingOrder.objects.create(
|
||||
customer=self.customer,
|
||||
fabric='测试布料',
|
||||
width='150cm',
|
||||
process=self.process,
|
||||
)
|
||||
|
||||
# 创建产品类别和产品
|
||||
category = basic_models.ProductCategory.objects.create(
|
||||
name='测试类别',
|
||||
merchant=self.merchant,
|
||||
)
|
||||
product = basic_models.Product.objects.create(
|
||||
name='测试产品',
|
||||
category=category,
|
||||
merchant=self.merchant,
|
||||
)
|
||||
|
||||
# 创建任务
|
||||
job = printing_models.PrintingJob.objects.create(
|
||||
printing_order=order,
|
||||
product=product,
|
||||
quantity=10,
|
||||
unit='件',
|
||||
size='100x200',
|
||||
pieces=5,
|
||||
)
|
||||
|
||||
# 创建 BusinessObject 并推进状态
|
||||
business_object = stateflow_models.BusinessObject.objects.create(
|
||||
name=f'PrintingJob-{job.id}',
|
||||
process=self.process,
|
||||
)
|
||||
job.business_object = business_object
|
||||
job.save()
|
||||
|
||||
from stateflow.services import advance_to_next_state
|
||||
advance_to_next_state(business_object, self.user)
|
||||
|
||||
# 尝试修改流程
|
||||
new_process = stateflow_models.Process.objects.create(name='新流程')
|
||||
|
||||
data = {
|
||||
'process': new_process.id,
|
||||
}
|
||||
|
||||
response = self.client.patch(
|
||||
f'/api/v1/printing-orders/{order.id}/',
|
||||
data,
|
||||
format='json'
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn('已开始', str(response.data))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user