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