Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kbilsted committed Feb 11, 2024
1 parent 50fdfa2 commit cbbc5b1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
28 changes: 18 additions & 10 deletions src/Demos/MicroWorkflow.Tests/AdoSingletonStepTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,32 @@ public void Setup()
public void When_creating_a_singleton_Then_it_is_created()
{
string? stepResult = null;
helper.Steps = [new Step(helper.RndName)
bool? stepResultIsSingleton = null;
var name = helper.RndName;
helper.Steps = [new Step(name)
{
Singleton = true,
FlowId = helper.FlowId
}];
helper.StepHandlers = [Handle(helper.RndName, step => { stepResult = $"hello"; return ExecutionResult.Done(); })];
helper.StepHandlers = [Handle(name, step => {
stepResult = $"hello";
stepResultIsSingleton = step.Singleton;
return ExecutionResult.Done();
})];
helper.StopWhenNoWork().BuildAndStart();

stepResult.Should().Be("hello");
stepResultIsSingleton.Should().BeTrue();
helper.AssertTableCounts(helper.FlowId, ready: 0, done: 1, failed: 0);
}

[Test]
public void When_adding_two_identical_singleton_steps_simultaniously_Then_fail()
{
var engine = helper.Build();

var step = new Step(helper.RndName) { Singleton = true, };
var step2 = new Step(helper.RndName) { Singleton = true, };
var name = helper.RndName;
var step = new Step(name) { Singleton = true, };
var step2 = new Step(name) { Singleton = true, };

Func<object> act = () => engine.Data.AddSteps([step, step2]);

Expand All @@ -48,10 +55,11 @@ public void When_adding_two_identical_singleton_steps_simultaniously_Then_fail()
public void When_adding_two_identical_singleton_steps_Then_fail_on_last_insert()
{
var engine = helper.Build();
var step = new Step(helper.RndName) { Singleton = true, };
var name = helper.RndName;
var step = new Step(name) { Singleton = true, };
engine.Data.AddStep(step);

var step2 = new Step(helper.RndName) { Singleton = true, };
var step2 = new Step(name) { Singleton = true, };
Func<object> act = () => engine.Data.AddStep(step2);

act.Should()
Expand All @@ -63,14 +71,14 @@ public void When_adding_two_identical_singleton_steps_Then_fail_on_last_insert()
public void When_AddStepIfNotExists_two_identical_singleton_steps_Then_insert_first_and_return_null_on_duplicate()
{
var engine = helper.Build();

var step = new Step(helper.RndName) { Singleton = true };
var name = helper.RndName;
var step = new Step(name) { Singleton = true };
SearchModel searchModel = new(Name: step.Name);
engine.Data.AddStepIfNotExists(step, searchModel)
.Should()
.HaveValue();

var step2 = new Step(helper.RndName) { Singleton = true };
var step2 = new Step(name) { Singleton = true };
engine.Data.AddStepIfNotExists(step2, searchModel)
.Should()
.BeNull();
Expand Down
16 changes: 10 additions & 6 deletions src/Demos/MicroWorkflow.Tests/AdoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void Setup()
public void When_executing_OneStep_with_initialstate_Then_that_state_is_accessible_and_step_is_executed()
{
string? stepResult = null;
bool? stepResultIsSingleton = null;
const string StepName = "OneStep";
helper.Steps = [new Step(StepName)
{
Expand All @@ -36,12 +37,14 @@ public void When_executing_OneStep_with_initialstate_Then_that_state_is_accessib
{
int counter = helper.Formatter!.Deserialize<int>(step.State);
stepResult = $"hello {counter}";
stepResultIsSingleton = step.Singleton;
return ExecutionResult.Done();
}))];

helper.StopWhenNoWork().BuildAndStart();

stepResult.Should().Be("hello 1234");
stepResultIsSingleton.Should().BeFalse();
helper.AssertTableCounts(helper.FlowId, ready: 0, done: 1, failed: 0);
}

Expand Down Expand Up @@ -173,7 +176,7 @@ public void When_executing_step_throwing_special_FailCurrentStepException_and_ad
public void When_executing_step_throwing_exception_Then_rerun_current_step_and_ensure_state_is_unchanged()
{
int? dbid = null;
const string name = "test-throw-exception";
string name = helper.RndName;
helper.StepHandlers = [Handle(name,
step =>
{
Expand All @@ -198,7 +201,7 @@ public void When_connecting_unsecurely_to_DB_Then_see_the_exception()
{
const string IllegalConnectionString = "Server=localhost;Database=adotest;Integrated Security=False;TrustServerCertificate=False";

helper.StepHandlers = [Handle("onestep_fails", step => step.Fail())];
helper.StepHandlers = [Handle("any", step => step.Fail())];
helper.ConnectionString = IllegalConnectionString;

Action act = () => helper.StopWhenNoWork().BuildAndStart();
Expand All @@ -211,7 +214,7 @@ public void When_connecting_unsecurely_to_DB_Then_see_the_exception()
[Test]
public void When_step_is_failing_Then_it_is_marked_as_failed()
{
const string name = "onestep_fails";
string name = helper.RndName;
helper.StepHandlers = [(name, new GenericImplementation(step => step.Fail()))];
helper.Steps = [new Step(name) { FlowId = helper.FlowId }];
helper.StopWhenNoWork().BuildAndStart();
Expand All @@ -223,12 +226,13 @@ public void When_step_is_failing_Then_it_is_marked_as_failed()
public void When_executing_step_for_the_first_time_Then_execution_count_is_1()
{
int? stepResult = null;
helper.StepHandlers = [(helper.RndName, new GenericImplementation(step =>
var name = helper.RndName;
helper.StepHandlers = [(name, new GenericImplementation(step =>
{
stepResult = step.ExecutionCount;
return ExecutionResult.Done();
}))];
helper.Steps = [new Step(helper.RndName)];
helper.Steps = [new Step(name)];
helper.StopWhenNoWork().BuildAndStart();

stepResult.Should().Be(1);
Expand All @@ -237,7 +241,7 @@ public void When_executing_step_for_the_first_time_Then_execution_count_is_1()
[Test]
public void When_step_returns_rerun_Then_it_is_rerun()
{
const string name = "OneStep_Repeating_Thrice";
string name = helper.RndName;
string? stepResult = null;
helper.StepHandlers = [Handle(name, step =>
{
Expand Down
5 changes: 3 additions & 2 deletions src/Demos/MicroWorkflow.Tests/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ public void Setup()
[Test]
public void When_adding_an_event_Then_an_id_PK_is_returned()
{
Step step = new(helper.RndName) { ScheduleTime = DateTime.Now.AddMonths(1) };
var name = helper.RndName;
Step step = new(name) { ScheduleTime = DateTime.Now.AddMonths(1) };

helper.Build();
var id = helper.Engine!.Data.AddStep(step);

var result = helper.Engine.Data.SearchSteps(new(Id: id), StepStatus.Ready);

result.Single().Name.Should().Be(helper.RndName);
result.Single().Name.Should().Be(name);
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion src/Demos/MicroWorkflow.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class TestHelper
{
public static string guid() => Guid.NewGuid().ToString();

public string RndName = "test" + guid();
public string RndName { get => (TestContext.CurrentContext.Test.FullName + guid()).Substring("MicroWorkflow.".Length); }
public NewtonsoftStateFormatterJson? Formatter;
public CancellationTokenSource cts = new();
public AutofacAdaptor? iocContainer;
Expand Down

0 comments on commit cbbc5b1

Please sign in to comment.