Skip to content

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

update unit tests

Damian W72dac7b4f43f
diff --git a/saleor/graphql/product/mutations/channels.py b/saleor/graphql/product/mutations/channels.py
index 67810ad8ec..d3ba381d03 100644
--- a/saleor/graphql/product/mutations/channels.py
+++ b/saleor/graphql/product/mutations/channels.py
@@ -198,9 +198,9 @@ class ProductChannelListingUpdate(BaseChannelListingMutation):
def add_variants(cls, channel, add_variants: List[Dict]):
if not add_variants:
return
- variants_id = cls.get_nodes_or_error(add_variants, "id", ProductVariant)
+ variants = cls.get_nodes_or_error(add_variants, "id", ProductVariant)
variant_channel_listings = []
- for variant in variants_id:
+ for variant in variants:
variant_channel_listings.append(
ProductVariantChannelListing(channel=channel, variant=variant)
)
@@ -228,17 +228,14 @@ class ProductChannelListingUpdate(BaseChannelListingMutation):
):
if not remove_variants:
return
- variants_id = cls.get_nodes_or_error(remove_variants, "id", ProductVariant)
- if (
- len(variants_id)
- == ProductVariantChannelListing.objects.filter(
- channel=channel, variant__product_id=product
- ).count()
- ):
- product_channel_listing.delete()
+ variants = cls.get_nodes_or_error(remove_variants, "id", ProductVariant)
ProductVariantChannelListing.objects.filter(
- channel=channel, variant_id__in=variants_id
+ channel=channel, variant__in=variants
).delete()
+ if not ProductVariantChannelListing.objects.filter(
+ channel=channel, variant__product_id=product
+ ).exists():
+ product_channel_listing.delete()
@classmethod
def remove_channels(cls, product: "ProductModel", remove_channels: List[Dict]):
diff --git a/saleor/graphql/product/tests/test_product_channel_listing_update.py b/saleor/graphql/product/tests/test_product_channel_listing_update.py
index cbd0b23b04..5993f8666c 100644
--- a/saleor/graphql/product/tests/test_product_channel_listing_update.py
+++ b/saleor/graphql/product/tests/test_product_channel_listing_update.py
@@ -1226,3 +1226,40 @@ def test_product_channel_listing_add_variant_with_existing_channel_listing(
assert len(errors) == 1
assert errors[0]["field"] == "addVariants"
assert errors[0]["code"] == ProductErrorCode.ALREADY_EXISTS.name
+
+
+def test_product_channel_listing_remove_last_variant_channel_listing(
+ staff_api_client, product, permission_manage_products, channel_USD
+):
+ # given
+ assert product.channel_listings.filter(channel=channel_USD).exists()
+ variant = product.variants.first()
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
+ variants = [variant_id]
+ variables = {
+ "id": product_id,
+ "input": {
+ "updateChannels": [
+ {
+ "channelId": channel_id,
+ "isPublished": True,
+ "removeVariants": variants,
+ }
+ ],
+ },
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ PRODUCT_CHANNEL_LISTING_UPDATE_MUTATION,
+ variables=variables,
+ permissions=(permission_manage_products,),
+ )
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["productChannelListingUpdate"]
+ assert not data["productChannelListingErrors"]
+ assert not product.channel_listings.filter(channel=channel_USD).exists()