Files
Budgeteer/BudgetApp.Tests/Helpers/TestDbContextFactory.cs
T
2026-03-01 19:00:23 +00:00

52 lines
1.5 KiB
C#

using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using BudgetApp.Data;
namespace BudgetApp.Tests.Helpers;
public sealed class TestDbContextFactory : IDisposable
{
private readonly SqliteConnection _connection;
public const string TestUserId = "test-user-id";
public const string OtherUserId = "other-user-id";
public TestDbContextFactory()
{
_connection = new SqliteConnection("DataSource=:memory:");
_connection.Open();
using var context = CreateContext();
context.Database.EnsureCreated();
context.Users.AddRange(
new ApplicationUser
{
Id = TestUserId,
UserName = "test@test.com",
NormalizedUserName = "TEST@TEST.COM",
Email = "test@test.com",
NormalizedEmail = "TEST@TEST.COM"
},
new ApplicationUser
{
Id = OtherUserId,
UserName = "other@test.com",
NormalizedUserName = "OTHER@TEST.COM",
Email = "other@test.com",
NormalizedEmail = "OTHER@TEST.COM"
});
context.SaveChanges();
}
public ApplicationDbContext CreateContext()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseSqlite(_connection)
.Options;
return new ApplicationDbContext(options);
}
public void Dispose() => _connection.Dispose();
}