0
0
Files
Extensions.Configuration.En…/RAIC.Extensions.Configuration.EntityFrameworkCore.PostgreSQL.Tests/SingleSettingTests.cs
Rhys Ickeringill d2054dda29 Add happy-path tests
2026-03-17 18:58:21 +11:00

113 lines
4.1 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace RAIC.Extensions.Configuration.EntityFrameworkCore.PostgreSQL.Tests;
/// <summary>
/// Happy path tests
/// </summary>
[ClassDataSource<HostFixture>(Shared = SharedType.PerClass)]
public class SingleSettingTests
{
private readonly IServiceScopeFactory _hostScopeFactory;
public SingleSettingTests(HostFixture hostFixture)
{
_hostScopeFactory = hostFixture;
}
[Test]
public async Task AddSettingTest()
{
// Pre-condition sanity check!
using (var preconditionScope = _hostScopeFactory.CreateScope())
{
var initialOptionsState = preconditionScope.ServiceProvider.GetRequiredService<IOptionsSnapshot<FixtureOptions>>().Value;
await Assert.That(initialOptionsState.SomeOption).IsEqualTo("Memory value");
}
// Act
using (var actScope = _hostScopeFactory.CreateScope())
{
var context = actScope.ServiceProvider.GetRequiredService<FixtureDbContext>();
context.Settings.Add(new() { Key = nameof(FixtureOptions.SomeOption), Value = "Db value" });
await context.SaveChangesAsync();
}
await Task.Delay(TimeSpan.FromSeconds(2)); // wait for longer than debounce interval before checking again
// Assert
using (var assertScope = _hostScopeFactory.CreateScope())
{
var finalOptionsState = assertScope.ServiceProvider.GetRequiredService<IOptionsSnapshot<FixtureOptions>>().Value;
await Assert.That(finalOptionsState.SomeOption).IsEqualTo("Db value");
}
}
[Test]
[DependsOn(nameof(AddSettingTest))]
public async Task UpdateSettingTest()
{
// Pre-condition sanity check!
using (var preconditionScope = _hostScopeFactory.CreateScope())
{
var initialOptionsState = preconditionScope.ServiceProvider.GetRequiredService<IOptionsSnapshot<FixtureOptions>>().Value;
await Assert.That(initialOptionsState.SomeOption).IsEqualTo("Db value");
}
// Act
using (var actScope = _hostScopeFactory.CreateScope())
{
var context = actScope.ServiceProvider.GetRequiredService<FixtureDbContext>();
context.Settings.Update(new() { Key = nameof(FixtureOptions.SomeOption), Value = "Db value updated" });
await context.SaveChangesAsync();
}
await Task.Delay(TimeSpan.FromSeconds(2)); // wait for longer than debounce interval before checking again
// Assert
using (var assertScope = _hostScopeFactory.CreateScope())
{
var finalOptionsState = assertScope.ServiceProvider.GetRequiredService<IOptionsSnapshot<FixtureOptions>>().Value;
await Assert.That(finalOptionsState.SomeOption).IsEqualTo("Db value updated");
}
}
[Test]
[DependsOn(nameof(UpdateSettingTest))]
public async Task RemoveSettingTest()
{
// Pre-condition sanity check!
using (var preconditionScope = _hostScopeFactory.CreateScope())
{
var initialOptionsState = preconditionScope.ServiceProvider.GetRequiredService<IOptionsSnapshot<FixtureOptions>>().Value;
await Assert.That(initialOptionsState.SomeOption).IsEqualTo("Db value updated");
}
// Act
using (var actScope = _hostScopeFactory.CreateScope())
{
var context = actScope.ServiceProvider.GetRequiredService<FixtureDbContext>();
await context.Settings.Where(s => s.Key == nameof(FixtureOptions.SomeOption)).ExecuteDeleteAsync();
await context.SaveChangesAsync();
}
await Task.Delay(TimeSpan.FromSeconds(2)); // wait for longer than debounce interval before checking again
// Assert
using (var assertScope = _hostScopeFactory.CreateScope())
{
var finalOptionsState = assertScope.ServiceProvider.GetRequiredService<IOptionsSnapshot<FixtureOptions>>().Value;
await Assert.That(finalOptionsState.SomeOption).IsEqualTo("Memory value");
}
}
}