Skip to content

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

Add tests for shipping prices

akjanikfd3314198697
diff --git a/saleor/graphql/shipping/types.py b/saleor/graphql/shipping/types.py
index 602e43d6ab..aaf973f549 100644
--- a/saleor/graphql/shipping/types.py
+++ b/saleor/graphql/shipping/types.py
@@ -34,4 +34,5 @@ class ShippingMethodCountry(DjangoObjectType):
class Meta:
description = 'Country to which shipping applies.'
model = models.ShippingMethodCountry
+ interfaces = [relay.Node]
exclude_fields = ['shipping_method', 'orders']
diff --git a/tests/api/test_shipping_method.py b/tests/api/test_shipping_method.py
index 221fd31352..77c6049297 100644
--- a/tests/api/test_shipping_method.py
+++ b/tests/api/test_shipping_method.py
@@ -14,7 +14,11 @@ def test_shipping_method_query(user_api_client, shipping_method):
name
description
pricePerCountry {
- countryCode
+ edges {
+ node {
+ countryCode
+ }
+ }
}
priceRange {
start {
@@ -115,7 +119,7 @@ def test_delete_shipping_method(admin_api_client, shipping_method):
}
}
}
- """
+ """
shipping_method_id = graphene.Node.to_global_id(
'ShippingMethod', shipping_method.pk)
variables = json.dumps({'id': shipping_method_id})
@@ -127,3 +131,95 @@ def test_delete_shipping_method(admin_api_client, shipping_method):
assert data['name'] == shipping_method.name
with pytest.raises(shipping_method._meta.model.DoesNotExist):
shipping_method.refresh_from_db()
+
+
+def test_create_shipping_price(admin_api_client, shipping_method):
+ query = """
+ mutation createShippingPrice(
+ $code: String!, $price: Decimal, $shippingMethod: ID!){
+ shippingPriceCreate(
+ input: {
+ countryCode: $code, price: $price,
+ shippingMethod: $shippingMethod}) {
+ shippingMethodCountry {
+ countryCode
+ price {
+ amount
+ }
+ }
+ }
+ }
+ """
+ code = 'PL'
+ price = '12.34'
+ shipping_method_id = graphene.Node.to_global_id(
+ 'ShippingMethod', shipping_method.pk)
+ variables = json.dumps(
+ {
+ 'shippingMethod': shipping_method_id,
+ 'code': code,
+ 'price': price})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ data = content['data']['shippingPriceCreate']['shippingMethodCountry']
+ assert data['countryCode'] == code
+ assert data['price']['amount'] == float(price)
+
+
+def test_update_shipping_price(admin_api_client, shipping_method, shipping_price):
+ query = """
+ mutation updateShippingPrice(
+ $id: ID!, $price: Decimal, $shippingMethod: ID!) {
+ shippingPriceUpdate(
+ id: $id, input: {price: $price, shippingMethod: $shippingMethod}) {
+ shippingMethodCountry {
+ price {
+ amount
+ }
+ }
+ }
+ }
+ """
+ # shipping_price = shipping_method.price_per_country.first()
+ price = '12.34'
+ assert not str(shipping_price.price) == price
+ shipping_method_id = graphene.Node.to_global_id(
+ 'ShippingMethod', shipping_method.pk)
+ shipping_price_id = graphene.Node.to_global_id(
+ 'ShippingMethodCountry', shipping_price.pk)
+ variables = json.dumps(
+ {
+ 'shippingMethod': shipping_method_id,
+ 'price': price,
+ 'id': shipping_price_id})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ data = content['data']['shippingPriceUpdate']['shippingMethodCountry']
+ assert data['price']['amount'] == float(price)
+
+
+def test_delete_shipping_price(admin_api_client, shipping_price):
+ query = """
+ mutation deleteShippingPrice($id: ID!) {
+ shippingPriceDelete(id: $id) {
+ shippingMethodCountry {
+ countryCode
+ }
+ }
+ }
+ """
+ shipping_price_id = graphene.Node.to_global_id(
+ 'ShippingMethodCountry', shipping_price.pk)
+ variables = json.dumps({'id': shipping_price_id})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ data = content['data']['shippingPriceDelete']['shippingMethodCountry']
+ assert data['countryCode'] == shipping_price.country_code
+ with pytest.raises(shipping_price._meta.model.DoesNotExist):
+ shipping_price.refresh_from_db()
diff --git a/tests/conftest.py b/tests/conftest.py
index f278eaf1ce..d7dee5fc40 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -31,7 +31,7 @@ from saleor.page.models import Page
from saleor.product.models import (
AttributeChoiceValue, Category, Collection, Product, ProductAttribute,
ProductImage, ProductType, ProductVariant)
-from saleor.shipping.models import ShippingMethod
+from saleor.shipping.models import ShippingMethod, ShippingMethodCountry
from saleor.site.models import AuthorizationKey, SiteSettings
@@ -206,6 +206,14 @@ def shipping_method(db): # pylint: disable=W0613
return shipping_method
+def shipping_price(shipping_method):
+ return ShippingMethodCountry.objects.create(
+ country_code='PL',
+ price=10,
+ shipping_method=shipping_method)
+
+
@pytest.fixture
def color_attribute(db): # pylint: disable=W0613
attribute = ProductAttribute.objects.create(