Skip to content

Commit

Permalink
Adds test generation unit test that instantiates and validates all sc…
Browse files Browse the repository at this point in the history
…enario generators
  • Loading branch information
craigedmunds committed Jan 2, 2025
1 parent c962eec commit cbe21c4
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 23 deletions.
36 changes: 23 additions & 13 deletions TestDataGenerator.Tests/ScenarioTests.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
using Btms.BlobService;
using Btms.BlobService.Extensions;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using TestDataGenerator.Extensions;
using TestDataGenerator.Scenarios;
using Xunit;

namespace TestDataGenerator.Tests;

public class ScenarioTests
{
[Fact]
public void EnsureAllScenarioDefaultsAreValid()
ServiceProvider _serviceProvider = BuilderExtensions.GetDefaultServiceProvider();

public static IEnumerable<object[]> GetAllScenarios()
{
return BuilderExtensions
.GetAllScenarios()
.Select(s => new object[] { s });
}

[Theory]
[MemberData(nameof(GetAllScenarios))]
public void EnsureAllScenarioDefaultsAreValid(Type scenarioType)
{
// var (configuration, _) = BuilderExtensions.GetConfig("Scenarios/Samples");
//
// var sp = new ServiceCollection()
// .AddBlobStorage(configuration)
// .AddSingleton<CachingBlobService>()
// .ConfigureTestGenerationServices()
// .BuildServiceProvider();

var scenarioTypes = BuilderExtensions.GetAllScenarios();

foreach (var scenarioType in scenarioTypes)
var scenario = (ScenarioGenerator)_serviceProvider.GetRequiredService(scenarioType);
var config = new ScenarioConfig()
{

}
Name = "Test", ArrivalDateRange = 1, Count = 1, Generator = scenario, CreationDateRange = 1
};

var messages = scenario.Generate(1, 1, DateTime.Today, config);

messages.Should().NotBeNull();
messages.Should().NotBeEmpty();
}
}
11 changes: 11 additions & 0 deletions TestDataGenerator/Extensions/BuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ public static IEnumerable<Type> GetAllScenarios()
&& !p.IsAbstract
&& p != scenarioService);
}

public static ServiceProvider GetDefaultServiceProvider()
{
var (configuration, _) = BuilderExtensions.GetConfig("Scenarios/Samples");

return new ServiceCollection()
.AddBlobStorage(configuration)
.AddSingleton<CachingBlobService>()
.ConfigureTestGenerationServices()
.BuildServiceProvider();
}

public static IServiceCollection ConfigureTestGenerationServices(this IServiceCollection services)
{
Expand Down
8 changes: 4 additions & 4 deletions TestDataGenerator/ImportNotificationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ protected override ImportNotificationBuilder<T> Validate()
n.Version.AssertHasValue("Import Notification Version missing");

// NB - this may not be correct...
if (n.ImportNotificationType != ImportNotificationTypeEnum.Cveda)
{
n.PartTwo!.InspectionRequired.AssertHasValue("Import Notification PartTwo InspectionRequired missing");
}
// if (n.ImportNotificationType != ImportNotificationTypeEnum.Cveda && n.PartTwo.HasValue())
// {
// n.PartTwo!.InspectionRequired.AssertHasValue("Import Notification PartTwo InspectionRequired missing");
// }

});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public override GeneratorResult Generate(int scenario, int item, DateTime entryD
.WithCreationDate(entryDate)
.WithRandomArrivalDateTime(config.ArrivalDateRange)
.WithReferenceNumber(ImportNotificationTypeEnum.Cveda, scenario, entryDate, item)
.WithVersionNumber()
.ValidateAndBuild();

logger.LogInformation("Created {NotificationReferenceNumber}",
Expand All @@ -20,6 +21,7 @@ public override GeneratorResult Generate(int scenario, int item, DateTime entryD
.WithCreationDate(entryDate)
.WithRandomArrivalDateTime(config.ArrivalDateRange)
.WithReferenceNumber(ImportNotificationTypeEnum.Cvedp, scenario, entryDate, item)
.WithVersionNumber()
.ValidateAndBuild();

logger.LogInformation("Created {NotificationReferenceNumber}",
Expand All @@ -29,6 +31,7 @@ public override GeneratorResult Generate(int scenario, int item, DateTime entryD
.WithCreationDate(entryDate)
.WithRandomArrivalDateTime(config.ArrivalDateRange)
.WithReferenceNumber(ImportNotificationTypeEnum.Ced, scenario, entryDate, item)
.WithVersionNumber()
.ValidateAndBuild();

logger.LogInformation("Created {NotificationReferenceNumber}",
Expand All @@ -38,6 +41,7 @@ public override GeneratorResult Generate(int scenario, int item, DateTime entryD
.WithCreationDate(entryDate)
.WithRandomArrivalDateTime(config.ArrivalDateRange)
.WithReferenceNumber(ImportNotificationTypeEnum.Chedpp, scenario, entryDate, item)
.WithVersionNumber()
.ValidateAndBuild();

logger.LogInformation("Created {NotificationReferenceNumber}",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"referenceNumber": null,
"version": 1,
"version": null,
"lastUpdated": null,
"lastUpdatedBy": {
"displayName": "Percy Inspector-Tester",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": 3989,
"referenceNumber": "CHEDD.GB.2024.1003989",
"version": 1,
"version": null,
"lastUpdated": "2024-08-19T08:45:36.18Z",
"lastUpdatedBy": {
"displayName": "Percy Inspector-Tester",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": 3991,
"referenceNumber": "CHEDPP.GB.2024.1003991",
"version": 1,
"version": null,
"lastUpdated": "2024-08-19T08:50:00.193Z",
"lastUpdatedBy": {
"displayName": "Percy Inspector-Tester",
Expand Down
4 changes: 2 additions & 2 deletions TestDataGenerator/Scenarios/Samples/cr-one-item.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
},
"header": {
"entryReference": null,
"entryVersionNumber": 2,
"previousVersionNumber": 1,
"entryVersionNumber": null,
"previousVersionNumber": null,
"declarationUCR": null,
"declarationPartNumber": null,
"declarationType": null,
Expand Down
2 changes: 1 addition & 1 deletion TestDataGenerator/Scenarios/Samples/decision-one-item.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"header": {
"entryReference": null,
"entryVersionNumber": 1,
"entryVersionNumber": null,
"decisionNumber": null
},
"items": [
Expand Down

0 comments on commit cbe21c4

Please sign in to comment.