Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
Update record constructors for TrustDataSet and EstablishmentDataSet
Browse files Browse the repository at this point in the history
  • Loading branch information
nwarms committed Mar 27, 2024
1 parent 7ead58f commit 03a84e2
Showing 1 changed file with 62 additions and 73 deletions.
135 changes: 62 additions & 73 deletions Dfe.Identifiers.Api.Test/IdentifiersControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -149,28 +149,29 @@ 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)]
[InlineData(EstablishmentsIdTypes.LAESTAB)]
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);

Expand All @@ -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);

Expand Down Expand Up @@ -250,12 +251,14 @@ private static async Task<List<Trust>> BuildDuplicateTrustSet(MstrContext contex

trusts.Add(trust);
}

context.Trusts.AddRange(trusts);
await context.SaveChangesAsync();
return trusts;
}

private static async Task<List<Trust>> BuildTrustSetWithEmptyData(MstrContext context, TrustIdTypes trustIdTypeToKeep)

private static async Task<List<Trust>> BuildTrustSetWithEmptyData(MstrContext context,
TrustIdTypes trustIdTypeToKeep)
{
var trusts = await BuildTrustSet(context);
foreach (var trust in trusts)
Expand All @@ -276,6 +279,7 @@ private static async Task<List<Trust>> BuildTrustSetWithEmptyData(MstrContext co
throw new ArgumentOutOfRangeException(nameof(trustIdTypeToKeep), trustIdTypeToKeep, null);
}
}

context.Trusts.UpdateRange(trusts);
await context.SaveChangesAsync();
return trusts;
Expand All @@ -286,109 +290,96 @@ private static TrustDataSet CreateEstablishmentSet(MstrContext context)
var trust = DatabaseModelBuilder.BuildTrust();
context.Add(trust);
context.SaveChanges();

var establishments = new List<EstablishmentDataSet>();

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<EducationEstablishmentTrust> LinkTrustToEstablishments(Trust trust,
List<Establishment> establishments)
{
var result = new List<EducationEstablishmentTrust>();

establishments.ForEach(establishment =>
{
var educationEstablishmentTrust = new EducationEstablishmentTrust()
{
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<EstablishmentDataSet>();

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);
Expand Down Expand Up @@ -417,15 +408,13 @@ public enum EstablishmentsIdTypes
LAESTAB
}

private record TrustDataSet
{
public Trust Trust { get; set; }
public List<EstablishmentDataSet> Establishments { get; set; }
}
private record TrustDataSet(
Trust Trust,
List<EstablishmentDataSet> Establishments
);

private record EstablishmentDataSet
{
public Establishment Establishment { get; set; }
public IfdPipeline IfdPipeline { get; set; }
}
private record EstablishmentDataSet(
Establishment Establishment,
IfdPipeline IfdPipeline
);
}

0 comments on commit 03a84e2

Please sign in to comment.