Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Test sorting products by price

Marcin Gębala9bc6a50262ce
diff --git a/tests/test_graphql.py b/tests/test_graphql.py
index 0086c8ad8d..9c6769a5c1 100644
--- a/tests/test_graphql.py
+++ b/tests/test_graphql.py
@@ -3,8 +3,8 @@ import json
import graphene
import pytest
from django.shortcuts import reverse
-
from saleor.product.models import Category, Product, ProductAttribute
+from prices import Money
from .utils import get_graphql_content
@@ -234,6 +234,49 @@ def test_filter_product_by_attributes(client, product_in_stock):
assert product_data['name'] == product_in_stock.name
+def test_sort_products(client, product_in_stock):
+ # set price of the first product
+ product_in_stock.price = Money('10.00', 'USD')
+ product_in_stock.save()
+
+ # create the second product with higher price
+ product_in_stock.pk = None
+ product_in_stock.price = Money('20.00', 'USD')
+ product_in_stock.save()
+
+ query = '''
+ query {
+ products(sortBy: "%(sort_by)s") {
+ edges {
+ node {
+ price {
+ amount
+ }
+ }
+ }
+ }
+ }
+ '''
+
+ asc_price_query = query % {'sort_by': 'price'}
+ response = client.post(reverse('api'), {'query': asc_price_query})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ product_data = content['data']['products']['edges'][0]['node']
+ price_0 = content['data']['products']['edges'][0]['node']['price']['amount']
+ price_1 = content['data']['products']['edges'][1]['node']['price']['amount']
+ assert price_0 < price_1
+
+ desc_price_query = query % {'sort_by': '-price'}
+ response = client.post(reverse('api'), {'query': desc_price_query})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ product_data = content['data']['products']['edges'][0]['node']
+ price_0 = content['data']['products']['edges'][0]['node']['price']['amount']
+ price_1 = content['data']['products']['edges'][1]['node']['price']['amount']
+ assert price_0 > price_1
+
+
def test_attributes_query(client, product_in_stock):
attributes = ProductAttribute.objects.prefetch_related('values')
query = '''