forked from dapr/dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable unit testing for Dapr Workflows (dapr#1035)
* Workflow SDK changes to enable unit testing Signed-off-by: Chris Gillum <cgillum@microsoft.com> * Sample workflow unit testing project Signed-off-by: Chris Gillum <cgillum@microsoft.com> --------- Signed-off-by: Chris Gillum <cgillum@microsoft.com>
- Loading branch information
1 parent
dfd4aeb
commit 924a05a
Showing
9 changed files
with
142 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 3 additions & 4 deletions
7
examples/Workflow/WorkflowConsoleApp/Activities/NotifyActivity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 3 additions & 6 deletions
9
examples/Workflow/WorkflowConsoleApp/Activities/ProcessPaymentActivity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
namespace WorkflowConsoleApp.Models | ||
{ | ||
record OrderPayload(string Name, double TotalCost, int Quantity = 1); | ||
record InventoryRequest(string RequestId, string ItemName, int Quantity); | ||
record InventoryResult(bool Success, InventoryItem orderPayload); | ||
record PaymentRequest(string RequestId, string ItemName, int Amount, double Currency); | ||
record OrderResult(bool Processed); | ||
record InventoryItem(string Name, double PerItemCost, int Quantity); | ||
public record OrderPayload(string Name, double TotalCost, int Quantity = 1); | ||
public record InventoryRequest(string RequestId, string ItemName, int Quantity); | ||
public record InventoryResult(bool Success, InventoryItem orderPayload); | ||
public record PaymentRequest(string RequestId, string ItemName, int Amount, double Currency); | ||
public record OrderResult(bool Processed); | ||
public record InventoryItem(string Name, double PerItemCost, int Quantity); | ||
} |
5 changes: 2 additions & 3 deletions
5
examples/Workflow/WorkflowConsoleApp/Workflows/OrderProcessingWorkflow.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
examples/Workflow/WorkflowUnitTest/OrderProcessingTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System.Threading.Tasks; | ||
using Dapr.Workflow; | ||
using Microsoft.DurableTask; | ||
using Moq; | ||
using WorkflowConsoleApp.Activities; | ||
using WorkflowConsoleApp.Models; | ||
using WorkflowConsoleApp.Workflows; | ||
using Xunit; | ||
|
||
namespace WorkflowUnitTest | ||
{ | ||
[Trait("Example", "true")] | ||
public class OrderProcessingTests | ||
{ | ||
[Fact] | ||
public async Task TestSuccessfulOrder() | ||
{ | ||
// Test payloads | ||
OrderPayload order = new(Name: "Paperclips", TotalCost: 99.95, Quantity: 10); | ||
PaymentRequest expectedPaymentRequest = new(It.IsAny<string>(), order.Name, order.Quantity, order.TotalCost); | ||
InventoryRequest expectedInventoryRequest = new(It.IsAny<string>(), order.Name, order.Quantity); | ||
InventoryResult inventoryResult = new(Success: true, new InventoryItem(order.Name, 9.99, order.Quantity)); | ||
|
||
// Mock the call to ReserveInventoryActivity | ||
Mock<WorkflowContext> mockContext = new(); | ||
mockContext | ||
.Setup(ctx => ctx.CallActivityAsync<InventoryResult>(nameof(ReserveInventoryActivity), It.IsAny<InventoryRequest>(), It.IsAny<TaskOptions>())) | ||
.Returns(Task.FromResult(inventoryResult)); | ||
|
||
// Run the workflow directly | ||
OrderResult result = await new OrderProcessingWorkflow().RunAsync(mockContext.Object, order); | ||
|
||
// Verify that workflow result matches what we expect | ||
Assert.NotNull(result); | ||
Assert.True(result.Processed); | ||
|
||
// Verify that ReserveInventoryActivity was called with a specific input | ||
mockContext.Verify( | ||
ctx => ctx.CallActivityAsync<InventoryResult>(nameof(ReserveInventoryActivity), expectedInventoryRequest, It.IsAny<TaskOptions>()), | ||
Times.Once()); | ||
|
||
// Verify that ProcessPaymentActivity was called with a specific input | ||
mockContext.Verify( | ||
ctx => ctx.CallActivityAsync(nameof(ProcessPaymentActivity), expectedPaymentRequest, It.IsAny<TaskOptions>()), | ||
Times.Once()); | ||
|
||
// Verify that there were two calls to NotifyActivity | ||
mockContext.Verify( | ||
ctx => ctx.CallActivityAsync(nameof(NotifyActivity), It.IsAny<Notification>(), It.IsAny<TaskOptions>()), | ||
Times.Exactly(2)); | ||
} | ||
|
||
[Fact] | ||
public async Task TestInsufficientInventory() | ||
{ | ||
// Test payloads | ||
OrderPayload order = new(Name: "Paperclips", TotalCost: 99.95, Quantity: int.MaxValue); | ||
InventoryRequest expectedInventoryRequest = new(It.IsAny<string>(), order.Name, order.Quantity); | ||
InventoryResult inventoryResult = new(Success: false, null); | ||
|
||
// Mock the call to ReserveInventoryActivity | ||
Mock<WorkflowContext> mockContext = new(); | ||
mockContext | ||
.Setup(ctx => ctx.CallActivityAsync<InventoryResult>(nameof(ReserveInventoryActivity), It.IsAny<InventoryRequest>(), It.IsAny<TaskOptions>())) | ||
.Returns(Task.FromResult(inventoryResult)); | ||
|
||
// Run the workflow directly | ||
OrderResult result = await new OrderProcessingWorkflow().RunAsync(mockContext.Object, order); | ||
|
||
// Verify that ReserveInventoryActivity was called with a specific input | ||
mockContext.Verify( | ||
ctx => ctx.CallActivityAsync<InventoryResult>(nameof(ReserveInventoryActivity), expectedInventoryRequest, It.IsAny<TaskOptions>()), | ||
Times.Once()); | ||
|
||
// Verify that ProcessPaymentActivity was never called | ||
mockContext.Verify( | ||
ctx => ctx.CallActivityAsync(nameof(ProcessPaymentActivity), It.IsAny<PaymentRequest>(), It.IsAny<TaskOptions>()), | ||
Times.Never()); | ||
|
||
// Verify that there were two calls to NotifyActivity | ||
mockContext.Verify( | ||
ctx => ctx.CallActivityAsync(nameof(NotifyActivity), It.IsAny<Notification>(), It.IsAny<TaskOptions>()), | ||
Times.Exactly(2)); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
examples/Workflow/WorkflowUnitTest/WorkflowUnitTest.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>10</LangVersion> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> | ||
<PackageReference Include="Moq" Version="4.18.4" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\WorkflowConsoleApp\WorkflowConsoleApp.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |