mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 09:33:13 +00:00
WorkOrderDataService.AddAsync overwrote the UTC CreatedDate set by WorkOrderService with local server time, offsetting the SLA response clock on any host not running in UTC. Data services now stamp CreatedDate, LastModificationTime and DeletionTime with DateTime.UtcNow, and a work order keeps the creation time its caller set.
68 lines
2.4 KiB
C#
68 lines
2.4 KiB
C#
using Data.SeaHavenIndustries;
|
|
using FluentAssertions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SeaHaven.DataServices.Implementation;
|
|
using Xunit;
|
|
|
|
namespace Api.SeaHavenIndustries.Tests;
|
|
|
|
/// <summary>
|
|
/// The SLA clock and every "created"/"modified" display read these stamps as UTC, so the data layer
|
|
/// must never write local server time into them.
|
|
/// </summary>
|
|
public class WorkOrderDataServiceTimestampTests
|
|
{
|
|
private static ApplicationDbContext NewContext()
|
|
{
|
|
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
|
.UseInMemoryDatabase(Guid.NewGuid().ToString())
|
|
.Options;
|
|
return new ApplicationDbContext(options);
|
|
}
|
|
|
|
private static WorkOrder NewWorkOrder() =>
|
|
new() { InternalWONumber = "WO-1", WorkerOrderTitle = "Leak", LocationId = 100 };
|
|
|
|
[Fact]
|
|
public async Task AddAsync_KeepsTheCreationTimeTheCallerSet()
|
|
{
|
|
await using var context = NewContext();
|
|
var createdAt = new DateTime(2026, 9, 25, 14, 0, 0, DateTimeKind.Utc);
|
|
var workOrder = NewWorkOrder();
|
|
workOrder.CreatedDate = createdAt;
|
|
|
|
var saved = await new WorkOrderDataService(context).AddAsync(workOrder);
|
|
|
|
saved.CreatedDate.Should().Be(createdAt);
|
|
saved.CreatedDate!.Value.Kind.Should().Be(DateTimeKind.Utc);
|
|
(await context.workOrders.SingleAsync()).CreatedDate.Should().Be(createdAt);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AddAsync_StampsUtcWhenTheCallerSetNoCreationTime()
|
|
{
|
|
await using var context = NewContext();
|
|
var before = DateTime.UtcNow;
|
|
|
|
var saved = await new WorkOrderDataService(context).AddAsync(NewWorkOrder());
|
|
|
|
saved.CreatedDate.Should().NotBeNull();
|
|
saved.CreatedDate!.Value.Kind.Should().Be(DateTimeKind.Utc);
|
|
saved.CreatedDate.Value.Should().BeOnOrAfter(before).And.BeOnOrBefore(DateTime.UtcNow);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateAsync_StampsTheModificationTimeInUtc()
|
|
{
|
|
await using var context = NewContext();
|
|
var service = new WorkOrderDataService(context);
|
|
var saved = await service.AddAsync(NewWorkOrder());
|
|
var before = DateTime.UtcNow;
|
|
|
|
await service.UpdateAsync(saved);
|
|
|
|
saved.LastModificationTime.Should().NotBeNull();
|
|
saved.LastModificationTime!.Value.Kind.Should().Be(DateTimeKind.Utc);
|
|
saved.LastModificationTime.Value.Should().BeOnOrAfter(before).And.BeOnOrBefore(DateTime.UtcNow);
|
|
}
|
|
}
|