-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Shopify] - Add Item As Variant Tests #4
base: main
Are you sure you want to change the base?
Changes from 2 commits
6a62152
320e599
89ec013
adf1cc6
6af2672
4ac1277
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
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"; | ||
ShpfyVariant: Record "Shpfy Variant"; | ||
ShpfyProduct: Record "Shpfy Product"; | ||
ShpfyProductInitTest: Codeunit "Shpfy Product Init Test"; | ||
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(); | ||
|
||
// [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 | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, it's fine. as it's a small query, but I feel we'd be breaking these kinds of tests over time, as we change the queries in the actual app. It might be better to only assert if the part of the query that you really care about is in it? |
||
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 | ||
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what the agreement is on how thorough the tests should be. I see you're mainly doing unit tests on a level of a procedure. I'd also like to see a tests or two that handle the cases where Option > 1 and one for removing the default variant.