mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-06 18:12:11 +00:00
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>
47 lines
1.6 KiB
C#
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 });
|
|
}
|
|
}
|