68 lines
2.2 KiB
C#
68 lines
2.2 KiB
C#
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using MinAttest.Application;
|
|
using MinAttest.Application.Abstractions;
|
|
using MinAttest.Application.Common.Behaviors;
|
|
using MinAttest.Infrastructure.Data;
|
|
using Xunit;
|
|
|
|
namespace MinAttest.Tests.Integration;
|
|
|
|
public class IntegrationTestFixture : IAsyncLifetime
|
|
{
|
|
public IServiceProvider Services { get; private set; } = default!;
|
|
public string ConnectionString { get; private set; } = string.Empty;
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
await Server.TestDb.StartAsync();
|
|
ConnectionString = Server.TestDb.ConnectionString;
|
|
|
|
var services = new ServiceCollection();
|
|
|
|
services.AddDbContext<AppDbContext>(opt => opt.UseSqlServer(ConnectionString));
|
|
services.AddScoped<IAppDbContext>(sp => sp.GetRequiredService<AppDbContext>());
|
|
|
|
services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(ApplicationAssembly).Assembly));
|
|
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
|
|
|
|
Services = services.BuildServiceProvider();
|
|
|
|
// Migrate and seed
|
|
using var scope = Services.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
await db.Database.MigrateAsync();
|
|
|
|
await SeedAsync(db);
|
|
}
|
|
|
|
public async Task DisposeAsync()
|
|
{
|
|
await Server.TestDb.StopAsync();
|
|
}
|
|
|
|
private static async Task SeedAsync(AppDbContext db)
|
|
{
|
|
if (!db.Persons.Any())
|
|
{
|
|
var personId = Guid.NewGuid();
|
|
db.Persons.Add(new MinAttest.Domain.Entities.Person
|
|
{
|
|
Id = personId,
|
|
NationalIdHash = "person-hash-123",
|
|
Email = "person@example.com"
|
|
});
|
|
|
|
var employerId = Guid.NewGuid();
|
|
db.Employers.Add(new MinAttest.Domain.Entities.Employer
|
|
{
|
|
Id = employerId,
|
|
OrgNumber = "999999999",
|
|
Name = "TestBedrift AS"
|
|
});
|
|
await db.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|