Skip to content

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

Add tests for dataloader with available quantity

fowczrek76ba65d9023b
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index 6e76292011..763a67edca 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -3372,111 +3372,6 @@ def test_variant_digital_content(
assert "id" in content["data"]["productVariant"]["digitalContent"]
-def test_variant_availability_without_inventory_tracking(
- api_client, variant_without_inventory_tracking, settings
-):
- query = """
- query variantAvailability($id: ID!) {
- productVariant(id: $id) {
- isAvailable
- stockQuantity
- }
- }
- """
- variant = variant_without_inventory_tracking
- variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
- response = api_client.post_graphql(query, variables)
- content = get_graphql_content(response)
- variant_data = content["data"]["productVariant"]
- assert variant_data["isAvailable"] is True
- assert variant_data["stockQuantity"] == settings.MAX_CHECKOUT_LINE_QUANTITY
-
-
-def test_variant_availability_without_inventory_tracking_not_available(
- api_client, variant_without_inventory_tracking, settings
-):
- query = """
- query variantAvailability($id: ID!) {
- productVariant(id: $id) {
- isAvailable
- stockQuantity
- }
- }
- """
- variant = variant_without_inventory_tracking
- variant.stocks.all().delete()
- variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
- response = api_client.post_graphql(query, variables)
- content = get_graphql_content(response)
- variant_data = content["data"]["productVariant"]
- assert variant_data["isAvailable"] is False
- assert variant_data["stockQuantity"] == 0
-
-
-@patch("saleor.graphql.product.types.products.get_available_quantity_for_customer")
-def test_variant_quantity_available_with_country_code(
- mock_get_available_quantity_for_customer, api_client, variant
-):
- query = """
- query variantAvailability($id: ID!, $country: CountryCode) {
- productVariant(id: $id) {
- quantityAvailable(countryCode: $country)
- }
- }
- """
- country = "PL"
- variables = {
- "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
- "country": country,
- }
- response = api_client.post_graphql(query, variables)
- content = get_graphql_content(response)
- variant_data = content["data"]["productVariant"]
- assert variant_data
- mock_get_available_quantity_for_customer.assert_called_once_with(variant, "PL")
-
-
-@patch("saleor.graphql.product.types.products.get_available_quantity_for_customer")
-def test_variant_quantity_available_without_country_code(
- mock_get_available_quantity_for_customer, api_client, variant
-):
- query = """
- query variantAvailability($id: ID!) {
- productVariant(id: $id) {
- quantityAvailable
- }
- }
- """
- variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
- response = api_client.post_graphql(query, variables)
- content = get_graphql_content(response)
- variant_data = content["data"]["productVariant"]
- assert variant_data
- mock_get_available_quantity_for_customer.assert_called_once_with(variant, None)
-
-
-@patch("saleor.graphql.product.types.products.get_available_quantity_for_customer")
-def test_variant_quantity_available_with_null_as_country_code(
- mock_get_available_quantity_for_customer, api_client, variant
-):
- query = """
- query variantAvailability($id: ID!, $country: CountryCode) {
- productVariant(id: $id) {
- quantityAvailable(countryCode: $country)
- }
- }
- """
- variables = {
- "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
- "country": None,
- }
- response = api_client.post_graphql(query, variables)
- content = get_graphql_content(response)
- variant_data = content["data"]["productVariant"]
- assert variant_data
- mock_get_available_quantity_for_customer.assert_called_once_with(variant, None)
-
-
@pytest.mark.parametrize(
"collection_filter, count",
[
diff --git a/saleor/graphql/product/tests/test_variant_availability.py b/saleor/graphql/product/tests/test_variant_availability.py
new file mode 100644
index 0000000000..7754782691
--- /dev/null
+++ b/saleor/graphql/product/tests/test_variant_availability.py
@@ -0,0 +1,184 @@
+import graphene
+from django.test import override_settings
+
+from ...tests.utils import get_graphql_content
+
+COUNTRY_CODE = "US"
+
+
+def test_variant_availability_without_inventory_tracking(
+ api_client, variant_without_inventory_tracking, settings
+):
+ query = """
+ query variantAvailability($id: ID!) {
+ productVariant(id: $id) {
+ isAvailable
+ stockQuantity
+ quantityAvailable
+ }
+ }
+ """
+ variant = variant_without_inventory_tracking
+ variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
+ response = api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ variant_data = content["data"]["productVariant"]
+ assert variant_data["isAvailable"] is True
+ assert variant_data["stockQuantity"] == settings.MAX_CHECKOUT_LINE_QUANTITY
+ assert variant_data["quantityAvailable"] == settings.MAX_CHECKOUT_LINE_QUANTITY
+
+
+def test_variant_availability_without_inventory_tracking_without_stocks(
+ api_client, variant_without_inventory_tracking, settings
+):
+ query = """
+ query variantAvailability($id: ID!) {
+ productVariant(id: $id) {
+ isAvailable
+ stockQuantity
+ quantityAvailable
+ }
+ }
+ """
+ variant = variant_without_inventory_tracking
+ variant.stocks.all().delete()
+ variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
+ response = api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ variant_data = content["data"]["productVariant"]
+ assert variant_data["isAvailable"] is True
+ assert variant_data["stockQuantity"] == settings.MAX_CHECKOUT_LINE_QUANTITY
+ assert variant_data["quantityAvailable"] == settings.MAX_CHECKOUT_LINE_QUANTITY
+
+
+def test_variant_quantity_available_without_country_code(
+ api_client, variant_with_many_stocks
+):
+ query = """
+ query variantAvailability($id: ID!) {
+ productVariant(id: $id) {
+ quantityAvailable
+ }
+ }
+ """
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant_with_many_stocks.pk)
+ }
+ response = api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ variant_data = content["data"]["productVariant"]
+ assert variant_data["quantityAvailable"] == 7
+
+
+QUERY_VARIANT_AVAILABILITY = """
+ query variantAvailability($id: ID!, $country: CountryCode) {
+ productVariant(id: $id) {
+ quantityAvailable(countryCode: $country)
+ }
+ }
+"""
+
+
+def test_variant_quantity_available_with_country_code(
+ api_client, variant_with_many_stocks,
+):
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant_with_many_stocks.pk),
+ "country": COUNTRY_CODE,
+ }
+ response = api_client.post_graphql(QUERY_VARIANT_AVAILABILITY, variables)
+ content = get_graphql_content(response)
+ variant_data = content["data"]["productVariant"]
+ assert variant_data["quantityAvailable"] == 7
+
+
+def test_variant_quantity_available_with_null_as_country_code(
+ api_client, variant_with_many_stocks
+):
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant_with_many_stocks.pk),
+ "country": None,
+ }
+ response = api_client.post_graphql(QUERY_VARIANT_AVAILABILITY, variables)
+ content = get_graphql_content(response)
+ variant_data = content["data"]["productVariant"]
+ assert variant_data["quantityAvailable"] == 7
+
+
+@override_settings(MAX_CHECKOUT_LINE_QUANTITY=15)
+def test_variant_quantity_available_with_max(
+ api_client, variant_with_many_stocks, settings
+):
+ stock = variant_with_many_stocks.stocks.first()
+ stock.quantity = 16
+ stock.save(update_fields=["quantity"])
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant_with_many_stocks.pk),
+ "country": COUNTRY_CODE,
+ }
+ response = api_client.post_graphql(QUERY_VARIANT_AVAILABILITY, variables)
+ content = get_graphql_content(response)
+ variant_data = content["data"]["productVariant"]
+ assert variant_data["quantityAvailable"] == settings.MAX_CHECKOUT_LINE_QUANTITY
+
+
+def test_variant_quantity_available_without_stocks(
+ api_client, variant_with_many_stocks,
+):
+ variant_with_many_stocks.stocks.all().delete()
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant_with_many_stocks.pk),
+ "country": COUNTRY_CODE,
+ }
+ response = api_client.post_graphql(QUERY_VARIANT_AVAILABILITY, variables)
+ content = get_graphql_content(response)
+ variant_data = content["data"]["productVariant"]
+ assert variant_data["quantityAvailable"] == 0
+
+
+def test_variant_quantity_available_with_allocations(
+ api_client,
+ variant_with_many_stocks,
+ order_line_with_allocation_in_many_stocks,
+ order_line_with_one_allocation,
+):
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant_with_many_stocks.pk),
+ "country": COUNTRY_CODE,
+ }
+ response = api_client.post_graphql(QUERY_VARIANT_AVAILABILITY, variables)
+ content = get_graphql_content(response)
+ variant_data = content["data"]["productVariant"]
+ assert variant_data["quantityAvailable"] == 3
+
+
+@override_settings(MAX_CHECKOUT_LINE_QUANTITY=15)
+def test_variant_quantity_available_without_inventory_tracking(
+ api_client, variant_with_many_stocks, settings
+):
+ variant_with_many_stocks.track_inventory = False
+ variant_with_many_stocks.save(update_fields=["track_inventory"])
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant_with_many_stocks.pk),
+ "country": COUNTRY_CODE,
+ }
+ response = api_client.post_graphql(QUERY_VARIANT_AVAILABILITY, variables)
+ content = get_graphql_content(response)
+ variant_data = content["data"]["productVariant"]
+ assert variant_data["quantityAvailable"] == settings.MAX_CHECKOUT_LINE_QUANTITY
+
+
+@override_settings(MAX_CHECKOUT_LINE_QUANTITY=15)
+def test_variant_quantity_available_without_inventory_tracking_and_stocks(
+ api_client, variant, settings
+):
+ variant.track_inventory = False
+ variant.save(update_fields=["track_inventory"])
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
+ "country": COUNTRY_CODE,
+ }
+ response = api_client.post_graphql(QUERY_VARIANT_AVAILABILITY, variables)
+ content = get_graphql_content(response)
+ variant_data = content["data"]["productVariant"]
+ assert variant_data["quantityAvailable"] == settings.MAX_CHECKOUT_LINE_QUANTITY
diff --git a/saleor/warehouse/availability.py b/saleor/warehouse/availability.py
index 69f7c48aca..dbd5c70486 100644
--- a/saleor/warehouse/availability.py
+++ b/saleor/warehouse/availability.py
@@ -64,6 +64,8 @@ def get_available_quantity_for_customer(
The returned value is limited by `MAX_CHECKOUT_LINE_QUANTITY` setting to
limit the quantity of a variant that can be added in one checkout line.
"""
+ if not variant.track_inventory:
+ return settings.MAX_CHECKOUT_LINE_QUANTITY
stocks = Stock.objects.filter(product_variant=variant)
if country_code:
@@ -73,8 +75,6 @@ def get_available_quantity_for_customer(
if not stocks:
return 0
- if not variant.track_inventory:
- return settings.MAX_CHECKOUT_LINE_QUANTITY
available_quantity_in_shipping_zones: Dict = defaultdict(int)
for shipping_zone_pk, available_quantity in stocks: