mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-01 01:43:13 +00:00
69 lines
2.4 KiB
C#
69 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);
|
||
|
|
}
|
||
|
|
}
|