Skip to content

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

Fix countries in Voucher type

Michał Jeloneked9b7aae8d5e
diff --git a/saleor/graphql/discount/types.py b/saleor/graphql/discount/types.py
index fb7ec47adf..c05775f4da 100644
--- a/saleor/graphql/discount/types.py
+++ b/saleor/graphql/discount/types.py
@@ -1,12 +1,14 @@
from textwrap import dedent
+import graphene
import graphene_django_optimizer as gql_optimizer
from graphene import relay
-from ...discount import models
from ..core.connection import CountableDjangoObjectType
from ..core.fields import PrefetchingConnectionField
+from ..core.types import CountryDisplay
from ..product.types import Category, Collection, Product
+from ...discount import models
class Sale(CountableDjangoObjectType):
@@ -59,6 +61,9 @@ class Voucher(CountableDjangoObjectType):
Product,
description='List of products this voucher applies to.'),
model_field='products')
+ countries = graphene.List(
+ CountryDisplay,
+ description='List of countries available for the shipping voucher.')
class Meta:
description = dedent("""
@@ -76,3 +81,8 @@ class Voucher(CountableDjangoObjectType):
def resolve_products(self, info, **kwargs):
return self.products.visible_to_user(info.context.user)
+
+ def resolve_countries(self, info, **kwargs):
+ return [
+ CountryDisplay(code=country.code, country=country.name)
+ for country in self.countries]
diff --git a/tests/api/test_discount.py b/tests/api/test_discount.py
index 04e6aea40e..fbb2b20740 100644
--- a/tests/api/test_discount.py
+++ b/tests/api/test_discount.py
@@ -1,5 +1,6 @@
import graphene
import pytest
+from django_countries import countries
from saleor.discount import DiscountValueType, VoucherType
from saleor.graphql.discount.enums import (
@@ -7,7 +8,15 @@ from saleor.graphql.discount.enums import (
from tests.api.utils import get_graphql_content
-def test_voucher_query(staff_api_client, voucher, permission_manage_discounts):
+def voucher_countries(voucher):
+ voucher.countries = countries
+ voucher.save(update_fields=['countries'])
+ return voucher
+
+
+def test_voucher_query(
+ staff_api_client, voucher_countries, permission_manage_discounts):
query = """
query vouchers {
vouchers(first: 1) {
@@ -21,6 +30,10 @@ def test_voucher_query(staff_api_client, voucher, permission_manage_discounts):
startDate
discountValueType
discountValue
+ countries {
+ code
+ country
+ }
}
}
}
@@ -30,14 +43,19 @@ def test_voucher_query(staff_api_client, voucher, permission_manage_discounts):
query, permissions=[permission_manage_discounts])
content = get_graphql_content(response)
data = content['data']['vouchers']['edges'][0]['node']
- assert data['type'] == voucher.type.upper()
- assert data['name'] == voucher.name
- assert data['code'] == voucher.code
- assert data['usageLimit'] == voucher.usage_limit
- assert data['used'] == voucher.used
- assert data['startDate'] == voucher.start_date.isoformat()
- assert data['discountValueType'] == voucher.discount_value_type.upper()
- assert data['discountValue'] == voucher.discount_value
+
+ assert data['type'] == voucher_countries.type.upper()
+ assert data['name'] == voucher_countries.name
+ assert data['code'] == voucher_countries.code
+ assert data['usageLimit'] == voucher_countries.usage_limit
+ assert data['used'] == voucher_countries.used
+ assert data['startDate'] == voucher_countries.start_date.isoformat()
+ assert data[
+ 'discountValueType'] == voucher_countries.discount_value_type.upper()
+ assert data['discountValue'] == voucher_countries.discount_value
+ assert data['countries'] == [{
+ 'country': country.name,
+ 'code': country.code} for country in voucher_countries.countries]
def test_sale_query(staff_api_client, sale, permission_manage_discounts):