feat: added product filter tests
This commit is contained in:
@@ -0,0 +1,30 @@
|
|||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from shop.tests.mixins import CreateProductsMixin
|
||||||
|
from shop.models import Product
|
||||||
|
from shop.filters import ProductFilter
|
||||||
|
|
||||||
|
|
||||||
|
class TestProductFilter(TestCase, CreateProductsMixin):
|
||||||
|
def setUp(self):
|
||||||
|
product_a = self.create_product(name="producto 1", sku='001', price=Decimal("40.00"))
|
||||||
|
product_b = self.create_product(name="producto 2", sku='002', price=Decimal("70.00"))
|
||||||
|
product_c = self.create_product(name="producto 3", sku='003', price=Decimal("100.00"))
|
||||||
|
|
||||||
|
def test_product_filter_by_price(self):
|
||||||
|
f = ProductFilter({"price_gt": "40"})
|
||||||
|
f.is_valid()
|
||||||
|
qs = f.filter_queryset(Product.objects.all())
|
||||||
|
assert qs.count() == 3
|
||||||
|
|
||||||
|
f = ProductFilter({"price_lt": "40"})
|
||||||
|
f.is_valid()
|
||||||
|
qs = f.filter_queryset(Product.objects.all())
|
||||||
|
assert qs.count() == 0
|
||||||
|
|
||||||
|
f = ProductFilter({"price_gt": "100"})
|
||||||
|
f.is_valid()
|
||||||
|
qs = f.filter_queryset(Product.objects.all())
|
||||||
|
assert qs.count() == 1
|
||||||
@@ -13,7 +13,7 @@ class CreateProductsMixin:
|
|||||||
description="Descripción",
|
description="Descripción",
|
||||||
price=Decimal("10.00"),
|
price=Decimal("10.00"),
|
||||||
) -> Product:
|
) -> Product:
|
||||||
tax = Tax.objects.create(
|
tax, created = Tax.objects.get_or_create(
|
||||||
code="IVA",
|
code="IVA",
|
||||||
value=21,
|
value=21,
|
||||||
)
|
)
|
||||||
@@ -27,5 +27,6 @@ class CreateProductsMixin:
|
|||||||
product=product,
|
product=product,
|
||||||
date=now(),
|
date=now(),
|
||||||
tax=tax,
|
tax=tax,
|
||||||
|
current=True,
|
||||||
)
|
)
|
||||||
return product
|
return product
|
||||||
|
|||||||
Reference in New Issue
Block a user