From 6a621527f272a3f8d99f7aefecf56d63b99c833d Mon Sep 17 00:00:00 2001 From: Piotr Michalak Date: Mon, 21 Oct 2024 09:31:30 +0200 Subject: [PATCH 1/5] create test --- .../ShopifyCreateItemAsVariantSub.Codeunit.al | 7 ++ .../ShpfyCreateItemVariantTest.Codeunit.al | 66 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 Apps/W1/Shopify/test/Products/ShopifyCreateItemAsVariantSub.Codeunit.al create mode 100644 Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al diff --git a/Apps/W1/Shopify/test/Products/ShopifyCreateItemAsVariantSub.Codeunit.al b/Apps/W1/Shopify/test/Products/ShopifyCreateItemAsVariantSub.Codeunit.al new file mode 100644 index 0000000000..73b6d956a1 --- /dev/null +++ b/Apps/W1/Shopify/test/Products/ShopifyCreateItemAsVariantSub.Codeunit.al @@ -0,0 +1,7 @@ +codeunit 139583 "Shopify CreateItemAsVariantSub" +{ + EventSubscriberInstance = Manual; + + + +} diff --git a/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al b/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al new file mode 100644 index 0000000000..d5aa83bc1c --- /dev/null +++ b/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al @@ -0,0 +1,66 @@ +codeunit 139581 "Shpfy Create Item Variant Test" +{ + Subtype = Test; + TestPermissions = Disabled; + + var + Shop: Record "Shpfy Shop"; + Any: Codeunit Any; + LibraryAssert: Codeunit "Library Assert"; + ShpfyInitializeTest: Codeunit "Shpfy Initialize Test"; + IsInitialized: Boolean; + + trigger OnRun() + begin + IsInitialized := false; + end; + + [Test] + procedure UnitTestCreateVariantFromItem() + var + Item: Record "Item"; + TempShopifyVariant: Record "Shpfy Variant" temporary; + ShpfyProductInitTest: Codeunit "Shpfy Product Init Test"; + + CreateProduct: Codeunit "Shpfy Create Product"; + CreateItemAsVariant: Codeunit "Shpfy Create Item As Variant"; + ParentProductId: BigInteger; + begin + // [SCENARIO] Create a variant from a given item + Initialize(); + + // [GIVEN] Item + Item := ShpfyProductInitTest.CreateItem(Shop."Item Templ. Code", Any.DecimalInRange(10, 100, 2), Any.DecimalInRange(100, 500, 2)); + // [GIVEN] Shopify product + ParentProductId := CreateShopifyProduct(Item.SystemId); + + // [WHEN] Invoke CreateItemAsVariant.CreateVariantFromItem + CreateItemAsVariant.SetParentProduct(ParentProductId); + CreateItemAsVariant.CreateVariantFromItem(Item); + + + + end; + + local procedure Initialize() + begin + if IsInitialized then + exit; + Any.SetDefaultSeed(); + Shop := ShpfyInitializeTest.CreateShop(); + Commit(); + IsInitialized := true; + end; + + local procedure CreateShopifyProduct(SystemId: Guid): BigInteger + var + ShopifyProduct: Record "Shpfy Product"; + begin + ShopifyProduct.Init(); + ShopifyProduct.Id := Any.IntegerInRange(10000, 99999); + ShopifyProduct."Shop Code" := Shop."Code"; + ShopifyProduct."Item SystemId" := SystemId; + ShopifyProduct.Insert(true); + exit(ShopifyProduct."Id"); + end; +} From 320e59914ee13977551fbfac08bfad4e7f24a4cd Mon Sep 17 00:00:00 2001 From: Piotr Michalak Date: Mon, 21 Oct 2024 12:34:39 +0200 Subject: [PATCH 2/5] Create Tests --- .../ShopifyCreateItemAsVariantSub.Codeunit.al | 7 -- .../ShpfyCreateItemAsVariantSub.Codeunit.al | 94 +++++++++++++++++++ .../ShpfyCreateItemVariantTest.Codeunit.al | 70 +++++++++++++- 3 files changed, 161 insertions(+), 10 deletions(-) delete mode 100644 Apps/W1/Shopify/test/Products/ShopifyCreateItemAsVariantSub.Codeunit.al create mode 100644 Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al diff --git a/Apps/W1/Shopify/test/Products/ShopifyCreateItemAsVariantSub.Codeunit.al b/Apps/W1/Shopify/test/Products/ShopifyCreateItemAsVariantSub.Codeunit.al deleted file mode 100644 index 73b6d956a1..0000000000 --- a/Apps/W1/Shopify/test/Products/ShopifyCreateItemAsVariantSub.Codeunit.al +++ /dev/null @@ -1,7 +0,0 @@ -codeunit 139583 "Shopify CreateItemAsVariantSub" -{ - EventSubscriberInstance = Manual; - - - -} diff --git a/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al b/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al new file mode 100644 index 0000000000..8012eda3c1 --- /dev/null +++ b/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al @@ -0,0 +1,94 @@ +codeunit 139583 "Shpfy CreateItemAsVariantSub" +{ + EventSubscriberInstance = Manual; + + var + GraphQueryTxt: Text; + NewVariantId: BigInteger; + + [EventSubscriber(ObjectType::Codeunit, Codeunit::"Shpfy Communication Events", 'OnClientSend', '', true, false)] + local procedure OnClientSend(HttpRequestMessage: HttpRequestMessage; var HttpResponseMessage: HttpResponseMessage) + begin + MakeResponse(HttpRequestMessage, HttpResponseMessage); + end; + + [EventSubscriber(ObjectType::Codeunit, Codeunit::"Shpfy Communication Events", 'OnGetContent', '', true, false)] + local procedure OnGetContent(HttpResponseMessage: HttpResponseMessage; var Response: Text) + begin + HttpResponseMessage.Content.ReadAs(Response); + end; + + local procedure MakeResponse(HttpRequestMessage: HttpRequestMessage; var HttpResponseMessage: HttpResponseMessage) + var + Uri: Text; + GraphQlQuery: Text; + CreateItemVariantTok: Label '{"query":"mutation { productVariantCreate(input: {productId: \"gid://shopify/Product/', locked = true; + GetOptionsStartTok: Label '{"query":"{product(id: \"gid://shopify/Product/', locked = true; + GetOptionsEndTok: Label '\") {id title options {id name}}}"}', Locked = true; + RemoveVariantStartTok: Label '{"query":"mutation {productVariantDelete(id: \"gid://shopify/ProductVariant/', Locked = true; + RemoveVariantEndTok: Label '\") {deletedProductVariantId userErrors{field message}}}"}', Locked = true; + GraphQLCmdTxt: Label '/graphql.json', Locked = true; + begin + case HttpRequestMessage.Method of + 'POST': + begin + Uri := HttpRequestMessage.GetRequestUri(); + if Uri.EndsWith(GraphQLCmdTxt) then + if HttpRequestMessage.Content.ReadAs(GraphQlQuery) then + case true of + GraphQlQuery.StartsWith(CreateItemVariantTok): + HttpResponseMessage := GetCreatedVariantResponse(); + GraphQlQuery.StartsWith(GetOptionsStartTok) and GraphQlQuery.EndsWith(GetOptionsEndTok): + HttpResponseMessage := GetProductOptionsResponse(); + GraphQlQuery.StartsWith(RemoveVariantStartTok) and GraphQlQuery.EndsWith(RemoveVariantEndTok): + begin + HttpResponseMessage := GetRemoveVariantResponse(); + GraphQueryTxt := GraphQlQuery; + end; + end; + end; + end; + end; + + local procedure GetCreatedVariantResponse(): HttpResponseMessage; + var + Any: Codeunit Any; + HttpResponseMessage: HttpResponseMessage; + BodyTxt: Text; + begin + NewVariantId := Any.IntegerInRange(100000, 999999); + BodyTxt := StrSubstNo('{ "data": { "productVariantCreate": { "legacyResourceId": %1 } } }', NewVariantId); + HttpResponseMessage.Content.WriteFrom(BodyTxt); + exit(HttpResponseMessage); + end; + + local procedure GetProductOptionsResponse(): HttpResponseMessage + var + HttpResponseMessage: HttpResponseMessage; + BodyTxt: Text; + begin + BodyTxt := '{"data": {"product": {"id": "gid://shopify/Product/123456", "title": "Product 1", "options": [{"id": "gid://shopify/ProductOption/1", "name": "Option 1"}]}}}'; + HttpResponseMessage.Content.WriteFrom(BodyTxt); + exit(HttpResponseMessage); + end; + + local procedure GetRemoveVariantResponse(): HttpResponseMessage + var + HttpResponseMessage: HttpResponseMessage; + BodyTxt: Text; + begin + BodyTxt := '{}'; + HttpResponseMessage.Content.WriteFrom(BodyTxt); + exit(HttpResponseMessage); + end; + + procedure GetNewVariantId(): BigInteger + begin + exit(NewVariantId); + end; + + procedure GetGraphQueryTxt(): Text + begin + exit(GraphQueryTxt); + end; +} \ No newline at end of file diff --git a/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al b/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al index d5aa83bc1c..d185371762 100644 --- a/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al +++ b/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al @@ -19,12 +19,13 @@ codeunit 139581 "Shpfy Create Item Variant Test" procedure UnitTestCreateVariantFromItem() var Item: Record "Item"; - TempShopifyVariant: Record "Shpfy Variant" temporary; + ShpfyVariant: Record "Shpfy Variant"; + ShpfyProduct: Record "Shpfy Product"; ShpfyProductInitTest: Codeunit "Shpfy Product Init Test"; - - CreateProduct: Codeunit "Shpfy Create Product"; CreateItemAsVariant: Codeunit "Shpfy Create Item As Variant"; + CreateItemAsVariantSub: Codeunit "Shpfy CreateItemAsVariantSub"; ParentProductId: BigInteger; + VariantId: BigInteger; begin // [SCENARIO] Create a variant from a given item Initialize(); @@ -35,13 +36,76 @@ codeunit 139581 "Shpfy Create Item Variant Test" ParentProductId := CreateShopifyProduct(Item.SystemId); // [WHEN] Invoke CreateItemAsVariant.CreateVariantFromItem + BindSubscription(CreateItemAsVariantSub); CreateItemAsVariant.SetParentProduct(ParentProductId); CreateItemAsVariant.CreateVariantFromItem(Item); + VariantId := CreateItemAsVariantSub.GetNewVariantId(); + UnbindSubscription(CreateItemAsVariantSub); + + // [THEN] Variant is created + LibraryAssert.IsTrue(ShpfyVariant.Get(VariantId), 'Variant not created'); + LibraryAssert.AreEqual(Item."No.", ShpfyVariant.Title, 'Title not set'); + LibraryAssert.AreEqual(Item."No.", ShpfyVariant."Option 1 Value", 'Option 1 Value not set'); + LibraryAssert.AreEqual('Variant', ShpfyVariant."Option 1 Name", 'Option 1 Name not set'); + LibraryAssert.AreEqual(ParentProductId, ShpfyVariant."Product Id", 'Parent product not set'); + LibraryAssert.IsTrue(ShpfyProduct.Get(ParentProductId), 'Parent product not found'); + LibraryAssert.IsTrue(ShpfyProduct."Has Variants", 'Has Variants not set'); + end; + + [Test] + procedure UnitTestGetProductOptions() + var + Item: Record "Item"; + ShpfyProductInitTest: Codeunit "Shpfy Product Init Test"; + ProductAPI: Codeunit "Shpfy Product API"; + CreateItemAsVariantSub: Codeunit "Shpfy CreateItemAsVariantSub"; + ProductId: BigInteger; + Options: Dictionary of [Text, Text]; + begin + // [SCENARIO] Get product options for a given shopify product + Initialize(); + + // [GIVEN] Item + Item := ShpfyProductInitTest.CreateItem(Shop."Item Templ. Code", Any.DecimalInRange(10, 100, 2), Any.DecimalInRange(100, 500, 2)); + // [GIVEN] Shopify product + ProductId := Any.IntegerInRange(10000, 99999); + + // [WHEN] Invoke ProductAPI.GetProductOptions + BindSubscription(CreateItemAsVariantSub); + Options := ProductAPI.GetProductOptions(ProductId); + UnbindSubscription(CreateItemAsVariantSub); + + // [THEN] Options are returned + LibraryAssert.AreEqual(1, Options.Count(), 'Options not returned'); + end; + + [Test] + procedure UnitTestDeleteProduvtVariant() + var + CreateItemAsVariantSub: Codeunit "Shpfy CreateItemAsVariantSub"; + VariantAPI: Codeunit "Shpfy Variant API"; + VariantId: BigInteger; + ActualQueryTxt: Text; + ExpexctedQueryTok: Label '{"query":"mutation {productVariantDelete(id: \"gid://shopify/ProductVariant/%1\") {deletedProductVariantId userErrors{field message}}}"}', Locked = true; + begin + // [SCENARIO] Delete a product variant + Initialize(); + // [GIVEN] Shopify Variant Id + VariantId := Any.IntegerInRange(10000, 99999); + // [WHEN] Invoke ProductAPI.DeleteProductVariant + BindSubscription(CreateItemAsVariantSub); + VariantAPI.DeleteProductVariant(VariantId); + ActualQueryTxt := CreateItemAsVariantSub.GetGraphQueryTxt(); + UnbindSubscription(CreateItemAsVariantSub); + // [THEN] Query is correct + LibraryAssert.AreEqual(StrSubstNo(ExpexctedQueryTok, VariantId), ActualQueryTxt, 'Query not correct'); end; + + local procedure Initialize() begin if IsInitialized then From 89ec013d789ca00109faea761f9a46350a937656 Mon Sep 17 00:00:00 2001 From: Piotr Michalak Date: Thu, 24 Oct 2024 07:46:47 +0200 Subject: [PATCH 3/5] add multiple options test --- .../ShpfyCreateItemAsVariantSub.Codeunit.al | 21 ++++++++++++- .../ShpfyCreateItemVariantTest.Codeunit.al | 30 ++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al b/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al index 8012eda3c1..72adf0486a 100644 --- a/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al +++ b/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al @@ -5,6 +5,7 @@ codeunit 139583 "Shpfy CreateItemAsVariantSub" var GraphQueryTxt: Text; NewVariantId: BigInteger; + MultipleOptions: Boolean; [EventSubscriber(ObjectType::Codeunit, Codeunit::"Shpfy Communication Events", 'OnClientSend', '', true, false)] local procedure OnClientSend(HttpRequestMessage: HttpRequestMessage; var HttpResponseMessage: HttpResponseMessage) @@ -39,7 +40,10 @@ codeunit 139583 "Shpfy CreateItemAsVariantSub" GraphQlQuery.StartsWith(CreateItemVariantTok): HttpResponseMessage := GetCreatedVariantResponse(); GraphQlQuery.StartsWith(GetOptionsStartTok) and GraphQlQuery.EndsWith(GetOptionsEndTok): - HttpResponseMessage := GetProductOptionsResponse(); + if MultipleOptions then + HttpResponseMessage := GetProductMultipleOptionsResponse() + else + HttpResponseMessage := GetProductOptionsResponse(); GraphQlQuery.StartsWith(RemoveVariantStartTok) and GraphQlQuery.EndsWith(RemoveVariantEndTok): begin HttpResponseMessage := GetRemoveVariantResponse(); @@ -82,6 +86,16 @@ codeunit 139583 "Shpfy CreateItemAsVariantSub" exit(HttpResponseMessage); end; + local procedure GetProductMultipleOptionsResponse(): HttpResponseMessage + var + HttpResponseMessage: HttpResponseMessage; + BodyTxt: Text; + begin + BodyTxt := '{"data": {"product": {"id": "gid://shopify/Product/123456", "title": "Product 1", "options": [{"id": "gid://shopify/ProductOption/1", "name": "Option 1"}, {"id": "gid://shopify/ProductOption/2", "name": "Option 2"}]}}}'; + HttpResponseMessage.Content.WriteFrom(BodyTxt); + exit(HttpResponseMessage); + end; + procedure GetNewVariantId(): BigInteger begin exit(NewVariantId); @@ -91,4 +105,9 @@ codeunit 139583 "Shpfy CreateItemAsVariantSub" begin exit(GraphQueryTxt); end; + + procedure SetMultipleOptions(NewMultipleOptions: Boolean) + begin + this.MultipleOptions := NewMultipleOptions; + end; } \ No newline at end of file diff --git a/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al b/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al index d185371762..48674353cf 100644 --- a/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al +++ b/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al @@ -80,7 +80,7 @@ codeunit 139581 "Shpfy Create Item Variant Test" end; [Test] - procedure UnitTestDeleteProduvtVariant() + procedure UnitTestDeleteProductVariant() var CreateItemAsVariantSub: Codeunit "Shpfy CreateItemAsVariantSub"; VariantAPI: Codeunit "Shpfy Variant API"; @@ -104,7 +104,35 @@ codeunit 139581 "Shpfy Create Item Variant Test" LibraryAssert.AreEqual(StrSubstNo(ExpexctedQueryTok, VariantId), ActualQueryTxt, 'Query not correct'); end; + [Test] + procedure UnitTestCreateVariantFromProductWithMultipleOptions() + var + Item: Record "Item"; + ShpfyProductInitTest: Codeunit "Shpfy Product Init Test"; + CreateItemAsVariant: Codeunit "Shpfy Create Item As Variant"; + CreateItemAsVariantSub: Codeunit "Shpfy CreateItemAsVariantSub"; + ProductId: BigInteger; + begin + // [SCENARIO] Create a variant from a product with multiple options + Initialize(); + // [GIVEN] Item + Item := ShpfyProductInitTest.CreateItem(Shop."Item Templ. Code", Any.DecimalInRange(10, 100, 2), Any.DecimalInRange(100, 500, 2)); + // [GIVEN] Shopify product + ProductId := CreateShopifyProduct(Item.SystemId); + + // [GIVEN] Multiple options for the product in Shopify + CreateItemAsVariantSub.SetMultipleOptions(true); + + // [WHEN] Invoke ProductAPI.CheckProductAndShopSettings + BindSubscription(CreateItemAsVariantSub); + CreateItemAsVariant.SetParentProduct(ProductId); + asserterror CreateItemAsVariant.CheckProductAndShopSettings(); + UnbindSubscription(CreateItemAsVariantSub); + + // [THEN] Error is thrown + LibraryAssert.ExpectedError('The product has more than one option. Items cannot be added as variants to a product with multiple options.'); + end; local procedure Initialize() begin From adf1cc6585ee322a6154962adc8ec6942f8d83bb Mon Sep 17 00:00:00 2001 From: Piotr Michalak Date: Thu, 24 Oct 2024 13:43:52 +0200 Subject: [PATCH 4/5] Add test for deliting default variant --- .../ShpfyCreateItemAsVariantSub.Codeunit.al | 21 ++++++++ .../ShpfyCreateItemVariantTest.Codeunit.al | 54 +++++++++++++++++-- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al b/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al index 72adf0486a..c5e8260256 100644 --- a/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al +++ b/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al @@ -5,6 +5,7 @@ codeunit 139583 "Shpfy CreateItemAsVariantSub" var GraphQueryTxt: Text; NewVariantId: BigInteger; + DefaultVariantId: BigInteger; MultipleOptions: Boolean; [EventSubscriber(ObjectType::Codeunit, Codeunit::"Shpfy Communication Events", 'OnClientSend', '', true, false)] @@ -28,6 +29,8 @@ codeunit 139583 "Shpfy CreateItemAsVariantSub" GetOptionsEndTok: Label '\") {id title options {id name}}}"}', Locked = true; RemoveVariantStartTok: Label '{"query":"mutation {productVariantDelete(id: \"gid://shopify/ProductVariant/', Locked = true; RemoveVariantEndTok: Label '\") {deletedProductVariantId userErrors{field message}}}"}', Locked = true; + GetVariantsTok: Label 'variants(first:200){pageInfo{hasNextPage} edges{cursor node{legacyResourceId updatedAt}}}', Locked = true; + GraphQLCmdTxt: Label '/graphql.json', Locked = true; begin case HttpRequestMessage.Method of @@ -49,6 +52,8 @@ codeunit 139583 "Shpfy CreateItemAsVariantSub" HttpResponseMessage := GetRemoveVariantResponse(); GraphQueryTxt := GraphQlQuery; end; + GraphQlQuery.Contains(GetVariantsTok): + HttpResponseMessage := GetDefaultVariantResponse(); end; end; end; @@ -60,6 +65,7 @@ codeunit 139583 "Shpfy CreateItemAsVariantSub" HttpResponseMessage: HttpResponseMessage; BodyTxt: Text; begin + Any.SetDefaultSeed(); NewVariantId := Any.IntegerInRange(100000, 999999); BodyTxt := StrSubstNo('{ "data": { "productVariantCreate": { "legacyResourceId": %1 } } }', NewVariantId); HttpResponseMessage.Content.WriteFrom(BodyTxt); @@ -96,6 +102,16 @@ codeunit 139583 "Shpfy CreateItemAsVariantSub" exit(HttpResponseMessage); end; + local procedure GetDefaultVariantResponse(): HttpResponseMessage + var + HttpResponseMessage: HttpResponseMessage; + BodyTxt: Text; + begin + BodyTxt := StrSubstNo('{ "data" : { "product" : { "variants" : { "edges" : [ { "node" : { "legacyResourceId" : %1 } } ] } } } }', DefaultVariantId); + HttpResponseMessage.Content.WriteFrom(BodyTxt); + exit(HttpResponseMessage); + end; + procedure GetNewVariantId(): BigInteger begin exit(NewVariantId); @@ -110,4 +126,9 @@ codeunit 139583 "Shpfy CreateItemAsVariantSub" begin this.MultipleOptions := NewMultipleOptions; end; + + procedure SetDefaultVariantId(NewDefaultVariantId: BigInteger) + begin + this.DefaultVariantId := NewDefaultVariantId; + end; } \ No newline at end of file diff --git a/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al b/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al index 48674353cf..a607459863 100644 --- a/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al +++ b/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al @@ -86,7 +86,6 @@ codeunit 139581 "Shpfy Create Item Variant Test" VariantAPI: Codeunit "Shpfy Variant API"; VariantId: BigInteger; ActualQueryTxt: Text; - ExpexctedQueryTok: Label '{"query":"mutation {productVariantDelete(id: \"gid://shopify/ProductVariant/%1\") {deletedProductVariantId userErrors{field message}}}"}', Locked = true; begin // [SCENARIO] Delete a product variant Initialize(); @@ -101,7 +100,8 @@ codeunit 139581 "Shpfy Create Item Variant Test" UnbindSubscription(CreateItemAsVariantSub); // [THEN] Query is correct - LibraryAssert.AreEqual(StrSubstNo(ExpexctedQueryTok, VariantId), ActualQueryTxt, 'Query not correct'); + LibraryAssert.IsTrue(ActualQueryTxt.Contains('{"query":"mutation {productVariantDelete('), 'Query not correct'); + LibraryAssert.IsTrue(ActualQueryTxt.Contains(StrSubstNo('id: \"gid://shopify/ProductVariant/%1\"', VariantId)), 'Variant Id not set'); end; [Test] @@ -134,11 +134,47 @@ codeunit 139581 "Shpfy Create Item Variant Test" LibraryAssert.ExpectedError('The product has more than one option. Items cannot be added as variants to a product with multiple options.'); end; + [Test] + procedure UnitTestRemoveDefaultVariantTest() + var + Item: Record Item; + ShpfyVariant: Record "Shpfy Variant"; + ShpfyProductInitTest: Codeunit "Shpfy Product Init Test"; + CreateItemAsVariant: Codeunit "Shpfy Create Item As Variant"; + CreateItemAsVariantSub: Codeunit "Shpfy CreateItemAsVariantSub"; + ProductId, VariantId : BigInteger; + begin + // [SCENARIO] Remove default variant + Initialize(); + + // [GIVEN] Item + Item := ShpfyProductInitTest.CreateItem(Shop."Item Templ. Code", Any.DecimalInRange(10, 100, 2), Any.DecimalInRange(100, 500, 2)); + // [GIVEN] Shopify product + ProductId := CreateShopifyProduct(Item.SystemId); + // [GIVEN] Shopify variant + VariantId := CreateShopifyVariant(ProductId); + // [GIVEN] Default variant exists in Shopify + CreateItemAsVariantSub.SetDefaultVariantId(VariantId); + + // [WHEN] Invoke CreateItemAsVariant.RemoveDefaultVariant + BindSubscription(CreateItemAsVariantSub); + CreateItemAsVariant.SetParentProduct(ProductId); + CreateItemAsVariant.FindDefaultVariantId(); + CreateItemAsVariant.CreateVariantFromItem(Item); + CreateItemAsVariant.RemoveDefaultVariant(); + UnbindSubscription(CreateItemAsVariantSub); + + // [THEN] Default variant is removed + ShpfyVariant.SetRange(Id, VariantId); + LibraryAssert.IsTrue(ShpfyVariant.IsEmpty(), 'Default variant not removed'); + + end; + local procedure Initialize() begin + Any.SetDefaultSeed(); if IsInitialized then exit; - Any.SetDefaultSeed(); Shop := ShpfyInitializeTest.CreateShop(); Commit(); IsInitialized := true; @@ -155,4 +191,16 @@ codeunit 139581 "Shpfy Create Item Variant Test" ShopifyProduct.Insert(true); exit(ShopifyProduct."Id"); end; + + local procedure CreateShopifyVariant(ProductId: BigInteger): BigInteger + var + ShpfyVariant: Record "Shpfy Variant"; + begin + ShpfyVariant.Init(); + ShpfyVariant.Id := Any.IntegerInRange(10000, 99999); + ShpfyVariant."Shop Code" := Shop."Code"; + ShpfyVariant."Product Id" := ProductId; + ShpfyVariant.Insert(false); + exit(ShpfyVariant."Id"); + end; } From 6af26726d2658f8a909aea539ccd0d572de5e02d Mon Sep 17 00:00:00 2001 From: Piotr Michalak Date: Fri, 25 Oct 2024 10:04:09 +0200 Subject: [PATCH 5/5] update test codeunit ids --- .../test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al | 2 +- .../test/Products/ShpfyCreateItemVariantTest.Codeunit.al | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al b/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al index c5e8260256..9c65bbc8c5 100644 --- a/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al +++ b/Apps/W1/Shopify/test/Products/ShpfyCreateItemAsVariantSub.Codeunit.al @@ -1,4 +1,4 @@ -codeunit 139583 "Shpfy CreateItemAsVariantSub" +codeunit 139620 "Shpfy CreateItemAsVariantSub" { EventSubscriberInstance = Manual; diff --git a/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al b/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al index a607459863..77761494a0 100644 --- a/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al +++ b/Apps/W1/Shopify/test/Products/ShpfyCreateItemVariantTest.Codeunit.al @@ -1,4 +1,4 @@ -codeunit 139581 "Shpfy Create Item Variant Test" +codeunit 139619 "Shpfy Create Item Variant Test" { Subtype = Test; TestPermissions = Disabled;