Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/CDMS-144-add-automated-t…
Browse files Browse the repository at this point in the history
…est' into feature/CDMS-144-add-automated-test
  • Loading branch information
Lim Sim committed Jan 10, 2025
2 parents 235c722 + 35f444b commit 7cd13cc
Show file tree
Hide file tree
Showing 77 changed files with 2,185 additions and 559 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
using Xunit.Abstractions;
using Btms.Analytics.Tests.Helpers;
using Btms.Analytics.Tests.Fixtures;
using Btms.Model.Extensions;
using TestDataGenerator.Config;
using TestGenerator.IntegrationTesting.Backend;

namespace Btms.Analytics.Tests;

public class ImportNotificationsByCommoditiesTests(ITestOutputHelper output)
: ScenarioDatasetBaseTest(output, Datasets.FunctionalAnalyticsDatasetName)
: ScenarioDatasetBaseTest(output, Datasets.FunctionalAnalyticsDatasetName, reloadData:false)
{

[Fact]
Expand All @@ -24,6 +25,7 @@ public async Task WhenCalledLastWeek_ReturnExpectedAggregation()
.ToList();

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

result.Count().Should().Be(8);
result.Select(r => r.Name).Order().Should().Equal(AnalyticsHelpers.GetImportNotificationSegments().Order());
Expand Down
7 changes: 5 additions & 2 deletions Btms.Analytics.Tests/ImportNotificationsByCreatedDateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public async Task WhenCalledLast48Hours_ReturnExpectedAggregation()
result[0].Name.Should().Be("CHEDA Linked");
result[0].Periods[0].Period.Should().BeOnOrBefore(DateTime.Today.Tomorrow());
result[0].Periods.Count.Should().Be(48);

result.Sum(r => r.Periods.Sum(p => p.Value))
.Should().BeGreaterThan(0);
}

[Fact]
Expand All @@ -58,7 +61,7 @@ public async Task WhenCalledLastMonth_ReturnExpectedAggregation()
r.Periods.Count.Should().Be(DateTime.Today.DaysSinceMonthAgo() + 1);
});

result[0].Periods.Sum(p => p.Value)
result.Sum(r => r.Periods.Sum(p => p.Value))
.Should().BeGreaterThan(0);
}

Expand Down Expand Up @@ -90,7 +93,7 @@ public async Task WhenCalledWithTimePeriodYieldingNoResults_ReturnEmptyAggregati
r.Periods.Count.Should().Be(24);
});

result[0].Periods.Sum(p => p.Value)
result.Sum(r => r.Periods.Sum(p => p.Value))
.Should().Be(0);
}
}
5 changes: 3 additions & 2 deletions Btms.Analytics/Extensions/AnalyticsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static string MetricsKey(this DatetimeSeries ds)

public static Dictionary<string, BsonDocument> GetAggregatedRecordsDictionary<T>(
this IMongoCollectionSet<T> collection,
ILogger logger,
FilterDefinition<T> filter,
ProjectionDefinition<T> projection,
ProjectionDefinition<BsonDocument> group,
Expand All @@ -52,7 +53,7 @@ public static Dictionary<string, BsonDocument> GetAggregatedRecordsDictionary<T>
.Project(projection)
.Group(group)
.Group(datasetGroup)
.ToList()
.Execute(logger)
.ToDictionary(createDatasetName, b => b);
}

Expand Down Expand Up @@ -157,7 +158,7 @@ internal static IEnumerable<TSource> Execute<TSource>(this IAggregateFluent<TSou
/// <returns></returns>
/// <exception cref="AnalyticsException"></exception>
public static Dictionary<TKey, TElement> ExecuteAsDictionary<TSource, TKey, TElement>(
this IEnumerable<TSource> source,
this IQueryable<TSource> source,
ILogger logger,
Func<TSource, TKey> keySelector,
Func<TSource, TElement> elementSelector)
Expand Down
21 changes: 21 additions & 0 deletions Btms.Analytics/Extensions/ImportNotificationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Btms.Model;
using Btms.Model.Ipaffs;

namespace Btms.Analytics.Extensions;

public static class ImportNotificationExtensions
{
public static IQueryable<ImportNotification> WhereFilteredByCreatedDateAndParams(this IQueryable<ImportNotification> source, DateTime from, DateTime to,
ImportNotificationTypeEnum[]? chedTypes = null, string? country = null)
{
return source
.Where(n => (n.CreatedSource >= from && n.CreatedSource < to)
&& (country == null || n.CommoditiesSummary!.CountryOfOrigin! == country)
&& (
chedTypes == null ||
chedTypes!.Length == 0 || n.ImportNotificationType == null ||
chedTypes!.Contains(n.ImportNotificationType!.Value)
)
);
}
}
7 changes: 2 additions & 5 deletions Btms.Analytics/ImportNotificationsAggregationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ public Task<SingleSeriesDataset> ByMaxVersion(DateTime from, DateTime to, Import
{
var data = context
.Notifications
.Where(n => (n.CreatedSource >= from && n.CreatedSource < to)
&& (country == null || n.CommoditiesSummary!.CountryOfOrigin! == country)
&& (chedTypes == null || chedTypes!.Length == 0 || n.ImportNotificationType == null || chedTypes!.Contains(n.ImportNotificationType!.Value))
)
.WhereFilteredByCreatedDateAndParams(from, to, chedTypes, country)
.GroupBy(n => new { MaxVersion =
n.AuditEntries.Where(a => a.CreatedBy == "Ipaffs").Max(a => a.Version )
})
Expand All @@ -149,7 +146,7 @@ private Task<MultiSeriesDatetimeDataset> AggregateByLinkedAndNotificationType(Da

var mongoResult = context
.Notifications
.GetAggregatedRecordsDictionary(filter, projection, group, datasetGroup, createDatasetName);
.GetAggregatedRecordsDictionary(logger, filter, projection, group, datasetGroup, createDatasetName);

var output = AnalyticsHelpers.GetImportNotificationSegments()
.Select(title => mongoResult.AsDataset(dateRange, title))
Expand Down
11 changes: 6 additions & 5 deletions Btms.Analytics/MovementExceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ public class MovementExceptions(IMongoDbContext context, ILogger logger)
ItemCount = m.Items.Count,
ChedTypes = m.BtmsStatus.ChedTypes,
Status = m.BtmsStatus,
DecisionMatched = !m.AlvsDecisionStatus.Decisions
.OrderBy(d => d.Context.AlvsDecisionNumber)
.Reverse()
.First()
.Context.DecisionMatched,
DecisionMatched = m.AlvsDecisionStatus.Context.DecisionComparison!.DecisionMatched,
// DecisionMatched = !m.AlvsDecisionStatus.Decisions
// .OrderBy(d => d.Context.AlvsDecisionNumber)
// .Reverse()
// .First()
// .Context.DecisionMatched,
HasNotificationRelationships = m.Relationships.Notifications.Data.Count > 0,
ContiguousAlvsClearanceRequestVersionsFrom1 =
m.ClearanceRequests.Select(c => c.Header!.EntryVersionNumber)
Expand Down
7 changes: 4 additions & 3 deletions Btms.Analytics/MovementsAggregationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private Task<MultiSeriesDatetimeDataset> Aggregate(DateTime[] dateRange, Func<Bs

var mongoResult = context
.Movements
.GetAggregatedRecordsDictionary(filter, projection, group, datasetGroup, createDatasetName);
.GetAggregatedRecordsDictionary(logger, filter, projection, group, datasetGroup, createDatasetName);

var output = AnalyticsHelpers.GetMovementStatusSegments()
.Select(title => mongoResult.AsDataset(dateRange, title))
Expand All @@ -286,10 +286,11 @@ public Task<SummarisedDataset<SingleSeriesDataset, StringBucketDimensionResult>>
.WhereFilteredByCreatedDateAndParams(from, to, chedTypes, country)
.SelectMany(m => m.AlvsDecisionStatus.Decisions.Select(
d => new {Decision = d, Movement = m } ))
.SelectMany(d => d.Decision.Context.Checks.Select(c => new { d.Decision, d.Movement, Check = c}))
.SelectMany(d => d.Decision.Context.DecisionComparison!.Checks.Select(c => new { d.Decision, d.Movement, Check = c}))
// .SelectMany(d => d.Decision.Context.Checks.Select(c => new { d.Decision, d.Movement, Check = c}))
.GroupBy(d => new
{
d.Decision.Context.DecisionStatus,
d.Decision.Context.DecisionComparison!.DecisionStatus,
d.Check.CheckCode,
d.Check.AlvsDecisionCode,
d.Check.BtmsDecisionCode
Expand Down
14 changes: 12 additions & 2 deletions Btms.Backend.Data/Mongo/MongoIndexService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ public Task StartAsync(CancellationToken cancellationToken)
{
return Task.WhenAll(
CreateIndex("MatchReferenceIdx",
Builders<ImportNotification>.IndexKeys.Ascending(m => m._MatchReference), cancellationToken),
Builders<ImportNotification>.IndexKeys.Ascending(n => n._MatchReference), cancellationToken),
CreateIndex("Created",
Builders<ImportNotification>.IndexKeys.Ascending(n => n.Created), cancellationToken),
CreateIndex("CreatedSource",
Builders<ImportNotification>.IndexKeys.Ascending(n => n.CreatedSource), cancellationToken),

CreateIndex("MatchReferenceIdx",
Builders<Movement>.IndexKeys.Ascending(m => m._MatchReferences), cancellationToken));
Builders<Movement>.IndexKeys.Ascending(m => m._MatchReferences), cancellationToken),
CreateIndex("Created",
Builders<Movement>.IndexKeys.Ascending(m => m.Created), cancellationToken),
CreateIndex("CreatedSource",
Builders<Movement>.IndexKeys.Ascending(m => m.CreatedSource), cancellationToken)

);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using FluentAssertions;
using TestDataGenerator.Scenarios;
using TestGenerator.IntegrationTesting.Backend;
using TestGenerator.IntegrationTesting.Backend.Extensions;
using TestGenerator.IntegrationTesting.Backend.Fixtures;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -16,15 +17,32 @@ public class AlvsDecisionNumber1Missing(ITestOutputHelper output)
{

[Fact]
public void ShouldHave2AlvsDecision()
public void AlvsDecisionShouldHaveCorrectChecks()
{
// Assert
var movement = Client.AsJsonApiClient()
.Get("api/movements")
.GetResourceObjects<Movement>()
.Single();

movement
Client
.GetSingleMovement()
.AlvsDecisionStatus.Context.DecisionComparison!.Checks
.Should().BeEquivalentTo([
new {
ItemNumber = 1,
CheckCode = "H222",
AlvsDecisionCode = "H01",
BtmsDecisionCode = "X00"
},
new {
ItemNumber = 1,
CheckCode = "H224",
AlvsDecisionCode = "H01",
BtmsDecisionCode = "X00"
}
]);
}

[Fact]
public void ShouldHave1AlvsDecision()
{
Client
.GetSingleMovement()
.AlvsDecisionStatus
.Decisions
.Count
Expand All @@ -35,13 +53,8 @@ public void ShouldHave2AlvsDecision()
[Fact]
public void ShouldHaveCorrectDecisionNumbers()
{
// Assert
var movement = Client.AsJsonApiClient()
.Get("api/movements")
.GetResourceObjects<Movement>()
.Single();

movement
Client
.GetSingleMovement()
.AlvsDecisionStatus
.Decisions
.Select(d => d.Context.AlvsDecisionNumber)
Expand All @@ -52,17 +65,12 @@ public void ShouldHaveCorrectDecisionNumbers()
[Fact]
public void ShouldHaveVersionNotCompleteDecisionStatus()
{

// Assert
var movement = Client.AsJsonApiClient()
.Get("api/movements")
.GetResourceObjects<Movement>()
.Single();

movement
Client
.GetSingleMovement()
.AlvsDecisionStatus
.Context.DecisionComparison!
.DecisionStatus
.Should()
.Be(DecisionStatusEnum.AlvsDecisionVersion1NotPresent);
.Be(DecisionStatusEnum.NoImportNotificationsLinked);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void SimpleChedPScenario_ShouldBeLinkedAndMatchDecision()

movement.AlvsDecisionStatus.Decisions
.First()
.Context.DecisionMatched
.Context.DecisionComparison!.DecisionMatched
.Should().BeTrue();

var decisionWithLinkAndContext = movement.AuditEntries
Expand Down
Loading

0 comments on commit 7cd13cc

Please sign in to comment.