Skip to content

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

Fix filtering by SINGLE_REFERENCE attribute values (#18239)

Jonatan Witoszek102e4e5b25e8
diff --git a/saleor/graphql/attribute/shared_filters.py b/saleor/graphql/attribute/shared_filters.py
index f8756983a4..4377bf96ef 100644
--- a/saleor/graphql/attribute/shared_filters.py
+++ b/saleor/graphql/attribute/shared_filters.py
@@ -537,7 +537,10 @@ def validate_attribute_value_input(attributes: list[dict], db_connection_name: s
invalid_input_type_list.append(index_str)
if "boolean" == value_key and input_type != AttributeInputType.BOOLEAN:
invalid_input_type_list.append(index_str)
- if "reference" == value_key and input_type != AttributeInputType.REFERENCE:
+ if "reference" == value_key and input_type not in [
+ AttributeInputType.REFERENCE,
+ AttributeInputType.SINGLE_REFERENCE,
+ ]:
invalid_input_type_list.append(index_str)
validate_attribute_value_reference_input(reference_value_list)
diff --git a/saleor/graphql/attribute/tests/test_utils.py b/saleor/graphql/attribute/tests/test_utils.py
index dd57ae8666..2d2da6a16a 100644
--- a/saleor/graphql/attribute/tests/test_utils.py
+++ b/saleor/graphql/attribute/tests/test_utils.py
@@ -4,10 +4,12 @@ import graphene
import pytest
from django.conf import settings
from django.core.exceptions import ValidationError
+from graphql import GraphQLError
from ....attribute import AttributeInputType
from ....product.error_codes import ProductErrorCode
from ..enums import AttributeValueBulkActionEnum
+from ..shared_filters import validate_attribute_value_input
from ..utils.attribute_assignment import AttributeAssignmentMixin
from ..utils.shared import AttrValuesForSelectableFieldInput, AttrValuesInput
from ..utils.type_handlers import (
@@ -2006,3 +2008,131 @@ def test_pre_save_multiselect_external_reference_action(color_attribute, product
(AttributeValueBulkActionEnum.NONE, value)
for value in color_attribute.values.all()
]
+
+
+ ("attribute_fixture", "value_payload"),
+ [
+ (
+ "product_type_product_reference_attribute",
+ {"reference": {"productIds": {"containsAny": ["ref-id"]}}},
+ ),
+ (
+ "product_type_product_single_reference_attribute",
+ {"reference": {"productVariantSkus": {"containsAny": ["SKU123"]}}},
+ ),
+ ],
+)
+def test_validate_attribute_value_input_accepts_reference_payloads(
+ request, attribute_fixture, value_payload
+):
+ attribute = request.getfixturevalue(attribute_fixture)
+ attributes = [{"slug": attribute.slug, "value": value_payload}]
+
+ # when & then
+ validate_attribute_value_input(attributes, "default")
+
+
+ ("attribute_fixture", "value_payload"),
+ [
+ (
+ "product_type_product_reference_attribute",
+ {"numeric": {"gte": 10}},
+ ),
+ (
+ "product_type_product_single_reference_attribute",
+ {"numeric": {"gte": 10}},
+ ),
+ ],
+)
+def test_validate_attribute_value_input_rejects_invalid_reference_payloads(
+ request, attribute_fixture, value_payload
+):
+ attribute = request.getfixturevalue(attribute_fixture)
+ attributes = [{"slug": attribute.slug, "value": value_payload}]
+
+ with pytest.raises(GraphQLError) as exc_info:
+ validate_attribute_value_input(attributes, "default")
+
+ message = str(exc_info.value)
+ assert "Incorrect input for attributes on position: 0" in message
+ assert "do not match the attribute input type" in message
+
+
+def test_validate_attribute_value_input_rejects_numeric_reference(numeric_attribute):
+ attributes = [
+ {
+ "slug": numeric_attribute.slug,
+ "value": {
+ "reference": {"productIds": {"containsAny": ["ref"]}},
+ },
+ }
+ ]
+
+ with pytest.raises(GraphQLError) as exc_info:
+ validate_attribute_value_input(attributes, "default")
+
+ message = str(exc_info.value)
+ assert "Incorrect input for attributes on position: 0" in message
+ assert "do not match the attribute input type" in message
+
+
+ "attributes",
+ [
+ [{"slug": "attr", "value": {}}],
+ [{"slug": "attr", "value": None}],
+ ],
+)
+def test_validate_attribute_value_input_rejects_empty_values(attributes):
+ with pytest.raises(GraphQLError) as exc_info:
+ validate_attribute_value_input(attributes, "default")
+
+ message = str(exc_info.value)
+ assert "Incorrect input for attributes on position: 0" in message
+ assert "cannot be empty or null" in message
+
+
+def test_validate_attribute_value_input_rejects_multiple_value_keys():
+ attributes = [
+ {
+ "slug": "attr",
+ "value": {"slug": "value1", "name": "value2"},
+ }
+ ]
+
+ with pytest.raises(GraphQLError) as exc_info:
+ validate_attribute_value_input(attributes, "default")
+
+ message = str(exc_info.value)
+ assert "Incorrect input for attributes on position: 0" in message
+ assert "must have only one input key" in message
+
+
+def test_validate_attribute_value_input_combines_invalid_entries(
+ product_type_product_reference_attribute,
+ numeric_attribute,
+ boolean_attribute,
+):
+ attributes = [
+ {
+ "slug": product_type_product_reference_attribute.slug,
+ "value": {"numeric": {"gte": 1}},
+ },
+ {
+ "slug": numeric_attribute.slug,
+ "value": {"boolean": True},
+ },
+ {
+ "slug": boolean_attribute.slug,
+ "value": {"reference": {"productIds": {"containsAny": ["ref"]}}},
+ },
+ ]
+
+ with pytest.raises(GraphQLError) as exc_info:
+ validate_attribute_value_input(attributes, "default")
+
+ message = str(exc_info.value)
+ assert "Incorrect input for attributes on position: 0,1,2" in message
+ assert "do not match the attribute input type" in message
diff --git a/saleor/graphql/product/tests/queries/products_filtrations/test_over_references_pages.py b/saleor/graphql/product/tests/queries/products_filtrations/test_over_references_pages.py
index 5dc23e789d..7ff00d04e8 100644
--- a/saleor/graphql/product/tests/queries/products_filtrations/test_over_references_pages.py
+++ b/saleor/graphql/product/tests/queries/products_filtrations/test_over_references_pages.py
@@ -1,6 +1,7 @@
+import graphene
import pytest
-from ......attribute import AttributeEntityType, AttributeInputType, AttributeType
+from ......attribute import AttributeEntityType, AttributeType
from ......attribute.models import Attribute, AttributeValue
from ......attribute.utils import associate_attribute_values_to_instance
from ......page.models import Page
@@ -11,87 +12,119 @@ from .shared import PRODUCTS_FILTER_QUERY, PRODUCTS_WHERE_QUERY
@pytest.mark.parametrize("query", [PRODUCTS_WHERE_QUERY, PRODUCTS_FILTER_QUERY])
@pytest.mark.parametrize(
- ("filter_type", "expected_count"), [("containsAny", 2), ("containsAll", 1)]
+ "scenario",
+ [
+ {
+ "attr_fixture": "product_type_page_reference_attribute",
+ "filter": "containsAny",
+ "expected": 2,
+ "product_assignments": {
+ "product_with_both_pages": ["ref-1", "ref-2"],
+ "product_with_second_page_only": ["ref-2"],
+ },
+ "search": ["ref-1", "ref-2"],
+ },
+ {
+ "attr_fixture": "product_type_page_reference_attribute",
+ "filter": "containsAll",
+ "expected": 1,
+ "product_assignments": {
+ "product_with_both_pages": ["ref-1", "ref-2"],
+ "product_with_second_page_only": ["ref-2"],
+ },
+ "search": ["ref-1", "ref-2"],
+ },
+ {
+ "attr_fixture": "product_type_page_single_reference_attribute",
+ "filter": "containsAny",
+ "expected": 2,
+ "product_assignments": {
+ "product_with_first_page": ["ref-1"],
+ "product_with_second_page": ["ref-2"],
+ },
+ "search": ["ref-1", "ref-2"],
+ },
+ {
+ "attr_fixture": "product_type_page_single_reference_attribute",
+ "filter": "containsAll",
+ "expected": 2,
+ "product_assignments": {
+ "product_with_first_page": ["ref-1"],
+ "product_second_overridden": ["ref-1"],
+ },
+ "search": ["ref-1"],
+ },
+ ],
)
def test_products_query_with_attr_slug_and_attribute_value_reference_to_pages(
query,
- filter_type,
- expected_count,
+ scenario,
+ request,
staff_api_client,
- product_type,
product_list,
+ product_type,
page_type,
- product_type_page_reference_attribute,
channel_USD,
):
# given
- product_type.product_attributes.add(product_type_page_reference_attribute)
+ reference_attribute = request.getfixturevalue(scenario["attr_fixture"])
+ product_type.product_attributes.add(reference_attribute)
- reference_page_1_slug = "referenced-page-1"
- reference_page_2_slug = "referenced-page-2"
- referenced_page_1, referenced_page_2 = Page.objects.bulk_create(
+ ref_page_1, ref_page_2 = Page.objects.bulk_create(
[
Page(
- title="Referenced Page 1",
- slug=reference_page_1_slug,
+ title="Reference Page 1",
+ slug="ref-1",
page_type=page_type,
is_published=True,
),
Page(
- title="Referenced Page 2",
- slug=reference_page_2_slug,
+ title="Reference Page 2",
+ slug="ref-2",
page_type=page_type,
is_published=True,
),
]
)
- attribute_value_1, attribute_value_2 = AttributeValue.objects.bulk_create(
+ attribute_values = AttributeValue.objects.bulk_create(
[
AttributeValue(
- attribute=product_type_page_reference_attribute,
- name=f"Page {referenced_page_1.pk}",
- slug=f"page-{referenced_page_1.pk}",
- reference_page=referenced_page_1,
+ attribute=reference_attribute,
+ name=f"Page {ref_page_1.pk}",
+ slug=f"page-{ref_page_1.pk}",
+ reference_page=ref_page_1,
),
AttributeValue(
- attribute=product_type_page_reference_attribute,
- name=f"Page {referenced_page_2.pk}",
- slug=f"page-{referenced_page_2.pk}",
- reference_page=referenced_page_2,
+ attribute=reference_attribute,
+ name=f"Page {ref_page_2.pk}",
+ slug=f"page-{ref_page_2.pk}",
+ reference_page=ref_page_2,
),
]
)
- product_with_both_references = product_list[0]
- associate_attribute_values_to_instance(
- product_with_both_references,
- {
- product_type_page_reference_attribute.pk: [
- attribute_value_1,
- attribute_value_2,
- ]
- },
- )
- product_with_single_reference = product_list[1]
- associate_attribute_values_to_instance(
- product_with_single_reference,
- {product_type_page_reference_attribute.pk: [attribute_value_2]},
- )
+ slug_to_value = {
+ ref_page_1.slug: attribute_values[0],
+ ref_page_2.slug: attribute_values[1],
+ }
+
+ for product, slugs in zip(
+ product_list, scenario["product_assignments"].values(), strict=False
+ ):
+ associate_attribute_values_to_instance(
+ product,
+ {reference_attribute.pk: [slug_to_value[slug] for slug in slugs]},
+ )
variables = {
"where": {
"attributes": [
{
- "slug": "product-page-reference",
+ "slug": reference_attribute.slug,
"value": {
"reference": {
- "pageSlugs": {
- filter_type: [
- reference_page_1_slug,
- reference_page_2_slug,
- ]
- }
+ "pageSlugs": {scenario["filter"]: scenario["search"]}
}
},
}
@@ -109,50 +142,62 @@ def test_products_query_with_attr_slug_and_attribute_value_reference_to_pages(
# then
content = get_graphql_content(response)
products_nodes = content["data"]["products"]["edges"]
- assert len(products_nodes) == expected_count
+ assert len(products_nodes) == scenario["expected"]
+ assert products_nodes[0]["node"]["id"] == graphene.Node.to_global_id(
+ "Product", product_list[0].pk
+ )
@pytest.mark.parametrize("query", [PRODUCTS_WHERE_QUERY, PRODUCTS_FILTER_QUERY])
@pytest.mark.parametrize(
- ("filter_type", "expected_count"), [("containsAny", 2), ("containsAll", 1)]
+ ("reference_attribute_fixture", "filter_type", "expected_count"),
+ [
+ # REFERENCE type - searches across all reference attributes
+ ("product_type_page_reference_attribute", "containsAny", 2),
+ ("product_type_page_reference_attribute", "containsAll", 1),
+ # SINGLE_REFERENCE - product has page1 in attr1 and page2 in attr2
+ ("product_type_page_single_reference_attribute", "containsAny", 2),
+ ("product_type_page_single_reference_attribute", "containsAll", 1),
+ ],
)
def test_products_query_with_attribute_value_reference_to_pages(
query,
+ reference_attribute_fixture,
filter_type,
expected_count,
+ request,
staff_api_client,
product_list,
product_type,
page_type,
- product_type_page_reference_attribute,
channel_USD,
):
# given
+ reference_attribute = request.getfixturevalue(reference_attribute_fixture)
second_page_reference_attribute = Attribute.objects.create(
slug="second-page-reference",
name="Page reference",
type=AttributeType.PRODUCT_TYPE,
- input_type=AttributeInputType.REFERENCE,
+ input_type=reference_attribute.input_type,
entity_type=AttributeEntityType.PAGE,
)
+
product_type.product_attributes.add(
- product_type_page_reference_attribute,
+ reference_attribute,
second_page_reference_attribute,
)
- reference_1 = "referenced-page-1"
- reference_2 = "referenced-page-2"
- referenced_page_1, referenced_page_2 = Page.objects.bulk_create(
+ ref_page_1, ref_page_2 = Page.objects.bulk_create(
[
Page(
- title="Referenced Page 1",
- slug=reference_1,
+ title="Reference Page 1",
+ slug="ref-1",
page_type=page_type,
is_published=True,
),
Page(
- title="Referenced Page 2",
- slug=reference_2,
+ title="Reference Page 2",
+ slug="ref-2",
page_type=page_type,
is_published=True,
),
@@ -162,24 +207,27 @@ def test_products_query_with_attribute_value_reference_to_pages(
attribute_value_1, attribute_value_2 = AttributeValue.objects.bulk_create(
[
AttributeValue(
- attribute=product_type_page_reference_attribute,
- name=f"Page {referenced_page_1.pk}",
- slug=f"page-{referenced_page_1.pk}",
- reference_page=referenced_page_1,
+ attribute=reference_attribute,
+ name=f"Page {ref_page_1.pk}",
+ slug=f"page-{ref_page_1.pk}",
+ reference_page=ref_page_1,
),
AttributeValue(
attribute=second_page_reference_attribute,
- name=f"Page {referenced_page_2.pk}",
- slug=f"page-{referenced_page_2.pk}",
- reference_page=referenced_page_2,
+ name=f"Page {ref_page_2.pk}",
+ slug=f"page-{ref_page_2.pk}",
+ reference_page=ref_page_2,
),
]
)
+
product_with_both_references = product_list[0]
+
+ # Always assign one value per attribute for this test (no slug test)
associate_attribute_values_to_instance(
product_with_both_references,
{
- product_type_page_reference_attribute.pk: [attribute_value_1],
+ reference_attribute.pk: [attribute_value_1],
second_page_reference_attribute.pk: [attribute_value_2],
},
)
@@ -196,7 +244,9 @@ def test_products_query_with_attribute_value_reference_to_pages(
{
"value": {
"reference": {
- "pageSlugs": {filter_type: [reference_1, reference_2]}
+ "pageSlugs": {
+ filter_type: [ref_page_1.slug, ref_page_2.slug]
+ }
}
},
}
@@ -214,127 +264,151 @@ def test_products_query_with_attribute_value_reference_to_pages(
# then
content = get_graphql_content(response)
products_nodes = content["data"]["products"]["edges"]
+
assert len(products_nodes) == expected_count
+ assert products_nodes[0]["node"]["id"] == graphene.Node.to_global_id(
+ "Product", product_list[0].pk
+ )
@pytest.mark.parametrize("query", [PRODUCTS_WHERE_QUERY, PRODUCTS_FILTER_QUERY])
@pytest.mark.parametrize(
- ("filter_type", "expected_count"), [("containsAny", 3), ("containsAll", 2)]
+ "scenario",
+ [
+ {
+ "attr_fixture": "product_type_page_reference_attribute",
+ "filter": "containsAny",
+ "expected": 3,
+ "product_assignments": {
+ "product_with_all_pages": ["ref-1", "ref-2", "ref-3"],
+ "product_with_same_all_pages": ["ref-1", "ref-2", "ref-3"],
+ "product_with_first_page_only": ["ref-1"],
+ },
+ "search": ["ref-1", "ref-2", "ref-3"],
+ },
+ {
+ "attr_fixture": "product_type_page_reference_attribute",
+ "filter": "containsAll",
+ "expected": 2,
+ "product_assignments": {
+ "product_with_all_pages": ["ref-1", "ref-2", "ref-3"],
+ "product_with_same_all_pages": ["ref-1", "ref-2", "ref-3"],
+ "product_with_first_page_only": ["ref-1"],
+ },
+ "search": ["ref-1", "ref-2", "ref-3"],
+ },
+ {
+ "attr_fixture": "product_type_page_single_reference_attribute",
+ "filter": "containsAny",
+ "expected": 3,
+ "product_assignments": {
+ "product_with_first_page": ["ref-1"],
+ "product_with_second_page": ["ref-2"],
+ "product_with_third_page": ["ref-3"],
+ },
+ "search": ["ref-1", "ref-2", "ref-3"],
+ },
+ {
+ "attr_fixture": "product_type_page_single_reference_attribute",
+ "filter": "containsAll",
+ "expected": 1,
+ "product_assignments": {
+ "product_with_first_page": ["ref-1"],
+ "product_with_second_page": ["ref-2"],
+ "product_with_third_page": ["ref-3"],
+ },
+ "search": ["ref-1"],
+ },
+ ],
)
def test_products_query_with_attr_slug_and_attribute_value_referenced_page_ids(
query,
- filter_type,
- expected_count,
+ scenario,
+ request,
staff_api_client,
product_list,
product_type,
page_type,
- product_type_page_reference_attribute,
channel_USD,
):
# given
- product_type.product_attributes.add(product_type_page_reference_attribute)
-
- referenced_first_page, referenced_second_page, referenced_third_page = (
- Page.objects.bulk_create(
- [
- Page(
- title="Referenced Page",
- slug="referenced-page",
- page_type=page_type,
- is_published=True,
- ),
- Page(
- title="Referenced Page",
- slug="referenced-page2",
- page_type=page_type,
- is_published=True,
- ),
- Page(
- title="Referenced Page",
- slug="referenced-page3",
- page_type=page_type,
- is_published=True,
- ),
- ]
- )
+ reference_attribute = request.getfixturevalue(scenario["attr_fixture"])
+ product_type.product_attributes.add(
+ reference_attribute,
)
- first_attr_value, second_attr_value, third_attr_value = (
- AttributeValue.objects.bulk_create(
- [
- AttributeValue(
- attribute=product_type_page_reference_attribute,
- name=f"Page {referenced_first_page.pk}",
- slug=f"page-{referenced_first_page.pk}",
- reference_page=referenced_first_page,
- ),
- AttributeValue(
- attribute=product_type_page_reference_attribute,
- name=f"Page {referenced_second_page.pk}",
- slug=f"page-{referenced_second_page.pk}",
- reference_page=referenced_second_page,
- ),
- AttributeValue(
- attribute=product_type_page_reference_attribute,
- name=f"Page {referenced_third_page.pk}",
- slug=f"page-{referenced_third_page.pk}",
- reference_page=referenced_third_page,
- ),
- ]
- )
- )
- first_product_with_all_ids = product_list[0]
- second_product_with_all_ids = product_list[1]
- product_with_single_id = product_list[2]
- associate_attribute_values_to_instance(
- first_product_with_all_ids,
- {
- product_type_page_reference_attribute.pk: [
- first_attr_value,
- second_attr_value,
- third_attr_value,
- ],
- },
+ ref_page_1, ref_page_2, ref_page_3 = Page.objects.bulk_create(
+ [
+ Page(
+ title="Reference Page 1",
+ slug="ref-1",
+ page_type=page_type,
+ is_published=True,
+ ),
+ Page(
+ title="Reference Page 2",
+ slug="ref-2",
+ page_type=page_type,
+ is_published=True,
+ ),
+ Page(
+ title="Reference Page 3",
+ slug="ref-3",
+ page_type=page_type,
+ is_published=True,
+ ),
+ ]
)
- associate_attribute_values_to_instance(
- second_product_with_all_ids,
- {
- product_type_page_reference_attribute.pk: [
- first_attr_value,
- second_attr_value,
- third_attr_value,
- ],
- },
+ attr_values = AttributeValue.objects.bulk_create(
+ [
+ AttributeValue(
+ attribute=reference_attribute,
+ name=f"Page {ref_page_1.pk}",
+ slug=f"page-{ref_page_1.pk}",
+ reference_page=ref_page_1,
+ ),
+ AttributeValue(
+ attribute=reference_attribute,
+ name=f"Page {ref_page_2.pk}",
+ slug=f"page-{ref_page_2.pk}",
+ reference_page=ref_page_2,
+ ),
+ AttributeValue(
+ attribute=reference_attribute,
+ name=f"Page {ref_page_3.pk}",
+ slug=f"page-{ref_page_3.pk}",
+ reference_page=ref_page_3,
+ ),
+ ]
)
- associate_attribute_values_to_instance(
- product_with_single_id,
- {product_type_page_reference_attribute.pk: [first_attr_value]},
- )
+ slug_to_value = {
+ ref_page_1.slug: attr_values[0],
+ ref_page_2.slug: attr_values[1],
+ ref_page_3.slug: attr_values[2],
+ }
+
+ for product, slugs in zip(
+ product_list, scenario["product_assignments"].values(), strict=False
+ ):
+ associate_attribute_values_to_instance(
+ product,
+ {reference_attribute.pk: [slug_to_value[slug] for slug in slugs]},
+ )
- referenced_first_global_id = to_global_id_or_none(referenced_first_page)
- referenced_second_global_id = to_global_id_or_none(referenced_second_page)
- referenced_third_global_id = to_global_id_or_none(referenced_third_page)
+ ref_lookup = {page.slug: page for page in [ref_page_1, ref_page_2, ref_page_3]}
+ search_ids = [to_global_id_or_none(ref_lookup[slug]) for slug in scenario["search"]]
variables = {
"where": {
"attributes": [
{
- "slug": product_type_page_reference_attribute.slug,
+ "slug": reference_attribute.slug,
"value": {
- "reference": {
- "referencedIds": {
- filter_type: [
- referenced_first_global_id,
- referenced_second_global_id,
- referenced_third_global_id,
- ]
- }
- }
+ "reference": {"referencedIds": {scenario["filter"]: search_ids}}
},
- }
+ },
]
},
"channel": channel_USD.slug,
@@ -349,4 +423,4 @@ def test_products_query_with_attr_slug_and_attribute_value_referenced_page_ids(
# then
content = get_graphql_content(response)
products_nodes = content["data"]["products"]["edges"]
- assert len(products_nodes) == expected_count
+ assert len(products_nodes) == scenario["expected"]
diff --git a/saleor/graphql/product/tests/queries/products_filtrations/test_over_references_products.py b/saleor/graphql/product/tests/queries/products_filtrations/test_over_references_products.py
index e7c0b10b8e..da3ac98595 100644
--- a/saleor/graphql/product/tests/queries/products_filtrations/test_over_references_products.py
+++ b/saleor/graphql/product/tests/queries/products_filtrations/test_over_references_products.py
@@ -1,7 +1,7 @@
import graphene
import pytest
-from ......attribute import AttributeEntityType, AttributeInputType, AttributeType
+from ......attribute import AttributeEntityType, AttributeType
from ......attribute.models import Attribute, AttributeValue
from ......attribute.utils import associate_attribute_values_to_instance
from ......product.models import Product
@@ -12,21 +12,69 @@ from .shared import PRODUCTS_FILTER_QUERY, PRODUCTS_WHERE_QUERY
@pytest.mark.parametrize("query", [PRODUCTS_WHERE_QUERY, PRODUCTS_FILTER_QUERY])
@pytest.mark.parametrize(
- ("filter_type", "expected_count"),
- [("containsAny", 2), ("containsAll", 1)],
+ (
+ "reference_attribute_fixture",
+ "filter_type",
+ "expected_count",
+ "product1_values",
+ "product2_values",
+ "search_values",
+ ),
+ [
+ # REFERENCE type - can assign multiple values to one product
+ (
+ "product_type_product_reference_attribute",
+ "containsAny",
+ 2,
+ [0, 1],
+ [1],
+ [0, 1],
+ ),
+ (
+ "product_type_product_reference_attribute",
+ "containsAll",
+ 1,
+ [0, 1],
+ [1],
+ [0, 1],
+ ),
+ # SINGLE_REFERENCE type - can only assign one value
+ (
+ "product_type_product_single_reference_attribute",
+ "containsAny",
+ 2,
+ [0],
+ [1],
+ [0, 1],
+ ),
+ # For SINGLE_REFERENCE containsAll, search for just one value that both products have
+ (
+ "product_type_product_single_reference_attribute",
+ "containsAll",
+ 2,
+ [0],
+ [0],
+ [0],
+ ),
+ ],
)
def test_products_query_with_attr_slug_and_attribute_value_reference_to_products(
query,
+ reference_attribute_fixture,
filter_type,
expected_count,
+ product1_values,
+ product2_values,
+ search_values,
+ request,
staff_api_client,
product_list,
product_type,
- product_type_product_reference_attribute,
channel_USD,
):
# given
- product_type.product_attributes.add(product_type_product_reference_attribute)
+ reference_attribute = request.getfixturevalue(reference_attribute_fixture)
+ product_type.product_attributes.add(reference_attribute)
ref_product_1, ref_product_2 = Product.objects.bulk_create(
[
@@ -43,16 +91,16 @@ def test_products_query_with_attr_slug_and_attribute_value_reference_to_products
]
)
- attribute_value_1, attribute_value_2 = AttributeValue.objects.bulk_create(
+ attribute_values = AttributeValue.objects.bulk_create(
[
AttributeValue(
- attribute=product_type_product_reference_attribute,
+ attribute=reference_attribute,
name=f"Product {ref_product_1.pk}",
slug=f"product-{ref_product_1.pk}",
reference_product=ref_product_1,
),
AttributeValue(
- attribute=product_type_product_reference_attribute,
+ attribute=reference_attribute,
name=f"Product {ref_product_2.pk}",
slug=f"product-{ref_product_2.pk}",
reference_product=ref_product_2,
@@ -61,33 +109,29 @@ def test_products_query_with_attr_slug_and_attribute_value_reference_to_products
)
product_with_both_references = product_list[0]
+ # Assign values based on product1_values indices
associate_attribute_values_to_instance(
product_with_both_references,
- {
- product_type_product_reference_attribute.pk: [
- attribute_value_1,
- attribute_value_2,
- ]
- },
+ {reference_attribute.pk: [attribute_values[i] for i in product1_values]},
)
product_with_single_reference = product_list[1]
associate_attribute_values_to_instance(
product_with_single_reference,
- {product_type_product_reference_attribute.pk: [attribute_value_2]},
+ {reference_attribute.pk: [attribute_values[i] for i in product2_values]},
)
+ # Build search slugs based on search_values indices
+ ref_products = [ref_product_1, ref_product_2]
+ search_slugs = [ref_products[i].slug for i in search_values]
+
variables = {
"where": {
"attributes": [
{
- "slug": "product-reference",
+ "slug": reference_attribute.slug,
"value": {
- "reference": {
- "productSlugs": {
- filter_type: [ref_product_1.slug, ref_product_2.slug]
- }
- }
+ "reference": {"productSlugs": {filter_type: search_slugs}}
},
}
]
@@ -105,37 +149,48 @@ def test_products_query_with_attr_slug_and_attribute_value_reference_to_products
content = get_graphql_content(response)
products_nodes = content["data"]["products"]["edges"]
assert len(products_nodes) == expected_count
- assert products_nodes[0]["node"]["id"] == graphene.Node.to_global_id(
- "Product", product_list[0].pk
- )
+ # Verify first product when we expect results
+ if expected_count > 0:
+ assert products_nodes[0]["node"]["id"] == graphene.Node.to_global_id(
+ "Product", product_list[0].pk
+ )
@pytest.mark.parametrize("query", [PRODUCTS_WHERE_QUERY, PRODUCTS_FILTER_QUERY])
@pytest.mark.parametrize(
- ("filter_type", "expected_count"),
- [("containsAny", 2), ("containsAll", 1)],
+ ("reference_attribute_fixture", "filter_type", "expected_count"),
+ [
+ # REFERENCE type - searches across all reference attributes
+ ("product_type_product_reference_attribute", "containsAny", 2),
+ ("product_type_product_reference_attribute", "containsAll", 1),
+ # SINGLE_REFERENCE - product has ref1 in attr1 and ref2 in attr2
+ ("product_type_product_single_reference_attribute", "containsAny", 2),
+ ("product_type_product_single_reference_attribute", "containsAll", 1),
+ ],
)
def test_products_query_with_attribute_value_reference_to_products(
query,
+ reference_attribute_fixture,
filter_type,
expected_count,
+ request,
staff_api_client,
product_list,
product_type,
- product_type_product_reference_attribute,
channel_USD,
):
# given
+ reference_attribute = request.getfixturevalue(reference_attribute_fixture)
second_product_reference_attribute = Attribute.objects.create(
slug="second-product-reference",
name="Product reference",
type=AttributeType.PRODUCT_TYPE,
- input_type=AttributeInputType.REFERENCE,
+ input_type=reference_attribute.input_type, # Use same type as main attribute
entity_type=AttributeEntityType.PRODUCT,
)
product_type.product_attributes.add(
- product_type_product_reference_attribute,
+ reference_attribute,
second_product_reference_attribute,
)
@@ -157,7 +212,7 @@ def test_products_query_with_attribute_value_reference_to_products(
attribute_value_1, attribute_value_2 = AttributeValue.objects.bulk_create(
[
AttributeValue(
- attribute=product_type_product_reference_attribute,
+ attribute=reference_attribute,
name=f"Product {ref_product_1.pk}",
slug=f"product-{ref_product_1.pk}",
reference_product=ref_product_1,
@@ -172,10 +227,11 @@ def test_products_query_with_attribute_value_reference_to_products(
)
product_with_both_references = product_list[0]
+ # Always assign one value per attribute for this test (no slug test)
associate_attribute_values_to_instance(
product_with_both_references,
{
- product_type_product_reference_attribute.pk: [attribute_value_1],
+ reference_attribute.pk: [attribute_value_1],
second_product_reference_attribute.pk: [attribute_value_2],
},
)
@@ -213,127 +269,117 @@ def test_products_query_with_attribute_value_reference_to_products(
content = get_graphql_content(response)
products_nodes = content["data"]["products"]["edges"]
assert len(products_nodes) == expected_count
- assert products_nodes[0]["node"]["id"] == graphene.Node.to_global_id(
- "Product", product_list[0].pk
- )
+ # Verify first product when we expect results
+ if expected_count > 0:
+ assert products_nodes[0]["node"]["id"] == graphene.Node.to_global_id(
+ "Product", product_list[0].pk
+ )
@pytest.mark.parametrize("query", [PRODUCTS_WHERE_QUERY, PRODUCTS_FILTER_QUERY])
@pytest.mark.parametrize(
- ("filter_type", "expected_count"), [("containsAny", 3), ("containsAll", 2)]
+ (
+ "reference_attribute_fixture",
+ "filter_type",
+ "expected_count",
+ "product_value_assignments",
+ "search_indices",
+ ),
+ [
+ # REFERENCE type - products can have multiple values
+ (
+ "product_type_product_reference_attribute",
+ "containsAny",
+ 3,
+ [[0, 1, 2], [0, 1, 2], [0]],
+ [0, 1, 2],
+ ), # Search for all 3 refs
+ (
+ "product_type_product_reference_attribute",
+ "containsAll",
+ 2,
+ [[0, 1, 2], [0, 1, 2], [0]],
+ [0, 1, 2],
+ ), # Search for all 3 refs
+ # SINGLE_REFERENCE - each product has one value
+ (
+ "product_type_product_single_reference_attribute",
+ "containsAny",
+ 3,
+ [[0], [1], [2]],
+ [0, 1, 2],
+ ), # Search for all 3 refs
+ # For containsAll with SINGLE_REFERENCE, search for single value
+ (
+ "product_type_product_single_reference_attribute",
+ "containsAll",
+ 1,
+ [[0], [1], [2]],
+ [0],
+ ), # Search for just ref[0], only product1 has it
+ ],
)
def test_products_query_with_attr_slug_and_attribute_value_referenced_product_ids(
query,
+ reference_attribute_fixture,
filter_type,
expected_count,
+ product_value_assignments,
+ search_indices,
+ request,
staff_api_client,
product_list,
product_type,
- product_type_product_reference_attribute,
channel_USD,
):
# given
- product_type.product_attributes.add(
- product_type_product_reference_attribute,
- )
+ reference_attribute = request.getfixturevalue(reference_attribute_fixture)
+ product_type.product_attributes.add(reference_attribute)
+
# Create additional products to use as references
- ref_product_1, ref_product_2, ref_product_3 = Product.objects.bulk_create(
+ ref_products = Product.objects.bulk_create(
[
Product(
- name="Reference Product 1",
- slug="ref-1",
- product_type=product_type,
- ),
- Product(
- name="Reference Product 2",
- slug="ref-2",
- product_type=product_type,
- ),
- Product(
- name="Reference Product 3",
- slug="ref-3",
+ name=f"Reference Product {i + 1}",
+ slug=f"ref-{i + 1}",
product_type=product_type,
- ),
+ )
+ for i in range(3)
]
)
- first_attr_value, second_attr_value, third_attr_value = (
- AttributeValue.objects.bulk_create(
- [
- AttributeValue(
- attribute=product_type_product_reference_attribute,
- name=f"Product {ref_product_1.pk}",
- slug=f"product-{ref_product_1.pk}",
- reference_product=ref_product_1,
- ),
- AttributeValue(
- attribute=product_type_product_reference_attribute,
- name=f"Product {ref_product_2.pk}",
- slug=f"product-{ref_product_2.pk}",
- reference_product=ref_product_2,
- ),
- AttributeValue(
- attribute=product_type_product_reference_attribute,
- name=f"Product {ref_product_3.pk}",
- slug=f"product-{ref_product_3.pk}",
- reference_product=ref_product_3,
- ),
- ]
- )
- )
- first_product_with_all_ids = product_list[0]
- second_product_with_all_ids = product_list[1]
- product_with_single_id = product_list[2]
-
- associate_attribute_values_to_instance(
- first_product_with_all_ids,
- {
- product_type_product_reference_attribute.pk: [
- first_attr_value,
- second_attr_value,
- third_attr_value,
- ],
- },
+ attr_values = AttributeValue.objects.bulk_create(
+ [
+ AttributeValue(
+ attribute=reference_attribute,
+ name=f"Product {ref_product.pk}",
+ slug=f"product-{ref_product.pk}",
+ reference_product=ref_product,
+ )
+ for ref_product in ref_products
+ ]
)
- associate_attribute_values_to_instance(
- second_product_with_all_ids,
- {
- product_type_product_reference_attribute.pk: [
- first_attr_value,
- second_attr_value,
- third_attr_value,
- ],
- },
- )
+ # Assign values based on product_value_assignments configuration
+ for product, value_indices in zip(
+ product_list, product_value_assignments, strict=False
+ ):
+ associate_attribute_values_to_instance(
+ product,
+ {reference_attribute.pk: [attr_values[i] for i in value_indices]},
+ )
- associate_attribute_values_to_instance(
- product_with_single_id,
- {
- product_type_product_reference_attribute.pk: [
- first_attr_value,
- ],
- },
- )
- ref_1_global_id = to_global_id_or_none(ref_product_1)
- ref_2_global_id = to_global_id_or_none(ref_product_2)
- ref_3_global_id = to_global_id_or_none(ref_product_3)
+ ref_global_ids = [to_global_id_or_none(ref) for ref in ref_products]
+ # Build search IDs based on search_indices
+ search_ids = [ref_global_ids[i] for i in search_indices]
variables = {
"where": {
"attributes": [
{
- "slug": product_type_product_reference_attribute.slug,
+ "slug": reference_attribute.slug,
"value": {
- "reference": {
- "referencedIds": {
- filter_type: [
- ref_1_global_id,
- ref_2_global_id,
- ref_3_global_id,
- ]
- }
- }
+ "reference": {"referencedIds": {filter_type: search_ids}}
},
},
]
diff --git a/saleor/graphql/product/tests/queries/products_filtrations/test_over_references_variants.py b/saleor/graphql/product/tests/queries/products_filtrations/test_over_references_variants.py
index 1fcd07aec0..242f8e151f 100644
--- a/saleor/graphql/product/tests/queries/products_filtrations/test_over_references_variants.py
+++ b/saleor/graphql/product/tests/queries/products_filtrations/test_over_references_variants.py
@@ -1,7 +1,7 @@
import graphene
import pytest
-from ......attribute import AttributeEntityType, AttributeInputType, AttributeType
+from ......attribute import AttributeEntityType, AttributeType
from ......attribute.models import Attribute, AttributeValue
from ......attribute.utils import associate_attribute_values_to_instance
from .....core.utils import to_global_id_or_none
@@ -11,26 +11,36 @@ from .shared import PRODUCTS_FILTER_QUERY, PRODUCTS_WHERE_QUERY
@pytest.mark.parametrize("query", [PRODUCTS_WHERE_QUERY, PRODUCTS_FILTER_QUERY])
@pytest.mark.parametrize(
- ("filter_type", "expected_count"), [("containsAny", 2), ("containsAll", 1)]
+ ("reference_attribute_fixture", "filter_type", "expected_count"),
+ [
+ # REFERENCE type - searches across all reference attributes
+ ("product_type_variant_reference_attribute", "containsAny", 2),
+ ("product_type_variant_reference_attribute", "containsAll", 1),
+ # SINGLE_REFERENCE - product has variant1 in attr1 and variant2 in attr2
+ ("product_type_variant_single_reference_attribute", "containsAny", 2),
+ ("product_type_variant_single_reference_attribute", "containsAll", 1),
+ ],
)
def test_products_query_with_attribute_value_reference_to_product_variants(
query,
+ reference_attribute_fixture,
filter_type,
expected_count,
+ request,
staff_api_client,
product_list,
product_type,
- product_type_variant_reference_attribute,
product_variant_list,
channel_USD,
):
# given
- product_type.product_attributes.add(product_type_variant_reference_attribute)
+ reference_attribute = request.getfixturevalue(reference_attribute_fixture)
+ product_type.product_attributes.add(reference_attribute)
second_variant_reference_attribute = Attribute.objects.create(
slug="second-product-reference",
name="Product reference",
type=AttributeType.PRODUCT_TYPE,
- input_type=AttributeInputType.REFERENCE,
+ input_type=reference_attribute.input_type,
entity_type=AttributeEntityType.PRODUCT_VARIANT,
)
@@ -48,7 +58,7 @@ def test_products_query_with_attribute_value_reference_to_product_variants(
attribute_value_1, attribute_value_2 = AttributeValue.objects.bulk_create(
[
AttributeValue(
- attribute=product_type_variant_reference_attribute,
+ attribute=reference_attribute,
name=f"Variant {first_variant.pk}",
slug=f"variant-{first_variant.pk}",
reference_variant=first_variant,
@@ -66,7 +76,7 @@ def test_products_query_with_attribute_value_reference_to_product_variants(
associate_attribute_values_to_instance(
product_with_both_references,
{
- product_type_variant_reference_attribute.pk: [attribute_value_1],
+ reference_attribute.pk: [attribute_value_1],
second_variant_reference_attribute.pk: [attribute_value_2],
},
)
@@ -107,28 +117,72 @@ def test_products_query_with_attribute_value_reference_to_product_variants(
content = get_graphql_content(response)
products_nodes = content["data"]["products"]["edges"]
assert len(products_nodes) == expected_count
- assert products_nodes[0]["node"]["id"] == graphene.Node.to_global_id(
- "Product", product_list[0].pk
- )
+ # Verify first product when we expect results
+ if expected_count > 0:
+ assert products_nodes[0]["node"]["id"] == graphene.Node.to_global_id(
+ "Product", product_list[0].pk
+ )
@pytest.mark.parametrize("query", [PRODUCTS_WHERE_QUERY, PRODUCTS_FILTER_QUERY])
@pytest.mark.parametrize(
- ("filter_type", "expected_count"), [("containsAny", 2), ("containsAll", 1)]
+ "scenario",
+ [
+ {
+ "attr_fixture": "product_type_variant_reference_attribute",
+ "filter": "containsAny",
+ "expected": 2,
+ "product_assignments": {
+ "product_with_both_variants": ["test-variant-1", "test-variant-2"],
+ "product_with_second_variant_only": ["test-variant-2"],
+ },
+ "search": ["test-variant-1", "test-variant-2"],
+ },
+ {
+ "attr_fixture": "product_type_variant_reference_attribute",
+ "filter": "containsAll",
+ "expected": 1,
+ "product_assignments": {
+ "product_with_both_variants": ["test-variant-1", "test-variant-2"],
+ "product_with_second_variant_only": ["test-variant-2"],
+ },
+ "search": ["test-variant-1", "test-variant-2"],
+ },
+ {
+ "attr_fixture": "product_type_variant_single_reference_attribute",
+ "filter": "containsAny",
+ "expected": 2,
+ "product_assignments": {
+ "product_with_first_variant": ["test-variant-1"],
+ "product_with_second_variant": ["test-variant-2"],
+ },
+ "search": ["test-variant-1", "test-variant-2"],
+ },
+ {
+ "attr_fixture": "product_type_variant_single_reference_attribute",
+ "filter": "containsAll",
+ "expected": 2,
+ "product_assignments": {
+ "product_with_first_variant": ["test-variant-1"],
+ "product_second_overridden": ["test-variant-1"],
+ },
+ "search": ["test-variant-1"],
+ },
+ ],
)
def test_products_query_with_attr_slug_and_attribute_value_reference_to_product_variants(
query,
- filter_type,
- expected_count,
+ scenario,
+ request,
staff_api_client,
product_list,
product_type,
- product_type_variant_reference_attribute,
product_variant_list,
channel_USD,
):
# given
- product_type.product_attributes.add(product_type_variant_reference_attribute)
+ reference_attribute = request.getfixturevalue(scenario["attr_fixture"])
+ product_type.product_attributes.add(reference_attribute)
first_variant_sku = "test-variant-1"
second_variant_sku = "test-variant-2"
@@ -141,16 +195,16 @@ def test_products_query_with_attr_slug_and_attribute_value_reference_to_product_
second_variant.sku = second_variant_sku
second_variant.save()
- attribute_value_1, attribute_value_2 = AttributeValue.objects.bulk_create(
+ attribute_values = AttributeValue.objects.bulk_create(
[
AttributeValue(
- attribute=product_type_variant_reference_attribute,
+ attribute=reference_attribute,
name=f"Variant {first_variant.pk}",
slug=f"variant-{first_variant.pk}",
reference_variant=first_variant,
),
AttributeValue(
- attribute=product_type_variant_reference_attribute,
+ attribute=reference_attribute,
name=f"Variant {second_variant.pk}",
slug=f"variant-{second_variant.pk}",
reference_variant=second_variant,
@@ -158,35 +212,28 @@ def test_products_query_with_attr_slug_and_attribute_value_reference_to_product_
]
)
- product_with_both_references = product_list[0]
- associate_attribute_values_to_instance(
- product_with_both_references,
- {
- product_type_variant_reference_attribute.pk: [
- attribute_value_1,
- attribute_value_2,
- ]
- },
- )
+ sku_to_value = {
+ first_variant_sku: attribute_values[0],
+ second_variant_sku: attribute_values[1],
+ }
- product_with_single_reference = product_list[1]
- associate_attribute_values_to_instance(
- product_with_single_reference,
- {product_type_variant_reference_attribute.pk: [attribute_value_2]},
- )
+ for product, skus in zip(
+ product_list, scenario["product_assignments"].values(), strict=False
+ ):
+ associate_attribute_values_to_instance(
+ product,
+ {reference_attribute.pk: [sku_to_value[sku] for sku in skus]},
+ )
variables = {
"where": {
"attributes": [
{
- "slug": "variant-reference",
+ "slug": reference_attribute.slug,
"value": {
"reference": {
"productVariantSkus": {
- filter_type: [
- first_variant_sku,
- second_variant_sku,
- ]
+ scenario["filter"]: scenario["search"]
}
}
},
@@ -205,108 +252,158 @@ def test_products_query_with_attr_slug_and_attribute_value_reference_to_product_
# then
content = get_graphql_content(response)
products_nodes = content["data"]["products"]["edges"]
- assert len(products_nodes) == expected_count
- assert products_nodes[0]["node"]["id"] == graphene.Node.to_global_id(
- "Product", product_list[0].pk
- )
+ assert len(products_nodes) == scenario["expected"]
+ # Verify first product when we expect results
+ if scenario["expected"] > 0:
+ assert products_nodes[0]["node"]["id"] == graphene.Node.to_global_id(
+ "Product", product_list[0].pk
+ )
@pytest.mark.parametrize("query", [PRODUCTS_WHERE_QUERY, PRODUCTS_FILTER_QUERY])
@pytest.mark.parametrize(
- ("filter_type", "expected_count"), [("containsAny", 3), ("containsAll", 2)]
+ "scenario",
+ [
+ {
+ "attr_fixture": "product_type_variant_reference_attribute",
+ "filter": "containsAny",
+ "expected": 3,
+ "product_assignments": {
+ "product_with_all_variants": [
+ "test-variant-1",
+ "test-variant-2",
+ "test-variant-3",
+ ],
+ "product_with_same_all_variants": [
+ "test-variant-1",
+ "test-variant-2",
+ "test-variant-3",
+ ],
+ "product_with_first_variant_only": ["test-variant-1"],
+ },
+ "search": ["test-variant-1", "test-variant-2", "test-variant-3"],
+ },
+ {
+ "attr_fixture": "product_type_variant_reference_attribute",
+ "filter": "containsAll",
+ "expected": 2,
+ "product_assignments": {
+ "product_with_all_variants": [
+ "test-variant-1",
+ "test-variant-2",
+ "test-variant-3",
+ ],
+ "product_with_same_all_variants": [
+ "test-variant-1",
+ "test-variant-2",
+ "test-variant-3",
+ ],
+ "product_with_first_variant_only": ["test-variant-1"],
+ },
+ "search": ["test-variant-1", "test-variant-2", "test-variant-3"],
+ },
+ {
+ "attr_fixture": "product_type_variant_single_reference_attribute",
+ "filter": "containsAny",
+ "expected": 3,
+ "product_assignments": {
+ "product_with_first_variant": ["test-variant-1"],
+ "product_with_second_variant": ["test-variant-2"],
+ "product_with_third_variant": ["test-variant-3"],
+ },
+ "search": ["test-variant-1", "test-variant-2", "test-variant-3"],
+ },
+ {
+ "attr_fixture": "product_type_variant_single_reference_attribute",
+ "filter": "containsAll",
+ "expected": 1,
+ "product_assignments": {
+ "product_with_first_variant": ["test-variant-1"],
+ "product_with_second_variant": ["test-variant-2"],
+ "product_with_third_variant": ["test-variant-3"],
+ },
+ "search": ["test-variant-1"],
+ },
+ ],
)
def test_products_query_with_attr_slug_attribute_value_referenced_variant_ids(
query,
- filter_type,
- expected_count,
+ scenario,
+ request,
staff_api_client,
product_list,
product_type,
- product_type_variant_reference_attribute,
product_variant_list,
channel_USD,
):
# given
- product_type.product_attributes.add(
- product_type_variant_reference_attribute,
- )
+ reference_attribute = request.getfixturevalue(scenario["attr_fixture"])
+ product_type.product_attributes.add(reference_attribute)
first_variant = product_variant_list[0]
second_variant = product_variant_list[1]
third_variant = product_variant_list[2]
- first_attr_value, second_attr_value, third_attr_value = (
- AttributeValue.objects.bulk_create(
- [
- AttributeValue(
- attribute=product_type_variant_reference_attribute,
- name=f"Variant {first_variant.pk}",
- slug=f"variant-{first_variant.pk}",
- reference_variant=first_variant,
- ),
- AttributeValue(
- attribute=product_type_variant_reference_attribute,
- name=f"Variant {second_variant.pk}",
- slug=f"variant-{second_variant.pk}",
- reference_variant=second_variant,
- ),
- AttributeValue(
- attribute=product_type_variant_reference_attribute,
- name=f"Variant {third_variant.pk}",
- slug=f"variant-{third_variant.pk}",
- reference_variant=third_variant,
- ),
- ]
- )
- )
- first_product_with_all_ids = product_list[0]
- second_product_with_all_ids = product_list[1]
- product_with_single_id = product_list[2]
- associate_attribute_values_to_instance(
- first_product_with_all_ids,
- {
- product_type_variant_reference_attribute.pk: [
- first_attr_value,
- second_attr_value,
- third_attr_value,
- ],
- },
- )
+ first_variant.sku = "test-variant-1"
+ first_variant.save()
- associate_attribute_values_to_instance(
- second_product_with_all_ids,
- {
- product_type_variant_reference_attribute.pk: [
- first_attr_value,
- second_attr_value,
- third_attr_value,
- ],
- },
- )
+ second_variant.sku = "test-variant-2"
+ second_variant.save()
- associate_attribute_values_to_instance(
- product_with_single_id,
- {product_type_variant_reference_attribute.pk: [first_attr_value]},
+ third_variant.sku = "test-variant-3"
+ third_variant.save()
+
+ attr_values = AttributeValue.objects.bulk_create(
+ [
+ AttributeValue(
+ attribute=reference_attribute,
+ name=f"Variant {first_variant.pk}",
+ slug=f"variant-{first_variant.pk}",
+ reference_variant=first_variant,
+ ),
+ AttributeValue(
+ attribute=reference_attribute,
+ name=f"Variant {second_variant.pk}",
+ slug=f"variant-{second_variant.pk}",
+ reference_variant=second_variant,
+ ),
+ AttributeValue(
+ attribute=reference_attribute,
+ name=f"Variant {third_variant.pk}",
+ slug=f"variant-{third_variant.pk}",
+ reference_variant=third_variant,
+ ),
+ ]
)
- referenced_first_global_id = to_global_id_or_none(first_variant)
- referenced_second_global_id = to_global_id_or_none(second_variant)
- referenced_third_global_id = to_global_id_or_none(third_variant)
+
+ # Assign values based on product_value_assignments configuration
+ sku_to_value = {
+ first_variant.sku: attr_values[0],
+ second_variant.sku: attr_values[1],
+ third_variant.sku: attr_values[2],
+ }
+
+ for product, skus in zip(
+ product_list, scenario["product_assignments"].values(), strict=False
+ ):
+ associate_attribute_values_to_instance(
+ product,
+ {reference_attribute.pk: [sku_to_value[sku] for sku in skus]},
+ )
+
+ ref_lookup = {
+ variant.sku: variant
+ for variant in [first_variant, second_variant, third_variant]
+ }
+ search_ids = [to_global_id_or_none(ref_lookup[sku]) for sku in scenario["search"]]
variables = {
"where": {
"attributes": [
{
- "slug": product_type_variant_reference_attribute.slug,
+ "slug": reference_attribute.slug,
"value": {
- "reference": {
- "referencedIds": {
- filter_type: [
- referenced_first_global_id,
- referenced_second_global_id,
- referenced_third_global_id,
- ]
- }
- }
+ "reference": {"referencedIds": {scenario["filter"]: search_ids}}
},
}
]
@@ -323,4 +420,4 @@ def test_products_query_with_attr_slug_attribute_value_referenced_variant_ids(
# then
content = get_graphql_content(response)
products_nodes = content["data"]["products"]["edges"]
- assert len(products_nodes) == expected_count
+ assert len(products_nodes) == scenario["expected"]