feat(notification-outbox): add Notification EF configuration and DbSet

This commit is contained in:
Joseph Doherty
2026-05-19 00:55:58 -04:00
parent 87ac9b8a4d
commit 761595309b
3 changed files with 123 additions and 0 deletions

View File

@@ -243,6 +243,97 @@ public class NotificationRepositoryTests : IDisposable
}
}
public class NotificationOutboxConfigurationTests : IDisposable
{
private readonly ScadaLinkDbContext _context;
public NotificationOutboxConfigurationTests()
{
_context = SqliteTestHelper.CreateInMemoryContext();
}
public void Dispose()
{
_context.Database.CloseConnection();
_context.Dispose();
}
[Fact]
public async Task Notification_FullyPopulated_RoundTrips()
{
var id = Guid.NewGuid().ToString();
var siteEnqueuedAt = new DateTimeOffset(2026, 5, 19, 8, 0, 0, TimeSpan.Zero);
var createdAt = new DateTimeOffset(2026, 5, 19, 8, 0, 5, TimeSpan.Zero);
var lastAttemptAt = new DateTimeOffset(2026, 5, 19, 8, 1, 0, TimeSpan.Zero);
var nextAttemptAt = new DateTimeOffset(2026, 5, 19, 8, 2, 0, TimeSpan.Zero);
var deliveredAt = new DateTimeOffset(2026, 5, 19, 8, 3, 0, TimeSpan.Zero);
var notification = new Notification(id, NotificationType.Email, "Ops List",
"High Tank Level", "Tank 4 exceeded the high level threshold.", "site-north")
{
TypeData = "{\"channel\":\"email\"}",
Status = NotificationStatus.Retrying,
RetryCount = 3,
LastError = "SMTP timeout",
ResolvedTargets = "ops@example.test;duty@example.test",
SourceInstanceId = "instance-42",
SourceScript = "TankLevelAlarm",
SiteEnqueuedAt = siteEnqueuedAt,
CreatedAt = createdAt,
LastAttemptAt = lastAttemptAt,
NextAttemptAt = nextAttemptAt,
DeliveredAt = deliveredAt,
};
_context.Notifications.Add(notification);
await _context.SaveChangesAsync();
_context.ChangeTracker.Clear();
var loaded = await _context.Notifications.FindAsync(id);
Assert.NotNull(loaded);
Assert.Equal(id, loaded!.NotificationId);
Assert.Equal(NotificationType.Email, loaded.Type);
Assert.Equal(NotificationStatus.Retrying, loaded.Status);
Assert.Equal("Ops List", loaded.ListName);
Assert.Equal("High Tank Level", loaded.Subject);
Assert.Equal("Tank 4 exceeded the high level threshold.", loaded.Body);
Assert.Equal("{\"channel\":\"email\"}", loaded.TypeData);
Assert.Equal(3, loaded.RetryCount);
Assert.Equal("SMTP timeout", loaded.LastError);
Assert.Equal("ops@example.test;duty@example.test", loaded.ResolvedTargets);
Assert.Equal("site-north", loaded.SourceSiteId);
Assert.Equal("instance-42", loaded.SourceInstanceId);
Assert.Equal("TankLevelAlarm", loaded.SourceScript);
Assert.Equal(siteEnqueuedAt, loaded.SiteEnqueuedAt);
Assert.Equal(createdAt, loaded.CreatedAt);
Assert.Equal(lastAttemptAt, loaded.LastAttemptAt);
Assert.Equal(nextAttemptAt, loaded.NextAttemptAt);
Assert.Equal(deliveredAt, loaded.DeliveredAt);
}
[Fact]
public async Task Notification_StatusPersistsAsString()
{
var id = Guid.NewGuid().ToString();
var notification = new Notification(id, NotificationType.Email, "Ops List",
"Subject", "Body", "site-north")
{
Status = NotificationStatus.Parked,
};
_context.Notifications.Add(notification);
await _context.SaveChangesAsync();
_context.ChangeTracker.Clear();
var statusText = await _context.Database
.SqlQuery<string>($"SELECT Status AS Value FROM Notifications WHERE NotificationId = {id}")
.SingleAsync();
Assert.Equal("Parked", statusText);
}
}
public class SiteRepositoryTests : IDisposable
{
private readonly ScadaLinkDbContext _context;