From f4750776e6c5eee2a85821f37ef4145254c71bbf Mon Sep 17 00:00:00 2001 From: mikestock-nimble Date: Mon, 22 Jan 2024 09:44:03 +0000 Subject: [PATCH 1/2] 153650: fixed issue where trusts were missing establishments logic for ordering by group uid was wrong, because group uid is a string converted the string to a number, so the ordering works correctly added utility method for converting strings to numbers to prove all scenarios would work --- .../Extensions/StringExtensionsTests.cs | 25 +++++++++++++++++++ .../Integration/TrustsV3IntegrationTests.cs | 16 ++++++------ TramsDataApi/Extensions/StringExtensions.cs | 10 ++++++++ TramsDataApi/Gateways/TrustGateway.cs | 16 ++++++++---- 4 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 TramsDataApi.Test/Extensions/StringExtensionsTests.cs create mode 100644 TramsDataApi/Extensions/StringExtensions.cs diff --git a/TramsDataApi.Test/Extensions/StringExtensionsTests.cs b/TramsDataApi.Test/Extensions/StringExtensionsTests.cs new file mode 100644 index 000000000..5913d8f96 --- /dev/null +++ b/TramsDataApi.Test/Extensions/StringExtensionsTests.cs @@ -0,0 +1,25 @@ +using TramsDataApi.Extensions; +using Xunit; + +namespace TramsDataApi.Test.Extensions +{ + public class StringExtensionsTests + { + [Theory] + [InlineData("123", 123)] + [InlineData("456", 456)] + [InlineData("0", 0)] + [InlineData("-789", -789)] + [InlineData("abc", 0)] // Invalid string + [InlineData(null, 0)] // Null string + [InlineData("", 0)] // Empty string + public void ToInt_ReturnsExpectedResult(string input, int expectedResult) + { + // Act + int result = input.ToInt(); + + // Assert + Assert.Equal(expectedResult, result); + } + } +} diff --git a/TramsDataApi.Test/Integration/TrustsV3IntegrationTests.cs b/TramsDataApi.Test/Integration/TrustsV3IntegrationTests.cs index 3cdd570bd..2fd07c315 100644 --- a/TramsDataApi.Test/Integration/TrustsV3IntegrationTests.cs +++ b/TramsDataApi.Test/Integration/TrustsV3IntegrationTests.cs @@ -197,7 +197,7 @@ public async Task ShouldReturnEstablishmentDataAgainstOpenTrust_WhenTrustHasAnEs string TrustUKPRN = "123456789"; var closedTrustGroup = _fixture.Build() - .With(f => f.GroupUid, "1") + .With(f => f.GroupUid, "234") .With(f => f.GroupId, groupID) .With(f=> f.GroupName, TrustName) .With(f => f.Ukprn, TrustUKPRN) @@ -208,14 +208,14 @@ public async Task ShouldReturnEstablishmentDataAgainstOpenTrust_WhenTrustHasAnEs .Create(); var openTrustGroup = _fixture.Build() - .With(f => f.GroupUid, "2") + .With(f => f.GroupUid, "1234") .With(f => f.GroupId, groupID) - .With(f => f.GroupName, TrustName) - .With(f => f.Ukprn, TrustUKPRN) - .With(f => f.GroupStatus, "Open") - .With(f => f.GroupStatusCode, "OPEN") - .With(f => f.GroupType, "Multi-academy trust") - .Create(); + .With(f => f.GroupName, TrustName) + .With(f => f.Ukprn, TrustUKPRN) + .With(f => f.GroupStatus, "Open") + .With(f => f.GroupStatusCode, "OPEN") + .With(f => f.GroupType, "Multi-academy trust") + .Create(); _legacyDbContext.Group.AddRange(closedTrustGroup, openTrustGroup); diff --git a/TramsDataApi/Extensions/StringExtensions.cs b/TramsDataApi/Extensions/StringExtensions.cs new file mode 100644 index 000000000..1f53092a0 --- /dev/null +++ b/TramsDataApi/Extensions/StringExtensions.cs @@ -0,0 +1,10 @@ +namespace TramsDataApi.Extensions +{ + public static class StringExtensions + { + public static int ToInt(this string value) + { + return int.TryParse(value, out var result) ? result : 0; + } + } +} diff --git a/TramsDataApi/Gateways/TrustGateway.cs b/TramsDataApi/Gateways/TrustGateway.cs index 72a567285..5e955c94f 100644 --- a/TramsDataApi/Gateways/TrustGateway.cs +++ b/TramsDataApi/Gateways/TrustGateway.cs @@ -1,14 +1,13 @@ +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; -using System.Linq.Expressions; -using Microsoft.EntityFrameworkCore; using TramsDataApi.DatabaseModels; using TramsDataApi.Extensions; namespace TramsDataApi.Gateways { - public class TrustGateway : ITrustGateway + public class TrustGateway : ITrustGateway { private readonly LegacyTramsDbContext _dbContext; @@ -24,7 +23,11 @@ public Group GetGroupByUkPrn(string ukPrn) public Group GetLatestGroupByUkPrn(string ukPrn) { - return _dbContext.Group.OrderByDescending(f=> f.GroupUid).FirstOrDefault(g => g.Ukprn == ukPrn); + var group = _dbContext.Group.Where(g => g.Ukprn == ukPrn).ToList(); + + var result = group.OrderByDescending(g => g.GroupUid.ToInt()).FirstOrDefault(); + + return result; } public Trust GetIfdTrustByGroupId(string groupId) @@ -90,6 +93,9 @@ public TrustMasterData GetMstrTrustByGroupId(string groupId) return _dbContext.TrustMasterData.FirstOrDefault(t => t.GroupID == groupId); } - + private static int ToInt(string value) + { + return int.TryParse(value, out var parsed) ? parsed : 0; + } } } From 6a0d2077e25bcc72a16483d865ff535feae4bc20 Mon Sep 17 00:00:00 2001 From: mikestock-nimble Date: Mon, 22 Jan 2024 09:47:33 +0000 Subject: [PATCH 2/2] removed unused function --- TramsDataApi/Gateways/TrustGateway.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/TramsDataApi/Gateways/TrustGateway.cs b/TramsDataApi/Gateways/TrustGateway.cs index 5e955c94f..6af82b4e0 100644 --- a/TramsDataApi/Gateways/TrustGateway.cs +++ b/TramsDataApi/Gateways/TrustGateway.cs @@ -92,10 +92,5 @@ public TrustMasterData GetMstrTrustByGroupId(string groupId) { return _dbContext.TrustMasterData.FirstOrDefault(t => t.GroupID == groupId); } - - private static int ToInt(string value) - { - return int.TryParse(value, out var parsed) ? parsed : 0; - } } }