Skip to content

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

Add tests for update and delete product mutations

Adam Janika66ef8241819
diff --git a/tests/conftest.py b/tests/conftest.py
index fe0d52a626..1d166fd8fb 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -167,6 +167,11 @@ def default_category(db): # pylint: disable=W0613
return Category.objects.create(name='Default', slug='default')
+def non_default_category(db): # pylint: disable=W0613
+ return Category.objects.create(name='Not default', slug='not-default')
+
+
@pytest.fixture
def default_stock_location(db):
return StockLocation.objects.create(name='Warehouse 1')
diff --git a/tests/dashboard/test_graphql.py b/tests/dashboard/test_graphql.py
index 1b9a7e7cdf..d1226eef28 100644
--- a/tests/dashboard/test_graphql.py
+++ b/tests/dashboard/test_graphql.py
@@ -273,39 +273,39 @@ def test_create_product(
$price: Float!,
$attributes: [ValuesInput]) {
productCreate(
- categoryId: $categoryId,
- productTypeId: $productTypeId,
- name: $name,
- description: $description,
- isPublished: $isPublished,
- isFeatured: $isFeatured,
- price: $price,
- attributes: $attributes) {
- product {
- category{
+ categoryId: $categoryId,
+ productTypeId: $productTypeId,
+ name: $name,
+ description: $description,
+ isPublished: $isPublished,
+ isFeatured: $isFeatured,
+ price: $price,
+ attributes: $attributes) {
+ product {
+ category{
+ name
+ }
+ description
+ isPublished
+ isFeatured
name
+ price{
+ amount
+ }
+ productType{
+ name
+ }
+ attributes{
+ name
+ value
+ }
+ }
+ errors {
+ message
+ field
+ }
}
- description
- isPublished
- isFeatured
- name
- price{
- amount
- }
- productType{
- name
- }
- attributes{
- name
- value
- }
- }
- errors {
- message
- field
}
- }
- }
"""
product_type_id = graphene.Node.to_global_id(
@@ -355,3 +355,108 @@ def test_create_product(
assert data['product']['category']['name'] == default_category.name
assert data['product']['attributes'][1]['value'] == None
assert data['product']['attributes'][0]['value'] == color_attr_slug
+
+
+def test_update_product(
+ client, default_category, non_default_category, product_in_stock):
+ query = """
+ mutation updateProduct(
+ $productId: ID!,
+ $categoryId: ID,
+ $name: String!,
+ $description: String!,
+ $isPublished: Boolean!,
+ $isFeatured: Boolean!,
+ $price: Float!,
+ $attributes: [ValuesInput]) {
+ productUpdate(
+ categoryId: $categoryId,
+ id: $productId,
+ name: $name,
+ description: $description,
+ isPublished: $isPublished,
+ isFeatured: $isFeatured,
+ price: $price,
+ attributes: $attributes) {
+ product {
+ category{
+ name
+ }
+ description
+ isPublished
+ isFeatured
+ name
+ price{
+ amount
+ }
+ productType{
+ name
+ }
+ attributes{
+ name
+ value
+ }
+ }
+ errors {
+ message
+ field
+ }
+ }
+ }
+ """
+ product_id = graphene.Node.to_global_id('Product', product_in_stock.pk)
+ category_id = graphene.Node.to_global_id(
+ 'Category', non_default_category.pk)
+ product_description = 'updated description'
+ product_name = 'updated name'
+ product_isPublished = True
+ product_isFeatured = False
+ product_price = 33
+
+ variables = json.dumps({
+ 'productId': product_id,
+ 'categoryId': category_id,
+ 'name': product_name,
+ 'description': product_description,
+ 'isPublished': product_isPublished,
+ 'isFeatured': product_isFeatured,
+ 'price': product_price})
+
+ response = client.post(
+ reverse('dashboard:api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ data = content['data']['productUpdate']
+ assert data['errors'] == []
+ assert data['product']['name'] == product_name
+ assert data['product']['description'] == product_description
+ assert data['product']['isFeatured'] == product_isFeatured
+ assert data['product']['isPublished'] == product_isPublished
+ assert not data['product']['category']['name'] == default_category.name
+
+
+def test_delete_product(client, product_in_stock):
+ query = """
+ mutation DeleteProduct($id: ID!) {
+ productDelete(id: $id) {
+ product {
+ name
+ id
+ }
+ errors {
+ field
+ message
+ }
+ }
+ }
+ """
+ variables = json.dumps({
+ 'id': graphene.Node.to_global_id('Product', product_in_stock.id)})
+ response = client.post(
+ reverse('dashboard:api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ data = content['data']['productDelete']
+ assert data['product']['name'] == product_in_stock.name
+ with pytest.raises(product_in_stock._meta.model.DoesNotExist):
+ product_in_stock.refresh_from_db()