Skip to content

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

Add pagination tests for attributes

fowczrek153c44aec992
diff --git a/tests/api/pagination/test_attribute.py b/tests/api/pagination/test_attribute.py
new file mode 100644
index 0000000000..2c7d0f6c14
--- /dev/null
+++ b/tests/api/pagination/test_attribute.py
@@ -0,0 +1,206 @@
+import graphene
+import pytest
+from prices import Money
+
+from saleor.product.models import (
+ Attribute,
+ AttributeProduct,
+ AttributeVariant,
+ Product,
+ ProductType,
+)
+
+from ..utils import get_graphql_content
+
+
+def attributes_for_pagination(collection, category):
+ attributes = Attribute.objects.bulk_create(
+ [
+ Attribute(
+ name="Attr1",
+ slug="attr1",
+ value_required=True,
+ storefront_search_position=4,
+ ),
+ Attribute(
+ name="AttrAttr1",
+ slug="attr_attr1",
+ value_required=True,
+ storefront_search_position=3,
+ ),
+ Attribute(
+ name="AttrAttr2",
+ slug="attr_attr2",
+ value_required=True,
+ storefront_search_position=2,
+ ),
+ Attribute(
+ name="Attr2",
+ slug="attr2",
+ value_required=False,
+ storefront_search_position=5,
+ ),
+ Attribute(
+ name="Attr3",
+ slug="attr3",
+ value_required=False,
+ storefront_search_position=1,
+ ),
+ ]
+ )
+
+ product_type = ProductType.objects.create(name="My Product Type")
+ product = Product.objects.create(
+ name="Test product",
+ product_type=product_type,
+ price=Money("10.00", "USD"),
+ category=category,
+ )
+ collection.products.add(product)
+ AttributeVariant.objects.bulk_create(
+ [
+ AttributeVariant(
+ product_type=product_type, attribute=attributes[1], sort_order=1
+ ),
+ AttributeVariant(
+ product_type=product_type, attribute=attributes[3], sort_order=2
+ ),
+ AttributeVariant(
+ product_type=product_type, attribute=attributes[4], sort_order=3
+ ),
+ ]
+ )
+ AttributeProduct.objects.bulk_create(
+ [
+ AttributeProduct(
+ product_type=product_type, attribute=attributes[2], sort_order=1
+ ),
+ AttributeProduct(
+ product_type=product_type, attribute=attributes[0], sort_order=2
+ ),
+ AttributeProduct(
+ product_type=product_type, attribute=attributes[1], sort_order=3
+ ),
+ ]
+ )
+
+ return attributes
+
+
+QUERY_ATTRIBUTES_PAGINATION = """
+ query (
+ $first: Int, $last: Int, $after: String, $before: String,
+ $sortBy: AttributeSortingInput, $filter: AttributeFilterInput
+ ){
+ attributes (
+ first: $first, last: $last, after: $after, before: $before,
+ sortBy: $sortBy, filter: $filter
+ ) {
+ edges {
+ node {
+ name
+ }
+ }
+ pageInfo{
+ startCursor
+ endCursor
+ hasNextPage
+ hasPreviousPage
+ }
+ }
+ }
+"""
+
+
+ "sort_by, attributes_order",
+ [
+ ({"field": "NAME", "direction": "ASC"}, ["Attr1", "Attr2", "Attr3"]),
+ ({"field": "NAME", "direction": "DESC"}, ["AttrAttr2", "AttrAttr1", "Attr3"]),
+ ({"field": "SLUG", "direction": "ASC"}, ["Attr1", "Attr2", "Attr3"]),
+ ({"field": "VALUE_REQUIRED", "direction": "ASC"}, ["Attr2", "Attr3", "Attr1"],),
+ (
+ {"field": "STOREFRONT_SEARCH_POSITION", "direction": "ASC"},
+ ["Attr3", "AttrAttr2", "AttrAttr1"],
+ ),
+ (
+ {"field": "DASHBOARD_VARIANT_POSITION", "direction": "ASC"},
+ ["AttrAttr1", "Attr2", "Attr3"],
+ ),
+ (
+ {"field": "DASHBOARD_PRODUCT_POSITION", "direction": "ASC"},
+ ["AttrAttr2", "Attr1", "AttrAttr1"],
+ ),
+ ],
+)
+def test_attributes_pagination_with_sorting(
+ sort_by, attributes_order, staff_api_client, attributes_for_pagination,
+):
+ page_size = 3
+
+ variables = {"first": page_size, "after": None, "sortBy": sort_by}
+ response = staff_api_client.post_graphql(QUERY_ATTRIBUTES_PAGINATION, variables,)
+ content = get_graphql_content(response)
+ attributes_nodes = content["data"]["attributes"]["edges"]
+ assert attributes_order[0] == attributes_nodes[0]["node"]["name"]
+ assert attributes_order[1] == attributes_nodes[1]["node"]["name"]
+ assert attributes_order[2] == attributes_nodes[2]["node"]["name"]
+ assert len(attributes_nodes) == page_size
+
+
+ "filter_by, attributes_order",
+ [
+ ({"search": "AttrAttr"}, ["AttrAttr1", "AttrAttr2"]),
+ ({"search": "attr_attr"}, ["AttrAttr1", "AttrAttr2"]),
+ ({"search": "Attr1"}, ["Attr1", "AttrAttr1"]),
+ ({"valueRequired": False}, ["Attr2", "Attr3"]),
+ ],
+)
+def test_attributes_pagination_with_filtering(
+ filter_by, attributes_order, staff_api_client, attributes_for_pagination,
+):
+ page_size = 2
+
+ variables = {"first": page_size, "after": None, "filter": filter_by}
+ response = staff_api_client.post_graphql(QUERY_ATTRIBUTES_PAGINATION, variables)
+ content = get_graphql_content(response)
+ attributes_nodes = content["data"]["attributes"]["edges"]
+ assert attributes_order[0] == attributes_nodes[0]["node"]["name"]
+ assert attributes_order[1] == attributes_nodes[1]["node"]["name"]
+ assert len(attributes_nodes) == page_size
+
+
+def test_attributes_pagination_with_filtering_in_collection(
+ staff_api_client, attributes_for_pagination, collection
+):
+ page_size = 2
+ attributes_order = ["Attr1", "Attr2"]
+ collection_id = graphene.Node.to_global_id("Collection", collection.id)
+ filter_by = {"inCollection": collection_id}
+
+ variables = {"first": page_size, "after": None, "filter": filter_by}
+ response = staff_api_client.post_graphql(QUERY_ATTRIBUTES_PAGINATION, variables)
+ content = get_graphql_content(response)
+ attributes_nodes = content["data"]["attributes"]["edges"]
+ assert attributes_order[0] == attributes_nodes[0]["node"]["name"]
+ assert attributes_order[1] == attributes_nodes[1]["node"]["name"]
+ assert len(attributes_nodes) == page_size
+
+
+def test_attributes_pagination_with_filtering_in_category(
+ staff_api_client, attributes_for_pagination, category
+):
+ page_size = 2
+ attributes_order = ["Attr1", "Attr2"]
+ category_id = graphene.Node.to_global_id("Category", category.id)
+ filter_by = {"inCategory": category_id}
+
+ variables = {"first": page_size, "after": None, "filter": filter_by}
+ response = staff_api_client.post_graphql(QUERY_ATTRIBUTES_PAGINATION, variables)
+ content = get_graphql_content(response)
+ attributes_nodes = content["data"]["attributes"]["edges"]
+ assert attributes_order[0] == attributes_nodes[0]["node"]["name"]
+ assert attributes_order[1] == attributes_nodes[1]["node"]["name"]
+ assert len(attributes_nodes) == page_size
diff --git a/tests/api/pagination/test_menu.py b/tests/api/pagination/test_menu.py
index 0264fbba64..0be205e824 100644
--- a/tests/api/pagination/test_menu.py
+++ b/tests/api/pagination/test_menu.py
@@ -10,11 +10,11 @@ def menus_for_pagination(db):
# We have "footer" and "navbar" from default saleor configuration
return Menu.objects.bulk_create(
[
- Menu(name="Menu1",),
- Menu(name="MenuMenu1",),
- Menu(name="MenuMenu2",),
- Menu(name="Menu2",),
- Menu(name="Menu3",),
+ Menu(name="menu1",),
+ Menu(name="menuMenu1",),
+ Menu(name="menuMenu2",),
+ Menu(name="menu2",),
+ Menu(name="menu3",),
]
)
@@ -47,8 +47,8 @@ QUERY_MENUS_PAGINATION = """
@pytest.mark.parametrize(
"sort_by, menus_order",
[
- ({"field": "NAME", "direction": "ASC"}, ["footer", "Menu1", "Menu2"]),
- ({"field": "NAME", "direction": "DESC"}, ["navbar", "MenuMenu2", "MenuMenu1"]),
+ ({"field": "NAME", "direction": "ASC"}, ["footer", "menu1", "menu2"]),
+ ({"field": "NAME", "direction": "DESC"}, ["navbar", "menuMenu2", "menuMenu1"]),
],
)
def test_menus_pagination_with_sorting(
@@ -69,16 +69,12 @@ def test_menus_pagination_with_sorting(
@pytest.mark.parametrize(
"filter_by, menus_order",
[
- ({"search": "MenuMenu"}, ["MenuMenu1", "MenuMenu2"]),
- ({"search": "Menu1"}, ["Menu1", "MenuMenu1"]),
+ ({"search": "menuMenu"}, ["menuMenu1", "menuMenu2"]),
+ ({"search": "menu1"}, ["menu1", "menuMenu1"]),
],
)
def test_menus_pagination_with_filtering(
- filter_by,
- menus_order,
- staff_api_client,
- permission_manage_discounts,
- menus_for_pagination,
+ filter_by, menus_order, staff_api_client, menus_for_pagination,
):
page_size = 2