a6edec2b12
ci / build-test (push) Successful in 2m45s
Correctness/data: - Fix demo cost double-count: reference importer no longer imports the Kosten Strom/Wasser columns for categories that are metered (only Heizung), so Wasser rollup is 70€ not 140€. - Spurious-decrease guard: only a reset/swap in the window (prevReading, thisReading] explains a decrease — an old historical reset no longer permanently disables the guard. - Gate swap auto-detection on MappingProfile.DetectCumulativeSwaps (flag was ignored). - Prorate basePrice by bucket length (day/month/year); guard virtual expressions against NaN/Inf. Concurrency/infra: - Blazor: register a DbContextFactory; CostService/DashboardService and the read pages now use short-lived per-operation contexts (no shared circuit DbContext); guard Trends re-entrancy. - /events: wrap event insert + consumption recompute in one transaction (atomic); 404 (not 500) on unknown meter. - MQTT worker: subscribe to newly-added topics on each tick; move client cleanup into finally. - Migrations: CREATE MATERIALIZED VIEW IF NOT EXISTS + if_not_exists on CAgg/compression/ hypertable calls (re-run-safe after a mid-migration crash). - HA worker: prune stale poll-schedule entries; export: null dangling ImportBatchIds on restore. API/security: - API fail-closed by default: with no keys and AllowAnonymousApi off, /api/v1 returns 401 (protects /export and /import). New MeterVault:AllowAnonymousApi opt-in. - Cap /readings batch at 5000; report ignored (unknown-meter) count; enums as strings in JSON. +4 regression tests (guard window, API closed, /events 404, no demo double-count). 98 tests green; Docker deploy re-verified healthy with the API fail-closed. Claude-Session: https://claude.ai/code/session_01WujdMtMJPbxDpDnMeK22rr
57 lines
2.5 KiB
C#
57 lines
2.5 KiB
C#
using MeterVault.Infrastructure.Persistence;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using Testcontainers.PostgreSql;
|
||
|
||
namespace MeterVault.Integration.Tests;
|
||
|
||
/// <summary>
|
||
/// Spins up exactly one TimescaleDB container per test run (shared via the "Timescale"
|
||
/// collection) and applies migrations once. Tests reuse it and isolate themselves with Respawn.
|
||
/// The image tag is pinned: the compression DDL (add_compression_policy) was renamed toward
|
||
/// add_columnstore_policy in newer Timescale, so a floating tag would risk breaking migrations.
|
||
/// </summary>
|
||
public sealed class TimescaleFixture : IAsyncLifetime, IDbContextFactory<MeterVaultDbContext>
|
||
{
|
||
private readonly PostgreSqlContainer _db = new PostgreSqlBuilder("timescale/timescaledb:2.17.2-pg16")
|
||
.WithDatabase("metervault")
|
||
.WithUsername("metervault")
|
||
.WithPassword("metervault")
|
||
.Build();
|
||
|
||
public string ConnectionString => _db.GetConnectionString();
|
||
|
||
public MeterVaultDbContext CreateContext()
|
||
{
|
||
var options = new DbContextOptionsBuilder<MeterVaultDbContext>()
|
||
.UseNpgsql(ConnectionString, npgsql =>
|
||
npgsql.MigrationsAssembly(typeof(MeterVaultDbContext).Assembly.FullName))
|
||
.UseSnakeCaseNamingConvention()
|
||
.Options;
|
||
return new MeterVaultDbContext(options);
|
||
}
|
||
|
||
/// <summary>Lets tests construct services that take an <see cref="IDbContextFactory{TContext}"/>.</summary>
|
||
public MeterVaultDbContext CreateDbContext() => CreateContext();
|
||
|
||
public async Task InitializeAsync()
|
||
{
|
||
await _db.StartAsync();
|
||
await using var ctx = CreateContext();
|
||
await ctx.Database.MigrateAsync();
|
||
|
||
// The fixtures are historical (2022–2023), so freshly-inserted chunks immediately fall
|
||
// past the 30-day compression horizon. Pause the compression job's scheduler so its
|
||
// background worker can't deadlock a test's import transaction. The policy still exists
|
||
// (SchemaTests verifies that); only its scheduling is disabled.
|
||
await ctx.Database.ExecuteSqlRawAsync(
|
||
"SELECT alter_job(job_id, scheduled => false) FROM timescaledb_information.jobs " +
|
||
"WHERE proc_name IN ('policy_compression', 'policy_columnstore');");
|
||
}
|
||
|
||
public async Task DisposeAsync() => await _db.DisposeAsync();
|
||
}
|
||
|
||
/// <summary>Binds the shared <see cref="TimescaleFixture"/> to all tests in the "Timescale" collection.</summary>
|
||
[CollectionDefinition("Timescale")]
|
||
public sealed class TimescaleCollection : ICollectionFixture<TimescaleFixture>;
|