Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix null and nonnull comparison in sorting product media (#11734)
Krzysztof Waliczekdac21891c509
diff --git a/saleor/graphql/product/tests/queries/test_product_query.py b/saleor/graphql/product/tests/queries/test_product_query.pyindex 13122d8979..34dee17b3f 100644--- a/saleor/graphql/product/tests/queries/test_product_query.py+++ b/saleor/graphql/product/tests/queries/test_product_query.py@@ -2197,6 +2197,34 @@ def test_query_product_media_sorting_default(assert media1 <= media2+def test_query_product_media_sorting_default_and_one_sort_order_null(+ staff_api_client, product_with_image_list_and_one_null_sort_order, channel_USD+):+ # given+ sort_by = None+ variables = {+ "id": graphene.Node.to_global_id(+ "Product", product_with_image_list_and_one_null_sort_order.pk+ ),+ "channel": channel_USD.slug,+ "sort_by": sort_by,+ }++ # when+ response = staff_api_client.post_graphql(+ QUERY_PRODUCT_WITH_SORTED_MEDIA,+ variables,+ )++ # then+ content = get_graphql_content(response)+ media = content["data"]["product"]["media"]+ media1 = media[0]["sortOrder"]+ media2 = media[1]["sortOrder"]+ assert media1 is None+ assert media2 is not None++QUERY_PRODUCT_WITH_ATTRIBUTE = """query Product($id: ID!, $channel: String, $slug: String!){product(id: $id, channel: $channel){diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.pyindex 0bfc0d298e..aa1b8aee25 100644--- a/saleor/graphql/product/types/products.py+++ b/saleor/graphql/product/types/products.py@@ -1278,9 +1278,21 @@ class Product(ChannelContextTypeWithMetadata[models.Product]):def sort_media(media) -> list[ProductMedia]:reversed = sort_by["direction"] == "-"++ # Nullable first,+ # achieved by adding the number of nonnull fields as firt element of tuple+ def key(x):+ values_tuple = tuple(+ getattr(x, field)+ for field in sort_by["field"]+ if getattr(x, field) is not None+ )+ values_tuple = (len(values_tuple),) + values_tuple+ return values_tuple+media_sorted = sorted(media,- key=lambda x: tuple(getattr(x, field) for field in sort_by["field"]),+ key=key,reverse=reversed,)return media_sorteddiff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex 4187a92509..e0dd5ce369 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -3283,6 +3283,34 @@ def product_with_image_list(product, image_list, media_root):return product+def product_with_image_list_and_one_null_sort_order(product_with_image_list):+ """+ As we allow to have `null` in `sort_order` in database, but our logic+ covers changing any new `null` values to proper `int` need to execute+ raw SQL query on database to test behaviour of `null` `sort_order`.++ SQL query behaviour:+ Updates one of the product media `sort_order` to `null`.+ """+ with connection.cursor() as cursor:+ cursor.execute(+ """+ UPDATE PRODUCT_PRODUCTMEDIA+ SET SORT_ORDER = NULL+ WHERE ID IN (+ SELECT ID FROM PRODUCT_PRODUCTMEDIA+ WHERE PRODUCT_ID = %s+ ORDER BY ID+ LIMIT 1+ )+ """,+ [product_with_image_list.pk],+ )+ product_with_image_list.refresh_from_db()+ return product_with_image_list++@pytest.fixturedef unavailable_product(product_type, category, channel_USD, default_tax_class):product = Product.objects.create(