Skip to content

Commit

Permalink
fix(locations): fix count calculation when FK is null. ref #420
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Oct 2, 2024
1 parent f77ec80 commit a075918
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions open_prices/locations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
16 changes: 13 additions & 3 deletions open_prices/locations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions open_prices/products/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions open_prices/proofs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a075918

Please sign in to comment.