Skip to content

Commit

Permalink
Fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paullocknimble committed Oct 18, 2024
1 parent 18b5377 commit f93e92d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Dfe.Academies.Academisation.IService.Query;
using Dfe.Academies.Academisation.Service.Commands.Legacy.Project;
using Dfe.Academies.Contracts.V4.Establishments;
using Dfe.Academies.Contracts.V4.Trusts;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit;
Expand Down Expand Up @@ -59,6 +60,9 @@ public async Task IncompleteProjects__UnknownSchool__LogAndContinue()
.ReturnsAsync(_fixture.Create<EstablishmentDto>())
.ReturnsAsync(_fixture.Create<EstablishmentDto>());

_establishmentGetDataQuery.Setup(m => m.GetTrustByReferenceNumber(It.IsAny<string>()))
.ReturnsAsync(_fixture.Build<TrustDto>().With(x => x.Ukprn, "101010").Create());

await _subject.Execute();

Assert.Multiple(
Expand All @@ -78,6 +82,9 @@ public async Task IncompleteProjects__KnownSchool__UpdateDb()
_establishmentGetDataQuery.Setup(m => m.GetEstablishment(It.IsAny<int>()))
.ReturnsAsync(_fixture.Create<EstablishmentDto>());

_establishmentGetDataQuery.Setup(m => m.GetTrustByReferenceNumber(It.IsAny<string>()))
.ReturnsAsync(_fixture.Build<TrustDto>().With(x => x.Ukprn, "101010").Create());

await _subject.Execute();

_updateCommand.Verify(m => m.Execute(project), Times.Once);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ internal static ConversionProjectServiceModel MapToServiceModel(this IProject pr
ProjectDatesSectionComplete = project.Details.ProjectDatesSectionComplete,
// if we have sent the project to complete then the project is readonly
IsReadOnly = project.IsReadOnly,
TrustUkprn = project.Details.TrustUkprn

};

return serviceModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
using Dfe.Academies.Academisation.Data.UnitTest.Contexts;
using Dfe.Academies.Academisation.Domain.Core.ProjectAggregate;
using Dfe.Academies.Academisation.Domain.ProjectAggregate;
using Dfe.Academies.Academisation.IService.ServiceModels.Academies;
using Dfe.Academies.Academisation.Service.Commands.Legacy.Project;
using Dfe.Academies.Academisation.Service.Queries;
using Dfe.Academies.Contracts.V4.Establishments;
using Dfe.Academies.Contracts.V4.Trusts;
using Dfe.Academisation.CorrelationIdMiddleware;
using MediatR;
using Microsoft.EntityFrameworkCore;
Expand All @@ -30,6 +32,7 @@ public class EnrichProjectCommandTests
private readonly EnrichProjectCommand _subject;

private readonly EstablishmentDto _establishment;
private readonly TrustDto _trust;

private readonly Fixture _fixture = new();
public EnrichProjectCommandTests()
Expand All @@ -38,6 +41,7 @@ public EnrichProjectCommandTests()

// mock establishment
_establishment = _fixture.Create<EstablishmentDto>();
_trust = _fixture.Build<TrustDto>().With(x => x.Ukprn, "101101").Create();
_httpClientFactory = new Mock<IHttpClientFactory>();

var httpClient = _mockHttpMessageHandler.ToHttpClient();
Expand All @@ -63,15 +67,23 @@ public EnrichProjectCommandTests()
public async Task SomeProjectsAreIncomplete__ForKnownEstablishment__EnrichIncompleteProjectsWithMissingData()
{
// Arrange
var (project1, project2, project3) = (CreateProject(), CreateProject(), CreateProject("Bristol", "South West"));
var (project1, project2, project3) = (CreateProject(), CreateProject(), CreateProject("Bristol", "South West", 101101));
_context.Projects.AddRange(project1, project2, project3);

await _context.SaveChangesAsync();

_mockHttpMessageHandler.When($"http://localhost/v4/establishment/urn/{project1.Details.Urn}")
.Respond("application/json", JsonConvert.SerializeObject(_establishment));

_mockHttpMessageHandler.When($"http://localhost/v4/establishment/urn/{project2.Details.Urn}")
.Respond("application/json", JsonConvert.SerializeObject(_establishment));

_mockHttpMessageHandler.When($"http://localhost/v4/trust/trustReferenceNumber/{project1.Details.TrustReferenceNumber}")
.Respond("application/json", JsonConvert.SerializeObject(_trust));

_mockHttpMessageHandler.When($"http://localhost/v4/trust/trustReferenceNumber/{project2.Details.TrustReferenceNumber}")
.Respond("application/json", JsonConvert.SerializeObject(_trust));

var httpClient = _mockHttpMessageHandler.ToHttpClient();
httpClient.BaseAddress = new Uri("http://localhost");
_httpClientFactory.Setup(m => m.CreateClient("AcademiesApi")).Returns(httpClient);
Expand Down Expand Up @@ -103,6 +115,10 @@ public async Task SomeProjectsAreIncomplete__ForUnknownEstablishment__ProjectsRe
await _context.SaveChangesAsync();

_mockHttpMessageHandler.When($"http://localhost/v4/establishment/urn/*").Respond(HttpStatusCode.NotFound);

_mockHttpMessageHandler.When($"http://localhost/v4/trust/trustReferenceNumber/*").Respond(HttpStatusCode.NotFound);


var httpClient = _mockHttpMessageHandler.ToHttpClient();
httpClient.BaseAddress = new Uri("http://localhost");
_httpClientFactory.Setup(m => m.CreateClient("AcademiesApi")).Returns(httpClient);
Expand All @@ -122,14 +138,15 @@ public async Task SomeProjectsAreIncomplete__ForUnknownEstablishment__ProjectsRe
);
}

private Project CreateProject(string? la = null, string? region = null)
private Project CreateProject(string? la = null, string? region = null, int? trustUkprn = null)
{
var project = _fixture.Create<Project>();

var projectDetails = _fixture.Build<ProjectDetails>()
.With(p => p.LocalAuthority, la)
.With(p => p.Urn, project.Details.Urn)
.With(p => p.Region, region)
.With(p => p.TrustUkprn, trustUkprn)
.Create();

project.Update(projectDetails);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public async Task ProjectExists___ProjectReturned()
.Excluding(x => x.IsFormAMat)
.Excluding(x => x.ApplicationSharePointId)
.Excluding(x => x.SchoolSharePointId)
.Excluding(x => x.IsReadOnly)
);

existingProject.Id.Should().Be(serviceModel.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public async Task ProjectExists___FullProjectIsUpdated()
.With(p => p.Id, existingProject.Id)
.With(p => p.Urn, existingProject.Details.Urn)
.With(p => p.ExternalApplicationFormSaved, existingProject.Details.ExternalApplicationFormSaved)
.With(p => p.IsReadOnly, existingProject.IsReadOnly)
// excluded from update so need to be set for equality to assert
.With(x => x.KeyStage2PerformanceAdditionalInformation, existingProject.Details.KeyStage2PerformanceAdditionalInformation)
.With(x => x.KeyStage4PerformanceAdditionalInformation, existingProject.Details.KeyStage4PerformanceAdditionalInformation)
Expand Down

0 comments on commit f93e92d

Please sign in to comment.