forked from erp-dev/erp
92 lines
3.0 KiB
Python
92 lines
3.0 KiB
Python
from django.contrib.auth import get_user_model
|
|
from django.test import TestCase
|
|
from rest_framework import status
|
|
from rest_framework.test import APIClient
|
|
|
|
from basic_info.models import Merchant, MerchantTypeEnum, Customer
|
|
from printing import models as printing_models
|
|
|
|
|
|
class PrintCountDeltaAPITestCase(TestCase):
|
|
"""打印次数增量接口测试"""
|
|
|
|
url = '/api/v1/print-count/delta/'
|
|
|
|
def setUp(self):
|
|
self.client = APIClient()
|
|
self.user = get_user_model().objects.create_user(username='tester', password='test123')
|
|
self.client.force_authenticate(self.user)
|
|
|
|
self.merchant = Merchant.objects.create(name='测试商户', type=MerchantTypeEnum.FACTORY)
|
|
self.customer = Customer.objects.create(
|
|
merchant=self.merchant,
|
|
name='客户甲',
|
|
mobile='13900000000',
|
|
created_by=None,
|
|
)
|
|
|
|
self.printing_order = printing_models.PrintingOrder.objects.create(
|
|
customer=self.customer,
|
|
fabric='棉布',
|
|
width='150cm',
|
|
)
|
|
self.plate_order = printing_models.PlateOrder.objects.create(
|
|
customer=self.customer,
|
|
design_code='DES-001',
|
|
)
|
|
|
|
def test_increment_printing_order_with_delta(self):
|
|
response = self.client.post(
|
|
self.url,
|
|
{
|
|
'object_type': 'printing_order',
|
|
'object_id': self.printing_order.id,
|
|
'delta': 3,
|
|
},
|
|
format='json',
|
|
)
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
self.printing_order.refresh_from_db()
|
|
self.assertEqual(self.printing_order.print_count, 3)
|
|
self.assertEqual(response.data['print_count'], 3)
|
|
self.assertEqual(response.data['delta'], 3)
|
|
|
|
def test_delta_defaults_to_one_when_invalid(self):
|
|
response = self.client.post(
|
|
self.url,
|
|
{
|
|
'object_type': 'plate_order',
|
|
'object_id': self.plate_order.id,
|
|
'delta': 'invalid',
|
|
},
|
|
format='json',
|
|
)
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
self.plate_order.refresh_from_db()
|
|
self.assertEqual(self.plate_order.print_count, 1)
|
|
self.assertEqual(response.data['delta'], 1)
|
|
|
|
def test_invalid_object_type_returns_400(self):
|
|
response = self.client.post(
|
|
self.url,
|
|
{
|
|
'object_type': 'unknown_type',
|
|
'object_id': self.printing_order.id,
|
|
},
|
|
format='json',
|
|
)
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
|
self.assertIn('object_type', response.data)
|
|
|
|
def test_not_found_returns_404(self):
|
|
response = self.client.post(
|
|
self.url,
|
|
{
|
|
'object_type': 'printing_order',
|
|
'object_id': 999999,
|
|
},
|
|
format='json',
|
|
)
|
|
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
|
|
|