53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using Microsoft.Data.Sqlite;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using BudgetApp.Data;
|
|
using BudgetApp.Data.Entities;
|
|
|
|
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();
|
|
}
|