You've already forked Extensions.Configuration.EntityFrameworkCore
107 lines
4.1 KiB
C#
107 lines
4.1 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace RAIC.Extensions.Configuration.EntityFrameworkCore.SqlServer.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(0.333)); // wait a bit to give config time to settle
|
|
|
|
// 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(0.333)); // wait a bit to give config time to settle
|
|
|
|
// 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(0.333)); // wait a bit to give config time to settle
|
|
|
|
// Assert
|
|
using (var assertScope = _hostScopeFactory.CreateScope())
|
|
{
|
|
var finalOptionsState = assertScope.ServiceProvider.GetRequiredService<IOptionsSnapshot<FixtureOptions>>().Value;
|
|
await Assert.That(finalOptionsState.SomeOption).IsEqualTo("Memory value");
|
|
}
|
|
}
|
|
}
|