Skip to content

Commit

Permalink
feat(products): new properties to calculate price min, max & average (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Sep 15, 2024
1 parent ab9ed15 commit f662710
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
20 changes: 20 additions & 0 deletions open_prices/products/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ def update_price_count(self):
self.price_count = self.prices.count()
self.save(update_fields=["price_count"])

def price__min(self, exclude_discounted=False):
if exclude_discounted:
return self.prices.exclude_discounted().calculate_min()
return self.prices.calculate_min()

def price__max(self, exclude_discounted=False):
if exclude_discounted:
return self.prices.exclude_discounted().calculate_max()
return self.prices.calculate_max()

def price__avg(self, exclude_discounted=False):
if exclude_discounted:
return self.prices.exclude_discounted().calculate_avg()
return self.prices.calculate_avg()

def price__stats(self, exclude_discounted=False):
if exclude_discounted:
return self.prices.exclude_discounted().calculate_stats()
return self.prices.calculate_stats()


@receiver(signals.post_save, sender=Product)
def product_post_create_fetch_and_save_data_from_openfoodfacts(
Expand Down
41 changes: 40 additions & 1 deletion open_prices/products/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from decimal import Decimal

from django.core.exceptions import ValidationError
from django.test import TestCase, TransactionTestCase

Expand Down Expand Up @@ -91,7 +93,12 @@ class ProductPropertyTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.product = ProductFactory(code="0123456789100", product_quantity=1000)
PriceFactory(product_code=cls.product.code, price=1.0)
PriceFactory(
product_code=cls.product.code,
price=1.0,
price_is_discounted=True,
price_without_discount=1.5,
)
PriceFactory(product_code=cls.product.code, price=2.0)

def test_update_price_count(self):
Expand All @@ -103,3 +110,35 @@ def test_update_price_count(self):
# update_price_count() should fix price_count
self.product.update_price_count()
self.assertEqual(self.product.price_count, 0)

def test_price__min(self):
self.assertEqual(self.product.price__min(), 1.0)
self.assertEqual(self.product.price__min(exclude_discounted=True), 2.0)

def test_price__max(self):
self.assertEqual(self.product.price__max(), 2.0)
self.assertEqual(self.product.price__max(exclude_discounted=True), 2.0)

def test_price__avg(self):
self.assertEqual(self.product.price__avg(), 1.5)
self.assertEqual(self.product.price__avg(exclude_discounted=True), 2.0)

def test_price__stats(self):
self.assertEqual(
self.product.price__stats(),
{
"price__count": 2,
"price__min": Decimal("1.0"),
"price__max": Decimal("2.0"),
"price__avg": Decimal("1.50"),
},
)
self.assertEqual(
self.product.price__stats(exclude_discounted=True),
{
"price__count": 1,
"price__min": Decimal("2.0"),
"price__max": Decimal("2.0"),
"price__avg": Decimal("2.00"),
},
)

0 comments on commit f662710

Please sign in to comment.