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;
///
/// DEP-2: refreshes an instance a command
/// loaded before it waited for the per-instance operation lock. The competing write goes through
/// ExecuteUpdate/ExecuteDelete so the tracked copy stays stale, as it would under a
/// second DbContext.
///
public class ReloadInstanceRepositoryTests : IDisposable
{
private readonly ScadaBridgeDbContext _context = SqliteTestHelper.CreateInMemoryContext();
private readonly DeploymentManagerRepository _repository;
/// Initializes the repository under test.
public ReloadInstanceRepositoryTests() => _repository = new DeploymentManagerRepository(_context);
/// Releases the database context.
public void Dispose()
{
_context.Database.CloseConnection();
_context.Dispose();
}
private async Task 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;
}
/// A tracked entity picks up a state written behind its back.
/// A task that represents the asynchronous operation.
[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);
}
/// An untracked entity gets its state re-read.
/// A task that represents the asynchronous operation.
[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);
}
/// A row deleted meanwhile is reported as removed, tracked or not.
/// Whether the instance is loaded with tracking.
/// A task that represents the asynchronous operation.
[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));
}
}