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

Add academisation projects to identifier search #43

Merged
merged 13 commits into from
May 8, 2024
319 changes: 105 additions & 214 deletions Dfe.Identifiers.Api.Test/Api/IdentifiersEndpointTests.cs

Large diffs are not rendered by default.

72 changes: 55 additions & 17 deletions Dfe.Identifiers.Api.Test/ApiTestFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Dfe.Identifiers.Infrastructure.Context;
using System.Net.Mime;
using Dfe.Identifiers.Infrastructure.Context;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

Expand All @@ -7,30 +9,66 @@ namespace Dfe.Identifiers.Api.Test
public class ApiTestFixture : IDisposable
{
private const string connectionStringKey = "ConnectionStrings:DefaultConnection";
private DbContextOptions<MstrContext> _dbContextOptions { get; init; }
private DbContextOptions<MstrContext> _mstrContextOptions { get; init; }
private DbContextOptions<AcademisationContext> _academisationContextOptions { get; init; }
private static readonly object _lock = new();
private static bool isInitalised = false;
public HttpClient Client { get; init; }

private readonly WebApplicationFactory<Startup> _application;

public ApiTestFixture()
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.AddUserSecrets<ApiTestFixture>();

Configuration = builder.Build();
_dbContextOptions = new DbContextOptionsBuilder<MstrContext>()
.UseSqlServer(Configuration[connectionStringKey])
.Options;
using var context = GetMstrContext();
context.Database.EnsureDeleted();
context.Database.Migrate();
lock (_lock)
{
if (!isInitalised)
{
string? connectionString = null;
_application = new WebApplicationFactory<Startup>()
.WithWebHostBuilder(builder =>
{
builder.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.AddUserSecrets<ApiTestFixture>();
var configuration = config.Build();
connectionString = configuration[connectionStringKey];
});
});

Client = _application.CreateClient();
Client.DefaultRequestHeaders.Add("ApiKey", "app-key");
Client.DefaultRequestHeaders.Add("ContentType", MediaTypeNames.Application.Json);
Client.BaseAddress = new Uri("https://identifiers-api.com");

_mstrContextOptions = new DbContextOptionsBuilder<MstrContext>()
.UseSqlServer(connectionString)
.Options;
_academisationContextOptions = new DbContextOptionsBuilder<AcademisationContext>()
.UseSqlServer(connectionString)
.Options;
}
SetupDatabase();
}

}

private IConfigurationRoot Configuration { get; init; }

public MstrContext GetMstrContext() => new(_dbContextOptions);
public MstrContext GetMstrContext() => new(_mstrContextOptions);
public AcademisationContext GetAcademisationContext() => new(_academisationContextOptions);

public void Dispose()
{
}

private void SetupDatabase()
{
using var mstrContext = GetMstrContext();
using var academisationContext = GetAcademisationContext();
mstrContext.Database.EnsureDeleted();
academisationContext.Database.EnsureDeleted();
mstrContext.Database.Migrate();
academisationContext.Database.Migrate();
}
}
}
6 changes: 6 additions & 0 deletions Dfe.Identifiers.Api.Test/Constants/IdentifierConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Dfe.Identifiers.Api.Test.Constants;

public static class IdentifierConstants
{
public static readonly string MixedSameUkprn = "SameUKPRN";
}
39 changes: 0 additions & 39 deletions Dfe.Identifiers.Api.Test/DatabaseModelBuilder.cs

This file was deleted.

13 changes: 13 additions & 0 deletions Dfe.Identifiers.Api.Test/Extensions/EstablishmentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Dfe.Identifiers.Domain.Models;

namespace Dfe.Identifiers.Api.Test.Extensions;

public static class EstablishmentExtensions
{
public static Establishment LinkLocalAuthorityToEstablishment(this Establishment establishment,
LocalAuthority localAuthority)
{
establishment.LocalAuthority = localAuthority;
return establishment;
}
}
Loading
Loading