|
| 1 | +codeunit 139619 "Shpfy Create Item Variant Test" |
| 2 | +{ |
| 3 | + Subtype = Test; |
| 4 | + TestPermissions = Disabled; |
| 5 | + |
| 6 | + var |
| 7 | + Shop: Record "Shpfy Shop"; |
| 8 | + Any: Codeunit Any; |
| 9 | + LibraryAssert: Codeunit "Library Assert"; |
| 10 | + ShpfyInitializeTest: Codeunit "Shpfy Initialize Test"; |
| 11 | + IsInitialized: Boolean; |
| 12 | + |
| 13 | + trigger OnRun() |
| 14 | + begin |
| 15 | + IsInitialized := false; |
| 16 | + end; |
| 17 | + |
| 18 | + [Test] |
| 19 | + procedure UnitTestCreateVariantFromItem() |
| 20 | + var |
| 21 | + Item: Record "Item"; |
| 22 | + ShpfyVariant: Record "Shpfy Variant"; |
| 23 | + ShpfyProduct: Record "Shpfy Product"; |
| 24 | + ShpfyProductInitTest: Codeunit "Shpfy Product Init Test"; |
| 25 | + CreateItemAsVariant: Codeunit "Shpfy Create Item As Variant"; |
| 26 | + CreateItemAsVariantSub: Codeunit "Shpfy CreateItemAsVariantSub"; |
| 27 | + ParentProductId: BigInteger; |
| 28 | + VariantId: BigInteger; |
| 29 | + begin |
| 30 | + // [SCENARIO] Create a variant from a given item |
| 31 | + Initialize(); |
| 32 | + |
| 33 | + // [GIVEN] Item |
| 34 | + Item := ShpfyProductInitTest.CreateItem(Shop."Item Templ. Code", Any.DecimalInRange(10, 100, 2), Any.DecimalInRange(100, 500, 2)); |
| 35 | + // [GIVEN] Shopify product |
| 36 | + ParentProductId := CreateShopifyProduct(Item.SystemId); |
| 37 | + |
| 38 | + // [WHEN] Invoke CreateItemAsVariant.CreateVariantFromItem |
| 39 | + BindSubscription(CreateItemAsVariantSub); |
| 40 | + CreateItemAsVariant.SetParentProduct(ParentProductId); |
| 41 | + CreateItemAsVariant.CreateVariantFromItem(Item); |
| 42 | + VariantId := CreateItemAsVariantSub.GetNewVariantId(); |
| 43 | + UnbindSubscription(CreateItemAsVariantSub); |
| 44 | + |
| 45 | + // [THEN] Variant is created |
| 46 | + LibraryAssert.IsTrue(ShpfyVariant.Get(VariantId), 'Variant not created'); |
| 47 | + LibraryAssert.AreEqual(Item."No.", ShpfyVariant.Title, 'Title not set'); |
| 48 | + LibraryAssert.AreEqual(Item."No.", ShpfyVariant."Option 1 Value", 'Option 1 Value not set'); |
| 49 | + LibraryAssert.AreEqual('Variant', ShpfyVariant."Option 1 Name", 'Option 1 Name not set'); |
| 50 | + LibraryAssert.AreEqual(ParentProductId, ShpfyVariant."Product Id", 'Parent product not set'); |
| 51 | + LibraryAssert.IsTrue(ShpfyProduct.Get(ParentProductId), 'Parent product not found'); |
| 52 | + LibraryAssert.IsTrue(ShpfyProduct."Has Variants", 'Has Variants not set'); |
| 53 | + end; |
| 54 | + |
| 55 | + [Test] |
| 56 | + procedure UnitTestGetProductOptions() |
| 57 | + var |
| 58 | + Item: Record "Item"; |
| 59 | + ShpfyProductInitTest: Codeunit "Shpfy Product Init Test"; |
| 60 | + ProductAPI: Codeunit "Shpfy Product API"; |
| 61 | + CreateItemAsVariantSub: Codeunit "Shpfy CreateItemAsVariantSub"; |
| 62 | + ProductId: BigInteger; |
| 63 | + Options: Dictionary of [Text, Text]; |
| 64 | + begin |
| 65 | + // [SCENARIO] Get product options for a given shopify product |
| 66 | + Initialize(); |
| 67 | + |
| 68 | + // [GIVEN] Item |
| 69 | + Item := ShpfyProductInitTest.CreateItem(Shop."Item Templ. Code", Any.DecimalInRange(10, 100, 2), Any.DecimalInRange(100, 500, 2)); |
| 70 | + // [GIVEN] Shopify product |
| 71 | + ProductId := Any.IntegerInRange(10000, 99999); |
| 72 | + |
| 73 | + // [WHEN] Invoke ProductAPI.GetProductOptions |
| 74 | + BindSubscription(CreateItemAsVariantSub); |
| 75 | + Options := ProductAPI.GetProductOptions(ProductId); |
| 76 | + UnbindSubscription(CreateItemAsVariantSub); |
| 77 | + |
| 78 | + // [THEN] Options are returned |
| 79 | + LibraryAssert.AreEqual(1, Options.Count(), 'Options not returned'); |
| 80 | + end; |
| 81 | + |
| 82 | + [Test] |
| 83 | + procedure UnitTestDeleteProductVariant() |
| 84 | + var |
| 85 | + CreateItemAsVariantSub: Codeunit "Shpfy CreateItemAsVariantSub"; |
| 86 | + VariantAPI: Codeunit "Shpfy Variant API"; |
| 87 | + VariantId: BigInteger; |
| 88 | + ActualQueryTxt: Text; |
| 89 | + begin |
| 90 | + // [SCENARIO] Delete a product variant |
| 91 | + Initialize(); |
| 92 | + |
| 93 | + // [GIVEN] Shopify Variant Id |
| 94 | + VariantId := Any.IntegerInRange(10000, 99999); |
| 95 | + |
| 96 | + // [WHEN] Invoke ProductAPI.DeleteProductVariant |
| 97 | + BindSubscription(CreateItemAsVariantSub); |
| 98 | + VariantAPI.DeleteProductVariant(VariantId); |
| 99 | + ActualQueryTxt := CreateItemAsVariantSub.GetGraphQueryTxt(); |
| 100 | + UnbindSubscription(CreateItemAsVariantSub); |
| 101 | + |
| 102 | + // [THEN] Query is correct |
| 103 | + LibraryAssert.IsTrue(ActualQueryTxt.Contains('{"query":"mutation {productVariantDelete('), 'Query not correct'); |
| 104 | + LibraryAssert.IsTrue(ActualQueryTxt.Contains(StrSubstNo('id: \"gid://shopify/ProductVariant/%1\"', VariantId)), 'Variant Id not set'); |
| 105 | + end; |
| 106 | + |
| 107 | + [Test] |
| 108 | + procedure UnitTestCreateVariantFromProductWithMultipleOptions() |
| 109 | + var |
| 110 | + Item: Record "Item"; |
| 111 | + ShpfyProductInitTest: Codeunit "Shpfy Product Init Test"; |
| 112 | + CreateItemAsVariant: Codeunit "Shpfy Create Item As Variant"; |
| 113 | + CreateItemAsVariantSub: Codeunit "Shpfy CreateItemAsVariantSub"; |
| 114 | + ProductId: BigInteger; |
| 115 | + begin |
| 116 | + // [SCENARIO] Create a variant from a product with multiple options |
| 117 | + Initialize(); |
| 118 | + |
| 119 | + // [GIVEN] Item |
| 120 | + Item := ShpfyProductInitTest.CreateItem(Shop."Item Templ. Code", Any.DecimalInRange(10, 100, 2), Any.DecimalInRange(100, 500, 2)); |
| 121 | + // [GIVEN] Shopify product |
| 122 | + ProductId := CreateShopifyProduct(Item.SystemId); |
| 123 | + |
| 124 | + // [GIVEN] Multiple options for the product in Shopify |
| 125 | + CreateItemAsVariantSub.SetMultipleOptions(true); |
| 126 | + |
| 127 | + // [WHEN] Invoke ProductAPI.CheckProductAndShopSettings |
| 128 | + BindSubscription(CreateItemAsVariantSub); |
| 129 | + CreateItemAsVariant.SetParentProduct(ProductId); |
| 130 | + asserterror CreateItemAsVariant.CheckProductAndShopSettings(); |
| 131 | + UnbindSubscription(CreateItemAsVariantSub); |
| 132 | + |
| 133 | + // [THEN] Error is thrown |
| 134 | + LibraryAssert.ExpectedError('The product has more than one option. Items cannot be added as variants to a product with multiple options.'); |
| 135 | + end; |
| 136 | + |
| 137 | + [Test] |
| 138 | + procedure UnitTestRemoveDefaultVariantTest() |
| 139 | + var |
| 140 | + Item: Record Item; |
| 141 | + ShpfyVariant: Record "Shpfy Variant"; |
| 142 | + ShpfyProductInitTest: Codeunit "Shpfy Product Init Test"; |
| 143 | + CreateItemAsVariant: Codeunit "Shpfy Create Item As Variant"; |
| 144 | + CreateItemAsVariantSub: Codeunit "Shpfy CreateItemAsVariantSub"; |
| 145 | + ProductId, VariantId : BigInteger; |
| 146 | + begin |
| 147 | + // [SCENARIO] Remove default variant |
| 148 | + Initialize(); |
| 149 | + |
| 150 | + // [GIVEN] Item |
| 151 | + Item := ShpfyProductInitTest.CreateItem(Shop."Item Templ. Code", Any.DecimalInRange(10, 100, 2), Any.DecimalInRange(100, 500, 2)); |
| 152 | + // [GIVEN] Shopify product |
| 153 | + ProductId := CreateShopifyProduct(Item.SystemId); |
| 154 | + // [GIVEN] Shopify variant |
| 155 | + VariantId := CreateShopifyVariant(ProductId); |
| 156 | + // [GIVEN] Default variant exists in Shopify |
| 157 | + CreateItemAsVariantSub.SetDefaultVariantId(VariantId); |
| 158 | + |
| 159 | + // [WHEN] Invoke CreateItemAsVariant.RemoveDefaultVariant |
| 160 | + BindSubscription(CreateItemAsVariantSub); |
| 161 | + CreateItemAsVariant.SetParentProduct(ProductId); |
| 162 | + CreateItemAsVariant.FindDefaultVariantId(); |
| 163 | + CreateItemAsVariant.CreateVariantFromItem(Item); |
| 164 | + CreateItemAsVariant.RemoveDefaultVariant(); |
| 165 | + UnbindSubscription(CreateItemAsVariantSub); |
| 166 | + |
| 167 | + // [THEN] Default variant is removed |
| 168 | + ShpfyVariant.SetRange(Id, VariantId); |
| 169 | + LibraryAssert.IsTrue(ShpfyVariant.IsEmpty(), 'Default variant not removed'); |
| 170 | + |
| 171 | + end; |
| 172 | + |
| 173 | + local procedure Initialize() |
| 174 | + begin |
| 175 | + Any.SetDefaultSeed(); |
| 176 | + if IsInitialized then |
| 177 | + exit; |
| 178 | + Shop := ShpfyInitializeTest.CreateShop(); |
| 179 | + Commit(); |
| 180 | + IsInitialized := true; |
| 181 | + end; |
| 182 | + |
| 183 | + local procedure CreateShopifyProduct(SystemId: Guid): BigInteger |
| 184 | + var |
| 185 | + ShopifyProduct: Record "Shpfy Product"; |
| 186 | + begin |
| 187 | + ShopifyProduct.Init(); |
| 188 | + ShopifyProduct.Id := Any.IntegerInRange(10000, 99999); |
| 189 | + ShopifyProduct."Shop Code" := Shop."Code"; |
| 190 | + ShopifyProduct."Item SystemId" := SystemId; |
| 191 | + ShopifyProduct.Insert(true); |
| 192 | + exit(ShopifyProduct."Id"); |
| 193 | + end; |
| 194 | + |
| 195 | + local procedure CreateShopifyVariant(ProductId: BigInteger): BigInteger |
| 196 | + var |
| 197 | + ShpfyVariant: Record "Shpfy Variant"; |
| 198 | + begin |
| 199 | + ShpfyVariant.Init(); |
| 200 | + ShpfyVariant.Id := Any.IntegerInRange(10000, 99999); |
| 201 | + ShpfyVariant."Shop Code" := Shop."Code"; |
| 202 | + ShpfyVariant."Product Id" := ProductId; |
| 203 | + ShpfyVariant.Insert(false); |
| 204 | + exit(ShpfyVariant."Id"); |
| 205 | + end; |
| 206 | +} |
0 commit comments