Skip to content

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

Fix sorting by publication date with pagination (#9741)

Iga Karbowiak454d90dce7ac
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5b250e3dfa..23150fa281 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Changed the order line `id` from `int` to `UUID`, the old ids still can be used
for old order lines.
- Fix invalid `ADDED_PRODUCTS` event parameter for `OrderLinesCreate` mutation - #9653 by @IKarbowiak
+- Fix sorting by publication date with pagination - #9741 by IKarbowiak
### Other changes
- Fix for sending incorrect prices to Avatax - #9633 by @korycins
diff --git a/saleor/graphql/product/sorters.py b/saleor/graphql/product/sorters.py
index 2e2aae0968..f120cb880c 100644
--- a/saleor/graphql/product/sorters.py
+++ b/saleor/graphql/product/sorters.py
@@ -2,7 +2,7 @@ import graphene
from django.db.models import (
BooleanField,
Count,
- DateField,
+ DateTimeField,
ExpressionWrapper,
F,
IntegerField,
@@ -121,7 +121,7 @@ class CollectionSortField(graphene.Enum):
).values_list("published_at")[:1]
)
return queryset.annotate(
- published_at=ExpressionWrapper(subquery, output_field=DateField())
+ published_at=ExpressionWrapper(subquery, output_field=DateTimeField())
)
@@ -226,7 +226,7 @@ class ProductOrderField(graphene.Enum):
).values_list("published_at")[:1]
)
return queryset.annotate(
- published_at=ExpressionWrapper(subquery, output_field=DateField())
+ published_at=ExpressionWrapper(subquery, output_field=DateTimeField())
)
@staticmethod
diff --git a/saleor/graphql/product/tests/deprecated/test_product_sorting.py b/saleor/graphql/product/tests/deprecated/test_product_sorting.py
index eb2051dad9..d57fe6ac54 100644
--- a/saleor/graphql/product/tests/deprecated/test_product_sorting.py
+++ b/saleor/graphql/product/tests/deprecated/test_product_sorting.py
@@ -1,11 +1,16 @@
-from datetime import timedelta
+from datetime import datetime, timedelta
import graphene
import pytest
+import pytz
from django.utils import timezone
from freezegun import freeze_time
-from .....product.models import ProductChannelListing
+from .....product.models import (
+ Collection,
+ CollectionChannelListing,
+ ProductChannelListing,
+)
from ....tests.utils import assert_graphql_error_with_message, get_graphql_content
GET_SORTED_PRODUCTS_QUERY = """
@@ -96,3 +101,154 @@ def test_products_with_sorting_and_without_channel(
# then
assert_graphql_error_with_message(response, "A default channel does not exist.")
+
+
+QUERY_PAGINATED_SORTED_PRODUCTS = """
+ query Products(
+ $first: Int, $sortBy: ProductOrder, $channel: String, $after: String
+ ) {
+ products(first: $first, sortBy: $sortBy, after: $after, channel: $channel) {
+ edges {
+ node {
+ id
+ slug
+ }
+ }
+ pageInfo{
+ startCursor
+ endCursor
+ hasNextPage
+ hasPreviousPage
+ }
+ }
+ }
+"""
+
+
+def test_pagination_for_sorting_products_by_publication_date(
+ api_client, channel_USD, product_list
+):
+ """Ensure that using the cursor in sorting products by publication date works
+ properly."""
+ # given
+ channel_listings = ProductChannelListing.objects.filter(channel_id=channel_USD.id)
+ listings_in_bulk = {listing.product_id: listing for listing in channel_listings}
+ for product in product_list:
+ listing = listings_in_bulk.get(product.id)
+ listing.published_at = datetime.now(pytz.UTC)
+
+ ProductChannelListing.objects.bulk_update(channel_listings, ["published_at"])
+
+ first = 2
+ variables = {
+ "sortBy": {"direction": "ASC", "field": "PUBLICATION_DATE"},
+ "channel": channel_USD.slug,
+ "first": first,
+ }
+
+ # first request
+ response = api_client.post_graphql(QUERY_PAGINATED_SORTED_PRODUCTS, variables)
+
+ content = get_graphql_content(response)
+ data = content["data"]["products"]
+ assert len(data["edges"]) == first
+ assert [node["node"]["slug"] for node in data["edges"]] == [
+ product.slug for product in product_list[:first]
+ ]
+ end_cursor = data["pageInfo"]["endCursor"]
+
+ variables["after"] = end_cursor
+
+ # when
+ # second request
+ response = api_client.post_graphql(QUERY_PAGINATED_SORTED_PRODUCTS, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["products"]
+ expected_count = len(product_list) - first
+ assert len(data["edges"]) == expected_count
+ assert [node["node"]["slug"] for node in data["edges"]] == [
+ product.slug for product in product_list[first:]
+ ]
+
+
+QUERY_PAGINATED_SORTED_COLLECTIONS = """
+ query (
+ $first: Int, $sort_by: CollectionSortingInput!, $after: String, $channel: String
+ ) {
+ collections(first: $first, sortBy: $sort_by, after: $after, channel: $channel) {
+ edges{
+ node{
+ slug
+ }
+ }
+ pageInfo{
+ startCursor
+ endCursor
+ hasNextPage
+ hasPreviousPage
+ }
+ }
+ }
+"""
+
+
+def test_pagination_for_sorting_collections_by_publication_date(
+ api_client, channel_USD
+):
+ """Ensure that using the cursor in sorting collections by publication date works
+ properly."""
+ # given
+ collections = Collection.objects.bulk_create(
+ [
+ Collection(name="Coll1", slug="collection-1"),
+ Collection(name="Coll2", slug="collection-2"),
+ Collection(name="Coll3", slug="collection-3"),
+ ]
+ )
+ now = datetime.now(pytz.UTC)
+ CollectionChannelListing.objects.bulk_create(
+ [
+ CollectionChannelListing(
+ channel=channel_USD,
+ collection=collection,
+ is_published=True,
+ published_at=now - timedelta(days=num),
+ )
+ for num, collection in enumerate(collections)
+ ]
+ )
+
+ first = 2
+ variables = {
+ "sort_by": {"direction": "DESC", "field": "PUBLICATION_DATE"},
+ "channel": channel_USD.slug,
+ "first": first,
+ }
+
+ # first request
+ response = api_client.post_graphql(QUERY_PAGINATED_SORTED_COLLECTIONS, variables)
+
+ content = get_graphql_content(response)
+ data = content["data"]["collections"]
+ assert len(data["edges"]) == first
+ assert [node["node"]["slug"] for node in data["edges"]] == [
+ collection.slug for collection in collections[:first]
+ ]
+ end_cursor = data["pageInfo"]["endCursor"]
+
+ variables["after"] = end_cursor
+
+ # when
+ # second request
+ response = api_client.post_graphql(QUERY_PAGINATED_SORTED_COLLECTIONS, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["collections"]
+ expected_count = len(collections) - first
+ assert len(data["edges"]) == expected_count
+ assert [node["node"]["slug"] for node in data["edges"]] == [
+ collection.slug for collection in collections[first:]
+ ]
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index 691de55cba..3df5f4195e 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -11677,6 +11677,87 @@ def test_categories_query_with_filter(
assert content["data"]["categories"]["totalCount"] == count
+QUERY_PAGINATED_SORTED_COLLECTIONS = """
+ query (
+ $first: Int, $sort_by: CollectionSortingInput!, $after: String, $channel: String
+ ) {
+ collections(first: $first, sortBy: $sort_by, after: $after, channel: $channel) {
+ edges{
+ node{
+ slug
+ }
+ }
+ pageInfo{
+ startCursor
+ endCursor
+ hasNextPage
+ hasPreviousPage
+ }
+ }
+ }
+"""
+
+
+def test_pagination_for_sorting_collections_by_published_at_date(
+ api_client, channel_USD
+):
+ """Ensure that using the cursor in sorting collections by published at date works
+ properly."""
+ # given
+ collections = Collection.objects.bulk_create(
+ [
+ Collection(name="Coll1", slug="collection-1"),
+ Collection(name="Coll2", slug="collection-2"),
+ Collection(name="Coll3", slug="collection-3"),
+ ]
+ )
+ now = datetime.now(pytz.UTC)
+ CollectionChannelListing.objects.bulk_create(
+ [
+ CollectionChannelListing(
+ channel=channel_USD,
+ collection=collection,
+ is_published=True,
+ published_at=now - timedelta(days=num),
+ )
+ for num, collection in enumerate(collections)
+ ]
+ )
+
+ first = 2
+ variables = {
+ "sort_by": {"direction": "DESC", "field": "PUBLISHED_AT"},
+ "channel": channel_USD.slug,
+ "first": first,
+ }
+
+ # first request
+ response = api_client.post_graphql(QUERY_PAGINATED_SORTED_COLLECTIONS, variables)
+
+ content = get_graphql_content(response)
+ data = content["data"]["collections"]
+ assert len(data["edges"]) == first
+ assert [node["node"]["slug"] for node in data["edges"]] == [
+ collection.slug for collection in collections[:first]
+ ]
+ end_cursor = data["pageInfo"]["endCursor"]
+
+ variables["after"] = end_cursor
+
+ # when
+ # second request
+ response = api_client.post_graphql(QUERY_PAGINATED_SORTED_COLLECTIONS, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["collections"]
+ expected_count = len(collections) - first
+ assert len(data["edges"]) == expected_count
+ assert [node["node"]["slug"] for node in data["edges"]] == [
+ collection.slug for collection in collections[first:]
+ ]
+
+
QUERY_CATEGORIES_WITH_SORT = """
query ($sort_by: CategorySortingInput!) {
categories(first:5, sortBy: $sort_by) {
diff --git a/saleor/graphql/product/tests/test_product_sorting.py b/saleor/graphql/product/tests/test_product_sorting.py
index f37fa36db4..e7204d56d0 100644
--- a/saleor/graphql/product/tests/test_product_sorting.py
+++ b/saleor/graphql/product/tests/test_product_sorting.py
@@ -1,8 +1,9 @@
import random
-from datetime import timedelta
+from datetime import datetime, timedelta
import graphene
import pytest
+import pytz
from django.utils import timezone
from freezegun import freeze_time
@@ -231,6 +232,76 @@ def test_sort_products_by_rating(
assert [node["node"]["id"] for node in data] == expected_ids
+QUERY_PAGINATED_SORTED_PRODUCTS = """
+ query Products(
+ $first: Int, $sortBy: ProductOrder, $channel: String, $after: String
+ ) {
+ products(first: $first, sortBy: $sortBy, after: $after, channel: $channel) {
+ edges {
+ node {
+ id
+ slug
+ }
+ }
+ pageInfo{
+ startCursor
+ endCursor
+ hasNextPage
+ hasPreviousPage
+ }
+ }
+ }
+"""
+
+
+def test_pagination_for_sorting_products_by_published_at_date(
+ api_client, channel_USD, product_list
+):
+ """Ensure that using the cursor in sorting products by published at date works
+ properly."""
+ # given
+ channel_listings = ProductChannelListing.objects.filter(channel_id=channel_USD.id)
+ listings_in_bulk = {listing.product_id: listing for listing in channel_listings}
+ for product in product_list:
+ listing = listings_in_bulk.get(product.id)
+ listing.published_at = datetime.now(pytz.UTC)
+
+ ProductChannelListing.objects.bulk_update(channel_listings, ["published_at"])
+
+ first = 2
+ variables = {
+ "sortBy": {"direction": "ASC", "field": "PUBLISHED_AT"},
+ "channel": channel_USD.slug,
+ "first": first,
+ }
+
+ # first request
+ response = api_client.post_graphql(QUERY_PAGINATED_SORTED_PRODUCTS, variables)
+
+ content = get_graphql_content(response)
+ data = content["data"]["products"]
+ assert len(data["edges"]) == first
+ assert [node["node"]["slug"] for node in data["edges"]] == [
+ product.slug for product in product_list[:first]
+ ]
+ end_cursor = data["pageInfo"]["endCursor"]
+
+ variables["after"] = end_cursor
+
+ # when
+ # second request
+ response = api_client.post_graphql(QUERY_PAGINATED_SORTED_PRODUCTS, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["products"]
+ expected_count = len(product_list) - first
+ assert len(data["edges"]) == expected_count
+ assert [node["node"]["slug"] for node in data["edges"]] == [
+ product.slug for product in product_list[first:]
+ ]
+
+
QUERY_SORT_BY_COLLECTION = """
query CollectionProducts($id: ID, $channel: String, $after: String) {
collection(id: $id channel: $channel) {