Skip to content

Commit

Permalink
Migrate NUnit assertions to the constraint model in preparation of th…
Browse files Browse the repository at this point in the history
…e NUnit 4 upgrade (#740)

* Add Nunit.Analyzer to test projects

* Autoformat of test projects for NUnit2003

* Autoformat of test projects for NUnit2004

* Autoformat of test projects for NUnit2005

* Autoformat of test projects for NUnit2015

* Autoformat of test projects for NUnit2036

* Autoformat of test projects for NUnit2045

* Fix remaining warnings and errors

---------

Co-authored-by: danielmarbach <danielmarbach@users.noreply.github.com>
  • Loading branch information
danielmarbach and danielmarbach authored Aug 20, 2024
1 parent f0affd9 commit 2f4e6dd
Show file tree
Hide file tree
Showing 39 changed files with 466 additions and 253 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NServiceBus.AcceptanceTests.Sources" Version="9.1.1" GeneratePathProperty="true" />
<PackageReference Include="Nunit" Version="3.14.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task Should_be_used()
.Done(c => c.SagaReceivedMessage)
.Run(runSettings);

Assert.True(context.ExtractorWasCalled);
Assert.That(context.ExtractorWasCalled, Is.True);
}

public class Context : ScenarioContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task Should_be_used()
.Done(c => c.SagaReceivedMessage)
.Run(runSettings);

Assert.True(context.ExtractorWasCalled);
Assert.That(context.ExtractorWasCalled, Is.True);
}

public class Context : ScenarioContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task Should_be_used()
.Done(c => c.SagaReceivedMessage)
.Run(runSettings);

Assert.True(context.ExtractorWasCalled);
Assert.That(context.ExtractorWasCalled, Is.True);
}

public class Context : ScenarioContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task Should_be_used()
.Done(c => c.SagaReceivedMessage)
.Run(runSettings);

Assert.True(context.ExtractorWasCalled);
Assert.That(context.ExtractorWasCalled, Is.True);
}

public class Context : ScenarioContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ public async Task Should_work()
.Done(c => c.SagaReceivedMessage)
.Run(runSettings);

Assert.True(context.PartitionStateMatched);
Assert.True(context.ContainerStateMatched);
Assert.Multiple(() =>
{
Assert.That(context.PartitionStateMatched, Is.True);
Assert.That(context.ContainerStateMatched, Is.True);
});
}

public class Context : ScenarioContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ public partial class When_using_outbox_synchronized_session_via_container
partial void AssertPartitionPart(Context scenarioContext)
{
string partitionKeyPath = scenarioContext.PartitionKeyPath;
Assert.AreEqual(SetupFixture.PartitionPathKey, partitionKeyPath);
Assert.AreEqual(new PartitionKey(scenarioContext.TestRunId.ToString()), scenarioContext.PartitionKey);
Assert.Multiple(() =>
{
Assert.That(partitionKeyPath, Is.EqualTo(SetupFixture.PartitionPathKey));
Assert.That(scenarioContext.PartitionKey, Is.EqualTo(new PartitionKey(scenarioContext.TestRunId.ToString())));
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NServiceBus.AcceptanceTests.Sources" Version="9.1.1" GeneratePathProperty="true" />
<PackageReference Include="Nunit" Version="3.14.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ public async Task Should_persist_into_the_provided_container()
var shippingProcessSagaData = await nonDefaultContainer.ReadItemAsync<dynamic>(context.ShippingProcessId,
new PartitionKey(context.ShippingProcessId));

Assert.AreEqual(HttpStatusCode.OK, orderProcessSagaData.StatusCode);
Assert.AreEqual(HttpStatusCode.OK, shippingProcessSagaData.StatusCode);
Assert.Multiple(() =>
{
Assert.That(orderProcessSagaData.StatusCode, Is.EqualTo(HttpStatusCode.OK));
Assert.That(shippingProcessSagaData.StatusCode, Is.EqualTo(HttpStatusCode.OK));
});
}

[TearDown]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static async Task ImportIntoCosmosDB(Context scenarioContext)
{
var response = await container.CreateItemStreamAsync(stream, new PartitionKey(actualSagaId.ToString()));

Assert.IsTrue(response.IsSuccessStatusCode, "Successfully imported");
Assert.That(response.IsSuccessStatusCode, Is.True, "Successfully imported");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NServiceBus.PersistenceTests.Sources" Version="9.1.1" GeneratePathProperty="true" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ public async Task OneTimeSetUp()
}

[OneTimeTearDown]
public Task OneTimeTearDown()
public async Task OneTimeTearDown()
{
return Container.DeleteContainerStreamAsync();
await Container.DeleteContainerStreamAsync();
CosmosDbClient.Dispose();
}

static string GetEnvironmentVariable(string variable, string fallbackEmulatorConnectionString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NServiceBus.AcceptanceTests.Sources" Version="9.1.1" GeneratePathProperty="true" />
<PackageReference Include="Nunit" Version="3.14.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ await Task.WhenAll(Enumerable.Range(0, scenarioContext.ConcurrentMessageCount).S
.Done(s => s.SagaCompleted)
.Run();

Assert.IsTrue(context.ConcurrentMessagesSent);
Assert.AreEqual(0, context.RetryCount);
Assert.Multiple(() =>
{
Assert.That(context.ConcurrentMessagesSent, Is.True);
Assert.That(context.RetryCount, Is.EqualTo(0));
});
}

static string MigrationDocument = @"{{
Expand Down Expand Up @@ -67,7 +70,7 @@ static async Task ImportIntoCosmosDB(HighContentionScenario scenarioContext)
{
var response = await container.CreateItemStreamAsync(stream, new PartitionKey(actualSagaId.ToString()));

Assert.IsTrue(response.IsSuccessStatusCode, "Successfully imported");
Assert.That(response.IsSuccessStatusCode, Is.True, "Successfully imported");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ public async Task Should_succeed_without_retries()
.Done(s => s.SagaCompleted)
.Run();

Assert.IsTrue(scenario.ConcurrentMessagesSent);
Assert.AreEqual(0, scenario.RetryCount);
Assert.Multiple(() =>
{
Assert.That(scenario.ConcurrentMessagesSent, Is.True);
Assert.That(scenario.RetryCount, Is.EqualTo(0));
});
}

public class HighContentionScenario : ScenarioContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NServiceBus.AcceptanceTests.Sources" Version="9.1.1" GeneratePathProperty="true" />
<PackageReference Include="Nunit" Version="3.14.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task Should_be_used()
.Done(c => c.SagaReceivedMessage)
.Run(runSettings);

Assert.True(context.ExtractorWasCalled);
Assert.That(context.ExtractorWasCalled, Is.True);
}

public class Context : ScenarioContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task Should_be_used()
.Done(c => c.SagaReceivedMessage)
.Run(runSettings);

Assert.True(context.ExtractorWasCalled);
Assert.That(context.ExtractorWasCalled, Is.True);
}

public class Context : ScenarioContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public partial class When_using_outbox_synchronized_session_via_container
partial void AssertPartitionPart(Context scenarioContext)
{
string partitionKeyPath = scenarioContext.PartitionKeyPath;
Assert.AreEqual(SetupFixture.PartitionPathKey, partitionKeyPath);
Assert.AreEqual(new PartitionKey(scenarioContext.TestRunId.ToString()), scenarioContext.PartitionKey);
Assert.That(partitionKeyPath, Is.EqualTo(SetupFixture.PartitionPathKey));
Assert.That(scenarioContext.PartitionKey, Is.EqualTo(new PartitionKey(scenarioContext.TestRunId.ToString())));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<ItemGroup>
<PackageReference Include="GitHubActionsTestLogger" Version="2.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Nunit" Version="3.14.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ public async Task Should_clear_added_pending_operations_and_restore_ones_from_ou

await behavior.Invoke(testableContext, c => Task.CompletedTask);

Assert.IsTrue(pendingTransportOperations.HasOperations, "Should have exactly one operation added found on the outbox record");
Assert.AreEqual("42", pendingTransportOperations.Operations.ElementAt(0).Message.MessageId, "Should have exactly one operation added found on the outbox record");
Assert.Multiple(() =>
{
Assert.That(pendingTransportOperations.HasOperations, Is.True, "Should have exactly one operation added found on the outbox record");
Assert.That(pendingTransportOperations.Operations.ElementAt(0).Message.MessageId, Is.EqualTo("42"), "Should have exactly one operation added found on the outbox record");
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ public void Should_have_default_values()
{
var configuration = new PessimisticLockingConfiguration();

Assert.That(configuration.LeaseLockTime, Is.EqualTo(TimeSpan.FromMinutes(1)));
Assert.That(configuration.LeaseLockAcquisitionTimeout, Is.EqualTo(TimeSpan.FromMinutes(1)));
Assert.That(configuration.LeaseLockAcquisitionMinimumRefreshDelay, Is.EqualTo(TimeSpan.FromMilliseconds(500)));
Assert.That(configuration.LeaseLockAcquisitionMaximumRefreshDelay, Is.EqualTo(TimeSpan.FromMilliseconds(1000)));
Assert.That(configuration.PessimisticLockingEnabled, Is.False);
Assert.Multiple(() =>
{
Assert.That(configuration.LeaseLockTime, Is.EqualTo(TimeSpan.FromMinutes(1)));
Assert.That(configuration.LeaseLockAcquisitionTimeout, Is.EqualTo(TimeSpan.FromMinutes(1)));
Assert.That(configuration.LeaseLockAcquisitionMinimumRefreshDelay, Is.EqualTo(TimeSpan.FromMilliseconds(500)));
Assert.That(configuration.LeaseLockAcquisitionMaximumRefreshDelay, Is.EqualTo(TimeSpan.FromMilliseconds(1000)));
Assert.That(configuration.PessimisticLockingEnabled, Is.False);
});
}

[Test]
Expand All @@ -35,7 +38,7 @@ public void Should_set_lease_lock_time([Random(1, 10, 5, Distinct = true)] doubl

configuration.SetLeaseLockTime(fromMinutes);

Assert.AreEqual(fromMinutes, configuration.LeaseLockTime);
Assert.That(configuration.LeaseLockTime, Is.EqualTo(fromMinutes));
}

[Test]
Expand All @@ -55,7 +58,7 @@ public void Should_set_lease_lock_acquisition_timeout([Random(1, 10, 5, Distinct

configuration.SetLeaseLockTime(fromMinutes);

Assert.AreEqual(fromMinutes, configuration.LeaseLockTime);
Assert.That(configuration.LeaseLockTime, Is.EqualTo(fromMinutes));
}

[Test]
Expand Down Expand Up @@ -95,7 +98,7 @@ public void Should_set_minimum_refresh_delay_when_below_default_maximum_refresh_
configuration.SetLeaseLockAcquisitionMinimumRefreshDelay(fromMilliseconds);
configuration.ValidateRefreshDelays();

Assert.AreEqual(fromMilliseconds, configuration.LeaseLockAcquisitionMinimumRefreshDelay);
Assert.That(configuration.LeaseLockAcquisitionMinimumRefreshDelay, Is.EqualTo(fromMilliseconds));
}

[Test]
Expand All @@ -109,7 +112,7 @@ public void Should_set_minimum_refresh_delay_when_below_maximum_refresh_delay([R
configuration.SetLeaseLockAcquisitionMinimumRefreshDelay(fromMilliseconds);
configuration.ValidateRefreshDelays();

Assert.AreEqual(fromMilliseconds, configuration.LeaseLockAcquisitionMinimumRefreshDelay);
Assert.That(configuration.LeaseLockAcquisitionMinimumRefreshDelay, Is.EqualTo(fromMilliseconds));
}

[Test]
Expand Down Expand Up @@ -149,7 +152,7 @@ public void Should_set_maximum_refresh_delay_when_above_default_minimum_refresh_
configuration.SetLeaseLockAcquisitionMaximumRefreshDelay(fromMilliseconds);
configuration.ValidateRefreshDelays();

Assert.AreEqual(fromMilliseconds, configuration.LeaseLockAcquisitionMaximumRefreshDelay);
Assert.That(configuration.LeaseLockAcquisitionMaximumRefreshDelay, Is.EqualTo(fromMilliseconds));
}

[Test]
Expand All @@ -163,7 +166,7 @@ public void Should_set_maximum_refresh_delay_when_above_minimum_refresh_delay([R
configuration.SetLeaseLockAcquisitionMaximumRefreshDelay(fromMilliseconds);
configuration.ValidateRefreshDelays();

Assert.AreEqual(fromMilliseconds, configuration.LeaseLockAcquisitionMaximumRefreshDelay);
Assert.That(configuration.LeaseLockAcquisitionMaximumRefreshDelay, Is.EqualTo(fromMilliseconds));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public class SagaIdGeneratorTests
{
[Test]
public void Should_return_the_same_guid_regardless_of_the_tfm() =>
Assert.AreEqual(new Guid("c718ea15-a555-f79f-fa37-62477f3b07ca"), CosmosSagaIdGenerator.Generate("SagaEntityTypeFullName", "CorrelationPropertyName", "SomeValue"));
Assert.That(CosmosSagaIdGenerator.Generate("SagaEntityTypeFullName", "CorrelationPropertyName", "SomeValue"), Is.EqualTo(new Guid("c718ea15-a555-f79f-fa37-62477f3b07ca")));

[TestCaseSource("RandomInput")]
[TestCaseSource(nameof(RandomInput))]
public void Should_return_the_same_guid_as_previous(string fullname, string propertyName, string propertyValue) =>
Assert.AreEqual(PreviousCosmosSagaIdGenerator.Generate(fullname, propertyName, propertyValue), CosmosSagaIdGenerator.Generate(fullname, propertyName, propertyValue));
Assert.That(CosmosSagaIdGenerator.Generate(fullname, propertyName, propertyValue), Is.EqualTo(PreviousCosmosSagaIdGenerator.Generate(fullname, propertyName, propertyValue)));

public static IEnumerable<object[]> RandomInput
{
Expand Down
Loading

0 comments on commit 2f4e6dd

Please sign in to comment.