diff --git a/open_prices/locations/models.py b/open_prices/locations/models.py index 6333e9af..7d9788c0 100644 --- a/open_prices/locations/models.py +++ b/open_prices/locations/models.py @@ -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() diff --git a/open_prices/locations/tests.py b/open_prices/locations/tests.py index 143aebfb..99ac9c3f 100644 --- a/open_prices/locations/tests.py +++ b/open_prices/locations/tests.py @@ -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 @@ -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, @@ -167,7 +175,7 @@ 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, ) @@ -175,6 +183,7 @@ def setUpTestData(cls): 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, ) @@ -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, @@ -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() @@ -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) diff --git a/open_prices/products/tests.py b/open_prices/products/tests.py index 7ac74153..a3c400cb 100644 --- a/open_prices/products/tests.py +++ b/open_prices/products/tests.py @@ -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 @@ -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, } @@ -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, diff --git a/open_prices/stats/tests.py b/open_prices/stats/tests.py index e171b912..7369c7ba 100644 --- a/open_prices/stats/tests.py +++ b/open_prices/stats/tests.py @@ -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 @@ -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, } @@ -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, diff --git a/open_prices/users/models.py b/open_prices/users/models.py index 77684319..0c009f96 100644 --- a/open_prices/users/models.py +++ b/open_prices/users/models.py @@ -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() @@ -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"]) diff --git a/open_prices/users/tests.py b/open_prices/users/tests.py index ab03d2fa..36b1d8d5 100644 --- a/open_prices/users/tests.py +++ b/open_prices/users/tests.py @@ -1,5 +1,6 @@ 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 @@ -7,10 +8,19 @@ 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, } @@ -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, ) @@ -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()