291 lines
15 KiB
C#
291 lines
15 KiB
C#
using System.Data.Common;
|
|
using System.Text.Json;
|
|
using JobTrackerApi.Data;
|
|
using JobTrackerApi.Models;
|
|
using JobTrackerApi.Services;
|
|
using Microsoft.Data.Sqlite;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
using Moq;
|
|
using Xunit;
|
|
using Xunit.Abstractions;
|
|
|
|
namespace JobTrackerApi.Tests;
|
|
|
|
public sealed class AnalyticsStatsQueryTests(ITestOutputHelper output)
|
|
{
|
|
[Theory]
|
|
[InlineData(0)]
|
|
[InlineData(250)]
|
|
[InlineData(2500)]
|
|
public async Task Stats_keep_narrow_server_aggregates_with_two_fewer_roundtrips(int size)
|
|
{
|
|
await using var fixture = await Fixture.Create();
|
|
var now = DateTime.Now;
|
|
fixture.Db.JobApplications.AddRange(Enumerable.Range(0, size).Select(i => fixture.Job("owner", "Applied", now.AddDays(-10))));
|
|
fixture.Db.JobApplications.Add(fixture.Job("foreign", "Private", now));
|
|
fixture.Db.JobApplications.Add(fixture.Job(null, "Unowned", now));
|
|
await fixture.Db.SaveChangesAsync();
|
|
fixture.Db.ChangeTracker.Clear();
|
|
fixture.Recorder.Commands.Clear();
|
|
|
|
var stats = await new AnalyticsService(fixture.Db).GetStatsAsync(default);
|
|
Assert.Equal(size, stats.Total);
|
|
Assert.Equal(size, stats.Active);
|
|
Assert.Equal(0, stats.Deleted);
|
|
Assert.Equal(size, stats.AppliedLast30Days);
|
|
Assert.Equal(size == 0 ? 0 : 10, stats.AverageDaysSinceApplied);
|
|
Assert.Equal(size == 0 ? 0 : 1, stats.ByStatus.Count);
|
|
if (size > 0) Assert.Equal(size, stats.ByStatus["Applied"]);
|
|
Assert.Empty(fixture.Db.ChangeTracker.Entries());
|
|
|
|
var rows = await fixture.Recorder.ReplayRows(fixture.Connection);
|
|
output.WriteLine(JsonSerializer.Serialize(new { Provider = "SQLite", OwnedRows = size, Commands = fixture.Recorder.Commands.Count, ReturnedRowsPerCommand = rows, Sql = fixture.Recorder.Commands.Select(x => x.Sql) }));
|
|
Assert.All(fixture.Recorder.Commands, command =>
|
|
{
|
|
Assert.DoesNotContain("Description", command.Sql);
|
|
Assert.DoesNotContain("CoverLetterText", command.Sql);
|
|
Assert.DoesNotContain("JobTitle", command.Sql);
|
|
});
|
|
Assert.Equal(size == 0 ? 2 : 3, fixture.Recorder.Commands.Count);
|
|
Assert.Equal(size == 0 ? 0 : size + 2, rows.Sum());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Stats_preserve_deleted_nullable_future_dates_status_keys_and_live_owner_filters()
|
|
{
|
|
await using var fixture = await Fixture.Create();
|
|
var now = DateTime.Now;
|
|
var statuses = new[] { "Applied", "Applied", "applied", "", " ", "Unknown", " Applied " };
|
|
DateTime?[] dates = [now.AddDays(-29.5), now.AddDays(-30.5), now.AddDays(-10.375), now.AddDays(2), null, null, null];
|
|
fixture.Db.JobApplications.AddRange(statuses.Select((status, i) => fixture.Job("owner", status, dates[i])));
|
|
fixture.Db.JobApplications.Add(fixture.Job("owner", "Deleted", now, deleted: true));
|
|
fixture.Db.JobApplications.Add(fixture.Job("owner", "Deleted", null, deleted: true));
|
|
fixture.Db.JobApplications.Add(fixture.Job("foreign", "Private", now.AddDays(-1)));
|
|
fixture.Db.JobApplications.Add(fixture.Job(null, "Unowned", now));
|
|
await fixture.Db.SaveChangesAsync();
|
|
|
|
var service = new AnalyticsService(fixture.Db);
|
|
var stats = await service.GetStatsAsync(default);
|
|
Assert.Equal(9, stats.Total);
|
|
Assert.Equal(7, stats.Active);
|
|
Assert.Equal(2, stats.Deleted);
|
|
Assert.Equal(3, stats.AppliedLast30Days); // Future dates still satisfy the existing lower bound.
|
|
Assert.Equal(17.6, stats.AverageDaysSinceApplied); // Nulls excluded; future age clamped to zero.
|
|
Assert.Equal(3, stats.ByStatus["Unknown"]);
|
|
Assert.Equal(2, stats.ByStatus["Applied"]);
|
|
Assert.Equal(1, stats.ByStatus["applied"]);
|
|
Assert.Equal(1, stats.ByStatus[" Applied "]);
|
|
Assert.Equal(4, stats.ByStatus.Count);
|
|
|
|
fixture.User.SetupGet(x => x.UserId).Returns("foreign");
|
|
Assert.Equal(1, (await service.GetStatsAsync(default)).Total);
|
|
fixture.User.SetupGet(x => x.UserId).Returns((string?)null);
|
|
var anonymous = await service.GetStatsAsync(default);
|
|
Assert.Equal(0, anonymous.Total);
|
|
Assert.Equal(0, anonymous.Active);
|
|
Assert.Equal(0, anonymous.AppliedLast30Days);
|
|
Assert.Equal(0, anonymous.AverageDaysSinceApplied);
|
|
Assert.Empty(anonymous.ByStatus);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(true)]
|
|
[InlineData(false)]
|
|
public async Task Deleted_only_and_null_applied_dates_keep_zero_averages(bool deleted)
|
|
{
|
|
await using var fixture = await Fixture.Create();
|
|
fixture.Db.JobApplications.Add(fixture.Job("owner", "Saved", null, deleted));
|
|
await fixture.Db.SaveChangesAsync();
|
|
fixture.Recorder.Commands.Clear();
|
|
var stats = await new AnalyticsService(fixture.Db).GetStatsAsync(default);
|
|
Assert.Equal(1, stats.Total);
|
|
Assert.Equal(deleted ? 0 : 1, stats.Active);
|
|
Assert.Equal(deleted ? 1 : 0, stats.Deleted);
|
|
Assert.Equal(0, stats.AppliedLast30Days);
|
|
Assert.Equal(0, stats.AverageDaysSinceApplied);
|
|
Assert.Equal(deleted ? 2 : 3, fixture.Recorder.Commands.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Applied_volume_keeps_the_exact_thirty_day_lower_bound_inclusive()
|
|
{
|
|
await using var fixture = await Fixture.Create();
|
|
var atBoundary = fixture.Job("owner", "Applied", null);
|
|
var beforeBoundary = fixture.Job("owner", "Applied", null);
|
|
fixture.Db.JobApplications.AddRange(atBoundary, beforeBoundary);
|
|
await fixture.Db.SaveChangesAsync();
|
|
var setBoundary = false;
|
|
fixture.Recorder.BeforeReader = () =>
|
|
{
|
|
var parameter = fixture.Recorder.Commands.Last().Parameters.FirstOrDefault(x => x.Key.Contains("last30", StringComparison.Ordinal));
|
|
if (setBoundary || parameter.Value is not DateTime cutoff) return;
|
|
// Use the service's captured cutoff, so this assertion cannot drift with the clock.
|
|
using var command = fixture.Connection.CreateCommand();
|
|
command.CommandText = "UPDATE JobApplications SET DateApplied = $date WHERE Id = $id";
|
|
command.Parameters.AddWithValue("$date", cutoff);
|
|
command.Parameters.AddWithValue("$id", atBoundary.Id);
|
|
command.ExecuteNonQuery();
|
|
command.Parameters["$date"].Value = cutoff.AddTicks(-1);
|
|
command.Parameters["$id"].Value = beforeBoundary.Id;
|
|
command.ExecuteNonQuery();
|
|
setBoundary = true;
|
|
};
|
|
var stats = await new AnalyticsService(fixture.Db).GetStatsAsync(default);
|
|
Assert.True(setBoundary);
|
|
Assert.Equal(1, stats.AppliedLast30Days);
|
|
Assert.Equal(30, stats.AverageDaysSinceApplied);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Repeated_stats_observe_mutations_without_caching()
|
|
{
|
|
await using var fixture = await Fixture.Create();
|
|
var job = fixture.Job("owner", "Saved", null);
|
|
fixture.Db.JobApplications.Add(job);
|
|
await fixture.Db.SaveChangesAsync();
|
|
var service = new AnalyticsService(fixture.Db);
|
|
var before = await service.GetStatsAsync(default);
|
|
Assert.Equal(1, before.Active);
|
|
job.IsDeleted = true;
|
|
job.DateApplied = DateTime.Now;
|
|
await fixture.Db.SaveChangesAsync();
|
|
var after = await service.GetStatsAsync(default);
|
|
Assert.Equal(1, after.Total);
|
|
Assert.Equal(0, after.Active);
|
|
Assert.Empty(after.ByStatus);
|
|
Assert.Equal(1, before.ByStatus["Saved"]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Cancellation_reaches_the_native_command_and_stops_later_queries()
|
|
{
|
|
await using var fixture = await Fixture.Create();
|
|
using var cancellation = new CancellationTokenSource();
|
|
fixture.Recorder.Commands.Clear();
|
|
fixture.Recorder.BeforeReader = () => cancellation.Cancel();
|
|
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => new AnalyticsService(fixture.Db).GetStatsAsync(cancellation.Token));
|
|
Assert.Single(fixture.Recorder.Commands);
|
|
Assert.Equal(cancellation.Token, fixture.Recorder.LastToken);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Pomelo_generates_the_actual_stats_aggregate_without_opening_a_database()
|
|
{
|
|
var connectionGuard = new ClosedConnectionGuard();
|
|
var capture = new PomeloCommandCapture();
|
|
var options = new DbContextOptionsBuilder<JobTrackerContext>()
|
|
.UseMySql("Server=127.0.0.1;Database=translation_only;User=none;Password=none;", new MariaDbServerVersion(new Version(12, 2, 2)))
|
|
.AddInterceptors(connectionGuard, capture).Options;
|
|
var user = new Mock<ICurrentUserService>();
|
|
user.SetupGet(x => x.UserId).Returns("owner");
|
|
await using var db = new JobTrackerContext(options, user.Object);
|
|
await Assert.ThrowsAsync<CapturedSqlException>(() => new AnalyticsService(db).GetStatsAsync(default));
|
|
Assert.Equal(System.Data.ConnectionState.Closed, db.Database.GetDbConnection().State);
|
|
Assert.Equal(1, connectionGuard.SuppressedOpenRequests);
|
|
Assert.Contains("COUNT(", capture.Sql);
|
|
Assert.Contains("CASE", capture.Sql);
|
|
Assert.Contains("`OwnerUserId`", capture.Sql);
|
|
Assert.Contains("`DateApplied`", capture.Sql);
|
|
Assert.Contains(">=", capture.Sql);
|
|
Assert.Contains("GROUP BY", capture.Sql);
|
|
Assert.DoesNotContain("Description", capture.Sql);
|
|
output.WriteLine("Pomelo SQL compiled but not executed: " + capture.Sql);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Legacy_null_status_still_merges_into_unknown()
|
|
{
|
|
var options = new DbContextOptionsBuilder<JobTrackerContext>().UseInMemoryDatabase(Guid.NewGuid().ToString(), config => config.EnableNullChecks(false)).Options;
|
|
var user = new Mock<ICurrentUserService>();
|
|
user.SetupGet(x => x.UserId).Returns("owner");
|
|
await using var db = new JobTrackerContext(options, user.Object);
|
|
db.JobApplications.AddRange(new JobApplication { OwnerUserId = "owner", Status = null! }, new JobApplication { OwnerUserId = "owner", Status = "Unknown" });
|
|
await db.SaveChangesAsync();
|
|
var stats = await new AnalyticsService(db).GetStatsAsync(default);
|
|
Assert.Equal(2, stats.ByStatus["Unknown"]);
|
|
}
|
|
|
|
private sealed class Fixture(SqliteConnection connection, JobTrackerContext db, Mock<ICurrentUserService> user, CommandRecorder recorder) : IAsyncDisposable
|
|
{
|
|
public SqliteConnection Connection => connection;
|
|
public JobTrackerContext Db => db;
|
|
public Mock<ICurrentUserService> User => user;
|
|
public CommandRecorder Recorder => recorder;
|
|
public JobApplication Job(string? owner, string status, DateTime? applied, bool deleted = false) => new()
|
|
{
|
|
OwnerUserId = owner, CompanyId = 1, JobTitle = "Synthetic", Status = status, DateApplied = applied, IsDeleted = deleted,
|
|
Description = new string('x', 4096), CoverLetterText = new string('y', 4096)
|
|
};
|
|
public static async Task<Fixture> Create()
|
|
{
|
|
var connection = new SqliteConnection("Data Source=:memory:");
|
|
await connection.OpenAsync();
|
|
var recorder = new CommandRecorder();
|
|
var user = new Mock<ICurrentUserService>();
|
|
user.SetupGet(x => x.UserId).Returns("owner");
|
|
var db = new JobTrackerContext(new DbContextOptionsBuilder<JobTrackerContext>().UseSqlite(connection).AddInterceptors(recorder).Options, user.Object);
|
|
await db.Database.EnsureCreatedAsync();
|
|
db.Users.AddRange(new ApplicationUser { Id = "owner" }, new ApplicationUser { Id = "foreign" });
|
|
db.Companies.Add(new Company { Id = 1, Name = "Synthetic", OwnerUserId = "owner" });
|
|
await db.SaveChangesAsync();
|
|
recorder.Commands.Clear();
|
|
return new Fixture(connection, db, user, recorder);
|
|
}
|
|
public async ValueTask DisposeAsync() { await db.DisposeAsync(); await connection.DisposeAsync(); }
|
|
}
|
|
|
|
private sealed class CommandRecorder : DbCommandInterceptor
|
|
{
|
|
public List<(string Sql, Dictionary<string, object?> Parameters)> Commands { get; } = [];
|
|
public Action? BeforeReader { get; set; }
|
|
public CancellationToken LastToken { get; private set; }
|
|
public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, CancellationToken cancellationToken = default)
|
|
{
|
|
Commands.Add((command.CommandText, command.Parameters.Cast<DbParameter>().ToDictionary(p => p.ParameterName, p => p.Value)));
|
|
LastToken = cancellationToken;
|
|
BeforeReader?.Invoke();
|
|
return ValueTask.FromResult(result);
|
|
}
|
|
public async Task<List<int>> ReplayRows(SqliteConnection connection)
|
|
{
|
|
var rows = new List<int>();
|
|
foreach (var captured in Commands)
|
|
{
|
|
await using var command = connection.CreateCommand();
|
|
command.CommandText = captured.Sql;
|
|
foreach (var parameter in captured.Parameters) command.Parameters.AddWithValue(parameter.Key, parameter.Value ?? DBNull.Value);
|
|
var count = 0;
|
|
await using var reader = await command.ExecuteReaderAsync();
|
|
while (await reader.ReadAsync()) count++;
|
|
rows.Add(count);
|
|
}
|
|
return rows;
|
|
}
|
|
}
|
|
|
|
private sealed class CapturedSqlException : Exception;
|
|
|
|
private sealed class ClosedConnectionGuard : DbConnectionInterceptor
|
|
{
|
|
public int SuppressedOpenRequests { get; private set; }
|
|
public override InterceptionResult ConnectionOpening(DbConnection connection, ConnectionEventData eventData, InterceptionResult result)
|
|
{
|
|
SuppressedOpenRequests++;
|
|
return InterceptionResult.Suppress();
|
|
}
|
|
public override ValueTask<InterceptionResult> ConnectionOpeningAsync(DbConnection connection, ConnectionEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default)
|
|
=> ValueTask.FromResult(ConnectionOpening(connection, eventData, result));
|
|
}
|
|
|
|
private sealed class PomeloCommandCapture : DbCommandInterceptor
|
|
{
|
|
public string Sql { get; private set; } = "";
|
|
public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, CancellationToken cancellationToken = default)
|
|
{
|
|
Sql = command.CommandText;
|
|
throw new CapturedSqlException();
|
|
}
|
|
}
|
|
}
|