diff --git a/OpenAlprWebhookProcessor.Server/Alerts/GetAlerts/GetAlertsRequestHandler.cs b/OpenAlprWebhookProcessor.Server/Alerts/GetAlerts/GetAlertsRequestHandler.cs index a14aad23..1d5906db 100644 --- a/OpenAlprWebhookProcessor.Server/Alerts/GetAlerts/GetAlertsRequestHandler.cs +++ b/OpenAlprWebhookProcessor.Server/Alerts/GetAlerts/GetAlertsRequestHandler.cs @@ -19,7 +19,7 @@ public async Task> HandleAsync(CancellationToken cancellationToken) { var alerts = new List(); - foreach (var dbAlert in await _processorContext.Alerts.ToListAsync()) + foreach (var dbAlert in await _processorContext.Alerts.ToListAsync(cancellationToken)) { var alert = new Alert() { diff --git a/Tests/AgentTests.cs b/Tests/AgentTests.cs index 9cc50e3a..0472c71e 100644 --- a/Tests/AgentTests.cs +++ b/Tests/AgentTests.cs @@ -17,7 +17,7 @@ public async Task AgentRequestSucceeds() { using (var processorContext = _contextCreator.CreateContext()) { - if (processorContext.Database.EnsureCreated()) + if (await processorContext.Database.EnsureCreatedAsync()) { processorContext.Agents.Add(new OpenAlprWebhookProcessor.Data.Agent() { diff --git a/Tests/Alerts/GetAlerts/GetAlerts.cs b/Tests/Alerts/GetAlerts/GetAlerts.cs new file mode 100644 index 00000000..1362a470 --- /dev/null +++ b/Tests/Alerts/GetAlerts/GetAlerts.cs @@ -0,0 +1,58 @@ +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using OpenAlprWebhookProcessor.Alerts; +using OpenAlprWebhookProcessor.Data; +using System.Data.Common; + +namespace Tests.Alerts.GetAlerts +{ + public class GetAlertsTests + { + private ProcessorContext _context; + private DbContextOptions _dbContextOptions; + private DbConnection _connection; + + [SetUp] + public async Task SetupAsync() + { + _connection = new SqliteConnection("Filename=:memory:"); + await _connection.OpenAsync(); + + _dbContextOptions = new DbContextOptionsBuilder() + .UseSqlite(_connection) + .Options; + + _context = new ProcessorContext(_dbContextOptions); + await _context.Database.EnsureCreatedAsync(); + + _context.Alerts.AddRange( + new OpenAlprWebhookProcessor.Data.Alert { Id = Guid.NewGuid(), PlateNumber = "ABC123", IsStrictMatch = true, Description = "Stolen vehicle" }, + new OpenAlprWebhookProcessor.Data.Alert { Id = Guid.NewGuid(), PlateNumber = "XYZ789", IsStrictMatch = false, Description = "Suspicious activity" } + ); + await _context.SaveChangesAsync(); + } + + [TearDown] + public void TearDown() + { + _context?.Dispose(); + _connection?.Close(); + _connection?.Dispose(); + } + + [Test] + public async Task HandleAsync_ReturnsAllAlerts() + { + // Arrange + var handler = new GetAlertsRequestHandler(_context); + var cancellationToken = CancellationToken.None; + + // Act + var result = await handler.HandleAsync(cancellationToken); + + // Assert + Assert.That(result, Is.Not.Null); + Assert.That(result, Has.Count.EqualTo(2)); + } + } +} \ No newline at end of file