Files
TakeoutSaaS.AdminApi/tests/TakeoutSaaS.Integration.Tests/Fixtures/SqliteTestDatabase.cs

47 lines
1.2 KiB
C#

using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using TakeoutSaaS.Infrastructure.App.Persistence;
namespace TakeoutSaaS.Integration.Tests.Fixtures;
public sealed class SqliteTestDatabase : IDisposable
{
private readonly SqliteConnection _connection;
private bool _initialized;
public SqliteTestDatabase()
{
_connection = new SqliteConnection("Filename=:memory:");
_connection.Open();
Options = new DbContextOptionsBuilder<TakeoutAdminDbContext>()
.UseSqlite(_connection)
.EnableSensitiveDataLogging()
.Options;
}
public DbContextOptions<TakeoutAdminDbContext> Options { get; }
public TakeoutAdminDbContext CreateContext(long tenantId, long userId = 0)
{
EnsureCreated();
return new TakeoutAdminDbContext(Options, new TestTenantProvider(tenantId), new TestCurrentUserAccessor(userId));
}
public void EnsureCreated()
{
if (_initialized)
{
return;
}
using var context = new TakeoutAdminDbContext(Options, new TestTenantProvider(1));
context.Database.EnsureCreated();
_initialized = true;
}
public void Dispose()
{
_connection.Dispose();
}
}