Example output
from rest_framework import status
from rest_framework.test import APITestCase
from django.urls import reverse
from yourapp.models import Product, ProductVariation, LetteringItemCategory, LetteringItemVariation, ProductColor
from yourapp.serializers import OrderSerializer
from yourapp.views import CreateOrder
class CreateOrderTests(APITestCase):
def setUp(self):
# Create sample data for testing
self.product = Product.objects.create(id=1)
self.product_color = ProductColor.objects.create(id=1)
self.lettering_item_category = LetteringItemCategory.objects.create(id=1, title='Test')
def create_order_request(self, product_color_id=None, lettering_items=None):
data = {
'lettering_items': lettering_items if lettering_items is not None else [],
'product_color_id': product_color_id if product_color_id is not None else 1,
'order': {
# order data fields
}
}
return self.client.post(reverse('create_order', args=[self.product.id]), data, format='json')
def test_create_order_success(self):
response = self.create_order_request(
product_color_id=self.product_color.id,
lettering_items=[
{'title': 'Test', 'text': 'item1'},
{'title': 'Test', 'text': 'item2'}
]
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(ProductVariation.objects.count(), 1)
self.assertEqual(LetteringItemVariation.objects.count(), 2)
def test_create_order_missing_product_color(self):
response = self.create_order_request(
product_color_id=None,
lettering_items=[
{'title': 'Test', 'text': 'item1'}
]
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIsNone(ProductVariation.objects.first().product_color)
def test_create_order_no_lettering_items(self):
response = self.create_order_request(
product_color_id=self.product_color.id,
lettering_items=[]
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(LetteringItemVariation.objects.count(), 0)