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 ElectricityBillRepositoryTestShould : IDisposable { private readonly string mandatorDir; private readonly ISettingsService settingsService; private readonly ElectricityBillRepository sut; public ElectricityBillRepositoryTestShould() { mandatorDir = Path.Combine(Path.GetTempPath(), "AccountingNext-tests-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(mandatorDir); settingsService = Substitute.For(); settingsService.GetMandatorPath().Returns(mandatorDir); sut = new ElectricityBillRepository(settingsService, new RepositoryCache(), NullLogger.Instance); } public void Dispose() { if (Directory.Exists(mandatorDir)) { Directory.Delete(mandatorDir, recursive: true); } } [Fact] public async Task ReturnEmptyListWhenFileDoesNotExistYet() { (await sut.GetElectricityBillListAsync()).ShouldBeEmpty(); } [Fact] public async Task AssignNewIdWhenSavingBillWithoutId() { var bill = new ElectricityBill { PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30), GridKwh = 3419.432m, PvKwh = 1936.328m, GridChf = 1009.55m, PvChf = 544.10m, SideCostsChf = 134.30m, AdvanceChf = 1212.00m }; await sut.SaveElectricityBillAsync(bill); var stored = (await sut.GetElectricityBillListAsync()).Single(); stored.Id.ShouldNotBeNullOrEmpty(); stored.Balance.ShouldBe(475.95m); } [Fact] public async Task UpdateInPlaceWhenSavingBillWithExistingId() { await sut.SaveElectricityBillAsync(new ElectricityBill { PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30), AdvanceChf = 1000m }); var id = (await sut.GetElectricityBillListAsync()).Single().Id; await sut.SaveElectricityBillAsync(new ElectricityBill { Id = id, PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30), AdvanceChf = 1300m }); var bills = await sut.GetElectricityBillListAsync(); bills.Count.ShouldBe(1); bills[0].AdvanceChf.ShouldBe(1300m); } [Fact] public async Task RemoveBillWhenDeletingById() { await sut.SaveElectricityBillAsync(new ElectricityBill { PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30) }); var id = (await sut.GetElectricityBillListAsync()).Single().Id; await sut.DeleteElectricityBillAsync(id); (await sut.GetElectricityBillListAsync()).ShouldBeEmpty(); } [Fact] public async Task StoreFileDirectlyInMandatorFolderWithoutYearSubfolder() { await sut.SaveElectricityBillAsync(new ElectricityBill { PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30) }); File.Exists(Path.Combine(mandatorDir, "electricity.json")).ShouldBeTrue(); } }