Skip to content

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

Rename *.availability to *.pricing and create a base availability

NyanKiyoshi76cb8024709a
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.py
index e2f1121115..bb34db265d 100644
--- a/saleor/graphql/product/types.py
+++ b/saleor/graphql/product/types.py
@@ -182,17 +182,23 @@ class ProductOrder(graphene.InputObjectType):
description='Specifies the direction in which to sort products')
-class VariantAvailability(graphene.ObjectType):
+class BasePricingInfo(graphene.ObjectType):
available = graphene.Boolean(
- description='Whether the variant is in stock and visible or not.')
+ description='Whether it is in stock and visible or not.')
on_sale = graphene.Boolean(
- description='Whether the variant is in sale or not.')
+ description='Whether it is in sale or not.')
discount = graphene.Field(
TaxedMoney,
description='The discount amount if in sale (null otherwise).')
discount_local_currency = graphene.Field(
TaxedMoney,
description='The discount amount in the local currency.')
+
+
+class VariantPricingInfo(BasePricingInfo):
+ discount_local_currency = graphene.Field(
+ TaxedMoney,
+ description='The discount amount in the local currency.')
price = graphene.Field(
TaxedMoney,
description='The price, with any discount subtracted.')
@@ -207,6 +213,23 @@ class VariantAvailability(graphene.ObjectType):
description = 'Represents availability of a variant in the storefront.'
+class ProductPricingInfo(BasePricingInfo):
+ price_range = graphene.Field(
+ TaxedMoneyRange,
+ description='The discounted price range of the product variants.')
+ price_range_undiscounted = graphene.Field(
+ TaxedMoneyRange,
+ description='The undiscounted price range of the product variants.')
+ price_range_local_currency = graphene.Field(
+ TaxedMoneyRange,
+ description=(
+ 'The discounted price range of the product variants '
+ 'in the local currency.'))
+
+ class Meta:
+ description = 'Represents availability of a product in the storefront.'
+
+
class ProductVariant(CountableDjangoObjectType):
stock_quantity = graphene.Int(
required=True, description='Quantity of a product available for sale.')
@@ -218,9 +241,13 @@ class ProductVariant(CountableDjangoObjectType):
price is used."""))
price = graphene.Field(Money, description='Price of the product variant.')
availability = graphene.Field(
- VariantAvailability, description=dedent(
+ VariantPricingInfo, description=dedent(
"""Informs about variant's availability in the
- storefront, current price and discounted price."""))
+ storefront, current price and discounted price."""),
+ deprecation_reason='Has been renamed to \'pricing\'.')
+ pricing = graphene.Field(
+ VariantPricingInfo, description=dedent("""Informs about product's pricing in the
+ storefront, current price and discounts."""))
attributes = graphene.List(
graphene.NonNull(SelectedAttribute), required=True,
description='List of attributes assigned to this variant.')
@@ -286,11 +313,13 @@ class ProductVariant(CountableDjangoObjectType):
@gql_optimizer.resolver_hints(
prefetch_related=('product', ), only=['price_override'])
- def resolve_availability(self, info):
+ def resolve_pricing(self, info):
context = info.context
availability = get_variant_availability(
self, context.discounts, context.taxes, context.currency)
- return VariantAvailability(**availability._asdict())
+ return VariantPricingInfo(**availability._asdict())
+
+ resolve_availability = resolve_pricing
@permission_required('product.manage_products')
def resolve_price_override(self, *_args):
@@ -330,33 +359,6 @@ class ProductVariant(CountableDjangoObjectType):
return None
-class ProductAvailability(graphene.ObjectType):
- available = graphene.Boolean(
- description='Whether the product is in stock and visible or not.')
- on_sale = graphene.Boolean(
- description='Whether the product is in stock and visible or not.')
- discount = graphene.Field(
- TaxedMoney,
- description='The discount amount if in sale (null otherwise).')
- discount_local_currency = graphene.Field(
- TaxedMoney,
- description='The discount amount in the local currency.')
- price_range = graphene.Field(
- TaxedMoneyRange,
- description='The discounted price range of the product variants.')
- price_range_undiscounted = graphene.Field(
- TaxedMoneyRange,
- description='The undiscounted price range of the product variants.')
- price_range_local_currency = graphene.Field(
- TaxedMoneyRange,
- description=(
- 'The discounted price range of the product variants '
- 'in the local currency.'))
-
- class Meta:
- description = 'Represents availability of a product in the storefront.'
-
-
class Product(CountableDjangoObjectType):
url = graphene.String(
description='The storefront URL for the product.', required=True)
@@ -369,13 +371,19 @@ class Product(CountableDjangoObjectType):
Image, description='The main thumbnail for a product.',
size=graphene.Argument(graphene.Int, description='Size of thumbnail'))
availability = graphene.Field(
- ProductAvailability, description=dedent(
+ ProductPricingInfo,
+ description=dedent(
"""Informs about product's availability in the
- storefront, current price and discounts."""))
+ storefront, current price and discounts."""),
+ deprecation_reason='Has been renamed to \'pricing\'.')
+ pricing = graphene.Field(
+ ProductPricingInfo, description=dedent("""Informs about product's pricing in the
+ storefront, current price and discounts."""))
price = graphene.Field(
Money,
description=dedent("""The product's base price (without any discounts
- applied)."""))
+ applied)."""),
+ deprecation_reason='Has been replaced by \'pricing.price_range_undiscounted\'')
tax_rate = TaxRateType(description='A type of tax rate.')
attributes = graphene.List(
graphene.NonNull(SelectedAttribute), required=True,
@@ -448,11 +456,13 @@ class Product(CountableDjangoObjectType):
@gql_optimizer.resolver_hints(
prefetch_related=('variants', 'collections'),
only=['publication_date', 'charge_taxes', 'price', 'tax_rate'])
- def resolve_availability(self, info):
+ def resolve_pricing(self, info):
context = info.context
availability = get_product_availability(
self, context.discounts, context.taxes, context.currency)
- return ProductAvailability(**availability._asdict())
+ return ProductPricingInfo(**availability._asdict())
+
+ resolve_availability = resolve_pricing
@gql_optimizer.resolver_hints(
prefetch_related='product_type__product_attributes__values')
diff --git a/tests/api/test_graphql.py b/tests/api/test_graphql.py
index 0cf99f352d..2fc721f0ea 100644
--- a/tests/api/test_graphql.py
+++ b/tests/api/test_graphql.py
@@ -116,7 +116,7 @@ def test_real_query(user_api_client, product):
localized
__typename
}
- availability {
+ pricing {
...ProductPriceFragmentQuery
__typename
}
@@ -126,7 +126,7 @@ def test_real_query(user_api_client, product):
__typename
}
- fragment ProductPriceFragmentQuery on ProductAvailability {
+ fragment ProductPriceFragmentQuery on ProductPricingInfo {
available
discount {
gross {
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 0081aac416..7b64efa196 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -103,7 +103,7 @@ def test_product_query(staff_api_client, product, permission_manage_products):
name
stockQuantity
}
- availability {
+ pricing {
available,
priceRange {
start {
@@ -148,7 +148,7 @@ def test_product_query(staff_api_client, product, permission_manage_products):
product_data = product_edges_data[0]['node']
assert product_data['name'] == product.name
assert product_data['url'] == product.get_absolute_url()
- gross = product_data['availability']['priceRange']['start']['gross']
+ gross = product_data['pricing']['priceRange']['start']['gross']
assert float(gross['amount']) == float(product.price.amount)
from saleor.product.utils.costs import get_product_costs_data
purchase_cost, margin = get_product_costs_data(product)
diff --git a/tests/api/test_variant_availability.py b/tests/api/test_variant_availability.py
deleted file mode 100644
index 50622571a4..0000000000
--- a/tests/api/test_variant_availability.py
+++ /dev/null
@@ -1,122 +0,0 @@
-from unittest.mock import Mock
-
-from saleor.product.models import ProductVariant
-from saleor.product.utils.availability import get_variant_availability
-from tests.api.utils import get_graphql_content
-
-QUERY_GET_VARIANT_AVAILABILITY = """
-query {
- products(first: 1) {
- edges {
- node {
- variants {
- availability {
- available
- onSale
-
- discount {
- currency
- net {
- amount
- }
- }
-
- priceUndiscounted {
- currency
- net {
- amount
- }
- }
-
- price {
- currency
- net {
- amount
- }
- }
- }
- }
- }
- }
- }
-}
-"""
-
-
-def test_get_variant_availability_on_sale(api_client, sale, product):
- response = api_client.post_graphql(QUERY_GET_VARIANT_AVAILABILITY, {})
- content = get_graphql_content(response)
-
- availability = (
- content['data']['products']['edges'][0]['node']
- ['variants'][0]['availability'])
-
- # ensure the availability was correctly retrieved and sent
- assert availability
-
- # check availability
- assert availability['available'] is True
- assert availability['onSale'] is True
-
- # check the discount
- assert availability['discount']['currency'] == 'USD'
- assert availability['discount']['net']['amount'] == 5.0
-
- # check the undiscounted price
- assert availability['priceUndiscounted']['currency'] == 'USD'
- assert availability['priceUndiscounted']['net']['amount'] == 10.0
-
- # check the discounted price
- assert availability['price']['currency'] == 'USD'
- assert availability['price']['net']['amount'] == 5.0
-
-
-def test_get_variant_availability_not_on_sale(api_client, product):
- response = api_client.post_graphql(QUERY_GET_VARIANT_AVAILABILITY, {})
- content = get_graphql_content(response)
-
- availability = (
- content['data']['products']['edges'][0]['node']
- ['variants'][0]['availability'])
-
- # ensure the availability was correctly retrieved and sent
- assert availability
-
- # check availability
- assert availability['available'] is True
- assert availability['onSale'] is False
-
- # check the discount
- assert availability['discount'] is None
-
- # check the undiscounted price
- assert availability['priceUndiscounted']['currency'] == 'USD'
- assert availability['priceUndiscounted']['net']['amount'] == 10.0
-
- # check the discounted price
- assert availability['price']['currency'] == 'USD'
- assert availability['price']['net']['amount'] == 10.0
-
-
-def test_variant_availability(
- variant: ProductVariant, monkeypatch, settings, taxes):
- availability = get_variant_availability(variant)
- assert availability.price == variant.get_price()
- assert availability.price_local_currency is None
-
- monkeypatch.setattr(
- 'django_prices_openexchangerates.models.get_rates',
- lambda c: {'PLN': Mock(rate=2)})
-
- settings.DEFAULT_COUNTRY = 'PL'
- settings.OPENEXCHANGERATES_API_KEY = 'fake-key'
-
- availability = get_variant_availability(variant, local_currency='PLN')
- assert availability.price_local_currency.currency == 'PLN'
- assert availability.available
-
- availability = get_variant_availability(variant, taxes=taxes)
- assert availability.price.tax.amount
- assert availability.price_undiscounted.tax.amount
- assert availability.price_undiscounted.tax.amount
- assert availability.available
diff --git a/tests/api/test_variant_pricing.py b/tests/api/test_variant_pricing.py
new file mode 100644
index 0000000000..31a0bdc66a
--- /dev/null
+++ b/tests/api/test_variant_pricing.py
@@ -0,0 +1,126 @@
+from unittest.mock import Mock
+
+from saleor.product.models import ProductVariant
+from saleor.product.utils.availability import get_variant_availability
+from tests.api.utils import get_graphql_content
+
+QUERY_GET_VARIANT_PRICING = """
+query {
+ products(first: 1) {
+ edges {
+ node {
+ variants {
+ pricing {
+ available
+ onSale
+
+ discount {
+ currency
+ net {
+ amount
+ }
+ }
+
+ priceUndiscounted {
+ currency
+ net {
+ amount
+ }
+ }
+
+ price {
+ currency
+ net {
+ amount
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
+"""
+
+
+def test_get_variant_pricing_on_sale(api_client, sale, product):
+ response = api_client.post_graphql(QUERY_GET_VARIANT_PRICING, {})
+ content = get_graphql_content(response)
+
+ pricing = (
+ content['data']['products']['edges'][0]['node']
+ ['variants'][0]['pricing'])
+
+ # ensure the availability was correctly retrieved and sent
+ assert pricing
+
+ # check availability
+ assert pricing['available'] is True
+ assert pricing['onSale'] is True
+
+ # check the discount
+ assert pricing['discount']['currency'] == product.price.currency
+ assert pricing['discount']['net']['amount'] == 5.0
+
+ # check the undiscounted price
+ assert (
+ pricing['priceUndiscounted']['currency'] == product.price.currency)
+ assert (
+ pricing['priceUndiscounted']['net']['amount'] == product.price.amount)
+
+ # check the discounted price
+ assert pricing['price']['currency'] == product.price.currency
+ assert pricing['price']['net']['amount'] == 5.0
+
+
+def test_get_variant_pricing_not_on_sale(api_client, product):
+ response = api_client.post_graphql(QUERY_GET_VARIANT_PRICING, {})
+ content = get_graphql_content(response)
+
+ pricing = (
+ content['data']['products']['edges'][0]['node']
+ ['variants'][0]['pricing'])
+
+ # ensure the availability was correctly retrieved and sent
+ assert pricing
+
+ # check availability
+ assert pricing['available'] is True
+ assert pricing['onSale'] is False
+
+ # check the discount
+ assert pricing['discount'] is None
+
+ # check the undiscounted price
+ assert (
+ pricing['priceUndiscounted']['currency'] == product.price.currency)
+ assert (
+ pricing['priceUndiscounted']['net']['amount'] == product.price.amount)
+
+ # check the discounted price
+ assert pricing['price']['currency'] == product.price.currency
+ assert pricing['price']['net']['amount'] == product.price.amount
+
+
+def test_variant_pricing(
+ variant: ProductVariant, monkeypatch, settings, taxes):
+ pricing = get_variant_availability(variant)
+ assert pricing.price == variant.get_price()
+ assert pricing.price_local_currency is None
+
+ monkeypatch.setattr(
+ 'django_prices_openexchangerates.models.get_rates',
+ lambda c: {'PLN': Mock(rate=2)})
+
+ settings.DEFAULT_COUNTRY = 'PL'
+ settings.OPENEXCHANGERATES_API_KEY = 'fake-key'
+
+ pricing = get_variant_availability(variant, local_currency='PLN')
+ assert pricing.price_local_currency.currency == 'PLN'
+ assert pricing.available
+
+ pricing = get_variant_availability(variant, taxes=taxes)
+ assert pricing.price.tax.amount
+ assert pricing.price_undiscounted.tax.amount
+ assert pricing.price_undiscounted.tax.amount
+ assert pricing.available