Skip to content

Commit

Permalink
Merge pull request saleor#1876 from elwoodxblues/api-products-ordering
Browse files Browse the repository at this point in the history
Test sorting products by price
  • Loading branch information
Marcin Gębala authored Mar 8, 2018
2 parents ae5343f + 9bc6a50 commit 4d31d4a
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion tests/test_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
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

Expand Down Expand Up @@ -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 = '''
Expand Down

0 comments on commit 4d31d4a

Please sign in to comment.