feat(data): add WeekReportRepository with tests

This commit is contained in:
mika kuns
2026-06-03 09:34:03 +02:00
parent 1d7b86dbef
commit 992fbf0763
2 changed files with 81 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
using ClaudeDo.Data.Models;
using Microsoft.EntityFrameworkCore;
namespace ClaudeDo.Data.Repositories;
public sealed class WeekReportRepository
{
private readonly ClaudeDoDbContext _context;
public WeekReportRepository(ClaudeDoDbContext context) => _context = context;
public async Task<WeekReportEntity?> GetByRangeAsync(
DateOnly start, DateOnly end, CancellationToken ct = default) =>
await _context.WeekReports.AsNoTracking()
.FirstOrDefaultAsync(r => r.StartDate == start && r.EndDate == end, ct);
public async Task UpsertAsync(DateOnly start, DateOnly end, string markdown, CancellationToken ct = default)
{
var row = await _context.WeekReports
.FirstOrDefaultAsync(r => r.StartDate == start && r.EndDate == end, ct);
if (row is null)
{
_context.WeekReports.Add(new WeekReportEntity
{
StartDate = start,
EndDate = end,
Markdown = markdown,
GeneratedAt = DateTime.UtcNow,
});
}
else
{
row.Markdown = markdown;
row.GeneratedAt = DateTime.UtcNow;
}
await _context.SaveChangesAsync(ct);
}
}

View File

@@ -0,0 +1,43 @@
using ClaudeDo.Data.Repositories;
using ClaudeDo.Worker.Tests.Infrastructure;
namespace ClaudeDo.Worker.Tests.Repositories;
public class WeekReportRepositoryTests : IDisposable
{
private readonly DbFixture _db = new();
public void Dispose() => _db.Dispose();
private static readonly DateOnly Start = new(2026, 5, 28);
private static readonly DateOnly End = new(2026, 6, 3);
[Fact]
public async Task Upsert_Insert_Then_GetByRange_RoundTrips()
{
using (var ctx = _db.CreateContext())
await new WeekReportRepository(ctx).UpsertAsync(Start, End, "# Report");
using var read = _db.CreateContext();
var row = await new WeekReportRepository(read).GetByRangeAsync(Start, End);
Assert.NotNull(row);
Assert.Equal("# Report", row!.Markdown);
}
[Fact]
public async Task Upsert_Existing_Overwrites_Markdown()
{
using (var ctx = _db.CreateContext())
await new WeekReportRepository(ctx).UpsertAsync(Start, End, "old");
using (var ctx = _db.CreateContext())
await new WeekReportRepository(ctx).UpsertAsync(Start, End, "new");
using var read = _db.CreateContext();
var row = await new WeekReportRepository(read).GetByRangeAsync(Start, End);
Assert.Equal("new", row!.Markdown);
}
[Fact]
public async Task GetByRange_MissingRange_ReturnsNull()
{
using var read = _db.CreateContext();
Assert.Null(await new WeekReportRepository(read).GetByRangeAsync(Start, End));
}
}