Skip to content

Commit

Permalink
Cleanup, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Sep 28, 2024
1 parent 6e5c667 commit 2b44136
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion open_prices/api/stats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ class StatsView(APIView):

@extend_schema(responses=TotalStatsSerializer, tags=["stats"])
def get(self, request: Request) -> Response:
serializer = TotalStatsSerializer(TotalStats.get_solo())
total_stats = TotalStats.get_solo()
serializer = TotalStatsSerializer(data=total_stats)
return Response(serializer.data, status=200)
1 change: 0 additions & 1 deletion open_prices/stats/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class Migration(migrations.Migration):
],
options={
"verbose_name": "Total Stats",
"verbose_name_plural": "Total Stats",
},
),
]
1 change: 0 additions & 1 deletion open_prices/stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class TotalStats(SingletonModel):

class Meta:
verbose_name = "Total Stats"
verbose_name_plural = "Total Stats"

def update_price_stats(self):
from open_prices.prices.models import Price
Expand Down
16 changes: 14 additions & 2 deletions open_prices/stats/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.test import TestCase

from open_prices.locations.factories import LocationFactory
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.stats.models import TotalStats
Expand Down Expand Up @@ -50,6 +51,17 @@ def setUpTestData(cls):
price=1.0,
owner=cls.user.user_id,
)
PriceFactory(
product_code=None,
category_tag="en:tomatoes",
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
labels_tags=["en:organic"],
origins_tags=["en:france"],
price=3,
price_per=price_constants.PRICE_PER_KILOGRAM,
owner=cls.user.user_id,
)
PriceFactory(
product_code="0123456789101",
location_osm_id=cls.location.osm_id,
Expand All @@ -64,9 +76,9 @@ def test_update_price_stats(self):
self.assertEqual(self.total_stats.price_category_count, 0)
# update_price_stats() will update price_counts
self.total_stats.update_price_stats()
self.assertEqual(self.total_stats.price_count, 2)
self.assertEqual(self.total_stats.price_count, 3)
self.assertEqual(self.total_stats.price_barcode_count, 2)
self.assertEqual(self.total_stats.price_category_count, 0)
self.assertEqual(self.total_stats.price_category_count, 1)

def test_update_product_stats(self):
self.assertEqual(self.total_stats.product_count, 0)
Expand Down

0 comments on commit 2b44136

Please sign in to comment.