Skip to content

Commit

Permalink
started writing api tests for establishment
Browse files Browse the repository at this point in the history
docker image for the db has gone out of date with the dev database
can't really progress here until we make a decision on how to sync them
  • Loading branch information
mikestock-nimble committed Dec 11, 2023
1 parent 3aa7483 commit dfcecd0
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
20 changes: 18 additions & 2 deletions TramsDataApi.Test/DbFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dfe.Academies.Academisation.Data;
using Dfe.Academies.Domain.Establishment;
using Dfe.Academies.Domain.Trust;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
Expand All @@ -12,7 +13,7 @@ public class DbFixture
{
public readonly string ConnString;
public readonly DbContextOptions<MstrContext> MstrContextOptions;

public DbFixture()
{
var projectDir = Directory.GetCurrentDirectory();
Expand Down Expand Up @@ -46,17 +47,32 @@ private static void ClearDatabase(MstrContext context)
{
context.TrustTypes.RemoveRange(context.TrustTypes);
context.Trusts.RemoveRange(context.Trusts);
context.LocalAuthorities.RemoveRange(context.LocalAuthorities);
context.Establishments.RemoveRange(context.Establishments);
context.SaveChanges();
}

private static void SeedDatabase(MstrContext context)
{
SeedTrustTypes(context);
SeedLocalAuthorities(context);
}

private static void SeedTrustTypes(MstrContext context)
{
context.TrustTypes.Add(new TrustType() { SK = 30, Code = "06", Name = "Multi-academy trust" });
context.TrustTypes.Add(new TrustType() { SK = 32, Code = "10", Name = "Single-academy trust" });
context.SaveChanges();
}

private static void SeedLocalAuthorities(MstrContext context)
{
context.LocalAuthorities.Add(new LocalAuthority() { SK = 1, Code = "202", Name = "Barnsley" });
context.LocalAuthorities.Add(new LocalAuthority() { SK = 2, Code = "203", Name = "Birmingham" });
context.LocalAuthorities.Add(new LocalAuthority() { SK = 3, Code = "204", Name = "Bradford" });
}
}

[CollectionDefinition("Database", DisableParallelization = true)]
public class DatabaseCollection : ICollectionFixture<DbFixture>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using AutoFixture;
using Dfe.Academies.Contracts.V4.Establishments;
using Dfe.Academies.Domain.Establishment;
using FluentAssertions;
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using Xunit;

namespace TramsDataApi.Test.Integration.V4
{
[Collection("Database")]
public class EstablishmentV4IntegrationTests : IClassFixture<TramsDataApiFactory>
{
private readonly HttpClient _client;
private static readonly Fixture _autoFixture = new Fixture();
private readonly TramsDataApiFactory _apiFixture;

private readonly string _apiUrlPrefix = "https://trams-api.com/v4";

public EstablishmentV4IntegrationTests(TramsDataApiFactory apiFixture)
{
_apiFixture = apiFixture;
_client = _apiFixture.CreateClient();
_client.DefaultRequestHeaders.Add("ApiKey", "testing-api-key");
_client.BaseAddress = new Uri(_apiUrlPrefix);
}

[Fact]
public async Task Get_EstablishmentByUkPrn_NoEstablishmentExists_Returns_NotFound()
{
var ukPrn = _autoFixture.Create<int>();
var getEstablishmentResponse = await _client.GetAsync($"/establishment/{ukPrn}");
getEstablishmentResponse.StatusCode.Should().Be(HttpStatusCode.NotFound);
}

[Fact]
public async Task Get_EstablishmentByUkPrn_EstablishmentExists_Returns_Ok()
{
using var context = _apiFixture.GetMstrContext();

var establishment = new Establishment();
establishment.LocalAuthority = null;
establishment.UKPRN = _autoFixture.Create<string>();
establishment.FK_LocalAuthority = 1;

context.Establishments.Add(establishment);
context.SaveChanges();

var getEstablishmentResponse = await _client.GetAsync($"/establishment/{establishment.UKPRN}");
getEstablishmentResponse.StatusCode.Should().Be(HttpStatusCode.OK);

var establishmentContent = await getEstablishmentResponse.Content.ReadFromJsonAsync<EstablishmentDto>();

// "ukprn": "string",
// "urn": "string",
// "name": "string",
// "localAuthorityCode": "string",
// "localAuthorityName": "string",
// "ofstedRating": "string",
// "ofstedLastInspection": "string",
// "statutoryLowAge": "string",
// "statutoryHighAge": "string",
// "schoolCapacity": "string",
// "pfi": "string",
// "establishmentNumber": "string",
// "pan": "string",
// "deficit": "string",
// "viabilityIssue": "string",
// "giasLastChangedDate": "string",
// "noOfBoys": "string",
// "noOfGirls": "string",
// "senUnitCapacity": "string",
// "senUnitOnRoll": "string",
// "religousEthos": "string",
// "headteacherTitle": "string",
// "headteacherFirstName": "string",
// "headteacherLastName": "string",
// "headteacherPreferredJobTitle": "string",

establishmentContent.Ukprn.Should().Be(establishment.UKPRN);
establishmentContent.Urn.Should().Be(establishment.URN.ToString());
establishmentContent.Name.Should().Be(establishment.EstablishmentName);
establishmentContent.LocalAuthorityCode.Should().Be(establishment.LocalAuthority.Code);
establishmentContent.LocalAuthorityName.Should().Be(establishment.LocalAuthority.Name);

}
}
}

0 comments on commit dfcecd0

Please sign in to comment.