Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Stats): fix User & Location stats after allowing any user to add prices on PRICE_TAG proofs #668

Merged
merged 4 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions open_prices/locations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ def update_price_count(self):
self.save(update_fields=["price_count"])

def update_user_count(self):
from open_prices.prices.models import Price
from open_prices.proofs.models import Proof

self.user_count = (
Price.objects.filter(location=self, owner__isnull=False)
Proof.objects.filter(location=self, owner__isnull=False)
.values_list("owner", flat=True)
.distinct()
.count()
Expand Down
18 changes: 14 additions & 4 deletions open_prices/locations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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 import constants as proof_constants
from open_prices.proofs.factories import ProofFactory
from open_prices.users.factories import UserFactory

Expand Down Expand Up @@ -158,7 +159,14 @@ def setUpTestData(cls):
cls.user = UserFactory()
cls.user_2 = UserFactory()
cls.location = LocationFactory(**LOCATION_OSM_NODE_652825274)
cls.proof = ProofFactory(
cls.proof_1 = ProofFactory(
type=proof_constants.TYPE_RECEIPT,
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
owner=cls.user.user_id,
)
cls.proof_2 = ProofFactory(
type=proof_constants.TYPE_PRICE_TAG,
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
owner=cls.user.user_id,
Expand All @@ -167,14 +175,15 @@ def setUpTestData(cls):
product_code="0123456789100",
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
proof_id=cls.proof.id,
proof_id=cls.proof_1.id,
price=1.0,
owner=cls.user.user_id,
)
PriceFactory(
product_code="0123456789101",
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
proof_id=cls.proof_2.id,
price=2.0,
owner=cls.user_2.user_id,
)
Expand All @@ -183,6 +192,7 @@ def setUpTestData(cls):
category_tag="en:tomatoes",
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
proof_id=cls.proof_2.id,
price=3,
price_per=price_constants.PRICE_PER_KILOGRAM,
owner=cls.user_2.user_id,
Expand All @@ -203,7 +213,7 @@ def test_update_user_count(self):
self.assertEqual(self.location.user_count, 0)
# update_user_count() should fix user_count
self.location.update_user_count()
self.assertEqual(self.location.user_count, 2)
self.assertEqual(self.location.user_count, 1) # proof owners

def test_update_product_count(self):
self.location.refresh_from_db()
Expand All @@ -217,4 +227,4 @@ def test_update_proof_count(self):
self.assertEqual(self.location.proof_count, 0)
# update_proof_count() should fix location_count
self.location.update_proof_count()
self.assertEqual(self.location.proof_count, 1)
self.assertEqual(self.location.proof_count, 2)
11 changes: 8 additions & 3 deletions open_prices/products/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.utils import timezone
from openfoodfacts import Flavor

from open_prices.locations import constants as location_constants
from open_prices.locations.factories import LocationFactory
from open_prices.prices.factories import PriceFactory
from open_prices.products import constants as product_constants
Expand Down Expand Up @@ -49,10 +50,14 @@
"unique_scans_n": 1051,
}

LOCATION_NODE_652825274 = {
LOCATION_OSM_NODE_652825274 = {
"type": location_constants.TYPE_OSM,
"osm_id": 652825274,
"osm_type": "NODE",
"osm_type": location_constants.OSM_TYPE_NODE,
"osm_name": "Monoprix",
"osm_lat": "45.1805534",
"osm_lon": "5.7153387",
"price_count": 15,
}


Expand Down Expand Up @@ -107,7 +112,7 @@ class ProductPropertyTest(TestCase):
def setUpTestData(cls):
cls.product = ProductFactory(code="0123456789100", product_quantity=1000)
cls.user = UserFactory()
cls.location = LocationFactory(**LOCATION_NODE_652825274)
cls.location = LocationFactory(**LOCATION_OSM_NODE_652825274)
cls.proof = ProofFactory(
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
Expand Down
11 changes: 8 additions & 3 deletions open_prices/stats/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import IntegrityError
from django.test import TestCase

from open_prices.locations import constants as location_constants
from open_prices.locations.factories import LocationFactory
from open_prices.prices import constants as price_constants
from open_prices.prices.factories import PriceFactory
Expand All @@ -9,10 +10,14 @@
from open_prices.stats.models import TotalStats
from open_prices.users.factories import UserFactory

LOCATION_NODE_652825274 = {
LOCATION_OSM_NODE_652825274 = {
"type": location_constants.TYPE_OSM,
"osm_id": 652825274,
"osm_type": "NODE",
"osm_type": location_constants.OSM_TYPE_NODE,
"osm_name": "Monoprix",
"osm_lat": "45.1805534",
"osm_lon": "5.7153387",
"price_count": 15,
}


Expand All @@ -32,7 +37,7 @@ def setUpTestData(cls):
cls.total_stats = TotalStats.get_solo()
cls.user = UserFactory()
cls.user_2 = UserFactory()
cls.location = LocationFactory(**LOCATION_NODE_652825274)
cls.location = LocationFactory(**LOCATION_OSM_NODE_652825274)
cls.location_2 = LocationFactory()
cls.proof_price_tag = ProofFactory(
type=proof_constants.TYPE_PRICE_TAG,
Expand Down
13 changes: 4 additions & 9 deletions open_prices/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def update_price_count(self):
self.save(update_fields=["price_count"])

def update_location_count(self):
from open_prices.prices.models import Price
from open_prices.proofs.models import Proof

self.location_count = (
Price.objects.filter(owner=self.user_id, location_id__isnull=False)
Proof.objects.filter(owner=self.user_id, location_id__isnull=False)
.values_list("location_id", flat=True)
.distinct()
.count()
Expand All @@ -65,14 +65,9 @@ def update_product_count(self):
self.save(update_fields=["product_count"])

def update_proof_count(self):
from open_prices.prices.models import Price
from open_prices.proofs.models import Proof

self.proof_count = (
Price.objects.filter(owner=self.user_id, proof_id__isnull=False)
.values_list("proof_id", flat=True)
.distinct()
.count()
)
self.proof_count = Proof.objects.filter(owner=self.user_id).count()
self.save(update_fields=["proof_count"])


Expand Down
31 changes: 21 additions & 10 deletions open_prices/users/tests.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
from django.test import TestCase

from open_prices.locations import constants as location_constants
from open_prices.locations.factories import LocationFactory
from open_prices.prices.factories import PriceFactory
from open_prices.prices.models import Price
from open_prices.proofs.factories import ProofFactory
from open_prices.users.factories import UserFactory
from open_prices.users.models import User

LOCATION_NODE_652825274 = {
LOCATION_OSM_NODE_652825274 = {
"type": location_constants.TYPE_OSM,
"osm_id": 652825274,
"osm_type": "NODE",
"osm_type": location_constants.OSM_TYPE_NODE,
"osm_name": "Monoprix",
"osm_lat": "45.1805534",
"osm_lon": "5.7153387",
"price_count": 15,
}
LOCATION_ONLINE_DECATHLON = {
"type": location_constants.TYPE_ONLINE,
"website_url": "https://www.decathlon.fr",
"price_count": 15,
}


Expand All @@ -29,24 +39,25 @@ class UserPropertyTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = UserFactory()
cls.location = LocationFactory(**LOCATION_NODE_652825274)
cls.location_1 = LocationFactory(**LOCATION_OSM_NODE_652825274)
cls.location_2 = LocationFactory(**LOCATION_ONLINE_DECATHLON)
cls.proof = ProofFactory(
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
location_osm_id=cls.location_1.osm_id,
location_osm_type=cls.location_1.osm_type,
owner=cls.user.user_id,
)
PriceFactory(
product_code="0123456789100",
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
location_osm_id=cls.location_1.osm_id,
location_osm_type=cls.location_1.osm_type,
proof_id=cls.proof.id,
price=1.0,
owner=cls.user.user_id,
)
PriceFactory(
product_code="0123456789101",
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
location_osm_id=cls.location_2.osm_id,
location_osm_type=cls.location_2.osm_type,
price=2.0,
owner=cls.user.user_id,
)
Expand All @@ -66,7 +77,7 @@ def test_update_location_count(self):
self.assertEqual(self.user.location_count, 0)
# update_location_count() should fix location_count
self.user.update_location_count()
self.assertEqual(self.user.location_count, 1)
self.assertEqual(self.user.location_count, 1) # proof locations

def test_update_product_count(self):
self.user.refresh_from_db()
Expand Down
Loading