shoc-backend/SeaHavenIndustries.Tests/WorkOrderFieldLockServiceTests.cs

53 lines
1.8 KiB
C#

using Data.SeaHavenIndustries;
using Microsoft.EntityFrameworkCore;
using SeaHaven.Services.Constants;
using SeaHaven.Services.Implementation;
namespace SeaHavenIndustries.Tests;
public class WorkOrderFieldLockServiceTests
{
private static ApplicationDbContext CreateContext()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
return new ApplicationDbContext(options);
}
[Fact]
public async Task LockFieldAsync_DuplicateCalls_EnqueuesSingleLock()
{
await using var context = CreateContext();
var locks = new WorkOrderFieldLockService(context);
await locks.LockFieldAsync(1, WorkOrderFieldNames.Description, "user-1");
await locks.LockFieldAsync(1, WorkOrderFieldNames.Description, "user-2");
await context.SaveChangesAsync();
var fieldLocks = await context.WorkOrderFieldLocks.ToListAsync();
Assert.Single(fieldLocks);
Assert.Equal(WorkOrderFieldNames.Description, fieldLocks[0].FieldName);
}
[Fact]
public async Task LockFieldAsync_WhenAlreadyPersisted_IsIdempotent()
{
await using var context = CreateContext();
context.WorkOrderFieldLocks.Add(new WorkOrderFieldLock
{
WorkOrderId = 2,
FieldName = WorkOrderFieldNames.AssignTo,
LockedAt = DateTime.UtcNow,
LockedByUserId = "user-1"
});
await context.SaveChangesAsync();
var locks = new WorkOrderFieldLockService(context);
await locks.LockFieldAsync(2, WorkOrderFieldNames.AssignTo, "user-2");
await context.SaveChangesAsync();
var fieldLocks = await context.WorkOrderFieldLocks.ToListAsync();
Assert.Single(fieldLocks);
}
}