Files
homm/backend/apps/storefront/tests.py
T
apuc a64d567cff Switch the demo catalog and copy to an electronics store
The shop sells tech, not clothing, so the demo data and page copy now
match that:

- seven categories: smartphones, laptops, tablets, audio, TVs, home
  appliances, accessories
- fifteen products with real-looking specs (screen, CPU, RAM, storage,
  energy class, warranty) instead of apparel attributes
- hero copy and meta description talk about tech and appliances
- banner updated in place by position/sort_order, so re-seeding does not
  leave a stale duplicate

Catalog polish:
- brand list and price range now follow the selected category instead of
  the whole catalog

Tests: storefront fixtures renamed to the tech domain; suite is 61
passing.
2026-11-22 22:46:56 +03:00

143 lines
5.7 KiB
Python

from decimal import Decimal
from django.test import TestCase
from apps.catalog.models import Category, Product
from apps.content.models import StaticPage
from .templatetags.storefront import attr_value, discount_percent, money
class StorefrontHelperTests(TestCase):
def test_money_formats_roubles_with_spaces(self):
self.assertEqual(money(Decimal("12990.00")), "12 990")
self.assertEqual(money("490"), "490")
self.assertEqual(money(None), "—")
def test_discount_percent(self):
self.assertEqual(discount_percent("12990", "14990"), 13)
self.assertEqual(discount_percent("12990", None), 0)
self.assertEqual(discount_percent("12990", "10000"), 0)
def test_attr_value_renders_lists(self):
self.assertEqual(attr_value(["S", "M"]), "S, M")
self.assertEqual(attr_value(True), "да")
self.assertEqual(attr_value("Чёрный"), "Чёрный")
class StorefrontPagesTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.laptops = Category.objects.create(
external_id="t-laptops", name="Ноутбуки", slug="noutbuki"
)
cls.phones = Category.objects.create(
external_id="t-phones", name="Смартфоны", slug="smartfony"
)
cls.laptop = Product.objects.create(
external_id="t-laptop",
sku="T-1",
name="Ноутбук тестовый",
slug="noutbuk-testovyy",
category=cls.laptops,
brand="WorkPro",
price=Decimal("12000.00"),
old_price=Decimal("15000.00"),
stock=5,
is_hit=True,
attributes={"Экран": '14" IPS', "ОЗУ": "16 ГБ"},
)
cls.phone = Product.objects.create(
external_id="t-phone",
sku="T-2",
name="Смартфон тестовый",
slug="smartfon-testovyy",
category=cls.phones,
brand="Aurora",
price=Decimal("2500.00"),
stock=0,
is_new=True,
)
cls.page = StaticPage.objects.create(
slug="dostavka", title="Доставка", content="Курьером", is_published=True
)
def test_home_shows_hits_and_new(self):
response = self.client.get("/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Ноутбук тестовый")
self.assertContains(response, "Смартфон тестовый")
def test_catalog_lists_only_active_products(self):
hidden = Product.objects.create(
external_id="t-hidden",
sku="T-3",
name="Скрытый товар",
slug="skrytyy-tovar",
price=Decimal("100.00"),
is_active=False,
)
response = self.client.get("/catalog/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Ноутбук тестовый")
self.assertNotContains(response, hidden.name)
def test_catalog_filters_by_category(self):
response = self.client.get("/catalog/?category=smartfony")
self.assertContains(response, "Смартфон тестовый")
self.assertNotContains(response, "Ноутбук тестовый")
def test_catalog_filters_by_price_and_stock(self):
response = self.client.get("/catalog/?price_max=5000&in_stock=1")
self.assertNotContains(response, "Ноутбук тестовый")
self.assertNotContains(response, "Смартфон тестовый")
response = self.client.get("/catalog/?price_min=10000")
self.assertContains(response, "Ноутбук тестовый")
def test_catalog_search(self):
response = self.client.get("/catalog/", {"search": "ноутбук"})
self.assertContains(response, "Ноутбук тестовый")
self.assertNotContains(response, "Смартфон тестовый")
def test_catalog_shows_filter_chips(self):
response = self.client.get("/catalog/?brand=Aurora&in_stock=1")
self.assertContains(response, "Бренд: Aurora")
self.assertContains(response, "Только в наличии")
def test_catalog_ordering(self):
response = self.client.get("/catalog/?ordering=price")
content = response.content.decode()
self.assertLess(
content.index("Смартфон тестовый"), content.index("Ноутбук тестовый")
)
def test_product_page(self):
response = self.client.get("/catalog/noutbuk-testovyy/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Ноутбук тестовый")
self.assertContains(response, "Экран")
self.assertContains(response, "IPS")
self.assertContains(response, "16 ГБ")
def test_product_page_404(self):
self.assertEqual(self.client.get("/catalog/no-such-product/").status_code, 404)
def test_cart_checkout_login_account_pages(self):
for url in ("/cart/", "/checkout/", "/login/", "/account/"):
with self.subTest(url=url):
self.assertEqual(self.client.get(url).status_code, 200)
def test_static_page(self):
response = self.client.get("/pages/dostavka/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Курьером")
def test_static_page_404(self):
self.assertEqual(self.client.get("/pages/no-such-page/").status_code, 404)
def test_api_still_works(self):
response = self.client.get("/api/v1/catalog/products/")
self.assertEqual(response.status_code, 200)
self.assertGreaterEqual(response.json()["count"], 2)