You've already forked Extensions.Configuration.EntityFrameworkCore
Add happy-path tests
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user