AccountingNext/Schaad.Accounting.Tests/ElectricityBillRepositoryTestShould.cs
Claudio Schaad a6e479094e Add "Strom" page for the yearly NeoVac electricity bills (#9)
Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
2026-10-27 18:40:53 +00:00

93 lines
3.2 KiB
C#

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<ISettingsService>();
settingsService.GetMandatorPath().Returns(mandatorDir);
sut = new ElectricityBillRepository(settingsService, new RepositoryCache(), NullLogger<ElectricityBillRepository>.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
};
await sut.SaveElectricityBillAsync(bill);
var stored = (await sut.GetElectricityBillListAsync()).Single();
stored.Id.ShouldNotBeNullOrEmpty();
stored.TotalChf.ShouldBe(1687.95m);
}
[Fact]
public async Task UpdateInPlaceWhenSavingBillWithExistingId()
{
await sut.SaveElectricityBillAsync(new ElectricityBill { PeriodFrom = new DateOnly(2023, 7, 1), PeriodTo = new DateOnly(2024, 6, 30), GridKwh = 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), GridKwh = 1300m });
var bills = await sut.GetElectricityBillListAsync();
bills.Count.ShouldBe(1);
bills[0].GridKwh.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();
}
}