-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add api integration test with testcontainer
- Loading branch information
Tom Brereton
authored and
Tom Brereton
committed
Apr 18, 2024
1 parent
5282128
commit 239cc19
Showing
21 changed files
with
176 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,31 @@ | ||
using Appointer.Api.Requests; | ||
using Appointer.Api.Responses; | ||
using Appointer.Domain.Accounts; | ||
using Appointer.Infrastructure.DbContext; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Appointer.Api.Controllers; | ||
|
||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class AccountController : ControllerBase | ||
{ | ||
public AccountController() | ||
private readonly AppointerDbContext _dbContext; | ||
|
||
public AccountController(AppointerDbContext dbContext) | ||
{ | ||
_dbContext = dbContext; | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> CreateAsync([FromBody] CreateAccountRequest request, | ||
CancellationToken cancellationToken) | ||
{ | ||
var response = new CreateAccountResponse(Guid.NewGuid(), request.FullName); | ||
var userAccount = new UserAccount(Guid.NewGuid(), request.FullName); | ||
await _dbContext.UserAccounts.AddAsync(userAccount, cancellationToken); | ||
await _dbContext.SaveChangesAsync(cancellationToken); | ||
var response = new CreateAccountResponse(userAccount.Id, userAccount.FullName); | ||
return Ok(response); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,25 @@ | ||
var builder = WebApplication.CreateBuilder(args); | ||
using Appointer.Infrastructure; | ||
|
||
// Add services to the container. | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.AddControllers(); | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
builder.Services.AddInfrastructure(builder.Configuration); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
|
||
app.Run(); | ||
|
||
public partial class Program{} | ||
public partial class Program | ||
{ | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Appointer.Domain.Accounts; | ||
|
||
public record UserAccount(Guid Id, string FullName, bool IsActive = true, bool IsDeleted = false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<AssemblyName>Appointer.Domain</AssemblyName> | ||
<RootNamespace>Appointer.Domain</RootNamespace> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Appointer.Domain.Accounts; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Appointer.Infrastructure.DbContext; | ||
|
||
public class AppointerDbContext : Microsoft.EntityFrameworkCore.DbContext | ||
{ | ||
public AppointerDbContext(DbContextOptions<AppointerDbContext> options) : base(options) | ||
{ | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppointerDbContext).Assembly); | ||
base.OnModelCreating(modelBuilder); | ||
} | ||
|
||
public DbSet<UserAccount> UserAccounts { get; set; } | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
tests/Api.IntegrationTests/Helpers/AppointerWebApplicationFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Appointer.Infrastructure.DbContext; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Testcontainers.MsSql; | ||
|
||
namespace Appointer.Api.IntegrationTests.Helpers; | ||
|
||
// ReSharper disable once ClassNeverInstantiated.Global | ||
public class AppointerWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram>, IAsyncLifetime | ||
where TProgram : class | ||
{ | ||
private readonly MsSqlContainer _msSqlContainer = new MsSqlBuilder() | ||
.WithCleanUp(true) | ||
.Build(); | ||
|
||
protected override void ConfigureWebHost(IWebHostBuilder builder) | ||
{ | ||
builder.ConfigureTestServices(services => | ||
{ | ||
services.RemoveDbContext<AppointerDbContext>(); | ||
services.AddDbContext<AppointerDbContext>(options => | ||
{ | ||
options.UseSqlServer(_msSqlContainer.GetConnectionString()); | ||
}); | ||
services.EnsureDbCreated<AppointerDbContext>(); | ||
}); | ||
} | ||
|
||
public async Task InitializeAsync() => await _msSqlContainer.StartAsync(); | ||
|
||
public new async Task DisposeAsync() => await _msSqlContainer.StopAsync(); | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/Api.IntegrationTests/Helpers/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Appointer.Api.IntegrationTests.Helpers; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static void RemoveDbContext<T>(this IServiceCollection services) where T : DbContext | ||
{ | ||
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<T>)); | ||
if (descriptor != null) services.Remove(descriptor); | ||
} | ||
|
||
public static void EnsureDbCreated<T>(this IServiceCollection services) where T : DbContext | ||
{ | ||
var serviceProvider = services.BuildServiceProvider(); | ||
using var scope = serviceProvider.CreateScope(); | ||
var scopedServices = scope.ServiceProvider; | ||
var context = scopedServices.GetRequiredService<T>(); | ||
context.Database.EnsureCreated(); | ||
} | ||
} |
Oops, something went wrong.