mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-06 14:42:09 +00:00
SaveAttachmentsAndPhotos committed once per attachment and re-fetched and re-saved the work order once per photo field. Add AddWorkOrderAttachmentsAsync for one commit, and load and save the work order once for all photo fields. File storage stays per-file, since it is I/O rather than a database call. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
132 lines
4.8 KiB
C#
132 lines
4.8 KiB
C#
using Data.SeaHavenIndustries;
|
|
using FluentAssertions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SeaHaven.DataServices.Implementation;
|
|
using Xunit;
|
|
|
|
namespace Api.SeaHavenIndustries.Tests;
|
|
|
|
public class WorkOrderDataServiceBatchingTests
|
|
{
|
|
private static ApplicationDbContext NewContext()
|
|
{
|
|
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
|
.UseInMemoryDatabase(Guid.NewGuid().ToString())
|
|
.Options;
|
|
return new ApplicationDbContext(options);
|
|
}
|
|
|
|
private static async Task SeedAsync(ApplicationDbContext context)
|
|
{
|
|
context.Locations.Add(new Locations { Id = 100, Name = "Site A", Address1 = "1 Main St" });
|
|
context.workOrders.AddRange(
|
|
new WorkOrder { Id = 1, InternalWONumber = "WO-1", WorkerOrderTitle = "First", LocationId = 100 },
|
|
new WorkOrder { Id = 2, InternalWONumber = "WO-2", WorkerOrderTitle = "Second", LocationId = 100 },
|
|
new WorkOrder { Id = 3, InternalWONumber = "WO-3", WorkerOrderTitle = "Third", LocationId = 100 },
|
|
new WorkOrder { Id = 4, InternalWONumber = "WO-T", WorkerOrderTitle = "Template", LocationId = 100, istemplate = true });
|
|
await context.SaveChangesAsync();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetByRequestedIdsWithDetailsAsync_ReturnsResultsInInputOrder()
|
|
{
|
|
await using var context = NewContext();
|
|
await SeedAsync(context);
|
|
var service = new WorkOrderDataService(context);
|
|
|
|
var result = await service.GetByRequestedIdsWithDetailsAsync(
|
|
new[] { 3, 1, 2 },
|
|
CancellationToken.None);
|
|
|
|
result.Select(w => w.Id).Should().Equal(new[] { 3, 1, 2 });
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetByRequestedIdsWithDetailsAsync_DeduplicatesIdsAndSkipsMissing()
|
|
{
|
|
await using var context = NewContext();
|
|
await SeedAsync(context);
|
|
var service = new WorkOrderDataService(context);
|
|
|
|
var result = await service.GetByRequestedIdsWithDetailsAsync(
|
|
new[] { 2, 2, 999, 1 },
|
|
CancellationToken.None);
|
|
|
|
result.Select(w => w.Id).Should().Equal(new[] { 2, 1 });
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetByRequestedIdsWithDetailsAsync_ExcludesTemplatesAndEagerLoadsLocation()
|
|
{
|
|
await using var context = NewContext();
|
|
await SeedAsync(context);
|
|
var service = new WorkOrderDataService(context);
|
|
|
|
var result = await service.GetByRequestedIdsWithDetailsAsync(
|
|
new[] { 1, 4 },
|
|
CancellationToken.None);
|
|
|
|
result.Select(w => w.Id).Should().Equal(new[] { 1 });
|
|
result.Single().Locations.Should().NotBeNull();
|
|
result.Single().Locations!.Name.Should().Be("Site A");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AddAuditLogsAsync_PersistsAllRowsInSingleSave()
|
|
{
|
|
await using var context = NewContext();
|
|
await SeedAsync(context);
|
|
var service = new WorkOrderDataService(context);
|
|
|
|
await service.AddAuditLogsAsync(new[]
|
|
{
|
|
new WorkOrderAuditLog { WorkOrderId = 1, FieldName = "Dispatch", Action = "Dispatch" },
|
|
new WorkOrderAuditLog { WorkOrderId = 2, FieldName = "Dispatch", Action = "Dispatch" }
|
|
});
|
|
|
|
context.WorkOrderAuditLogs.Should().HaveCount(2);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AddWorkOrderAttachmentsAsync_PersistsAllRowsInSingleSave()
|
|
{
|
|
await using var context = NewContext();
|
|
await SeedAsync(context);
|
|
var service = new WorkOrderDataService(context);
|
|
|
|
await service.AddWorkOrderAttachmentsAsync(new[]
|
|
{
|
|
new WorkOrderAttachments { WorkorderId = 1, Attachments = "https://files/a.pdf" },
|
|
new WorkOrderAttachments { WorkorderId = 1, Attachments = "https://files/b.pdf" }
|
|
});
|
|
|
|
context.workOrderAttachments.Should().HaveCount(2);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AddWorkOrderAttachmentsAsync_WithEmptyCollection_DoesNothing()
|
|
{
|
|
await using var context = NewContext();
|
|
await SeedAsync(context);
|
|
var service = new WorkOrderDataService(context);
|
|
|
|
await service.AddWorkOrderAttachmentsAsync(Array.Empty<WorkOrderAttachments>());
|
|
|
|
context.workOrderAttachments.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetByIdsOrderedAsync_PreservesInputOrderDeduplicatesAndSkipsMissing()
|
|
{
|
|
await using var context = NewContext();
|
|
context.Vendors.AddRange(
|
|
new Vendor { Id = 7, CompanyName = "V7", Email = "v7@x.io" },
|
|
new Vendor { Id = 8, CompanyName = "V8", Email = "v8@x.io" });
|
|
await context.SaveChangesAsync();
|
|
var service = new VendorDataService(context);
|
|
|
|
var result = await service.GetByIdsOrderedAsync(new[] { 8, 8, 7, 999 }, CancellationToken.None);
|
|
|
|
result.Select(v => v.Id).Should().Equal(new[] { 8, 7 });
|
|
}
|
|
}
|