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

Commit

Permalink
Add initial unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nwarms committed Mar 26, 2024
1 parent 6efd9db commit 8baca44
Show file tree
Hide file tree
Showing 5 changed files with 501 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Dfe.Identifiers.Api.Test/ApiTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Net.Mime;
using Dfe.Identifiers.Api.Context;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace Dfe.Identifiers.Api.Test
{
public class ApiTestFixture : IDisposable
{
// private const string ConnectionStringKey = "ConnectionStrings:DefaultConnection";
private const string connectionString = "Server=localhost,1433;Database=ApiTests;User Id=sa;TrustServerCertificate=True;Password=StrongPassword905";
private DbContextOptions<MstrContext> _dbContextOptions { get; init; }

public ApiTestFixture()
{
_dbContextOptions = new DbContextOptionsBuilder<MstrContext>()
.UseSqlServer(connectionString)
.Options;
using var context = GetMstrContext();
context.Database.EnsureDeleted();
context.Database.Migrate();
}

public MstrContext GetMstrContext() => new MstrContext(_dbContextOptions);

public void Dispose()
{
}

// private static string BuildDatabaseConnectionString(IConfigurationBuilder config)
// {
// var currentConfig = config.Build();
// var connection = currentConfig[ConnectionStringKey];
// var sqlBuilder = new SqlConnectionStringBuilder(connection);
// sqlBuilder.InitialCatalog = "ApiTests";
//
// var result = sqlBuilder.ToString();
//
// return result;
// }
}
}
51 changes: 51 additions & 0 deletions Dfe.Identifiers.Api.Test/DatabaseModelBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using AutoFixture;
using Dfe.Identifiers.Api.Models;

namespace Dfe.Identifiers.Api.Test
{
public static class DatabaseModelBuilder
{
private static readonly Fixture _fixture = new Fixture();

public static Trust BuildTrust()
{
var result = _fixture.Create<Trust>();
result.SK = null;
result.TrustStatus = "Open";
result.TrustTypeId = 30;
result.TrustType = null;
result.TrustStatusId = null;
result.RegionId = null;
result.TrustBandingId = null;
result.CurrentSingleListGrouping = result.CurrentSingleListGrouping.Substring(0, 19);
result.FollowUpLetterSent = result.CurrentSingleListGrouping.Substring(0, 19);
result.PrioritisedForReview = result.PrioritisedForReview.Substring(0, 19);
result.RID = result.RID.Substring(0, 10);

return result;
}

public static Establishment BuildEstablishment()
{
var result = _fixture.Create<Establishment>();
result.SK = null;
result.IfdPipeline = null;
result.LocalAuthority = null;
result.EstablishmentType = null;
result.PK_GIAS_URN = _fixture.Create<int>().ToString();
result.EstablishmentTypeId = 228;
result.LocalAuthorityId = 1;

return result;
}

public static IfdPipeline BuildIfdPipeline()
{
var result = _fixture.Create<IfdPipeline>();
result.SK = null;
result.GeneralDetailsUrn = _fixture.Create<int>().ToString();

return result;
}
}
}
19 changes: 19 additions & 0 deletions Dfe.Identifiers.Api.Test/Extensions/ActionResultExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Dfe.Identifiers.Api.Models;
using FluentAssertions;
using Dfe.Identifiers.Api.Models.Identifiers;
using Microsoft.AspNetCore.Mvc;

namespace Dfe.Identifiers.Api.Test.Extensions;

public static class ActionResultExtension
{
public static Models.Identifiers.Identifiers? GetIdentifiers(this ActionResult<Models.Identifiers.Identifiers> actionResult)
{
return (Models.Identifiers.Identifiers?)((OkObjectResult?)actionResult.Result)?.Value;
}

public static int? GetStatusCode<T>(this ActionResult<T> actionResult)
{
return ((ObjectResult?)actionResult.Result)?.StatusCode;
}
}
1 change: 1 addition & 0 deletions Dfe.Identifiers.Api.Test/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
Loading

0 comments on commit 8baca44

Please sign in to comment.