shoc-backend/Api.SeaHavenIndustries.Tests/CommentDataServiceTests.cs
npal 6c4de10349 perf(work-orders): delete a work order's comments in one commit
DeleteWorkOrderCascadeAsync deleted comments one at a time: each call
re-fetched the row and committed separately (2N round trips, N commits).
Add DeleteAllForWorkOrderAsync, which loads the set once and removes it in
a single SaveChangesAsync.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-05 20:14:50 -05:00

47 lines
1.6 KiB
C#

using Data.SeaHavenIndustries;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using SeaHaven.DataServices.Implementation;
using Xunit;
namespace Api.SeaHavenIndustries.Tests;
public class CommentDataServiceTests
{
private static ApplicationDbContext NewContext()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
return new ApplicationDbContext(options);
}
[Fact]
public async Task DeleteAllForWorkOrderAsync_RemovesOnlyThatWorkOrdersCommentsInOneSave()
{
await using var context = NewContext();
context.Comments.AddRange(
new Comments { Id = 1, WorkerOrderId = 5, Commenttext = "a" },
new Comments { Id = 2, WorkerOrderId = 5, Commenttext = "b" },
new Comments { Id = 3, WorkerOrderId = 9, Commenttext = "c" });
await context.SaveChangesAsync();
var service = new CommentDataService(context);
await service.DeleteAllForWorkOrderAsync(5);
context.Comments.Select(c => c.Id).Should().Equal(new[] { 3 });
}
[Fact]
public async Task DeleteAllForWorkOrderAsync_WithNoComments_DoesNothing()
{
await using var context = NewContext();
context.Comments.Add(new Comments { Id = 1, WorkerOrderId = 9, Commenttext = "untouched" });
await context.SaveChangesAsync();
var service = new CommentDataService(context);
await service.DeleteAllForWorkOrderAsync(5);
context.Comments.Select(c => c.Id).Should().Equal(new[] { 1 });
}
}