Skip to content

Commit

Permalink
Products: add price stats properties
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Sep 15, 2024
1 parent ab9ed15 commit aab4740
Show file tree
Hide file tree
Showing 2 changed files with 59 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
40 changes: 39 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,34 @@ 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)

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 aab4740

Please sign in to comment.