using Microsoft.Extensions.Logging.Abstractions; using NSubstitute; using Schaad.Accounting.Interfaces; using Schaad.Accounting.Models; using Schaad.Accounting.Repositories; using Shouldly; namespace Schaad.Accounting.Tests; public class WaterHeatingBillRepositoryTestShould : IDisposable { private readonly string mandatorDir; private readonly ISettingsService settingsService; private readonly WaterHeatingBillRepository sut; public WaterHeatingBillRepositoryTestShould() { mandatorDir = Path.Combine(Path.GetTempPath(), "AccountingNext-tests-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(mandatorDir); settingsService = Substitute.For(); settingsService.GetMandatorPath().Returns(mandatorDir); sut = new WaterHeatingBillRepository(settingsService, new RepositoryCache(), NullLogger.Instance); } public void Dispose() { if (Directory.Exists(mandatorDir)) { Directory.Delete(mandatorDir, recursive: true); } } [Fact] public async Task ReturnEmptyListWhenFileDoesNotExistYet() { (await sut.GetWaterHeatingBillListAsync()).ShouldBeEmpty(); } [Fact] public async Task AssignNewIdWhenSavingBillWithoutId() { var bill = new WaterHeatingBill { PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30), WarmWaterM3 = 12.345m, ColdWaterM3 = 30.5m, WaterCostsChf = 304.30m, HeatingCostsChf = 451.65m }; await sut.SaveWaterHeatingBillAsync(bill); var stored = (await sut.GetWaterHeatingBillListAsync()).Single(); stored.Id.ShouldNotBeNullOrEmpty(); stored.TotalM3.ShouldBe(42.845m); stored.TotalChf.ShouldBe(755.95m); } [Fact] public async Task UpdateInPlaceWhenSavingBillWithExistingId() { await sut.SaveWaterHeatingBillAsync(new WaterHeatingBill { PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30), WarmWaterM3 = 10m }); var id = (await sut.GetWaterHeatingBillListAsync()).Single().Id; await sut.SaveWaterHeatingBillAsync(new WaterHeatingBill { Id = id, PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30), WarmWaterM3 = 13m }); var bills = await sut.GetWaterHeatingBillListAsync(); bills.Count.ShouldBe(1); bills[0].WarmWaterM3.ShouldBe(13m); } [Fact] public async Task RemoveBillWhenDeletingById() { await sut.SaveWaterHeatingBillAsync(new WaterHeatingBill { PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30) }); var id = (await sut.GetWaterHeatingBillListAsync()).Single().Id; await sut.DeleteWaterHeatingBillAsync(id); (await sut.GetWaterHeatingBillListAsync()).ShouldBeEmpty(); } [Fact] public async Task StoreFileDirectlyInMandatorFolderWithoutYearSubfolder() { await sut.SaveWaterHeatingBillAsync(new WaterHeatingBill { PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30) }); File.Exists(Path.Combine(mandatorDir, "water-heating.json")).ShouldBeTrue(); } }