diff --git a/open_prices/locations/models.py b/open_prices/locations/models.py index 979469cf..70b8396a 100644 --- a/open_prices/locations/models.py +++ b/open_prices/locations/models.py @@ -108,7 +108,7 @@ def update_user_count(self): from open_prices.prices.models import Price self.user_count = ( - Price.objects.filter(location=self) + Price.objects.filter(location=self, owner__isnull=False) .values_list("owner", flat=True) .distinct() .count() @@ -119,7 +119,7 @@ def update_product_count(self): from open_prices.prices.models import Price self.product_count = ( - Price.objects.filter(location=self) + Price.objects.filter(location=self, product_id__isnull=False) .values_list("product_id", flat=True) .distinct() .count() diff --git a/open_prices/locations/tests.py b/open_prices/locations/tests.py index b68e5332..b087e71e 100644 --- a/open_prices/locations/tests.py +++ b/open_prices/locations/tests.py @@ -6,6 +6,7 @@ from open_prices.locations import constants as location_constants from open_prices.locations.factories import LocationFactory from open_prices.locations.models import Location +from open_prices.prices import constants as price_constants from open_prices.prices.factories import PriceFactory from open_prices.proofs.factories import ProofFactory from open_prices.users.factories import UserFactory @@ -116,16 +117,25 @@ def setUpTestData(cls): price=2.0, owner=cls.user_2.user_id, ) + PriceFactory( + product_code=None, + category_tag="en:tomatoes", + location_osm_id=cls.location.osm_id, + location_osm_type=cls.location.osm_type, + price=3, + price_per=price_constants.PRICE_PER_KILOGRAM, + owner=cls.user_2.user_id, + ) def test_update_price_count(self): self.location.refresh_from_db() - self.assertEqual(self.location.price_count, 2) + self.assertEqual(self.location.price_count, 3) # price post_save # bulk delete prices to skip signals self.location.prices.all().delete() - self.assertEqual(self.location.price_count, 2) # should be 0 + self.assertEqual(self.location.price_count, 3) # should be 0 # update_price_count() should fix price_count self.location.update_price_count() - self.assertEqual(self.location.price_count, 0) + self.assertEqual(self.location.price_count, 0) # all deleted def test_update_user_count(self): self.location.refresh_from_db() diff --git a/open_prices/products/tests.py b/open_prices/products/tests.py index d0d28dfd..11a95279 100644 --- a/open_prices/products/tests.py +++ b/open_prices/products/tests.py @@ -161,13 +161,13 @@ def test_price__stats(self): def test_update_price_count(self): self.product.refresh_from_db() - self.assertEqual(self.product.price_count, 2) + self.assertEqual(self.product.price_count, 2) # price post_save # bulk delete prices to skip signals self.product.prices.all().delete() self.assertEqual(self.product.price_count, 2) # should be 0 # update_price_count() should fix price_count self.product.update_price_count() - self.assertEqual(self.product.price_count, 0) + self.assertEqual(self.product.price_count, 0) # all deleted def test_update_location_count(self): self.product.refresh_from_db() diff --git a/open_prices/proofs/tests.py b/open_prices/proofs/tests.py index fcc84469..256a22a0 100644 --- a/open_prices/proofs/tests.py +++ b/open_prices/proofs/tests.py @@ -112,13 +112,13 @@ def test_is_type_single_shop(self): def test_update_price_count(self): self.proof.refresh_from_db() - self.assertEqual(self.proof.price_count, 2) + self.assertEqual(self.proof.price_count, 2) # price post_save # bulk delete prices to skip signals self.proof.prices.all().delete() self.assertEqual(self.proof.price_count, 2) # should be 0 # update_price_count() should fix price_count self.proof.update_price_count() - self.assertEqual(self.proof.price_count, 0) + self.assertEqual(self.proof.price_count, 0) # all deleted def test_update_location(self): # existing