M4: aggregation + tariff-aware cost engine

- ContinuousAggregates migration: consumption_daily/monthly/yearly in Europe/Berlin
  buckets via migrationBuilder.Sql(..., suppressTransaction: true), with refresh policies
  (end_offset >= 1 bucket; current bucket covered by real-time aggregation).
- TariffResolver (Core): time-ranged price resolution, scope precedence meter > type > global.
- CostService: Dapper-aggregated monthly consumption × resolved unit price (+base, -feed-in),
  month-dominant pricing; category rollups over member meters + meterless manual costs.
- Tests: water cost reconciles to the sheet's Kosten column, Wasser category rollup (Dez=70€),
  monthly CAgg refresh matches base, TariffResolver unit tests.

91 tests green (56 Core + 35 integration).

Claude-Session: https://claude.ai/code/session_01WujdMtMJPbxDpDnMeK22rr
This commit is contained in:
2026-07-13 11:52:53 +02:00
parent 4b0cad67df
commit 6fc710d55a
8 changed files with 1363 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
using MeterVault.Core.Costing;
using MeterVault.Core.Domain;
namespace MeterVault.Core.Tests;
public sealed class TariffResolverTests
{
private static Tariff Unit(double value, TariffScope scope, int? scopeId, DateOnly from, DateOnly? to = null) => new()
{
Component = TariffComponent.UnitPrice,
Value = value,
Unit = "EUR/kWh",
ScopeType = scope,
ScopeId = scopeId,
ValidFrom = from,
ValidTo = to,
};
[Fact]
public void Picks_the_price_valid_for_the_date()
{
// Electricity price history from the Strom sheet.
var tariffs = new[]
{
Unit(0.16, TariffScope.EnergyType, 1, new DateOnly(2022, 9, 1), new DateOnly(2022, 12, 31)),
Unit(0.44, TariffScope.EnergyType, 1, new DateOnly(2023, 1, 1), new DateOnly(2023, 4, 30)),
Unit(0.37, TariffScope.EnergyType, 1, new DateOnly(2023, 5, 1)),
};
Assert.Equal(0.16, TariffResolver.ResolveValue(tariffs, TariffComponent.UnitPrice, 5, 1, new DateOnly(2022, 10, 15)));
Assert.Equal(0.44, TariffResolver.ResolveValue(tariffs, TariffComponent.UnitPrice, 5, 1, new DateOnly(2023, 2, 15)));
Assert.Equal(0.37, TariffResolver.ResolveValue(tariffs, TariffComponent.UnitPrice, 5, 1, new DateOnly(2024, 6, 15)));
}
[Fact]
public void Meter_scope_overrides_energy_type_and_global()
{
var tariffs = new[]
{
Unit(0.30, TariffScope.Global, null, new DateOnly(2023, 1, 1)),
Unit(0.40, TariffScope.EnergyType, 1, new DateOnly(2023, 1, 1)),
Unit(0.50, TariffScope.Meter, 7, new DateOnly(2023, 1, 1)),
};
Assert.Equal(0.50, TariffResolver.ResolveValue(tariffs, TariffComponent.UnitPrice, 7, 1, new DateOnly(2023, 6, 1)));
Assert.Equal(0.40, TariffResolver.ResolveValue(tariffs, TariffComponent.UnitPrice, 8, 1, new DateOnly(2023, 6, 1)));
Assert.Equal(0.30, TariffResolver.ResolveValue(tariffs, TariffComponent.UnitPrice, 8, 2, new DateOnly(2023, 6, 1)));
}
[Fact]
public void Returns_zero_when_nothing_applies()
{
var tariffs = new[] { Unit(0.25, TariffScope.EnergyType, 1, new DateOnly(2024, 1, 1)) };
Assert.Equal(0d, TariffResolver.ResolveValue(tariffs, TariffComponent.UnitPrice, 1, 1, new DateOnly(2023, 1, 1)));
}
}