62 lines
2.9 KiB
C#
62 lines
2.9 KiB
C#
using FluentAssertions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
using SlpModularCms.Modules.Contact.Data;
|
|
using SlpModularCms.Modules.Contact.Repositories;
|
|
|
|
namespace SlpModularCms.Modules.Contact.Tests;
|
|
|
|
/// <summary>
|
|
/// The purge queries run at start-up (the retention service) and used a 'Take' without an 'OrderBy', which EF Core
|
|
/// reports as RowLimitingOperationWithoutOrderByWarning (it showed up in Sentry after every deploy). The in-memory
|
|
/// provider never reports it, so this uses the real relational provider and turns the warning into an exception: the
|
|
/// query is compiled before any connection is opened, so an unreachable server shows that compilation succeeded.
|
|
/// </summary>
|
|
public class ContactRetentionQueryTests
|
|
{
|
|
private static ContactDbContext RelationalContextThatThrowsOnRowLimitWarning() => new(
|
|
new DbContextOptionsBuilder<ContactDbContext>()
|
|
.UseMySQL("Server=127.0.0.1;Port=1;Database=x;Uid=x;Pwd=x;Connection Timeout=1")
|
|
.ConfigureWarnings(w => w.Throw(CoreEventId.RowLimitingOperationWithoutOrderByWarning))
|
|
.Options);
|
|
|
|
[Fact]
|
|
public async Task Purging_expired_messages_has_a_deterministic_order()
|
|
{
|
|
await using var context = RelationalContextThatThrowsOnRowLimitWarning();
|
|
var sut = new ContactRepository(context);
|
|
|
|
var act = () => sut.DeleteExpiredMessagesAsync(DateTimeOffset.UtcNow, CancellationToken.None);
|
|
|
|
var thrown = (await act.Should().ThrowAsync<Exception>()).Which;
|
|
thrown.ToString().Should().NotContain("RowLimitingOperationWithoutOrderByWarning");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Purging_old_idempotency_records_has_a_deterministic_order()
|
|
{
|
|
await using var context = RelationalContextThatThrowsOnRowLimitWarning();
|
|
var sut = new ContactRepository(context);
|
|
|
|
var act = () => sut.DeleteIdempotencyOlderThanAsync(DateTimeOffset.UtcNow, CancellationToken.None);
|
|
|
|
var thrown = (await act.Should().ThrowAsync<Exception>()).Which;
|
|
thrown.ToString().Should().NotContain("RowLimitingOperationWithoutOrderByWarning");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task The_message_list_and_the_mail_queue_have_a_deterministic_order()
|
|
{
|
|
await using var context = RelationalContextThatThrowsOnRowLimitWarning();
|
|
var sut = new ContactRepository(context);
|
|
|
|
var list = () => sut.ListAsync("s", [Data.Entities.ContactMessageStatus.New], 0, 25, CancellationToken.None);
|
|
var due = () => sut.GetDueForMailAsync("s", DateTimeOffset.UtcNow, 6, 10, CancellationToken.None);
|
|
|
|
(await list.Should().ThrowAsync<Exception>()).Which.ToString()
|
|
.Should().NotContain("RowLimitingOperationWithoutOrderByWarning");
|
|
(await due.Should().ThrowAsync<Exception>()).Which.ToString()
|
|
.Should().NotContain("RowLimitingOperationWithoutOrderByWarning");
|
|
}
|
|
}
|