From aab474063046eebc7c12c3abe951feea26677ba0 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Mon, 16 Sep 2024 00:48:07 +0200 Subject: [PATCH] Products: add price stats properties --- open_prices/products/models.py | 20 +++++++++++++++++ open_prices/products/tests.py | 40 +++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/open_prices/products/models.py b/open_prices/products/models.py index 99158bd3..f4f055f2 100644 --- a/open_prices/products/models.py +++ b/open_prices/products/models.py @@ -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( diff --git a/open_prices/products/tests.py b/open_prices/products/tests.py index be8c304c..3b2a3e40 100644 --- a/open_prices/products/tests.py +++ b/open_prices/products/tests.py @@ -1,3 +1,5 @@ +from decimal import Decimal + from django.core.exceptions import ValidationError from django.test import TestCase, TransactionTestCase @@ -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): @@ -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"), + }, + )