Skip to content

Commit

Permalink
feat(Price API): Filter on product category tags
Browse files Browse the repository at this point in the history
  • Loading branch information
TTalex committed Jan 12, 2025
1 parent 8985244 commit 2a966e1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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(
field_name="product__categories_tags", lookup_expr="icontains"
)

class Meta:
model = Price
Expand Down
18 changes: 18 additions & 0 deletions open_prices/api/prices/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
"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
Expand Down Expand Up @@ -113,11 +122,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 +309,13 @@ 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"
with self.assertNumQueries(1 + 1):
response = self.client.get(url)
self.assertEqual(response.data["total"], 1)


class PriceDetailApiTest(TestCase):
@classmethod
Expand Down

0 comments on commit 2a966e1

Please sign in to comment.