You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
2.1 KiB
47 lines
2.1 KiB
2 days ago
|
from rest_framework.test import APITestCase
|
||
|
from rest_framework import status
|
||
|
from .models import Product
|
||
|
from django.urls import reverse
|
||
|
|
||
|
class ProductAPITest(APITestCase):
|
||
|
|
||
|
def setUp(self):
|
||
|
# Создание тестового продукта
|
||
|
self.product_data = {
|
||
|
'name': 'Test Product',
|
||
|
'manufacturer_name': 'Test Manufacturer',
|
||
|
'manufacturer_country': 'Test Country',
|
||
|
'unit_of_measure': 'pcs',
|
||
|
'shelf_life_days': 365,
|
||
|
'barcode': '123456789012',
|
||
|
}
|
||
|
self.product = Product.objects.create(**self.product_data)
|
||
|
self.url = reverse('product-list') # Замени на свой URL
|
||
|
|
||
|
def test_create_product(self):
|
||
|
"""Test creating a new product"""
|
||
|
response = self.client.post(self.url, self.product_data, format='json')
|
||
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||
|
self.assertEqual(Product.objects.count(), 2)
|
||
|
self.assertEqual(Product.objects.latest('id').name, self.product_data['name'])
|
||
|
|
||
|
def test_get_product(self):
|
||
|
"""Test retrieving a product"""
|
||
|
response = self.client.get(self.url)
|
||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||
|
self.assertEqual(len(response.data), 1) # Только один продукт в базе
|
||
|
|
||
|
def test_update_product(self):
|
||
|
"""Test updating a product"""
|
||
|
updated_data = {'name': 'Updated Product'}
|
||
|
response = self.client.put(reverse('product-detail', args=[self.product.id]), updated_data, format='json')
|
||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||
|
self.product.refresh_from_db()
|
||
|
self.assertEqual(self.product.name, 'Updated Product')
|
||
|
|
||
|
def test_delete_product(self):
|
||
|
"""Test deleting a product"""
|
||
|
response = self.client.delete(reverse('product-detail', args=[self.product.id]))
|
||
|
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
|
||
|
self.assertEqual(Product.objects.count(), 0) # Продукт должен быть удален
|