35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.Data.Sqlite;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace backend.tests;
|
|
|
|
public class ApiFactory : WebApplicationFactory<Program>
|
|
{
|
|
private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"assistant-test-{Guid.NewGuid():N}.db");
|
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
{
|
|
builder.UseEnvironment("Test");
|
|
builder.ConfigureAppConfiguration((_, cfg) =>
|
|
{
|
|
cfg.AddInMemoryCollection(new Dictionary<string, string?>
|
|
{
|
|
["ConnectionStrings:Default"] = $"Data Source={_dbPath}",
|
|
});
|
|
});
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
base.Dispose(disposing);
|
|
SqliteConnection.ClearAllPools();
|
|
foreach (var suffix in new[] { "", "-shm", "-wal", "-journal" })
|
|
{
|
|
var p = _dbPath + suffix;
|
|
try { if (File.Exists(p)) File.Delete(p); } catch { /* best effort */ }
|
|
}
|
|
}
|
|
}
|