using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; namespace RAIC.Extensions.Configuration.EntityFrameworkCore.SqlServer.Tests; /// /// Happy path tests /// [ClassDataSource(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>().Value; await Assert.That(initialOptionsState.SomeOption).IsEqualTo("Memory value"); } // Act using (var actScope = _hostScopeFactory.CreateScope()) { var context = actScope.ServiceProvider.GetRequiredService(); 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>().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>().Value; await Assert.That(initialOptionsState.SomeOption).IsEqualTo("Db value"); } // Act using (var actScope = _hostScopeFactory.CreateScope()) { var context = actScope.ServiceProvider.GetRequiredService(); 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>().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>().Value; await Assert.That(initialOptionsState.SomeOption).IsEqualTo("Db value updated"); } // Act using (var actScope = _hostScopeFactory.CreateScope()) { var context = actScope.ServiceProvider.GetRequiredService(); 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>().Value; await Assert.That(finalOptionsState.SomeOption).IsEqualTo("Memory value"); } } }