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 WaterBillRepositoryTestShould : IDisposable { private readonly string mandatorDir; private readonly ISettingsService settingsService; private readonly WaterBillRepository sut; public WaterBillRepositoryTestShould() { mandatorDir = Path.Combine(Path.GetTempPath(), "AccountingNext-tests-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(mandatorDir); settingsService = Substitute.For(); settingsService.GetMandatorPath().Returns(mandatorDir); sut = new WaterBillRepository(settingsService, new RepositoryCache(), NullLogger.Instance); } public void Dispose() { if (Directory.Exists(mandatorDir)) { Directory.Delete(mandatorDir, recursive: true); } } [Fact] public async Task ReturnEmptyListWhenFileDoesNotExistYet() { (await sut.GetWaterBillListAsync()).ShouldBeEmpty(); } [Fact] public async Task AssignNewIdWhenSavingBillWithoutId() { var bill = new WaterBill { PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30), WarmWaterM3 = 12.345m, ColdWaterM3 = 30.5m, WarmWaterChf = 250.40m, ColdWaterChf = 98.15m, BaseCostsChf = 120m }; await sut.SaveWaterBillAsync(bill); var stored = (await sut.GetWaterBillListAsync()).Single(); stored.Id.ShouldNotBeNullOrEmpty(); stored.TotalM3.ShouldBe(42.845m); stored.TotalChf.ShouldBe(468.55m); } [Fact] public async Task UpdateInPlaceWhenSavingBillWithExistingId() { await sut.SaveWaterBillAsync(new WaterBill { PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30), WarmWaterM3 = 10m }); var id = (await sut.GetWaterBillListAsync()).Single().Id; await sut.SaveWaterBillAsync(new WaterBill { Id = id, PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30), WarmWaterM3 = 13m }); var bills = await sut.GetWaterBillListAsync(); bills.Count.ShouldBe(1); bills[0].WarmWaterM3.ShouldBe(13m); } [Fact] public async Task RemoveBillWhenDeletingById() { await sut.SaveWaterBillAsync(new WaterBill { PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30) }); var id = (await sut.GetWaterBillListAsync()).Single().Id; await sut.DeleteWaterBillAsync(id); (await sut.GetWaterBillListAsync()).ShouldBeEmpty(); } [Fact] public async Task StoreFileDirectlyInMandatorFolderWithoutYearSubfolder() { await sut.SaveWaterBillAsync(new WaterBill { PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30) }); File.Exists(Path.Combine(mandatorDir, "water.json")).ShouldBeTrue(); } }