Merge branch 'review/wp7' into review/integration
This commit is contained in:
+94
@@ -0,0 +1,94 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
||||
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// DEP-2: <see cref="DeploymentManagerRepository.ReloadInstanceAsync"/> refreshes an instance a command
|
||||
/// loaded before it waited for the per-instance operation lock. The competing write goes through
|
||||
/// <c>ExecuteUpdate</c>/<c>ExecuteDelete</c> so the tracked copy stays stale, as it would under a
|
||||
/// second DbContext.
|
||||
/// </summary>
|
||||
public class ReloadInstanceRepositoryTests : IDisposable
|
||||
{
|
||||
private readonly ScadaBridgeDbContext _context = SqliteTestHelper.CreateInMemoryContext();
|
||||
private readonly DeploymentManagerRepository _repository;
|
||||
|
||||
/// <summary>Initializes the repository under test.</summary>
|
||||
public ReloadInstanceRepositoryTests() => _repository = new DeploymentManagerRepository(_context);
|
||||
|
||||
/// <summary>Releases the database context.</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_context.Database.CloseConnection();
|
||||
_context.Dispose();
|
||||
}
|
||||
|
||||
private async Task<int> SeedAsync(InstanceState state)
|
||||
{
|
||||
var site = new Site("S", "SI-S");
|
||||
_context.Sites.Add(site);
|
||||
var template = new Template("T");
|
||||
_context.Templates.Add(template);
|
||||
await _context.SaveChangesAsync();
|
||||
var instance = new Instance("Inst") { SiteId = site.Id, TemplateId = template.Id, State = state };
|
||||
_context.Instances.Add(instance);
|
||||
await _context.SaveChangesAsync();
|
||||
return instance.Id;
|
||||
}
|
||||
|
||||
/// <summary>A tracked entity picks up a state written behind its back.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task ReloadInstanceAsync_Tracked_SeesStateWrittenMeanwhile()
|
||||
{
|
||||
var id = await SeedAsync(InstanceState.NotDeployed);
|
||||
var instance = (await _repository.GetInstanceByIdAsync(id))!;
|
||||
await _context.Instances.Where(i => i.Id == id)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(i => i.State, InstanceState.Enabled));
|
||||
Assert.Equal(InstanceState.NotDeployed, instance.State); // stale, as in the race
|
||||
|
||||
var removed = await _repository.ReloadInstanceAsync(instance);
|
||||
|
||||
Assert.False(removed);
|
||||
Assert.Equal(InstanceState.Enabled, instance.State);
|
||||
Assert.Equal(EntityState.Unchanged, _context.Entry(instance).State);
|
||||
}
|
||||
|
||||
/// <summary>An untracked entity gets its state re-read.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task ReloadInstanceAsync_Untracked_SeesStateWrittenMeanwhile()
|
||||
{
|
||||
var id = await SeedAsync(InstanceState.Enabled);
|
||||
var instance = await _context.Instances.AsNoTracking().SingleAsync(i => i.Id == id);
|
||||
await _context.Instances.Where(i => i.Id == id)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(i => i.State, InstanceState.Disabled));
|
||||
|
||||
var removed = await _repository.ReloadInstanceAsync(instance);
|
||||
|
||||
Assert.False(removed);
|
||||
Assert.Equal(InstanceState.Disabled, instance.State);
|
||||
}
|
||||
|
||||
/// <summary>A row deleted meanwhile is reported as removed, tracked or not.</summary>
|
||||
/// <param name="tracked">Whether the instance is loaded with tracking.</param>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Theory]
|
||||
[InlineData(true)]
|
||||
[InlineData(false)]
|
||||
public async Task ReloadInstanceAsync_RowDeleted_ReportsRemoved(bool tracked)
|
||||
{
|
||||
var id = await SeedAsync(InstanceState.Enabled);
|
||||
var instance = tracked
|
||||
? (await _repository.GetInstanceByIdAsync(id))!
|
||||
: await _context.Instances.AsNoTracking().SingleAsync(i => i.Id == id);
|
||||
await _context.Instances.Where(i => i.Id == id).ExecuteDeleteAsync();
|
||||
|
||||
Assert.True(await _repository.ReloadInstanceAsync(instance));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user