Skip to content

Commit

Permalink
refactor(stats): new model update_price_count() methods (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Sep 10, 2024
1 parent 639cee6 commit b25146d
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 5 deletions.
6 changes: 5 additions & 1 deletion open_prices/locations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ def save(self, *args, **kwargs):
self.full_clean()
super().save(*args, **kwargs)

def update_price_count(self):
self.price_count = self.prices.count()
self.save(update_fields=["price_count"])


@receiver(signals.post_save, sender=Location)
def location_post_create_fetch_data_from_openstreetmap(
def location_post_create_fetch_and_save_data_from_openstreetmap(
sender, instance, created, **kwargs
):
if not settings.TESTING:
Expand Down
36 changes: 33 additions & 3 deletions open_prices/locations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
from open_prices.locations.models import Location
from open_prices.prices.factories import PriceFactory

LOCATION_NODE_652825274 = {
"osm_id": 652825274,
"osm_type": "NODE",
"osm_name": "Monoprix",
}


class LocationModelSaveTest(TestCase):
@classmethod
Expand Down Expand Up @@ -50,9 +56,7 @@ def test_location_id_type_validation(self):

def test_location_decimal_truncate_on_create(self):
location = LocationFactory(
osm_id=652825274,
osm_type="NODE",
osm_name="Monoprix",
**LOCATION_NODE_652825274,
osm_lat="45.1805534",
osm_lon="5.7153387000", # will be truncated
price_count=15,
Expand Down Expand Up @@ -82,3 +86,29 @@ def test_with_stats(self):
location = Location.objects.with_stats().get(id=self.location_with_price.id)
self.assertEqual(location.price_count_annotated, 1)
self.assertEqual(location.price_count, 1)


class LocationPropertyTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.location = LocationFactory(**LOCATION_NODE_652825274)
PriceFactory(
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
price=1.0,
)
PriceFactory(
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
price=2.0,
)

def test_update_price_count(self):
self.location.refresh_from_db()
self.assertEqual(self.location.price_count, 2)
# bulk delete prices to skip signals
self.location.prices.all().delete()
self.assertEqual(self.location.price_count, 2) # should be 0
# update_price_count() should fix price_count
self.location.update_price_count()
self.assertEqual(self.location.price_count, 0)
4 changes: 4 additions & 0 deletions open_prices/products/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ def save(self, *args, **kwargs):
self.full_clean()
super().save(*args, **kwargs)

def update_price_count(self):
self.price_count = self.prices.count()
self.save(update_fields=["price_count"])


@receiver(signals.post_save, sender=Product)
def product_post_create_fetch_and_save_data_from_openfoodfacts(
Expand Down
18 changes: 18 additions & 0 deletions open_prices/products/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,21 @@ def test_with_stats(self):
product = Product.objects.with_stats().get(id=self.product_with_price.id)
self.assertEqual(product.price_count_annotated, 1)
self.assertEqual(product.price_count, 1)


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=2.0)

def test_update_price_count(self):
self.product.refresh_from_db()
self.assertEqual(self.product.price_count, 2)
# 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)
5 changes: 4 additions & 1 deletion open_prices/proofs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def set_location(self):
location, created = Location.objects.get_or_create(
osm_id=self.location_osm_id,
osm_type=self.location_osm_type,
# defaults={"proof_count": 1},
)
self.location = location

Expand All @@ -128,3 +127,7 @@ def save(self, *args, **kwargs):
if not self.id:
self.set_location()
super().save(*args, **kwargs)

def update_price_count(self):
self.price_count = self.prices.count()
self.save(update_fields=["price_count"])
18 changes: 18 additions & 0 deletions open_prices/proofs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,21 @@ def test_with_stats(self):
proof = Proof.objects.with_stats().get(id=self.proof_with_price.id)
self.assertEqual(proof.price_count_annotated, 1)
self.assertEqual(proof.price_count, 1)


class ProofPropertyTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.proof = ProofFactory()
PriceFactory(proof_id=cls.proof.id, price=1.0)
PriceFactory(proof_id=cls.proof.id, price=2.0)

def test_update_price_count(self):
self.proof.refresh_from_db()
self.assertEqual(self.proof.price_count, 2)
# 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)
6 changes: 6 additions & 0 deletions open_prices/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class Meta:
def is_authenticated(self):
return True

def update_price_count(self):
from open_prices.prices.models import Price

self.price_count = Price.objects.filter(owner=self).count()
self.save(update_fields=["price_count"])


class Session(models.Model):
user = models.ForeignKey(
Expand Down
19 changes: 19 additions & 0 deletions open_prices/users/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.test import TestCase

from open_prices.prices.factories import PriceFactory
from open_prices.prices.models import Price
from open_prices.users.factories import UserFactory
from open_prices.users.models import User

Expand All @@ -14,3 +15,21 @@ def setUpTestData(cls):

def test_has_prices(self):
self.assertEqual(User.objects.has_prices().count(), 1)


class UserPropertyTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = UserFactory()
PriceFactory(owner=cls.user.user_id, price=1.0)
PriceFactory(owner=cls.user.user_id, price=2.0)

def test_update_price_count(self):
self.user.refresh_from_db()
self.assertEqual(self.user.price_count, 2)
# bulk delete prices to skip signals
Price.objects.filter(owner=self.user).delete()
self.assertEqual(self.user.price_count, 2) # should be 0
# update_price_count() should fix price_count
self.user.update_price_count()
self.assertEqual(self.user.price_count, 0)

0 comments on commit b25146d

Please sign in to comment.