Skip to content

Commit

Permalink
CDMS-179 patches CDMS change to BTMS
Browse files Browse the repository at this point in the history
  • Loading branch information
craigedmunds committed Dec 6, 2024
1 parent c497790 commit 84e584c
Show file tree
Hide file tree
Showing 35 changed files with 748 additions and 185 deletions.
61 changes: 0 additions & 61 deletions Btms.Analytics.Tests/AggregationTestFixture.cs

This file was deleted.

1 change: 1 addition & 0 deletions Btms.Analytics.Tests/Btms.Analytics.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<RootNamespace>Btms.Analytics.Tests</RootNamespace>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

Expand Down
11 changes: 11 additions & 0 deletions Btms.Analytics.Tests/Fixtures/BasicSampleDataTestCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Xunit;

namespace Btms.Analytics.Tests.Fixtures;

[CollectionDefinition(nameof(BasicSampleDataTestCollection))]
public class BasicSampleDataTestCollection : ICollectionFixture<BasicSampleDataTestFixture>
{
// This class has no code, and is never created. Its purpose is simply
// to be the place to apply [CollectionDefinition] and all the
// ICollectionFixture<> interfaces.
}
66 changes: 66 additions & 0 deletions Btms.Analytics.Tests/Fixtures/BasicSampleDataTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Btms.Analytics.Tests.Helpers;
using Btms.Backend.Data;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using TestDataGenerator.Scenarios;

namespace Btms.Analytics.Tests.Fixtures;

#pragma warning disable S3881
public class BasicSampleDataTestFixture : IDisposable
#pragma warning restore S3881
{
public IHost App;
public IImportNotificationsAggregationService ImportNotificationsAggregationService;
public IMovementsAggregationService MovementsAggregationService;

public IMongoDbContext MongoDbContext;
public BasicSampleDataTestFixture()
{
var builder = TestContextHelper.CreateBuilder<BasicSampleDataTestFixture>();

App = builder.Build();
var rootScope = App.Services.CreateScope();

MongoDbContext = rootScope.ServiceProvider.GetRequiredService<IMongoDbContext>();
ImportNotificationsAggregationService = rootScope.ServiceProvider.GetRequiredService<IImportNotificationsAggregationService>();
MovementsAggregationService = rootScope.ServiceProvider.GetRequiredService<IMovementsAggregationService>();

// Would like to pick this up from env/config/DB state
var insertToMongo = true;

if (insertToMongo)
{
MongoDbContext.ResetCollections().GetAwaiter().GetResult();

// Ensure we have some data scenarios around 24/48 hour tests
App.PushToConsumers(App.CreateScenarioConfig<ChedASimpleMatchScenarioGenerator>(10, 3, arrivalDateRange: 0))
.GetAwaiter().GetResult();

App.PushToConsumers(App.CreateScenarioConfig<ChedPSimpleMatchScenarioGenerator>(10, 3, arrivalDateRange: 2))
.GetAwaiter().GetResult();

App.PushToConsumers(App.CreateScenarioConfig<CrNoMatchScenarioGenerator>(10, 3, arrivalDateRange: 0))
.GetAwaiter().GetResult();

// Create some more variable data over the rest of time
App.PushToConsumers(
App.CreateScenarioConfig<ChedASimpleMatchScenarioGenerator>(10, 7, arrivalDateRange: 10))
.GetAwaiter().GetResult();

App.PushToConsumers(App.CreateScenarioConfig<ChedANoMatchScenarioGenerator>(5, 3, arrivalDateRange: 10))
.GetAwaiter().GetResult();

App.PushToConsumers(App.CreateScenarioConfig<ChedPSimpleMatchScenarioGenerator>(1, 3, arrivalDateRange: 10))
.GetAwaiter().GetResult();

App.PushToConsumers(App.CreateScenarioConfig<CrNoMatchScenarioGenerator>(1, 3, arrivalDateRange: 10))
.GetAwaiter().GetResult();
}
}

public void Dispose()
{
// ... clean up test data from the database ...
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Xunit;

namespace Btms.Analytics.Tests;
namespace Btms.Analytics.Tests.Fixtures;

[CollectionDefinition("Aggregation Test collection")]
public class AggregationTestCollection : ICollectionFixture<AggregationTestFixture>
[CollectionDefinition(nameof(MultiItemDataTestCollection))]
public class MultiItemDataTestCollection : ICollectionFixture<MultiItemDataTestFixture>
{
// This class has no code, and is never created. Its purpose is simply
// to be the place to apply [CollectionDefinition] and all the
Expand Down
49 changes: 49 additions & 0 deletions Btms.Analytics.Tests/Fixtures/MultiItemDataTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Btms.Analytics.Tests.Helpers;
using Btms.Backend.Data;
using Microsoft.Extensions.DependencyInjection;
using TestDataGenerator.Scenarios;

namespace Btms.Analytics.Tests.Fixtures;

#pragma warning disable S3881
public class MultiItemDataTestFixture : IDisposable
#pragma warning restore S3881
{
public readonly IImportNotificationsAggregationService ImportNotificationsAggregationService;
public readonly IMovementsAggregationService MovementsAggregationService;

public IMongoDbContext MongoDbContext;
public MultiItemDataTestFixture()
{
var builder = TestContextHelper.CreateBuilder<MultiItemDataTestFixture>();

var app = builder.Build();
var rootScope = app.Services.CreateScope();

MongoDbContext = rootScope.ServiceProvider.GetRequiredService<IMongoDbContext>();
ImportNotificationsAggregationService = rootScope.ServiceProvider.GetRequiredService<IImportNotificationsAggregationService>();
MovementsAggregationService = rootScope.ServiceProvider.GetRequiredService<IMovementsAggregationService>();

// Would like to pick this up from env/config/DB state
var insertToMongo = true;

if (insertToMongo)
{
MongoDbContext.ResetCollections().GetAwaiter().GetResult();

app.PushToConsumers(app.CreateScenarioConfig<ChedAManyCommoditiesScenarioGenerator>(10, 3, arrivalDateRange: 0))
.GetAwaiter().GetResult();

app.PushToConsumers(app.CreateScenarioConfig<CrNoMatchScenarioGenerator>(10, 3, arrivalDateRange: 0))
.GetAwaiter().GetResult();

app.PushToConsumers(app.CreateScenarioConfig<ChedASimpleMatchScenarioGenerator>(10, 3, arrivalDateRange: 0))
.GetAwaiter().GetResult();
}
}

public void Dispose()
{
// ... clean up test data from the database ...
}
}
26 changes: 26 additions & 0 deletions Btms.Analytics.Tests/Helpers/MultiSeriesDatasetAssertions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Btms.Common.Extensions;
using FluentAssertions;
using FluentAssertions.Collections;

namespace Btms.Analytics.Tests.Helpers;

public class MultiSeriesDatasetAssertions(List<MultiSeriesDataset>? test)
: GenericCollectionAssertions<MultiSeriesDataset>(test)
{
[CustomAssertion]
public void BeSameLength(string because = "", params object[] becauseArgs)
{
test!.Select(r => r.Results.Count)
.Distinct()
.Count()
.Should()
.Be(1);
}

[CustomAssertion]
public void HaveResults(string because = "", params object[] becauseArgs)
{
test!.Sum(d => d.Results.Sum(r => r.Value))
.Should().BeGreaterThan(0);
}
}
16 changes: 16 additions & 0 deletions Btms.Analytics.Tests/Helpers/SingleSeriesDatasetAssertions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Btms.Common.Extensions;
using FluentAssertions;
using FluentAssertions.Collections;

namespace Btms.Analytics.Tests.Helpers;

public class SingleSeriesDatasetAssertions(SingeSeriesDataset? test)
{
[CustomAssertion]
public void HaveResults(string because = "", params object[] becauseArgs)
{
test!.Values
.Values.Sum()
.Should().BeGreaterThan(0);
}
}
13 changes: 13 additions & 0 deletions Btms.Analytics.Tests/Helpers/TestAssertionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Btms.Analytics.Tests.Helpers;

public static class TestAssertionExtensions
{
public static MultiSeriesDatasetAssertions Should(this List<MultiSeriesDataset>? instance)
{
return new MultiSeriesDatasetAssertions(instance);
}
public static SingleSeriesDatasetAssertions Should(this SingeSeriesDataset? instance)
{
return new SingleSeriesDatasetAssertions(instance);
}
}
3 changes: 3 additions & 0 deletions Btms.Analytics.Tests/Helpers/TestContextHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using TestDataGenerator.Helpers;
using Xunit;
using Xunit.Abstractions;

[assembly: CollectionBehavior(DisableTestParallelization = true)]

namespace Btms.Analytics.Tests.Helpers;

public static class TestContextHelper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
using Xunit;
using Xunit.Abstractions;

using Btms.Analytics.Tests.Fixtures;

namespace Btms.Analytics.Tests;

[Collection("Aggregation Test collection")]
public class GetImportNotificationsByArrivalDateTests(
AggregationTestFixture aggregationTestFixture,
[Collection(nameof(BasicSampleDataTestCollection))]
public class ImportNotificationsByArrivalDateTests(
BasicSampleDataTestFixture basicSampleDataTestFixture,
ITestOutputHelper testOutputHelper)
{

Expand All @@ -16,7 +18,7 @@ public async Task WhenCalledNextMonth_ReturnExpectedAggregation()
{
testOutputHelper.WriteLine("Querying for aggregated data");

var result = (await aggregationTestFixture.ImportNotificationsAggregationService
var result = (await basicSampleDataTestFixture.ImportNotificationsAggregationService
.ByArrival(DateTime.Today, DateTime.Today.MonthLater()))
.ToList();

Expand Down
40 changes: 40 additions & 0 deletions Btms.Analytics.Tests/ImportNotificationsByCommoditiesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Btms.Analytics.Extensions;
using Btms.Common.Extensions;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
using Btms.Analytics.Tests.Helpers;
using Btms.Analytics.Tests.Fixtures;

namespace Btms.Analytics.Tests;

[Collection(nameof(MultiItemDataTestCollection))]
public class ImportNotificationsByCommoditiesTests(
MultiItemDataTestFixture multiItemDataTestFixture,
ITestOutputHelper testOutputHelper)
{

[Fact]
public async Task WhenCalledLastWeek_ReturnExpectedAggregation()
{
testOutputHelper.WriteLine("Querying for aggregated data");
var result = (await multiItemDataTestFixture.ImportNotificationsAggregationService
.ByCommodityCount(DateTime.Today.WeekAgo(), DateTime.Today.Tomorrow()))
.ToList();;

testOutputHelper.WriteLine("{0} aggregated items found", result.Count);

result.Count().Should().Be(8);
result.Select(r => r.Name).Order().Should().Equal(AnalyticsHelpers.GetImportNotificationSegments().Order());

result.Should().AllSatisfy(r =>
{
r.Dimension.Should().Be("ItemCount");
r.Results.Count().Should().NotBe(0);
});

result.Should().HaveResults();

result.Should().BeSameLength();
}
}
Loading

0 comments on commit 84e584c

Please sign in to comment.