Skip to content

Commit 33412fc

Browse files
add route workflow test (#141)
1 parent 9f192c5 commit 33412fc

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright (c) LittleLittleCloud. All rights reserved.
2+
// RouteWorkflowTest.cs
3+
4+
using FluentAssertions;
5+
using Meziantou.Extensions.Logging.Xunit;
6+
using Microsoft.Extensions.Logging;
7+
using StepWise.Core.Extension;
8+
using Xunit;
9+
using Xunit.Abstractions;
10+
11+
namespace StepWise.Core.Tests;
12+
13+
/// <summary>
14+
/// RouteWorkflowTest
15+
/// The workflow is
16+
/// ```mermaid
17+
/// graph TD
18+
/// Start --> A
19+
/// Start --> B
20+
/// Start --> C
21+
/// A --> End
22+
/// B --> End
23+
/// C --> End
24+
/// </summary>
25+
public class RouteWorkflowTest
26+
{
27+
private readonly ITestOutputHelper _testOutputHelper;
28+
private readonly ILogger _logger;
29+
30+
public RouteWorkflowTest(ITestOutputHelper testOutputHelper)
31+
{
32+
_testOutputHelper = testOutputHelper;
33+
_logger = new XUnitLoggerProvider(testOutputHelper).CreateLogger(nameof(RouteWorkflowTest));
34+
}
35+
36+
[Step]
37+
public async Task<string> Start()
38+
{
39+
return "Start";
40+
}
41+
42+
[Step]
43+
[DependOn(nameof(Start))]
44+
public async Task<string> A(
45+
[FromStep(nameof(Start))] string start)
46+
{
47+
return "A";
48+
}
49+
50+
[Step]
51+
[DependOn(nameof(Start))]
52+
public async Task<string> B(
53+
[FromStep(nameof(Start))] string start)
54+
{
55+
return "B";
56+
}
57+
58+
[Step]
59+
[DependOn(nameof(Start))]
60+
public async Task<string> C()
61+
{
62+
return "C";
63+
}
64+
65+
[Step]
66+
[DependOn(nameof(A))]
67+
[DependOn(nameof(B))]
68+
[DependOn(nameof(C))]
69+
public async Task<string> End(
70+
[FromStep(nameof(A))] string a,
71+
[FromStep(nameof(C))] string c)
72+
{
73+
return "End";
74+
}
75+
76+
[Fact]
77+
public async Task ItRunWorkflowTestAsync()
78+
{
79+
var engine = StepWiseEngine.CreateFromInstance(this, _logger);
80+
var results = new List<StepRun>();
81+
await foreach (var stepRun in engine.ExecuteAsync(maxConcurrency: 1))
82+
{
83+
results.Add(stepRun);
84+
}
85+
86+
results.Count().Should().Be(24);
87+
88+
// should get five variables: Start, A, B, C, End
89+
var variables = results.Where(r => r.StepRunType == StepRunType.Variable).Select(r => r.Variable!).ToList();
90+
variables.Count().Should().Be(5);
91+
variables.Should().Contain(v => v.Name == nameof(Start));
92+
variables.Should().Contain(v => v.Name == nameof(A));
93+
variables.Should().Contain(v => v.Name == nameof(B));
94+
variables.Should().Contain(v => v.Name == nameof(C));
95+
variables.Should().Contain(v => v.Name == nameof(End));
96+
97+
// The first variable should be Start, and the last variable should be End
98+
variables.First().Name.Should().Be(nameof(Start));
99+
variables.Last().Name.Should().Be(nameof(End));
100+
}
101+
102+
[Fact]
103+
public async Task ItRunStepATestAsync()
104+
{
105+
var engine = StepWiseEngine.CreateFromInstance(this, _logger);
106+
var results = new List<StepRun>();
107+
await foreach (var stepRun in engine.ExecuteStepAsync(nameof(A), maxConcurrency: 1))
108+
{
109+
results.Add(stepRun);
110+
}
111+
results.Count().Should().Be(9);
112+
// should get two variables: Start, A
113+
var variables = results.Where(r => r.StepRunType == StepRunType.Variable).Select(r => r.Variable!).ToList();
114+
variables.Count().Should().Be(2);
115+
variables.Should().Contain(v => v.Name == nameof(Start));
116+
variables.Should().Contain(v => v.Name == nameof(A));
117+
// The first variable should be Start, and the last variable should be A
118+
variables.First().Name.Should().Be(nameof(Start));
119+
variables.Last().Name.Should().Be(nameof(A));
120+
}
121+
}

0 commit comments

Comments
 (0)