Skip to content

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

Add case for missing database object

akjanik8d50f2a0fad1
diff --git a/saleor/graphql/utils.py b/saleor/graphql/utils.py
index d39336f371..532942e764 100644
--- a/saleor/graphql/utils.py
+++ b/saleor/graphql/utils.py
@@ -28,6 +28,11 @@ def get_nodes(ids, graphene_type):
raise Exception(
"Could not resolve to a nodes with the global id list of '%s'."
% ids)
+ nodes_pk_list = [str(node.pk) for node in nodes]
+ for pk in pks:
+ assert pk in nodes_pk_list, (
+ 'There is no node of type {} with pk {}'.format(_type, pk)
+ )
return nodes
diff --git a/tests/api/test_graphql.py b/tests/api/test_graphql.py
index 2c18a2dabb..00f3bbd32e 100644
--- a/tests/api/test_graphql.py
+++ b/tests/api/test_graphql.py
@@ -237,13 +237,33 @@ def test_model_form_update_mutation(model_form_class):
def test_get_nodes(product_list):
global_ids = [to_global_id('Product', product.pk) for product in product_list]
+ # Make sure function works even if duplicated ids are provided
+ global_ids.append(to_global_id('Product', product_list[0].pk))
# Return products corresponding to global ids
products = get_nodes(global_ids, Product)
assert list(products) == product_list
+ # Raise an error if requested id has no related database object
+ nonexistent_item = Mock(type='Product', pk=123)
+ nonexistent_item_global_id = to_global_id(
+ nonexistent_item.type, nonexistent_item.pk)
+ global_ids.append(nonexistent_item_global_id)
+ msg = 'There is no node of type {} with pk {}'.format(
+ nonexistent_item.type, nonexistent_item.pk)
+ with pytest.raises(AssertionError, message=msg):
+ get_nodes(global_ids, Product)
+ global_ids.pop()
+
# Raise an error if one of the node is of wrong type
invalid_item = Mock(type='test', pk=123)
invalid_item_global_id = to_global_id(invalid_item.type, invalid_item.pk)
global_ids.append(invalid_item_global_id)
- with pytest.raises(AssertionError):
+ with pytest.raises(AssertionError, message='Must receive an Product id.'):
+ get_nodes(global_ids, Product)
+
+ # Raise an error if no nodes were found
+ global_ids = []
+ msg = 'Could not resolve to a nodes with the global id list of {}.'.format(
+ global_ids)
+ with pytest.raises(Exception, message=msg):
get_nodes(global_ids, Product)