Skip to content

Added integration tests for Persons API #559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Dfe.Academies.PersonsApi.Tests.Integration.Mocks;
using Microsoft.EntityFrameworkCore;
using PersonsApi;
using System.Net;

namespace Dfe.Academies.PersonsApi.Tests.Integration.Controllers
{
public class When_Fetching_Mp_By_Constituency : IClassFixture<CustomWebApplicationFactory<Startup>>
{
private readonly CustomWebApplicationFactory<Startup> _factory;

public When_Fetching_Mp_By_Constituency(CustomWebApplicationFactory<Startup> factory)
{
_factory = factory;
}

[Fact]
public async Task it_should_return_mp_when_constituency_exist()
{
var client = _factory.CreateClient();

var dbcontext = _factory.GetDbContext();

await dbcontext.Constituencies.Where(x => x.ConstituencyName == "Test Constituency 1")
.ExecuteUpdateAsync(x => x.SetProperty(p => p.ConstituencyName, "NewConstituencyName"));

var constituencyName = Uri.EscapeDataString("NewConstituencyName");

var response = await client.GetAsync($"v1/Constituencies/{constituencyName}/mp");

var content = await response.Content.ReadAsStringAsync();

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

[Fact]
public async Task it_should_return_notfound_when_constituency_doesnt_exist()
{
var client = _factory.CreateClient();

var response = await client.GetAsync($"v1/Constituencies/test/mp");

var content = await response.Content.ReadAsStringAsync();

Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.7" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Dfe.Academies.Api.Infrastructure\Dfe.Academies.Infrastructure.csproj" />
<ProjectReference Include="..\PersonsApi\PersonsApi.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using Dfe.Academies.Academisation.Data;
using Dfe.Academies.Domain.Persons;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.Data.Common;

namespace Dfe.Academies.PersonsApi.Tests.Integration.Mocks
{
public class CustomWebApplicationFactory<TProgram>
: WebApplicationFactory<TProgram> where TProgram : class
{
private SqliteConnection? _connection;

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var dbContextDescriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<MopContext>));

services.Remove(dbContextDescriptor!);

var dbConnectionDescriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbConnection));

services.Remove(dbConnectionDescriptor!);

services.AddSingleton<DbConnection>(container =>
{
if (_connection == null)
{
_connection = new SqliteConnection("DataSource=:memory:");
_connection.Open();
}

return _connection;
});

services.AddDbContext<MopContext>((container, options) =>
{
var connection = container.GetRequiredService<DbConnection>();
options.UseSqlite(connection);
});

var serviceProvider = services.BuildServiceProvider();

using (var scope = serviceProvider.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<MopContext>();

db.Database.EnsureCreated();

SeedTestData(db);
}
});

builder.UseEnvironment("Development");
}

public MopContext GetDbContext()
{
var scopeFactory = Services.GetRequiredService<IServiceScopeFactory>();
var scope = scopeFactory.CreateScope();
return scope.ServiceProvider.GetRequiredService<MopContext>();
}

private static void SeedTestData(MopContext context)
{
context.MemberContactDetails.Add(new MemberContactDetails
{
MemberID = 1,
Email = "test1@example.com",
TypeId = 1
});
context.MemberContactDetails.Add(new MemberContactDetails
{
MemberID = 2,
Email = "test2@example.com",
TypeId = 2
});

context.Constituencies.Add(new Constituency
{
ConstituencyId = 1,
ConstituencyName = "Test Constituency 1",
NameList = "Wood, John",
NameDisplayAs = "John Wood",
NameFullTitle = "John Wood MP",
LastRefresh = DateTime.UtcNow,
MemberID = 1
});
context.Constituencies.Add(new Constituency
{
ConstituencyId = 2,
ConstituencyName = "Test Constituency 2",
NameList = "Wood, Joe",
NameDisplayAs = "Joe Wood",
NameFullTitle = "Joe Wood MP",
LastRefresh = DateTime.UtcNow,
MemberID= 2
});

context.SaveChanges();
}

protected override void ConfigureClient(HttpClient client)
{
client.DefaultRequestHeaders.Add("ApiKey", "app-key");

base.ConfigureClient(client);
}
}
}
7 changes: 7 additions & 0 deletions TramsDataApi.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dfe.Academies.Utils", "Dfe.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PersonsApi", "PersonsApi\PersonsApi.csproj", "{039FE264-0819-409A-8E01-53CA082812B3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dfe.Academies.PersonsApi.Tests.Integration", "Dfe.Academies.PersonsApi.Tests.Integration\Dfe.Academies.PersonsApi.Tests.Integration.csproj", "{693F82DB-BD57-48C0-B558-783153354DA8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -58,12 +60,17 @@ Global
{039FE264-0819-409A-8E01-53CA082812B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{039FE264-0819-409A-8E01-53CA082812B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{039FE264-0819-409A-8E01-53CA082812B3}.Release|Any CPU.Build.0 = Release|Any CPU
{693F82DB-BD57-48C0-B558-783153354DA8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{693F82DB-BD57-48C0-B558-783153354DA8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{693F82DB-BD57-48C0-B558-783153354DA8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{693F82DB-BD57-48C0-B558-783153354DA8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{AF49D395-A01A-43E9-A643-6B2266BA62C5} = {B3CDCA56-9765-4C2B-A4A4-2738C5A268EB}
{693F82DB-BD57-48C0-B558-783153354DA8} = {B3CDCA56-9765-4C2B-A4A4-2738C5A268EB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F0704299-A9C2-448A-B816-E5BCCB345AF8}
Expand Down
Loading