Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add tests for defining page with product reference attributes
IKarbowiak22646707aa78
diff --git a/saleor/graphql/page/tests/test_page.py b/saleor/graphql/page/tests/test_page.pyindex 20756906b7..486510df28 100644--- a/saleor/graphql/page/tests/test_page.py+++ b/saleor/graphql/page/tests/test_page.py@@ -592,7 +592,7 @@ def test_create_page_with_file_attribute_required_no_file_url_given(assert errors[0]["attributes"] == [file_attribute_id]-def test_create_page_with_reference_attribute(+def test_create_page_with_page_reference_attribute(staff_api_client, permission_manage_pages, page_type, page_reference_attribute, page):# given@@ -662,7 +662,7 @@ def test_create_page_with_reference_attribute(assert page_reference_attribute.values.count() == values_count + 1-def test_create_page_with_reference_attribute_not_required_no_references_given(+def test_create_page_with_page_reference_attribute_not_required_no_references_given(staff_api_client, permission_manage_pages, page_type, page_reference_attribute):# given@@ -713,7 +713,7 @@ def test_create_page_with_reference_attribute_not_required_no_references_given(assert len(data["page"]["attributes"][0]["values"]) == 0-def test_create_page_with_reference_attribute_required_no_references_given(+def test_create_page_with_page_reference_attribute_required_no_references_given(staff_api_client, permission_manage_pages, page_type, page_reference_attribute):# given@@ -761,6 +761,179 @@ def test_create_page_with_reference_attribute_required_no_references_given(assert errors[0]["attributes"] == [file_attribute_id]+def test_create_page_with_product_reference_attribute(+ staff_api_client,+ permission_manage_pages,+ page_type,+ product_reference_attribute,+ product,+):+ # given+ page_slug = "test-slug"+ page_content = "test content"+ page_content_json = json.dumps({"content": "test content"})+ page_title = "test title"+ page_is_published = True+ page_type = PageType.objects.create(+ name="Test page type 2", slug="test-page-type-2"+ )+ page_type_id = graphene.Node.to_global_id("PageType", page_type.pk)++ ref_attribute_id = graphene.Node.to_global_id(+ "Attribute", product_reference_attribute.pk+ )+ page_type.page_attributes.add(product_reference_attribute)+ reference = graphene.Node.to_global_id("Product", product.pk)++ values_count = product_reference_attribute.values.count()++ # test creating root page+ variables = {+ "title": page_title,+ "content": page_content,+ "contentJson": page_content_json,+ "isPublished": page_is_published,+ "slug": page_slug,+ "pageType": page_type_id,+ "attributes": [{"id": ref_attribute_id, "references": [reference]}],+ }++ # when+ response = staff_api_client.post_graphql(+ CREATE_PAGE_MUTATION, variables, permissions=[permission_manage_pages]+ )++ # then+ content = get_graphql_content(response)+ data = content["data"]["pageCreate"]+ errors = data["pageErrors"]++ assert not errors+ assert data["page"]["title"] == page_title+ assert data["page"]["content"] == page_content+ assert data["page"]["contentJson"] == page_content_json+ assert data["page"]["slug"] == page_slug+ assert data["page"]["isPublished"] == page_is_published+ assert data["page"]["pageType"]["id"] == page_type_id+ assert len(data["page"]["attributes"]) == 1+ page_id = data["page"]["id"]+ _, new_page_pk = graphene.Node.from_global_id(page_id)+ expected_attr_data = {+ "attribute": {"slug": product_reference_attribute.slug},+ "values": [+ {+ "slug": f"{new_page_pk}_{product.pk}",+ "file": None,+ "name": product.name,+ "reference": reference,+ }+ ],+ }+ assert data["page"]["attributes"][0] == expected_attr_data++ product_reference_attribute.refresh_from_db()+ assert product_reference_attribute.values.count() == values_count + 1+++def test_create_page_with_product_reference_attribute_not_required_no_references_given(+ staff_api_client, permission_manage_pages, page_type, product_reference_attribute+):+ # given+ page_slug = "test-slug"+ page_content = "test content"+ page_content_json = json.dumps({"content": "test content"})+ page_title = "test title"+ page_is_published = True+ page_type = PageType.objects.create(+ name="Test page type 2", slug="test-page-type-2"+ )+ page_type_id = graphene.Node.to_global_id("PageType", page_type.pk)++ file_attribute_id = graphene.Node.to_global_id(+ "Attribute", product_reference_attribute.pk+ )+ page_type.page_attributes.add(product_reference_attribute)++ product_reference_attribute.value_required = False+ product_reference_attribute.save(update_fields=["value_required"])++ # test creating root page+ variables = {+ "title": page_title,+ "content": page_content,+ "contentJson": page_content_json,+ "isPublished": page_is_published,+ "slug": page_slug,+ "pageType": page_type_id,+ "attributes": [{"id": file_attribute_id, "file": ""}],+ }++ # when+ response = staff_api_client.post_graphql(+ CREATE_PAGE_MUTATION, variables, permissions=[permission_manage_pages]+ )++ content = get_graphql_content(response)+ data = content["data"]["pageCreate"]+ assert data["pageErrors"] == []+ assert data["page"]["title"] == page_title+ assert data["page"]["content"] == page_content+ assert data["page"]["contentJson"] == page_content_json+ assert data["page"]["slug"] == page_slug+ assert data["page"]["isPublished"] == page_is_published+ assert data["page"]["pageType"]["id"] == page_type_id+ assert len(data["page"]["attributes"]) == 1+ assert len(data["page"]["attributes"][0]["values"]) == 0+++def test_create_page_with_product_reference_attribute_required_no_references_given(+ staff_api_client, permission_manage_pages, page_type, product_reference_attribute+):+ # given+ page_slug = "test-slug"+ page_content = "test content"+ page_content_json = json.dumps({"content": "test content"})+ page_title = "test title"+ page_is_published = True+ page_type = PageType.objects.create(+ name="Test page type 2", slug="test-page-type-2"+ )+ page_type_id = graphene.Node.to_global_id("PageType", page_type.pk)++ file_attribute_id = graphene.Node.to_global_id(+ "Attribute", product_reference_attribute.pk+ )+ page_type.page_attributes.add(product_reference_attribute)++ product_reference_attribute.value_required = True+ product_reference_attribute.save(update_fields=["value_required"])++ # test creating root page+ variables = {+ "title": page_title,+ "content": page_content,+ "contentJson": page_content_json,+ "isPublished": page_is_published,+ "slug": page_slug,+ "pageType": page_type_id,+ "attributes": [{"id": file_attribute_id, "file": ""}],+ }++ # when+ response = staff_api_client.post_graphql(+ CREATE_PAGE_MUTATION, variables, permissions=[permission_manage_pages]+ )++ content = get_graphql_content(response)+ data = content["data"]["pageCreate"]+ errors = data["pageErrors"]+ assert not data["page"]+ assert len(errors) == 1+ assert errors[0]["code"] == PageErrorCode.REQUIRED.name+ assert errors[0]["field"] == "attributes"+ assert errors[0]["attributes"] == [file_attribute_id]++def test_page_delete_mutation(staff_api_client, page, permission_manage_pages):query = """mutation DeletePage($id: ID!) {@@ -995,7 +1168,7 @@ def test_update_page_with_file_attribute_new_value_is_not_created(assert updated_attribute in data["page"]["attributes"]-def test_update_page_with_reference_attribute_new_value(+def test_update_page_with_page_reference_attribute_new_value(staff_api_client, permission_manage_pages, page_list, page_reference_attribute):# given@@ -1047,7 +1220,7 @@ def test_update_page_with_reference_attribute_new_value(assert page_reference_attribute.values.count() == values_count + 1-def test_update_page_with_reference_attribute_existing_value(+def test_update_page_with_page_reference_attribute_existing_value(staff_api_client, permission_manage_pages, page_list, page_reference_attribute):# given@@ -1106,6 +1279,123 @@ def test_update_page_with_reference_attribute_existing_value(assert page_reference_attribute.values.count() == values_count+def test_update_page_with_product_reference_attribute_new_value(+ staff_api_client,+ permission_manage_pages,+ page,+ product_reference_attribute,+ product,+):+ # given+ query = UPDATE_PAGE_MUTATION++ page_type = page.page_type+ page_type.page_attributes.add(product_reference_attribute)++ values_count = product_reference_attribute.values.count()+ ref_attribute_id = graphene.Node.to_global_id(+ "Attribute", product_reference_attribute.pk+ )+ reference = graphene.Node.to_global_id("Product", product.pk)++ page_id = graphene.Node.to_global_id("Page", page.id)++ variables = {+ "id": page_id,+ "input": {"attributes": [{"id": ref_attribute_id, "references": [reference]}]},+ }++ # when+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_pages]+ )++ # then+ content = get_graphql_content(response)+ data = content["data"]["pageUpdate"]++ assert not data["pageErrors"]+ assert data["page"]+ updated_attribute = {+ "attribute": {"slug": product_reference_attribute.slug},+ "values": [+ {+ "slug": f"{page.pk}_{product.pk}",+ "name": product.name,+ "file": None,+ "reference": reference,+ }+ ],+ }+ assert updated_attribute in data["page"]["attributes"]++ product_reference_attribute.refresh_from_db()+ assert product_reference_attribute.values.count() == values_count + 1+++def test_update_page_with_product_reference_attribute_existing_value(+ staff_api_client,+ permission_manage_pages,+ page,+ product_reference_attribute,+ product,+):+ # given+ query = UPDATE_PAGE_MUTATION++ page_type = page.page_type+ page_type.page_attributes.add(product_reference_attribute)++ attr_value = AttributeValue.objects.create(+ attribute=product_reference_attribute,+ name=page.title,+ slug=f"{page.pk}_{product.pk}",+ )+ associate_attribute_values_to_instance(+ page, product_reference_attribute, attr_value+ )++ values_count = product_reference_attribute.values.count()+ ref_attribute_id = graphene.Node.to_global_id(+ "Attribute", product_reference_attribute.pk+ )+ reference = graphene.Node.to_global_id("Product", product.pk)++ page_id = graphene.Node.to_global_id("Page", page.id)++ variables = {+ "id": page_id,+ "input": {"attributes": [{"id": ref_attribute_id, "references": [reference]}]},+ }++ # when+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_pages]+ )++ # then+ content = get_graphql_content(response)+ data = content["data"]["pageUpdate"]++ assert not data["pageErrors"]+ assert data["page"]+ updated_attribute = {+ "attribute": {"slug": product_reference_attribute.slug},+ "values": [+ {+ "slug": attr_value.slug,+ "file": None,+ "name": page.title,+ "reference": reference,+ }+ ],+ }+ assert updated_attribute in data["page"]["attributes"]++ product_reference_attribute.refresh_from_db()+ assert product_reference_attribute.values.count() == values_count++@freeze_time("2020-03-18 12:00:00")def test_public_page_sets_publication_date(staff_api_client, permission_manage_pages, page_typediff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex 6b613adb12..b87186f75f 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -845,6 +845,17 @@ def page_reference_attribute(db):)+def product_reference_attribute(db):+ return Attribute.objects.create(+ slug="product-reference",+ name="Product reference",+ type=AttributeType.PRODUCT_TYPE,+ input_type=AttributeInputType.REFERENCE,+ entity_type=AttributeEntityType.PRODUCT,+ )++@pytest.fixturedef size_page_attribute(db):attribute = Attribute.objects.create(