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

Support sorting product by update date #3470

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ All notable, unreleased changes to this project will be documented in this file.
- Use first and last name of a customer or staff member in UI - #3247 by @Bonifacy1, @dominik-zeglen
- Bump `urllib3` and `elasticsearch` to latest versions - #3460 by @maarcingebala
- Resort imports in tests - #3471 by @jxltom
- Support sorting products by update date - #3470 by @jxltom
8 changes: 7 additions & 1 deletion saleor/graphql/product/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ class StockAvailability(graphene.Enum):
class ProductOrderField(graphene.Enum):
NAME = 'name'
PRICE = 'price'
DATE = 'updated_at'

@property
def description(self):
if self == ProductOrderField.NAME:
return 'Sort products by name.'
return 'Sort products by price.'

if self == ProductOrderField.PRICE:
return 'Sort products by price.'

if self == ProductOrderField.DATE:
return 'Sort products by update date.'


class OrderDirection(graphene.Enum):
Expand Down
1 change: 1 addition & 0 deletions saleor/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,7 @@ input ProductOrder {
enum ProductOrderField {
NAME
PRICE
DATE
}

type ProductType implements Node {
Expand Down
25 changes: 23 additions & 2 deletions tests/api/test_product.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from datetime import datetime
from unittest.mock import Mock, patch

import graphene
import pytest
from django.utils.dateparse import parse_datetime
from django.utils.text import slugify
from graphql_relay import to_global_id
from prices import Money
Expand Down Expand Up @@ -304,13 +306,15 @@ def test_filter_products_by_collections(


def test_sort_products(user_api_client, product):
# set price of the first product
# set price and update date of the first product
product.price = Money('10.00', 'USD')
product.updated_at = datetime.utcnow()
product.save()

# create the second product with higher price
# Create the second product with higher price and date
product.pk = None
product.price = Money('20.00', 'USD')
product.updated_at = datetime.utcnow()
product.save()

query = """
Expand All @@ -321,6 +325,7 @@ def test_sort_products(user_api_client, product):
price {
amount
}
updatedAt
}
}
}
Expand All @@ -343,6 +348,22 @@ def test_sort_products(user_api_client, product):
price_1 = content['data']['products']['edges'][1]['node']['price']['amount']
assert price_0 > price_1

asc_date_query = query % {
'sort_by_product_order': '{field: DATE, direction:ASC}'}
response = user_api_client.post_graphql(asc_date_query)
content = get_graphql_content(response)
date_0 = content['data']['products']['edges'][0]['node']['updatedAt'] ## parse_datetime
date_1 = content['data']['products']['edges'][1]['node']['updatedAt']
assert parse_datetime(date_0) < parse_datetime(date_1)

desc_date_query = query % {
'sort_by_product_order': '{field: DATE, direction:DESC}'}
response = user_api_client.post_graphql(desc_date_query)
content = get_graphql_content(response)
date_0 = content['data']['products']['edges'][0]['node']['updatedAt']
date_1 = content['data']['products']['edges'][1]['node']['updatedAt']
assert parse_datetime(date_0) > parse_datetime(date_1)


def test_create_product(
staff_api_client, product_type, category, size_attribute,
Expand Down