from decimal import Decimal from django.test import TestCase from rest_framework.test import APIClient from .models import Category, Product class CatalogApiTests(TestCase): def setUp(self): self.client = APIClient() self.category = Category.objects.create( external_id="cat-1", name="Диваны", slug="divany" ) self.product = Product.objects.create( external_id="prod-1", sku="SKU-1", name="Диван Осло", slug="divan-oslo", category=self.category, price=Decimal("49990.00"), stock=5, stock_status=Product.StockStatus.IN_STOCK, is_hit=True, is_new=True, ) def test_product_list(self): response = self.client.get("/api/v1/catalog/products/") self.assertEqual(response.status_code, 200) self.assertEqual(response.data["count"], 1) self.assertEqual(response.data["results"][0]["sku"], "SKU-1") def test_hits_endpoint(self): response = self.client.get("/api/v1/catalog/products/hits/") self.assertEqual(response.status_code, 200) self.assertEqual(response.data["count"], 1) def test_filter_by_price(self): response = self.client.get( "/api/v1/catalog/products/", {"price_min": "50000"} ) self.assertEqual(response.status_code, 200) self.assertEqual(response.data["count"], 0) def test_product_detail(self): response = self.client.get("/api/v1/catalog/products/divan-oslo/") self.assertEqual(response.status_code, 200) self.assertEqual(response.data["name"], "Диван Осло")