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

feat(Prices): API: new filter on product categories_tags #676

Merged
merged 2 commits into from
Jan 12, 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
3 changes: 3 additions & 0 deletions open_prices/api/prices/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class PriceFilter(django_filters.FilterSet):
created__lte = django_filters.DateTimeFilter(
field_name="created", lookup_expr="lte"
)
product__categories_tags__contains = django_filters.CharFilter(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that we know this works, it'll be easy to add extra product filters 👀

field_name="product__categories_tags", lookup_expr="icontains"
)

class Meta:
model = Price
Expand Down
39 changes: 37 additions & 2 deletions open_prices/api/prices/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,45 @@
"osm_lon": "5.7153387",
}

PRODUCT_8001505005707 = {
"code": "8001505005707",
"product_name": "Nocciolata",
"categories_tags": ["en:breakfasts", "en:spreads"],
"labels_tags": ["en:no-gluten", "en:organic"],
"brands_tags": ["rigoni-di-asiago"],
"price_count": 15,
}


class PriceListApiTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.url = reverse("api:prices-list")
PriceFactory(price=15)
location_osm = LocationFactory(**LOCATION_OSM_NODE_652825274)
proof = ProofFactory(type=proof_constants.TYPE_RECEIPT)
PriceFactory(
price=15,
proof_id=proof.id,
location_id=location_osm.id,
location_osm_id=location_osm.osm_id,
location_osm_type=location_osm.osm_type,
)
PriceFactory(price=0)
PriceFactory(price=50)

def test_price_list(self):
# anonymous
with self.assertNumQueries(1 + 1): # thanks to select_related
# thanks to select_related, we only have 2 queries:
# - 1 to count the number of prices
# - 1 to get the prices and their associated proof/location/product
with self.assertNumQueries(1 + 1):
response = self.client.get(self.url)
self.assertEqual(response.data["total"], 3)
self.assertEqual(len(response.data["items"]), 3)
self.assertTrue("id" in response.data["items"][0])
self.assertEqual(response.data["items"][0]["price"], 15.00) # default order
self.assertTrue("proof" in response.data["items"][0])
self.assertTrue("location" in response.data["items"][0])


class PriceListPaginationApiTest(TestCase):
Expand Down Expand Up @@ -113,11 +135,13 @@ def setUpTestData(cls):
cls.user_proof_receipt = ProofFactory(
type=proof_constants.TYPE_RECEIPT, owner=cls.user_session.user.user_id
)
cls.product = ProductFactory(**PRODUCT_8001505005707)
cls.user_price = PriceFactory(
**PRICE_8001505005707,
receipt_quantity=2,
proof_id=cls.user_proof_receipt.id,
owner=cls.user_session.user.user_id,
product=cls.product,
)
PriceFactory(
type=price_constants.TYPE_CATEGORY,
Expand Down Expand Up @@ -298,6 +322,17 @@ def test_price_list_filter_by_created(self):
response = self.client.get(url)
self.assertEqual(response.data["total"], 0)

def test_price_list_filter_by_product_categories_tags(self):
self.assertEqual(Price.objects.count(), 5)
url = self.url + "?product__categories_tags__contains=en:breakfasts"
# thanks to select_related, we only have 2 queries:
# - 1 to count the number of prices
# - 1 to get the prices (even when filtering on product fields)
with self.assertNumQueries(1 + 1):
response = self.client.get(url)
self.assertEqual(response.data["total"], 1)
self.assertTrue("product" in response.data["items"][0])


class PriceDetailApiTest(TestCase):
@classmethod
Expand Down
4 changes: 2 additions & 2 deletions open_prices/api/proofs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_proof_list(self):
# anonymous
# thanks to select_related, we only have 2 queries:
# - 1 to count the number of proofs of the user
# - 1 to get the proofs and their associated locations (select_related)
# - 1 to get the proofs and their associated location
with self.assertNumQueries(2):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
Expand Down Expand Up @@ -418,7 +418,7 @@ def setUpTestData(cls):

def test_price_tag_list(self):
# Check that we can access price tags anonymously
# We only have 2 queries:
# We only have 3 queries:
# - 1 to count the number of price tags
# - 1 to get the price tags and their associated proof
# - 1 to get the price tag predictions (prefetch related)
Expand Down
Loading