From 03a84e27886c3885f8eff5afaa22d81ff41e3b76 Mon Sep 17 00:00:00 2001 From: Nick Warms Date: Wed, 27 Mar 2024 10:30:40 +0000 Subject: [PATCH] Update record constructors for TrustDataSet and EstablishmentDataSet --- .../IdentifiersControllerTests.cs | 135 ++++++++---------- 1 file changed, 62 insertions(+), 73 deletions(-) diff --git a/Dfe.Identifiers.Api.Test/IdentifiersControllerTests.cs b/Dfe.Identifiers.Api.Test/IdentifiersControllerTests.cs index b7e190d..01e0a0e 100644 --- a/Dfe.Identifiers.Api.Test/IdentifiersControllerTests.cs +++ b/Dfe.Identifiers.Api.Test/IdentifiersControllerTests.cs @@ -83,7 +83,7 @@ public async Task Get_TrustIdentifiers_AndDuplicateTrustsExists_Returns_Ok(Trust }; var cancellationToken = new CancellationToken(); - var response = await Sut.GetIdentifiers(identifier!, cancellationToken); + var response = await Sut.GetIdentifiers(identifier, cancellationToken); response.Result.Should().NotBeNull(); response.Result.Should().BeOfType(typeof(OkObjectResult)); @@ -105,7 +105,7 @@ public async Task Get_TrustIdentifiers_AndDuplicateTrustsExists_Returns_Ok(Trust public async Task Get_TrustIdentifiers_AndNoOtherIdentifiersExist_Returns_Ok(TrustIdTypes trustIdType) { using var context = Fixture.GetMstrContext(); - + var trustData = await BuildTrustSetWithEmptyData(context, trustIdType); var selectedTrust = trustData.First(); var identifier = trustIdType switch @@ -115,7 +115,7 @@ public async Task Get_TrustIdentifiers_AndNoOtherIdentifiersExist_Returns_Ok(Tru TrustIdTypes.GroupUID => selectedTrust.GroupUID, _ => throw new ArgumentOutOfRangeException(nameof(trustIdType), trustIdType, null) }; - + var cancellationToken = new CancellationToken(); var response = await Sut.GetIdentifiers(identifier!, cancellationToken); @@ -129,14 +129,14 @@ public async Task Get_TrustIdentifiers_AndNoOtherIdentifiersExist_Returns_Ok(Tru trusts.Length.Should().Be(1); AssertTrustIdentifierResponse(trusts.First(), selectedTrust); } - + [Fact] public async Task Get_TrustIdentifiers_AndTrustDoesNotExist_Returns_EmptyList() { using var context = Fixture.GetMstrContext(); - + await BuildTrustSet(context); - + var cancellationToken = new CancellationToken(); var response = await Sut.GetIdentifiers("NoTrustExists", cancellationToken); @@ -149,9 +149,9 @@ public async Task Get_TrustIdentifiers_AndTrustDoesNotExist_Returns_EmptyList() var trusts = content!.Trusts; trusts.Length.Should().Be(0); } - + // ESTABLISHMENTS - + [Theory] [InlineData(EstablishmentsIdTypes.URN)] [InlineData(EstablishmentsIdTypes.UKPRN)] @@ -159,18 +159,19 @@ public async Task Get_TrustIdentifiers_AndTrustDoesNotExist_Returns_EmptyList() public async Task Get_EstablishmentIdentifiers_AndEstablishmentExists_Returns_Ok(EstablishmentsIdTypes idType) { using var context = Fixture.GetMstrContext(); - + var trustData = CreateEstablishmentSet(context); - + var selectedEstablishment = trustData.Establishments.First().Establishment; var identifier = idType switch { EstablishmentsIdTypes.URN => $"{selectedEstablishment.URN}", EstablishmentsIdTypes.UKPRN => selectedEstablishment.UKPRN, - EstablishmentsIdTypes.LAESTAB => $"{selectedEstablishment.LocalAuthority.Code}%2F{selectedEstablishment.EstablishmentNumber}", + EstablishmentsIdTypes.LAESTAB => + $"{selectedEstablishment.LocalAuthority.Code}%2F{selectedEstablishment.EstablishmentNumber}", _ => throw new ArgumentOutOfRangeException(nameof(idType), idType, null) }; - + var cancellationToken = new CancellationToken(); var response = await Sut.GetIdentifiers(identifier!, cancellationToken); @@ -184,18 +185,18 @@ public async Task Get_EstablishmentIdentifiers_AndEstablishmentExists_Returns_Ok establishments.Length.Should().Be(1); AssertEstablishmentsIdentifierResponse(establishments.First(), selectedEstablishment); } - + // Mixed [Fact] public async Task Get_Identifiers_AndEstablishmentAndTrustExists_Returns_Ok() { using var context = Fixture.GetMstrContext(); - + var mixedData = CreateSameUKPRNDataSet(context); - + var selectedEstablishment = mixedData.Establishments.First().Establishment; var selectedTrust = mixedData.Trust; - + var cancellationToken = new CancellationToken(); var response = await Sut.GetIdentifiers(MixedSameUkprn, cancellationToken); @@ -250,12 +251,14 @@ private static async Task> BuildDuplicateTrustSet(MstrContext contex trusts.Add(trust); } + context.Trusts.AddRange(trusts); await context.SaveChangesAsync(); return trusts; } - - private static async Task> BuildTrustSetWithEmptyData(MstrContext context, TrustIdTypes trustIdTypeToKeep) + + private static async Task> BuildTrustSetWithEmptyData(MstrContext context, + TrustIdTypes trustIdTypeToKeep) { var trusts = await BuildTrustSet(context); foreach (var trust in trusts) @@ -276,6 +279,7 @@ private static async Task> BuildTrustSetWithEmptyData(MstrContext co throw new ArgumentOutOfRangeException(nameof(trustIdTypeToKeep), trustIdTypeToKeep, null); } } + context.Trusts.UpdateRange(trusts); await context.SaveChangesAsync(); return trusts; @@ -286,43 +290,39 @@ private static TrustDataSet CreateEstablishmentSet(MstrContext context) var trust = DatabaseModelBuilder.BuildTrust(); context.Add(trust); context.SaveChanges(); - + var establishments = new List(); - + for (var idx = 0; idx < 3; idx++) { var localAuthority = context.LocalAuthorities.First(la => la.SK % 3 == idx); var establishmentDataSet = CreateEstablishment(localAuthority); - + context.Establishments.Add(establishmentDataSet.Establishment); context.IfdPipelines.Add(establishmentDataSet.IfdPipeline); - + establishments.Add(establishmentDataSet); } - + context.SaveChanges(); - + var trustToEstablishmentLinks = LinkTrustToEstablishments(trust, establishments.Select(d => d.Establishment).ToList()); - + context.EducationEstablishmentTrusts.AddRange(trustToEstablishmentLinks); - + context.SaveChanges(); - - var result = new TrustDataSet() - { - Trust = trust, - Establishments = establishments - }; - + + var result = new TrustDataSet(Trust: trust, Establishments: establishments); + return result; } - + private static List LinkTrustToEstablishments(Trust trust, List establishments) { var result = new List(); - + establishments.ForEach(establishment => { var educationEstablishmentTrust = new EducationEstablishmentTrust() @@ -330,65 +330,56 @@ private static List LinkTrustToEstablishments(Trust TrustId = (int)trust.SK, EducationEstablishmentId = (int)establishment.SK }; - + result.Add(educationEstablishmentTrust); }); - + return result; } - + private static TrustDataSet CreateSameUKPRNDataSet(MstrContext context) { var trust = DatabaseModelBuilder.BuildTrust(); trust.UKPRN = MixedSameUkprn; context.Add(trust); context.SaveChanges(); - + //Establishment var establishments = new List(); - + var establishmentDataSet = CreateEstablishment(context.LocalAuthorities.First()); establishmentDataSet.Establishment.UKPRN = MixedSameUkprn; - + context.Establishments.Add(establishmentDataSet.Establishment); context.IfdPipelines.Add(establishmentDataSet.IfdPipeline); - + establishments.Add(establishmentDataSet); - + context.SaveChanges(); - + var trustToEstablishmentLinks = LinkTrustToEstablishments(trust, establishments.Select(d => d.Establishment).ToList()); - + context.EducationEstablishmentTrusts.AddRange(trustToEstablishmentLinks); - + context.SaveChanges(); - - var result = new TrustDataSet() - { - Trust = trust, - Establishments = establishments - }; - + + var result = new TrustDataSet(Trust: trust, Establishments: establishments); + return result; } - + private static EstablishmentDataSet CreateEstablishment(LocalAuthority la) { var establishment = DatabaseModelBuilder.BuildEstablishment(); var ifdPipeline = DatabaseModelBuilder.BuildIfdPipeline(); - + establishment.LocalAuthority = la; ifdPipeline.GeneralDetailsUrn = establishment.PK_GIAS_URN; - - return new EstablishmentDataSet() - { - Establishment = establishment, - IfdPipeline = ifdPipeline - }; - - + + return new EstablishmentDataSet(Establishment: establishment, IfdPipeline: ifdPipeline); } + private static void AssertTrustIdentifierResponse(TrustIdentifiers actual, Trust expected) { actual.TrustReference.Should().Be(expected.GroupID); @@ -417,15 +408,13 @@ public enum EstablishmentsIdTypes LAESTAB } - private record TrustDataSet - { - public Trust Trust { get; set; } - public List Establishments { get; set; } - } + private record TrustDataSet( + Trust Trust, + List Establishments + ); - private record EstablishmentDataSet - { - public Establishment Establishment { get; set; } - public IfdPipeline IfdPipeline { get; set; } - } + private record EstablishmentDataSet( + Establishment Establishment, + IfdPipeline IfdPipeline + ); } \ No newline at end of file