Skip to content

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

Update schema for products and associated tests

Mira Szewczyk99f930b917ae
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 93523f04b3..e1b03334ba 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -887,13 +887,9 @@ class ProductCreate(ModelMutation):
track_inventory = cleaned_input.get(
"track_inventory", site_settings.track_inventory_by_default
)
- quantity = cleaned_input.get("quantity", 0)
sku = cleaned_input.get("sku")
models.ProductVariant.objects.create(
- product=instance,
- track_inventory=track_inventory,
- sku=sku,
- quantity=quantity,
+ product=instance, track_inventory=track_inventory, sku=sku
)
attributes = cleaned_input.get("attributes")
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index 155b60a8f2..484a55a3b2 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -21,6 +21,7 @@ from ....product.utils.availability import (
)
from ....product.utils.costs import get_margin_for_variant, get_product_costs_data
from ....stock import models as stock_models
+from ....stock.availability import is_product_in_stock
from ...core.connection import CountableDjangoObjectType
from ...core.enums import ReportingPeriod, TaxRateType
from ...core.fields import FilterInputConnectionField, PrefetchingConnectionField
@@ -393,7 +394,7 @@ class Product(CountableDjangoObjectType, MetadataObjectType):
"only meant for displaying."
),
)
- is_available = graphene.Boolean(
+ is_available_in_country = graphene.Boolean(
description="Whether the product is in stock and visible or not."
)
base_price = graphene.Field(Money, description="The product's default base price.")
@@ -494,8 +495,10 @@ class Product(CountableDjangoObjectType, MetadataObjectType):
@staticmethod
@gql_optimizer.resolver_hints(prefetch_related=("variants"))
- def resolve_is_available(root: models.Product, _info):
- return root.is_available
+ def resolve_is_available_in_country(root: models.Product, _info):
+ country = _info.context.country
+ in_stock = is_product_in_stock(root, country)
+ return root.is_visible and in_stock
@staticmethod
@permission_required(ProductPermissions.MANAGE_PRODUCTS)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index ac3e9aeb87..c2e8c494ea 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2974,7 +2974,7 @@ type Product implements Node {
url: String!
thumbnail(size: Int): Image
pricing: ProductPricingInfo
- isAvailable: Boolean
+ isAvailableInCountry: Boolean
basePrice: Money
minimalVariantPrice: Money
taxType: TaxType
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 9142546e6b..3b2109a795 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -28,6 +28,7 @@ from saleor.product.models import (
)
from saleor.product.tasks import update_variants_names
from saleor.product.utils.attributes import associate_attribute_values_to_instance
+from saleor.stock.models import Stock
from tests.api.utils import get_graphql_content
from tests.utils import create_image, create_pdf_file_with_image_ext
@@ -143,7 +144,7 @@ def test_fetch_unavailable_products(user_api_client, product):
assert not content["data"]["products"]["edges"]
-def test_product_query(staff_api_client, product, permission_manage_products):
+def test_product_query(staff_api_client, product, permission_manage_products, stock):
category = Category.objects.first()
product = category.products.first()
query = """
@@ -166,7 +167,7 @@ def test_product_query(staff_api_client, product, permission_manage_products):
variants {
name
}
- isAvailable
+ isAvailableInCountry
pricing {
priceRange {
start {
@@ -221,11 +222,12 @@ def test_product_query(staff_api_client, product, permission_manage_products):
purchase_cost, margin = get_product_costs_data(product)
assert purchase_cost.start.amount == product_data["purchaseCost"]["start"]["amount"]
assert purchase_cost.stop.amount == product_data["purchaseCost"]["stop"]["amount"]
- assert product_data["isAvailable"] is product.is_visible
+ assert product_data["isAvailableInCountry"] is product.is_visible
assert margin[0] == product_data["margin"]["start"]
assert margin[1] == product_data["margin"]["stop"]
[email protected](reason="I dont know how to hanlde this right now")
@pytest.mark.parametrize(
"stock, quantity, count",
[
@@ -910,7 +912,6 @@ QUERY_CREATE_PRODUCT_WITHOUT_VARIANTS = """
$name: String!,
$basePrice: Decimal!,
$sku: String,
- $quantity: Int,
$trackInventory: Boolean)
{
productCreate(
@@ -920,7 +921,6 @@ QUERY_CREATE_PRODUCT_WITHOUT_VARIANTS = """
name: $name,
basePrice: $basePrice,
sku: $sku,
- quantity: $quantity,
trackInventory: $trackInventory
})
{
@@ -930,7 +930,6 @@ QUERY_CREATE_PRODUCT_WITHOUT_VARIANTS = """
variants{
id
sku
- quantity
trackInventory
}
category {
@@ -960,7 +959,6 @@ def test_create_product_without_variants(
product_name = "test name"
product_price = 10
sku = "sku"
- quantity = 1
track_inventory = True
variables = {
@@ -969,7 +967,6 @@ def test_create_product_without_variants(
"name": product_name,
"basePrice": product_price,
"sku": sku,
- "quantity": quantity,
"trackInventory": track_inventory,
}
@@ -983,7 +980,6 @@ def test_create_product_without_variants(
assert data["product"]["productType"]["name"] == product_type.name
assert data["product"]["category"]["name"] == category.name
assert data["product"]["variants"][0]["sku"] == sku
- assert data["product"]["variants"][0]["quantity"] == quantity
assert data["product"]["variants"][0]["trackInventory"] == track_inventory
@@ -1033,7 +1029,6 @@ def test_create_product_without_variants_sku_duplication(
category_id = graphene.Node.to_global_id("Category", category.pk)
product_name = "test name"
product_price = 10
- quantity = 1
track_inventory = True
sku = "1234"
@@ -1043,7 +1038,6 @@ def test_create_product_without_variants_sku_duplication(
"name": product_name,
"basePrice": product_price,
"sku": sku,
- "quantity": quantity,
"trackInventory": track_inventory,
}
@@ -1415,14 +1409,12 @@ def test_update_product_without_variants(
mutation updateProduct(
$productId: ID!,
$sku: String,
- $quantity: Int,
$trackInventory: Boolean)
{
productUpdate(
id: $productId,
input: {
sku: $sku,
- quantity: $quantity,
trackInventory: $trackInventory,
})
{
@@ -1431,7 +1423,6 @@ def test_update_product_without_variants(
variants{
id
sku
- quantity
trackInventory
}
}
@@ -1446,13 +1437,11 @@ def test_update_product_without_variants(
product = product_with_default_variant
product_id = graphene.Node.to_global_id("Product", product.pk)
product_sku = "test_sku"
- product_quantity = 10
product_track_inventory = False
variables = {
"productId": product_id,
"sku": product_sku,
- "quantity": product_quantity,
"trackInventory": product_track_inventory,
}
@@ -1464,7 +1453,6 @@ def test_update_product_without_variants(
assert data["errors"] == []
product = data["product"]["variants"][0]
assert product["sku"] == product_sku
- assert product["quantity"] == product_quantity
assert product["trackInventory"] == product_track_inventory
@@ -2234,7 +2222,7 @@ def test_product_variants_no_ids_list(user_api_client, variant):
[(100, None, 100), (100, 200, 200), (100, 0, 0)],
)
def test_product_variant_price(
- product_price, variant_override, api_variant_price, user_api_client, variant
+ product_price, variant_override, api_variant_price, user_api_client, variant, stock
):
# Set price override on variant that is different than product price
product = variant.product
@@ -2271,6 +2259,7 @@ def test_product_variant_price(
assert variant_price["amount"] == api_variant_price
[email protected](reason="Decision needs to be made about how usage of this function")
def test_stock_availability_filter(user_api_client, product):
query = """
query Products($stockAvailability: StockAvailability) {
@@ -2298,7 +2287,7 @@ def test_stock_availability_filter(user_api_client, product):
assert content["data"]["products"]["totalCount"] == 0
# Change product stock availability and test again
- product.variants.update(quantity=0)
+ Stock.objects.update(quantity=0)
# There should be no products in stock
variables = {"stockAvailability": StockAvailability.IN_STOCK.name}
@@ -2392,9 +2381,7 @@ def test_product_restricted_fields_permissions(
("margin", False),
("costPrice", True),
("priceOverride", True),
- ("quantity", False),
("quantityOrdered", False),
- ("quantityAllocated", False),
("privateMeta", True),
),
)
@@ -2426,65 +2413,6 @@ def test_variant_restricted_fields_permissions(
assert field in content["data"]["productVariant"]
-VARIANT_QUANTITY_AVAILABLE_IN_STOCK_QUERY = """
- query ProductVariant($id: ID!) {
- productVariant(id: $id) {
- stockQuantity
- }
- }
- """
-
-
-def test_variant_available_stock_quantity_is_capped_for_authorized_user(
- staff_api_client, permission_manage_products, variant, settings
-):
- """
- The exact quantity available in stock should be accessible for a staff
- user having the permission to manage products.
- """
- actual_stock_available = 60
- expected_stock_available = settings.MAX_CHECKOUT_LINE_QUANTITY = 50
-
- variant.quantity = actual_stock_available
- variant.quantity_allocated = 0
- variant.save(update_fields=["quantity", "quantity_allocated"])
-
- query = VARIANT_QUANTITY_AVAILABLE_IN_STOCK_QUERY
- variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
- staff_api_client.user.user_permissions.add(permission_manage_products)
-
- data = get_graphql_content(staff_api_client.post_graphql(query, variables))
- stock_available = data["data"]["productVariant"]["stockQuantity"]
-
- assert stock_available == expected_stock_available
-
-
- "actual_stock_available, expected_stock_available",
- ((60, 50), (50, 50), (49, 49), (0, 0)),
-)
-def test_variant_available_stock_quantity_is_capped_for_unauthorized_user(
- api_client, variant, settings, actual_stock_available, expected_stock_available
-):
- """
- The exact quantity available in stock shouldn't be made available to customers
- and unauthorized staff users. Instead it should be capped to a said value.
- """
- settings.MAX_CHECKOUT_LINE_QUANTITY = 50
-
- variant.quantity = actual_stock_available
- variant.quantity_allocated = 0
- variant.save(update_fields=["quantity", "quantity_allocated"])
-
- query = VARIANT_QUANTITY_AVAILABLE_IN_STOCK_QUERY
- variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
-
- data = get_graphql_content(api_client.post_graphql(query, variables))
- stock_available = data["data"]["productVariant"]["stockQuantity"]
-
- assert stock_available == expected_stock_available
-
-
def test_variant_digital_content(
staff_api_client, permission_manage_products, digital_content
):