def test_add_and_remove_and_clear(): product = get_default_product() supplier = get_default_supplier() request = get_request_with_basket() basket = request.basket with pytest.raises(ValidationError): basket_commands.handle_add(request, basket, product_id=product.pk, quantity=-3) # Ordering antimatter is not supported # These will get merged into one line... basket_commands.handle_add(request, basket, **{"product_id": product.pk, "quantity": 1, "supplier_id": supplier.pk}) basket_commands.handle_add(request, basket, **{"product_id": product.pk, "quantity": 2}) # ... so there will be 3 products but one line assert basket.product_count == 3 lines = basket.get_lines() assert len(lines) == 1 # ... and deleting that line will clear the basket... basket_commands.handle_del(request, basket, lines[0].line_id) assert basket.product_count == 0 # ... and adding another product will create a new line... basket_commands.handle_add(request, basket, product_id=product.pk, quantity=1) assert basket.product_count == 1 # ... that can be cleared. basket_commands.handle_clear(request, basket) assert basket.product_count == 0
def destroy(self, request, *args, **kwargs): result = handle_del(self.request, self.request.basket, kwargs['line_id']) if not result['ok']: raise ValidationError({ 'code': 'line_not_found', 'error': _('Couldn\'t delete the line') }) self.request.basket.save() new_basket = self.get_basket() return Response(APIBasketSerializer(new_basket, context={'request': self.request}).data)