Skip to content

Commit

Permalink
Starting to implement 24 hour line charts (#46)
Browse files Browse the repository at this point in the history
* Starting to implement 24 hour line charts

Generating series data by hour. Randomises test data creation times

Added test collection with a shared context between tests

Undoing merge

* Fixed failing tests

* Tidying up tests

* Adds final tests for movement analysis
  • Loading branch information
craigedmunds authored Nov 28, 2024
1 parent 8f5411a commit 542c528
Show file tree
Hide file tree
Showing 29 changed files with 845 additions and 274 deletions.
11 changes: 11 additions & 0 deletions Cdms.Analytics.Tests/AggregationTestCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Xunit;

namespace Cdms.Analytics.Tests;

[CollectionDefinition("Aggregation Test collection")]
public class AggregationTestCollection : ICollectionFixture<AggregationTestFixture>
{
// 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.
}
70 changes: 70 additions & 0 deletions Cdms.Analytics.Tests/AggregationTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Cdms.Analytics.Tests.Helpers;
using Cdms.Backend.Data;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using TestDataGenerator.Scenarios;

namespace Cdms.Analytics.Tests;

#pragma warning disable S3881
public class AggregationTestFixture : IDisposable
#pragma warning restore S3881
{
public IHost App;
public ILinkingAggregationService LinkingAggregationService;

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

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

MongoDbContext = rootScope.ServiceProvider.GetRequiredService<IMongoDbContext>();
LinkingAggregationService = rootScope.ServiceProvider.GetRequiredService<ILinkingAggregationService>();

MongoDbContext.DropCollections().GetAwaiter().GetResult();

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

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

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

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

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

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

// SetupData().GetAwaiter().GetResult();
}

// public async Task SetupData()
// {
// await MongoDbContext.DropCollections();
//
// var scenario = App.CreateScenarioConfig<ChedASimpleMatchScenarioGenerator>(10, 3, arrivalDateRange: 10);
// await App.PushToConsumers(scenario, 1);
//
// var noMatchScenario = App.CreateScenarioConfig<ChedANoMatchScenarioGenerator>(5, 3, arrivalDateRange: 10);
// await App.PushToConsumers(noMatchScenario, 2);
//
// var chedPScenario = App.CreateScenarioConfig<ChedPSimpleMatchScenarioGenerator>(1, 3, arrivalDateRange: 10);
// await App.PushToConsumers(chedPScenario, 3);
// }

public void Dispose()
{
// ... clean up test data from the database ...
}
}
73 changes: 18 additions & 55 deletions Cdms.Analytics.Tests/GetImportNotificationsByArrivalDateTests.cs
Original file line number Diff line number Diff line change
@@ -1,78 +1,41 @@
using Cdms.Analytics.Tests.Helpers;
using Cdms.Backend.Data;
using Cdms.Backend.Data.Extensions;
using Cdms.Business.Extensions;
using Cdms.Consumers.Extensions;
using Cdms.Model.Extensions;
using Cdms.SyncJob.Extensions;
using Cdms.Common.Extensions;
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using TestDataGenerator.Helpers;
using TestDataGenerator.Scenarios;
using Xunit;
using Xunit.Abstractions;

namespace Cdms.Analytics.Tests;

public class GetImportNotificationsByArrivalDateTests
[Collection("Aggregation Test collection")]
public class GetImportNotificationsByArrivalDateTests(
AggregationTestFixture aggregationTestFixture,
ITestOutputHelper testOutputHelper)
{
private readonly IHost app;

private readonly ILogger<GetImportNotificationsByArrivalDateTests> logger;

private readonly IMatchingAggregationService svc;

private readonly IMongoDbContext mongoContext;

public GetImportNotificationsByArrivalDateTests(ITestOutputHelper testOutputHelper)
{
var builder = TestContextHelper.CreateBuilder<GetImportNotificationsByArrivalDateTests>(testOutputHelper);

app = builder.Build();
var rootScope = app.Services.CreateScope();
logger = app.Services.GetRequiredService<ILogger<GetImportNotificationsByArrivalDateTests>>();
svc = rootScope.ServiceProvider.GetRequiredService<IMatchingAggregationService>();
mongoContext = rootScope.ServiceProvider.GetRequiredService<IMongoDbContext>();
}

[Fact]
public async Task WhenCalled_ReturnExpectedAggregation()
public async Task WhenCalledNextMonth_ReturnExpectedAggregation()
{
await mongoContext.DropCollections();

var scenario = app.CreateScenarioConfig<ChedASimpleMatchScenarioGenerator>(10, 3, arrivalDateRange: 10);
await app.PushToConsumers(scenario, 1);

var noMatchScenario = app.CreateScenarioConfig<ChedANoMatchScenarioGenerator>(5, 3, arrivalDateRange: 10);
await app.PushToConsumers(noMatchScenario, 2);
testOutputHelper.WriteLine("Querying for aggregated data");

var chedPScenario = app.CreateScenarioConfig<ChedPSimpleMatchScenarioGenerator>(1, 3, arrivalDateRange: 10);
await app.PushToConsumers(chedPScenario, 3);

logger.LogInformation("Querying for aggregated data");
var result = (await svc
.GetImportNotificationLinkingByArrival())
var result = (await aggregationTestFixture.LinkingAggregationService
.GetImportNotificationLinkingByArrival(DateTime.Today, DateTime.Today.MonthLater()))
.ToList();

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

logger.LogInformation("{count} aggregated items found", result.Count);

// logger.LogInformation(result.ToJsonString());
//// logger.LogInformation(result.ToJsonString());

result.Count.Should().Be(3);

result[0].Name.Should().Be("Cveda Linked");
result[0].Dates[0].Date.Should().BeOnOrAfter(DateOnly.FromDateTime(DateTime.Today));
result[0].Dates.Count.Should().Be(30);
result[0].Periods[0].Period.Should().BeOnOrAfter(DateTime.Today);
result[0].Periods.Count.Should().Be(DateTime.Today.DaysUntilMonthLater());

result[1].Name.Should().Be("Cveda Not Linked");
result[1].Dates[0].Date.Should().BeOnOrAfter(DateOnly.FromDateTime(DateTime.Today));
result[1].Dates.Count.Should().Be(30);
result[1].Periods[0].Period.Should().BeOnOrAfter(DateTime.Today);
result[1].Periods.Count.Should().Be(DateTime.Today.DaysUntilMonthLater());

result[2].Name.Should().Be("Cvedp Linked");
result[2].Dates[0].Date.Should().BeOnOrAfter(DateOnly.FromDateTime(DateTime.Today));
result[2].Dates.Count.Should().Be(30);
result[2].Periods[0].Period.Should().BeOnOrAfter(DateTime.Today);
result[2].Periods.Count.Should().Be(DateTime.Today.DaysUntilMonthLater());
}
}
79 changes: 37 additions & 42 deletions Cdms.Analytics.Tests/GetImportNotificationsByCreatedDateTests.cs
Original file line number Diff line number Diff line change
@@ -1,69 +1,64 @@
using Cdms.Analytics.Tests.Helpers;
using Cdms.Backend.Data;
using Cdms.Common.Extensions;
using Cdms.Model.Extensions;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using TestDataGenerator.Scenarios;
using Xunit;
using Xunit.Abstractions;

namespace Cdms.Analytics.Tests;

public class GetImportNotificationsByCreatedDateTests
[Collection("Aggregation Test collection")]
public class GetImportNotificationsByCreatedDateTests(
AggregationTestFixture aggregationTestFixture,
ITestOutputHelper testOutputHelper)
{
private readonly IHost app;

private readonly ILogger<GetImportNotificationsByCreatedDateTests> logger;

[Fact]
public async Task WhenCalledLast24Hours_ReturnExpectedAggregation()
{
var result = (await aggregationTestFixture.LinkingAggregationService
.GetImportNotificationLinkingByCreated(DateTime.Now.NextHour(), DateTime.Now.NextHour().Tomorrow(),AggregationPeriod.Hour))
.ToList();

private readonly IMatchingAggregationService svc;
testOutputHelper.WriteLine(result.ToJsonString());

private readonly IMongoDbContext mongoContext;
result.Count.Should().Be(3);

public GetImportNotificationsByCreatedDateTests(ITestOutputHelper testOutputHelper)
{
var builder = TestContextHelper.CreateBuilder<GetImportNotificationsByCreatedDateTests>(testOutputHelper);
result[0].Periods.Count(p => p.Value > 0).Should().BeGreaterThan(1);

app = builder.Build();
var rootScope = app.Services.CreateScope();
logger = app.Services.GetRequiredService<ILogger<GetImportNotificationsByCreatedDateTests>>();
svc = rootScope.ServiceProvider.GetRequiredService<IMatchingAggregationService>();
mongoContext = rootScope.ServiceProvider.GetRequiredService<IMongoDbContext>();
result[0].Name.Should().Be("Cveda Linked");
result[0].Periods[0].Period.Should().BeOnOrBefore(DateTime.Today.Tomorrow());
result[0].Periods.Count.Should().Be(24);

result[1].Name.Should().Be("Cveda Not Linked");
result[1].Periods[0].Period.Should().BeOnOrBefore(DateTime.Today.Tomorrow());
result[1].Periods.Count.Should().Be(24);

result[2].Name.Should().Be("Cvedp Linked");
result[2].Periods[0].Period.Should().BeOnOrBefore(DateTime.Today.Tomorrow());
result[2].Periods.Count.Should().Be(24);
}

[Fact]
public async Task WhenCalled_ReturnExpectedAggregation()
public async Task WhenCalledLastMonth_ReturnExpectedAggregation()
{
await mongoContext.DropCollections();

var scenario = app.CreateScenarioConfig<ChedASimpleMatchScenarioGenerator>(10, 2);
await app.PushToConsumers(scenario, 1);

var noMatchScenario = app.CreateScenarioConfig<ChedANoMatchScenarioGenerator>(5, 2);
await app.PushToConsumers(noMatchScenario, 2);

var chedPScenario = app.CreateScenarioConfig<ChedPSimpleMatchScenarioGenerator>(1, 1);
await app.PushToConsumers(chedPScenario, 3);

var result = (await svc
.GetImportNotificationLinkingByCreated())
var result = (await aggregationTestFixture.LinkingAggregationService
.GetImportNotificationLinkingByCreated(DateTime.Today.MonthAgo(), DateTime.Today.Tomorrow()))
.ToList();

logger.LogInformation(result.ToJsonString());
testOutputHelper.WriteLine(result.ToJsonString());

result.Count.Should().Be(3);

result[0].Name.Should().Be("Cveda Linked");
result[0].Dates[0].Date.Should().BeOnOrBefore(DateOnly.FromDateTime(DateTime.Today));
result[0].Dates.Count.Should().Be(30);
result[0].Periods[0].Period.Should().BeOnOrBefore(DateTime.Today);
result[0].Periods.Count.Should().Be(DateTime.Today.DaysSinceMonthAgo() + 1);

result[1].Name.Should().Be("Cveda Not Linked");
result[1].Dates[0].Date.Should().BeOnOrBefore(DateOnly.FromDateTime(DateTime.Today));
result[1].Dates.Count.Should().Be(30);
result[1].Periods[0].Period.Should().BeOnOrBefore(DateTime.Today);
result[1].Periods.Count.Should().Be(DateTime.Today.DaysSinceMonthAgo() + 1);

result[2].Name.Should().Be("Cvedp Linked");
result[2].Dates[0].Date.Should().BeOnOrBefore(DateOnly.FromDateTime(DateTime.Today));
result[2].Dates.Count.Should().Be(30);
result[2].Periods[0].Period.Should().BeOnOrBefore(DateTime.Today);
result[2].Periods.Count.Should().Be(DateTime.Today.DaysSinceMonthAgo() + 1);
}
}
39 changes: 39 additions & 0 deletions Cdms.Analytics.Tests/GetImportNotificationsStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Cdms.Common.Extensions;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

namespace Cdms.Analytics.Tests;

[Collection("Aggregation Test collection")]
public class GetImportNotificationsStatus(
AggregationTestFixture aggregationTestFixture,
ITestOutputHelper testOutputHelper)
{

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

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

result.Values.Count.Should().Be(3);
result.Values.Keys.Order().Should().Equal("Cveda Linked", "Cveda Not Linked", "Cvedp Linked");
}

[Fact]
public async Task WhenCalledLast24Hours_ReturnExpectedAggregation()
{
testOutputHelper.WriteLine("Querying for aggregated data");
var result = (await aggregationTestFixture.LinkingAggregationService
.GetImportNotificationLinkingStatus(DateTime.Now.AddDays(-1), DateTime.Now));

testOutputHelper.WriteLine($"{result.Values.Count} aggregated items found");

result.Values.Count.Should().Be(3);
result.Values.Keys.Order().Should().Equal("Cveda Linked", "Cveda Not Linked", "Cvedp Linked");
}
}
48 changes: 48 additions & 0 deletions Cdms.Analytics.Tests/GetMovementsByArrivalDateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Cdms.Common.Extensions;
using Cdms.Model.Extensions;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

namespace Cdms.Analytics.Tests;

[Collection("Aggregation Test collection")]
public class GetMovementsByArrivalDateTests(
AggregationTestFixture aggregationTestFixture,
ITestOutputHelper testOutputHelper) {

[Fact]
public async Task WhenCalledNext24Hours_ReturnExpectedAggregation()
{

var result = (await aggregationTestFixture.LinkingAggregationService
.GetMovementsLinkingByArrival(DateTime.Now, DateTime.Now.Tomorrow(), AggregationPeriod.Hour))
.ToList();

testOutputHelper.WriteLine(result.ToJsonString());

result.Count.Should().Be(1);

result[0].Name.Should().Be("Linked");
result[0].Periods[0].Period.Should().BeOnOrAfter(DateTime.Today);
result[0].Periods.Count.Should().Be(24);
}

[Fact]
public async Task WhenCalledNextMonth_ReturnExpectedAggregation()
{
var result = (await aggregationTestFixture.LinkingAggregationService
.GetMovementsLinkingByArrival(DateTime.Today, DateTime.Today.MonthLater()))
.ToList();

testOutputHelper.WriteLine(result.ToJsonString());

result.Count.Should().Be(2);

result[0].Name.Should().Be("Linked");
result[0].Periods[0].Period.Should().BeOnOrAfter(DateTime.Today);
result[0].Periods.Count.Should().Be(DateTime.Today.DaysUntilMonthLater());

result[1].Name.Should().Be("Not Linked");
}
}
Loading

0 comments on commit 542c528

Please sign in to comment.