Skip to content

Commit

Permalink
feat(prices): add API filter by labels_tags & origins_tags (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Sep 14, 2024
1 parent 4da6da2 commit dab02ae
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 17 deletions.
6 changes: 6 additions & 0 deletions open_prices/api/prices/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ class PriceFilter(django_filters.FilterSet):
product_id__isnull = django_filters.BooleanFilter(
field_name="product_id", lookup_expr="isnull"
)
labels_tags__contains = django_filters.CharFilter(
field_name="labels_tags", lookup_expr="icontains"
)
origins_tags__contains = django_filters.CharFilter(
field_name="origins_tags", lookup_expr="icontains"
)
price__gt = django_filters.NumberFilter(field_name="price", lookup_expr="gt")
price__gte = django_filters.NumberFilter(field_name="price", lookup_expr="gte")
price__lt = django_filters.NumberFilter(field_name="price", lookup_expr="lt")
Expand Down
75 changes: 58 additions & 17 deletions open_prices/api/prices/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
"date": "2024-01-01",
}

PRICE_APPLES = {
"category_tag": "en:apples",
"price_per": price_constants.PRICE_PER_UNIT,
"price": 1,
"currency": "EUR",
"date": "2023-08-30",
}


class PriceListApiTest(TestCase):
@classmethod
Expand Down Expand Up @@ -98,11 +106,23 @@ def setUpTestData(cls):
)
PriceFactory(
product_code=None,
category_tag="en:croissants",
price_per=price_constants.PRICE_PER_UNIT,
price=1,
currency="EUR",
date="2023-08-30",
**PRICE_APPLES,
labels_tags=[],
origins_tags=["en:spain"],
owner=cls.user_session.user.user_id,
)
PriceFactory(
product_code=None,
**PRICE_APPLES,
labels_tags=["en:organic"],
origins_tags=["en:unknown"],
owner=cls.user_session.user.user_id,
)
PriceFactory(
product_code=None,
**PRICE_APPLES,
labels_tags=["en:organic"],
origins_tags=["en:france"],
owner=cls.user_session.user.user_id,
)
PriceFactory(
Expand All @@ -118,7 +138,7 @@ def setUpTestData(cls):

def test_price_list_without_filter(self):
response = self.client.get(self.url)
self.assertEqual(response.data["total"], 3)
self.assertEqual(response.data["total"], 5)

def test_price_list_filter_by_product(self):
# product_code
Expand All @@ -129,16 +149,37 @@ def test_price_list_filter_by_product(self):
# product_id__isnull
url = self.url + "?product_id__isnull=true"
response = self.client.get(url)
self.assertEqual(response.data["total"], 1)
self.assertEqual(response.data["items"][0]["category_tag"], "en:croissants")
self.assertEqual(response.data["total"], 3)
self.assertEqual(response.data["items"][0]["category_tag"], "en:apples")
url = self.url + "?product_id__isnull=false"
response = self.client.get(url)
self.assertEqual(response.data["total"], 2)
# category_tag
url = self.url + "?category_tag=croissants"
url = self.url + "?category_tag=apples"
response = self.client.get(url)
self.assertEqual(response.data["total"], 0)
url = self.url + "?category_tag=en:croissants"
url = self.url + "?category_tag=en:apples"
response = self.client.get(url)
self.assertEqual(response.data["total"], 3)

def test_price_list_filter_by_tags(self):
# labels_tags
url = self.url + "?labels_tags__contains=en:organic"
response = self.client.get(url)
self.assertEqual(response.data["total"], 2)
self.assertEqual(response.data["items"][0]["labels_tags"], ["en:organic"])
# origins_tags
url = self.url + "?origins_tags__contains=en:france"
response = self.client.get(url)
self.assertEqual(response.data["total"], 1)
url = self.url + "?origins_tags__contains=en:unknown"
response = self.client.get(url)
self.assertEqual(response.data["total"], 1)
# combine
url = (
self.url
+ "?category_tag=en:apples&labels_tags__contains=en:organic&origins_tags__contains=en:france"
)
response = self.client.get(url)
self.assertEqual(response.data["total"], 1)

Expand All @@ -155,7 +196,7 @@ def test_price_list_filter_by_price(self):
self.assertEqual(response.data["items"][0]["price"], 50.00)
url = self.url + "?price__lte=20"
response = self.client.get(url)
self.assertEqual(response.data["total"], 2)
self.assertEqual(response.data["total"], 4)
self.assertEqual(response.data["items"][0]["price"], 15.00)
# price_is_discounted
url = self.url + "?price_is_discounted=true"
Expand All @@ -164,12 +205,12 @@ def test_price_list_filter_by_price(self):
self.assertEqual(response.data["items"][0]["price"], 50.00)
url = self.url + "?price_is_discounted=false"
response = self.client.get(url)
self.assertEqual(response.data["total"], 2)
self.assertEqual(response.data["total"], 4)

def test_price_list_filter_by_currency(self):
url = self.url + "?currency=EUR"
response = self.client.get(url)
self.assertEqual(response.data["total"], 2)
self.assertEqual(response.data["total"], 4)

def test_price_list_filter_by_location(self):
# location_osm_id
Expand All @@ -186,7 +227,7 @@ def test_price_list_filter_by_location(self):
self.assertEqual(response.data["total"], 1)
url = self.url + "?location_id__isnull=false"
response = self.client.get(url)
self.assertEqual(response.data["total"], 2)
self.assertEqual(response.data["total"], 4)

def test_price_list_filter_by_proof(self):
# proof_id
Expand All @@ -196,7 +237,7 @@ def test_price_list_filter_by_proof(self):
# proof_id__isnull
url = self.url + "?proof_id__isnull=true"
response = self.client.get(url)
self.assertEqual(response.data["total"], 2)
self.assertEqual(response.data["total"], 4)
url = self.url + "?proof_id__isnull=false"
response = self.client.get(url)
self.assertEqual(response.data["total"], 1)
Expand All @@ -222,12 +263,12 @@ def test_price_list_filter_by_date(self):
def test_price_list_filter_by_owner(self):
url = self.url + f"?owner={self.user_session.user.user_id}"
response = self.client.get(url)
self.assertEqual(response.data["total"], 2)
self.assertEqual(response.data["total"], 4)

def test_price_list_filter_by_created(self):
url = self.url + "?created__gte=2024-01-01T00:00:00Z"
response = self.client.get(url)
self.assertEqual(response.data["total"], 3)
self.assertEqual(response.data["total"], 5)
url = self.url + "?created__lte=2024-01-01T00:00:00Z"
response = self.client.get(url)
self.assertEqual(response.data["total"], 0)
Expand Down
3 changes: 3 additions & 0 deletions open_prices/api/products/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,22 @@ def test_product_list_filter_by_product_name(self):
)

def test_product_list_filter_by_tags(self):
# categories_tags
url = self.url + "?categories_tags__contains=en:breakfasts"
response = self.client.get(url)
self.assertEqual(response.data["total"], 1)
self.assertEqual(
response.data["items"][0]["categories_tags"],
["en:breakfasts", "en:spreads"],
)
# labels_tags
url = self.url + "?labels_tags__contains=en:no-gluten"
response = self.client.get(url)
self.assertEqual(response.data["total"], 1)
self.assertEqual(
response.data["items"][0]["labels_tags"], ["en:no-gluten", "en:organic"]
)
# brands_tags
url = self.url + "?brands_tags__contains=rigoni-di-asiago"
response = self.client.get(url)
self.assertEqual(response.data["total"], 1)
Expand Down

0 comments on commit dab02ae

Please sign in to comment.