New page /analytics shows the current mandator's expenses across all years, with drill-down all -> subclass -> account -> booking-text group, year range and month/quarter/year filters, KPI tiles, a stacked bar chart and a category list. Filters and drill-down position live in the URL. - MultiYearRepository reads any mandator/year (read-only, shared cache) - ExpenseAnalysis flattens all years and answers queries in memory: net amounts (refunds count negative), accounts keyed by number with the latest name, bookings counted in the year of their books, year-over-year comparison over the same months - Chart.js 4.5.1 vendored in wwwroot/lib/chartjs, wrapped by wwwroot/js/charts.js and StackedBarChart.razor Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
84 lines
2.9 KiB
C#
84 lines
2.9 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using NSubstitute;
|
|
using Schaad.Accounting.Datasets;
|
|
using Schaad.Accounting.Interfaces;
|
|
using Schaad.Accounting.Repositories;
|
|
using Shouldly;
|
|
|
|
namespace Schaad.Accounting.Tests;
|
|
|
|
public class MultiYearRepositoryTestShould : IDisposable
|
|
{
|
|
private readonly string dataPath;
|
|
private readonly ISettingsService settingsService;
|
|
private readonly MultiYearRepository sut;
|
|
|
|
public MultiYearRepositoryTestShould()
|
|
{
|
|
dataPath = Path.Combine(Path.GetTempPath(), "AccountingNext-tests-" + Guid.NewGuid().ToString("N"));
|
|
Directory.CreateDirectory(dataPath);
|
|
|
|
settingsService = Substitute.For<ISettingsService>();
|
|
settingsService.GetSettings().Returns(new SettingsDataset { DataPath = dataPath });
|
|
|
|
sut = new MultiYearRepository(settingsService, new RepositoryCache(), NullLogger<MultiYearRepository>.Instance);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Directory.Exists(dataPath))
|
|
{
|
|
Directory.Delete(dataPath, recursive: true);
|
|
}
|
|
}
|
|
|
|
private void WriteFile(string mandator, int year, string fileName, string json)
|
|
{
|
|
var dir = Path.Combine(dataPath, mandator, year.ToString());
|
|
Directory.CreateDirectory(dir);
|
|
File.WriteAllText(Path.Combine(dir, fileName), json);
|
|
}
|
|
|
|
[Fact]
|
|
public void ListOnlyYearsWithAccountsWhenGettingYears()
|
|
{
|
|
WriteFile("Mandator", 2025, "Accounts.json", "[]");
|
|
WriteFile("Mandator", 2023, "Accounts.json", "[]");
|
|
Directory.CreateDirectory(Path.Combine(dataPath, "Mandator", "2026"));
|
|
WriteFile("Other", 2024, "Accounts.json", "[]");
|
|
|
|
sut.GetYears("Mandator").ShouldBe([2023, 2025]);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReturnNoYearsWhenMandatorDoesNotExist()
|
|
{
|
|
sut.GetYears("Unknown").ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReadRequestedYearWhenLoadingYearData()
|
|
{
|
|
WriteFile("Mandator", 2024, "Accounts.json", """[{ "Id": "a1", "Number": 4000, "Name": "Food" }]""");
|
|
WriteFile("Mandator", 2024, "SubClasses.json", """[{ "Number": 40, "Name": "Verpflegung" }]""");
|
|
WriteFile("Mandator", 2024, "Transactions.json", """[{ "Id": "t1", "Value": 12.5, "TargetAccountId": "a1" }]""");
|
|
|
|
var data = await sut.GetYearDataAsync("Mandator", 2024);
|
|
|
|
data.Year.ShouldBe(2024);
|
|
data.Accounts.Single().Name.ShouldBe("Food");
|
|
data.SubClasses.Single().Name.ShouldBe("Verpflegung");
|
|
data.Transactions.Single().Value.ShouldBe(12.5m);
|
|
settingsService.DidNotReceive().GetDbPath();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReturnEmptyListsWithoutCreatingFoldersWhenFilesAreMissing()
|
|
{
|
|
var data = await sut.GetYearDataAsync("Mandator", 2020);
|
|
|
|
data.Accounts.ShouldBeEmpty();
|
|
data.Transactions.ShouldBeEmpty();
|
|
Directory.Exists(Path.Combine(dataPath, "Mandator")).ShouldBeFalse();
|
|
}
|
|
}
|