mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-10-10 07:34:14 -04:00
54 lines
1.9 KiB
C#
54 lines
1.9 KiB
C#
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.AspNetCore.TestHost;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
|
|
namespace OpenNest.Server.Tests;
|
|
|
|
/// <summary>
|
|
/// Hosts the production routes in memory against a private, disposable SQLite file.
|
|
/// The production <see cref="NestDatabase"/> registration is replaced before anything
|
|
/// resolves it, so tests never open the default data/nests.db or an OPENNEST_DB path.
|
|
/// Disposing the factory disposes the host (and its database) before deleting the directory.
|
|
/// </summary>
|
|
public sealed class ServerFactory : WebApplicationFactory<global::Program>
|
|
{
|
|
private bool _cleanedUp;
|
|
private NestDatabase? _database;
|
|
|
|
public ServerFactory(string? databasePath = null)
|
|
{
|
|
DirectoryPath = Path.Combine(Path.GetTempPath(), "opennest-server-tests-" + Guid.NewGuid().ToString("N"));
|
|
Directory.CreateDirectory(DirectoryPath);
|
|
DatabasePath = databasePath ?? Path.Combine(DirectoryPath, "nests.db");
|
|
}
|
|
|
|
/// <summary>Private temporary directory owned by this factory.</summary>
|
|
public string DirectoryPath { get; }
|
|
|
|
public string DatabasePath { get; }
|
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
{
|
|
builder.ConfigureTestServices(services =>
|
|
{
|
|
services.RemoveAll<NestDatabase>();
|
|
// Register an externally owned instance; this factory closes it before deleting its file.
|
|
services.AddSingleton(_database = new NestDatabase(DatabasePath, pooling: false));
|
|
});
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
base.Dispose(disposing);
|
|
if (!disposing || _cleanedUp)
|
|
return;
|
|
|
|
_cleanedUp = true;
|
|
_database?.Dispose();
|
|
if (Directory.Exists(DirectoryPath))
|
|
Directory.Delete(DirectoryPath, recursive: true);
|
|
}
|
|
}
|