Files
duempelkas/tests/Duempelkas.Tests/LedgerUiFixture.cs
T

34 lines
1.4 KiB
C#

using Bunit;
using Duempelkas.Infrastructure;
using Duempelkas.Infrastructure.Persistence;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Duempelkas.Tests;
internal sealed class LedgerUiFixture : IDisposable
{
public BunitContext Context { get; } = new();
private readonly SqliteConnection anchor;
private readonly string folder = Path.Combine(Path.GetTempPath(), "duempelkas-ui-" + Guid.NewGuid().ToString("N"));
public LedgerUiFixture(bool accounts = true)
{
Directory.CreateDirectory(folder);
var connection = $"Data Source=ui-{Guid.NewGuid():N};Mode=Memory;Cache=Shared";
anchor = new SqliteConnection(connection);
anchor.Open();
Context.Services.AddInfrastructure(connection, folder);
Context.JSInterop.Mode = JSRuntimeMode.Loose;
using var db = Context.Services.GetRequiredService<IDbContextFactory<FinanceDbContext>>().CreateDbContext();
db.Database.EnsureCreated();
if (accounts)
{
db.Accounts.AddRange(new() { Name = "Barkasse", CarryoverBalance = 100m }, new() { Name = "Girokonto" });
db.SaveChanges();
}
}
public T Service<T>() where T : notnull => Context.Services.GetRequiredService<T>();
public void Dispose() { Context.Dispose(); anchor.Dispose(); Directory.Delete(folder, true); }
}