AccountingNext/Schaad.Accounting.Tests/BillPeriodTestShould.cs
Claudio Schaad 1e9e19ebad Add "Wasser" page for the water bills
Same layout as the Strom page: grid and dialog for the bills (warm and
cold water in m3 and CHF, base costs; total m3 and CHF are computed),
stored per mandator in water.json, and charts for consumption and total
costs with the period toggle (bill, calendar year, half-year).

The period grouping is now generic: ElectricityPeriod becomes
BillPeriod<T> over IPeriodBill<T>, implemented by ElectricityBill and
WaterBill. water.json can be edited in the JSON editor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 20:56:18 +01:00

142 lines
4.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Schaad.Accounting.Models;
using Shouldly;
namespace Schaad.Accounting.Tests;
public class BillPeriodTestShould
{
[Fact]
public void KeepEachBillUnchangedWhenGroupingPerBill()
{
var later = YearlyBill(2024);
var first = YearlyBill(2023);
var periods = BillPeriod<ElectricityBill>.FromBills([later, first], BillPeriodKind.Bill);
periods.Select(p => p.Bill).ShouldBe([first, later]);
periods.ShouldAllBe(p => !p.IsSplit);
}
[Fact]
public void LabelBillAsBillingYearWhenItCoversAWholeYear()
{
var periods = BillPeriod<ElectricityBill>.FromBills([YearlyBill(2023)], BillPeriodKind.Bill);
periods[0].Label.ShouldBe("23/24");
}
[Fact]
public void LabelBillWithDatesWhenItIsNotAWholeYear()
{
var bill = Bill(new DateOnly(2024, 1, 1), new DateOnly(2024, 6, 30));
var periods = BillPeriod<ElectricityBill>.FromBills([bill], BillPeriodKind.Bill);
periods[0].Label.ShouldBe("01.01.24–30.06.24");
}
[Fact]
public void SplitYearlyBillIntoTwoHalvesWhenPeriodRunsJulyToJune()
{
var bill = YearlyBill(2023);
bill.GridKwh = 3000m;
bill.PvKwh = 2000m;
bill.GridChf = 900m;
bill.PvChf = 400m;
bill.SideCostsChf = 100m;
var halves = BillPeriod<ElectricityBill>.FromBills([bill], BillPeriodKind.HalfYear);
halves.Count.ShouldBe(2);
halves[0].From.ShouldBe(new DateOnly(2023, 7, 1));
halves[1].From.ShouldBe(new DateOnly(2024, 1, 1));
foreach (var half in halves)
{
half.IsSplit.ShouldBeTrue();
half.Bill.GridKwh.ShouldBe(1500m);
half.Bill.PvKwh.ShouldBe(1000m);
half.Bill.GridChf.ShouldBe(450m);
half.Bill.PvChf.ShouldBe(200m);
half.Bill.SideCostsChf.ShouldBe(50m);
}
}
[Fact]
public void SplitYearlyBillIntoTwoCalendarYearsWhenPeriodRunsJulyToJune()
{
var first = YearlyBill(2023);
first.GridKwh = 3000m;
var second = YearlyBill(2024);
second.GridKwh = 5000m;
var years = BillPeriod<ElectricityBill>.FromBills([first, second], BillPeriodKind.Year);
years.Select(y => y.Label).ShouldBe(["2023", "2024", "2025"]);
years.Select(y => y.Bill.GridKwh).ShouldBe([1500m, 4000m, 2500m]);
years.ShouldAllBe(y => y.IsSplit);
}
[Fact]
public void SplitWaterBillIntoTwoHalvesWhenPeriodRunsJulyToJune()
{
var bill = new WaterBill { PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30), WarmWaterM3 = 10m, ColdWaterM3 = 30m, BaseCostsChf = 200m };
var halves = BillPeriod<WaterBill>.FromBills([bill], BillPeriodKind.HalfYear);
halves.Select(h => h.Label).ShouldBe(["H2 23", "H1 24"]);
halves.ShouldAllBe(h => h.Bill.WarmWaterM3 == 5m && h.Bill.ColdWaterM3 == 15m && h.Bill.BaseCostsChf == 100m);
}
[Fact]
public void KeepPricePerKwhOfTheBillWhenSplittingIt()
{
var bill = YearlyBill(2023);
bill.GridKwh = 3000m;
bill.GridChf = 900m;
var halves = BillPeriod<ElectricityBill>.FromBills([bill], BillPeriodKind.HalfYear);
halves.ShouldAllBe(h => h.Bill.GridChfPerKwh == 0.3m);
}
[Fact]
public void AddUpBillsWithoutSplittingWhenTheyShareAPeriod()
{
var first = Bill(new DateOnly(2024, 1, 1), new DateOnly(2024, 3, 31));
first.GridKwh = 100m;
var second = Bill(new DateOnly(2024, 4, 1), new DateOnly(2024, 6, 30));
second.GridKwh = 200m;
var halves = BillPeriod<ElectricityBill>.FromBills([first, second], BillPeriodKind.HalfYear);
halves.Count.ShouldBe(1);
halves[0].Bill.GridKwh.ShouldBe(300m);
halves[0].IsSplit.ShouldBeFalse();
}
[Fact]
public void IncludeEmptyHalfYearWhenNoBillCoversIt()
{
var first = YearlyBill(2022);
var later = YearlyBill(2024);
var halves = BillPeriod<ElectricityBill>.FromBills([later, first], BillPeriodKind.HalfYear);
halves.Select(h => h.Label).ShouldBe(["H2 22", "H1 23", "H2 23", "H1 24", "H2 24", "H1 25"]);
halves[2].Bill.ShouldBeNull();
halves[3].Bill.ShouldBeNull();
}
[Theory]
[InlineData(BillPeriodKind.Bill)]
[InlineData(BillPeriodKind.Year)]
[InlineData(BillPeriodKind.HalfYear)]
public void ReturnEmptyListWhenThereAreNoBills(BillPeriodKind kind)
{
BillPeriod<ElectricityBill>.FromBills([], kind).ShouldBeEmpty();
}
private static ElectricityBill YearlyBill(int startYear) => Bill(new DateOnly(startYear, 7, 1), new DateOnly(startYear + 1, 6, 30));
private static ElectricityBill Bill(DateOnly from, DateOnly to) => new() { PeriodFrom = from, PeriodTo = to };
}