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 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, 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)
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 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, 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)
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 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) as exc: get_nodes(global_ids, Product) assert exc.value.args == (msg, ) 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(GraphQLError) as exc: get_nodes(global_ids, Product) assert exc.value.args == ( f"Must receive Product id: {invalid_item_global_id}", ) # Raise an error if no nodes were found global_ids = [] msg = f"Could not resolve to a node with the global id list of '{global_ids}'." with pytest.raises(Exception) as exc: get_nodes(global_ids, Product) assert exc.value.args == (msg, ) # Raise an error if pass wrong ids global_ids = ["a", "bb"] msg = f"Could not resolve to a node with the global id list of '{global_ids}'." with pytest.raises(Exception) as exc: get_nodes(global_ids, Product) assert exc.value.args == (msg, )
def clean_input(cls, info, instance, input, errors): shipping_address = input.pop('shipping_address', None) billing_address = input.pop('billing_address', None) cleaned_input = super().clean_input(info, instance, input, errors) lines = input.pop('lines', None) if lines: variant_ids = [line.get('variant_id') for line in lines] variants = get_nodes(ids=variant_ids, graphene_type=ProductVariant) quantities = [line.get('quantity') for line in lines] line_errors = check_lines_quantity(variants, quantities) if line_errors: for err in line_errors: cls.add_error(errors, field=err[0], message=err[1]) else: cleaned_input['variants'] = variants cleaned_input['quantities'] = quantities cleaned_input['status'] = OrderStatus.DRAFT display_gross_prices = info.context.site.settings.display_gross_prices cleaned_input['display_gross_prices'] = display_gross_prices # Set up default addresses if possible user = cleaned_input.get('user') if user and not shipping_address: cleaned_input[ 'shipping_address'] = user.default_shipping_address if user and not billing_address: cleaned_input[ 'billing_address'] = user.default_billing_address if shipping_address: shipping_address = Address(**shipping_address) cls.clean_instance(shipping_address, errors) cleaned_input['shipping_address'] = shipping_address if billing_address: billing_address = Address(**billing_address) cls.clean_instance(billing_address, errors) cleaned_input['billing_address'] = billing_address return cleaned_input
def filter_warehouses(qs, _, value): if value: warehouses = get_nodes(value, "Warehouse", Warehouse) qs = qs.filter(warehouse__in=warehouses) return qs
def filter_deliverers(qs, _, value): if value: deliverers = get_nodes(value, "WmsDeliverer", models.WmsDeliverer) qs = qs.filter(deliverer__in=deliverers) return qs
def filter_created_by(qs, _, value): if value: created_by = get_nodes(value, "User", User) qs = qs.filter(created_by__in=created_by) return qs
def filter_recipients(qs, _, value): if value: recipients = get_nodes(value, "User", User) qs = qs.filter(recipient__in=recipients) return qs