Refactor. Add data layer

This commit is contained in:
rossmill76 committed 2026-03-06 11:05:01 +00:00
1 parent 3b7e4e6ec5
commit b6b794f0b4
82 files changed
+2930 -2811

No files matched your search

+13 -13
View File
@@ -1,5 +1,5 @@
using BudgetApp.Data;
using BudgetApp.Services;
using BudgetApp.Data.Entities;
using BudgetApp.Data.Repositories;
using BudgetApp.Tests.Helpers;
using Xunit;
@@ -20,7 +20,7 @@ public class AccountServiceTests
}
using var context = factory.CreateContext();
var service = new AccountService(context);
var service = new AccountRepository(context);
var result = await service.GetAccountsAsync(TestDbContextFactory.TestUserId);
@@ -41,7 +41,7 @@ public class AccountServiceTests
}
using var context = factory.CreateContext();
var service = new AccountService(context);
var service = new AccountRepository(context);
var result = await service.GetAccountsAsync(TestDbContextFactory.TestUserId);
@@ -63,7 +63,7 @@ public class AccountServiceTests
}
using var context = factory.CreateContext();
var service = new AccountService(context);
var service = new AccountRepository(context);
var result = await service.GetAccountAsync(accountId, TestDbContextFactory.TestUserId);
@@ -85,7 +85,7 @@ public class AccountServiceTests
}
using var context = factory.CreateContext();
var service = new AccountService(context);
var service = new AccountRepository(context);
var result = await service.GetAccountAsync(accountId, TestDbContextFactory.OtherUserId);
@@ -97,7 +97,7 @@ public class AccountServiceTests
{
using var factory = new TestDbContextFactory();
using var context = factory.CreateContext();
var service = new AccountService(context);
var service = new AccountRepository(context);
var account = new Account { Name = "New", UserId = TestDbContextFactory.TestUserId, CurrencyCode = "GBP" };
await service.CreateAccountAsync(account);
@@ -120,7 +120,7 @@ public class AccountServiceTests
}
using var context = factory.CreateContext();
var service = new AccountService(context);
var service = new AccountRepository(context);
await service.UpdateBalanceAsync(accountId, TestDbContextFactory.TestUserId, 250m);
using var verifyCtx = factory.CreateContext();
@@ -142,7 +142,7 @@ public class AccountServiceTests
}
using var context = factory.CreateContext();
var service = new AccountService(context);
var service = new AccountRepository(context);
await service.UpdateBalanceAsync(accountId, TestDbContextFactory.OtherUserId, 999m);
using var verifyCtx = factory.CreateContext();
@@ -163,7 +163,7 @@ public class AccountServiceTests
}
using var context = factory.CreateContext();
var service = new AccountService(context);
var service = new AccountRepository(context);
await service.UpdateAccountDetailsAsync(accountId, TestDbContextFactory.TestUserId, "New", "#fff", "USD");
using var verifyCtx = factory.CreateContext();
@@ -187,7 +187,7 @@ public class AccountServiceTests
}
using var context = factory.CreateContext();
var service = new AccountService(context);
var service = new AccountRepository(context);
await service.UpdateAccountDetailsAsync(accountId, TestDbContextFactory.OtherUserId, "Hacked", "#fff", "USD");
using var verifyCtx = factory.CreateContext();
@@ -208,7 +208,7 @@ public class AccountServiceTests
}
using var context = factory.CreateContext();
var service = new AccountService(context);
var service = new AccountRepository(context);
await service.DeleteAccountAsync(accountId, TestDbContextFactory.TestUserId);
using var verifyCtx = factory.CreateContext();
@@ -229,7 +229,7 @@ public class AccountServiceTests
}
using var context = factory.CreateContext();
var service = new AccountService(context);
var service = new AccountRepository(context);
await service.DeleteAccountAsync(accountId, TestDbContextFactory.OtherUserId);
using var verifyCtx = factory.CreateContext();
@@ -1,5 +1,5 @@
using BudgetApp.Data;
using BudgetApp.Services;
using BudgetApp.Data.Entities;
using BudgetApp.Data.Repositories;
using BudgetApp.Tests.Helpers;
using Microsoft.EntityFrameworkCore;
using Xunit;
@@ -21,7 +21,7 @@ public class CategoryServiceTests
}
using var context = factory.CreateContext();
var service = new CategoryService(context);
var service = new CategoryRepository(context);
var result = await service.GetCategoriesAsync(TestDbContextFactory.TestUserId);
@@ -42,7 +42,7 @@ public class CategoryServiceTests
}
using var context = factory.CreateContext();
var service = new CategoryService(context);
var service = new CategoryRepository(context);
var result = await service.GetCategoriesAsync(TestDbContextFactory.TestUserId);
@@ -55,7 +55,7 @@ public class CategoryServiceTests
{
using var factory = new TestDbContextFactory();
using var context = factory.CreateContext();
var service = new CategoryService(context);
var service = new CategoryRepository(context);
await service.EnsureDefaultCategoriesAsync(TestDbContextFactory.TestUserId);
@@ -77,13 +77,13 @@ public class CategoryServiceTests
using (var ctx = factory.CreateContext())
{
var service = new CategoryService(ctx);
var service = new CategoryRepository(ctx);
await service.EnsureDefaultCategoriesAsync(TestDbContextFactory.TestUserId);
}
using (var ctx = factory.CreateContext())
{
var service = new CategoryService(ctx);
var service = new CategoryRepository(ctx);
await service.EnsureDefaultCategoriesAsync(TestDbContextFactory.TestUserId);
}
@@ -111,7 +111,7 @@ public class CategoryServiceTests
}
using var context = factory.CreateContext();
var service = new CategoryService(context);
var service = new CategoryRepository(context);
await service.EnsureDefaultCategoriesAsync(TestDbContextFactory.TestUserId);
using var verifyCtx = factory.CreateContext();
@@ -137,7 +137,7 @@ public class CategoryServiceTests
}
using var context = factory.CreateContext();
var service = new CategoryService(context);
var service = new CategoryRepository(context);
await service.EnsureDefaultCategoriesAsync(TestDbContextFactory.TestUserId);
using var verifyCtx = factory.CreateContext();
@@ -154,13 +154,13 @@ public class CategoryServiceTests
using (var ctx = factory.CreateContext())
{
var service = new CategoryService(ctx);
var service = new CategoryRepository(ctx);
await service.EnsureDefaultCategoriesAsync(TestDbContextFactory.TestUserId);
}
using (var ctx = factory.CreateContext())
{
var service = new CategoryService(ctx);
var service = new CategoryRepository(ctx);
await service.EnsureDefaultCategoriesAsync(TestDbContextFactory.OtherUserId);
}
@@ -174,7 +174,7 @@ public class CategoryServiceTests
{
using var factory = new TestDbContextFactory();
using var context = factory.CreateContext();
var service = new CategoryService(context);
var service = new CategoryRepository(context);
await service.CreateCategoryAsync(new Category
{
@@ -201,7 +201,7 @@ public class CategoryServiceTests
}
using var context = factory.CreateContext();
var service = new CategoryService(context);
var service = new CategoryRepository(context);
await service.UpdateCategoryAsync(categoryId, TestDbContextFactory.TestUserId, "New", "🎯");
using var verifyCtx = factory.CreateContext();
@@ -224,7 +224,7 @@ public class CategoryServiceTests
}
using var context = factory.CreateContext();
var service = new CategoryService(context);
var service = new CategoryRepository(context);
await service.UpdateCategoryAsync(categoryId, TestDbContextFactory.OtherUserId, "Hacked", "💀");
using var verifyCtx = factory.CreateContext();
@@ -245,7 +245,7 @@ public class CategoryServiceTests
}
using var context = factory.CreateContext();
var service = new CategoryService(context);
var service = new CategoryRepository(context);
await service.DeleteCategoryAsync(categoryId, TestDbContextFactory.TestUserId);
using var verifyCtx = factory.CreateContext();
@@ -266,7 +266,7 @@ public class CategoryServiceTests
}
using var context = factory.CreateContext();
var service = new CategoryService(context);
var service = new CategoryRepository(context);
await service.DeleteCategoryAsync(categoryId, TestDbContextFactory.OtherUserId);
using var verifyCtx = factory.CreateContext();
@@ -1,7 +1,7 @@
using BudgetApp.Data;
using BudgetApp.Data.Entities;
using BudgetApp.Data.Repositories;
using BudgetApp.Services;
using BudgetApp.Tests.Helpers;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace BudgetApp.Tests.Services;
@@ -181,9 +181,9 @@ public class DashboardServiceTests
}
using var context = factory.CreateContext();
var transactionService = new TransactionService(context);
var accountService = new AccountService(context);
var categoryService = new CategoryService(context);
var transactionService = new TransactionRepository(context);
var accountService = new AccountRepository(context);
var categoryService = new CategoryRepository(context);
var dashboardService = new DashboardService(transactionService, accountService, categoryService);
var result = await dashboardService.GetDashboardDataAsync(TestDbContextFactory.TestUserId);
@@ -214,9 +214,9 @@ public class DashboardServiceTests
}
using var context = factory.CreateContext();
var transactionService = new TransactionService(context);
var accountService = new AccountService(context);
var categoryService = new CategoryService(context);
var transactionService = new TransactionRepository(context);
var accountService = new AccountRepository(context);
var categoryService = new CategoryRepository(context);
var dashboardService = new DashboardService(transactionService, accountService, categoryService);
var result = await dashboardService.GetDashboardDataAsync(TestDbContextFactory.TestUserId);
@@ -243,9 +243,9 @@ public class DashboardServiceTests
}
using var context = factory.CreateContext();
var transactionService = new TransactionService(context);
var accountService = new AccountService(context);
var categoryService = new CategoryService(context);
var transactionService = new TransactionRepository(context);
var accountService = new AccountRepository(context);
var categoryService = new CategoryRepository(context);
var dashboardService = new DashboardService(transactionService, accountService, categoryService);
var from = DateTime.Today.AddDays(-10);
@@ -1,5 +1,6 @@
using BudgetApp.Data;
using BudgetApp.Services;
using BudgetApp.Data.Entities;
using BudgetApp.Data.Repositories;
using BudgetApp.Tests.Helpers;
using Microsoft.EntityFrameworkCore;
using Xunit;
@@ -32,7 +33,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
var result = await service.GetTransactionsAsync(TestDbContextFactory.TestUserId);
Assert.Equal(2, result.Count);
@@ -55,7 +56,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
var result = await service.GetTransactionsAsync(TestDbContextFactory.TestUserId);
Assert.Single(result);
@@ -85,7 +86,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
var result = await service.GetTransactionsAsync(TestDbContextFactory.TestUserId);
Assert.Single(result);
@@ -113,7 +114,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
var result = await service.GetRecentTransactionsAsync(TestDbContextFactory.TestUserId, 10);
Assert.Equal(2, result.Count);
@@ -141,7 +142,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
var result = await service.GetRecentTransactionsAsync(TestDbContextFactory.TestUserId, 3);
Assert.Equal(3, result.Count);
@@ -166,7 +167,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
var result = await service.GetUpcomingTransactionsAsync(TestDbContextFactory.TestUserId, 10);
Assert.Single(result);
@@ -197,7 +198,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
var result = await service.GetTransactionsInRangeAsync(TestDbContextFactory.TestUserId, from, to);
Assert.Equal(3, result.Count);
@@ -229,7 +230,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
var result = await service.GetTransactionsByAccountAsync(accountId, TestDbContextFactory.TestUserId);
Assert.Single(result);
@@ -250,7 +251,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
var result = await service.GetTransactionsByAccountAsync(accountId, TestDbContextFactory.OtherUserId);
Assert.Empty(result);
@@ -271,7 +272,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.CreateTransactionAsync(new Transaction
{
Description = "Groceries",
@@ -296,7 +297,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.CreateTransactionAsync(new Transaction
{
Description = "One-off",
@@ -323,7 +324,7 @@ public class TransactionServiceTests
var startDate = new DateTime(2025, 1, 1);
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.CreateTransactionAsync(new Transaction
{
Description = "Recurring",
@@ -353,7 +354,7 @@ public class TransactionServiceTests
var startDate = new DateTime(2025, 1, 1);
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.CreateTransactionAsync(new Transaction
{
Description = "Recurring",
@@ -383,7 +384,7 @@ public class TransactionServiceTests
var startDate = new DateTime(2025, 1, 1);
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.CreateTransactionAsync(new Transaction
{
Description = "Weekly",
@@ -421,7 +422,7 @@ public class TransactionServiceTests
var startDate = new DateTime(2025, 1, 15);
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.CreateTransactionAsync(new Transaction
{
Description = "Monthly",
@@ -481,7 +482,7 @@ public class TransactionServiceTests
var newDate = DateTime.Today.AddDays(5);
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.UpdateTransactionAsync(
transactionId, TestDbContextFactory.TestUserId,
"Updated", 99m, newDate, TransactionType.Income,
@@ -520,7 +521,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.UpdateTransactionAsync(
transactionId, TestDbContextFactory.OtherUserId,
"Hacked", 999m, DateTime.Today, TransactionType.Income, 1, null);
@@ -548,7 +549,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.DeleteTransactionAsync(transactionId, TestDbContextFactory.TestUserId);
using var verifyCtx = factory.CreateContext();
@@ -570,7 +571,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.DeleteTransactionAsync(transactionId, TestDbContextFactory.OtherUserId);
using var verifyCtx = factory.CreateContext();
@@ -623,7 +624,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.DeleteTransactionSeriesAsync(childId, TestDbContextFactory.TestUserId);
using var verifyCtx = factory.CreateContext();
@@ -672,7 +673,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.DeleteTransactionSeriesAsync(childId, TestDbContextFactory.TestUserId);
using var verifyCtx = factory.CreateContext();
@@ -709,7 +710,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.DeleteTransactionSeriesAsync(parentId, TestDbContextFactory.TestUserId);
using var verifyCtx = factory.CreateContext();
@@ -731,7 +732,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.DeleteTransactionSeriesAsync(transactionId, TestDbContextFactory.OtherUserId);
using var verifyCtx = factory.CreateContext();
@@ -772,7 +773,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.MaterializeRecurringTransactionsAsync(TestDbContextFactory.TestUserId, startDate.AddDays(100));
using var verifyCtx = factory.CreateContext();
@@ -808,7 +809,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.MaterializeRecurringTransactionsAsync(TestDbContextFactory.TestUserId, startDate.AddDays(365));
using var verifyCtx = factory.CreateContext();
@@ -839,7 +840,7 @@ public class TransactionServiceTests
}
using var context = factory.CreateContext();
var service = new TransactionService(context);
var service = new TransactionRepository(context);
await service.MaterializeRecurringTransactionsAsync(TestDbContextFactory.TestUserId);
using var verifyCtx = factory.CreateContext();
@@ -860,7 +861,7 @@ public class TransactionServiceTests
var current = new DateTime(2025, 6, 15);
var expected = current.AddDays(expectedDays).AddMonths(expectedMonths).AddYears(expectedYears);
var result = TransactionService.GetNextOccurrence(current, type);
var result = TransactionRepository.GetNextOccurrence(current, type);
Assert.Equal(expected, result);
}
@@ -868,7 +869,7 @@ public class TransactionServiceTests
[Fact]
public void GetNextOccurrence_None_ReturnsMaxValue()
{
var result = TransactionService.GetNextOccurrence(DateTime.Today, RecurrenceType.None);
var result = TransactionRepository.GetNextOccurrence(DateTime.Today, RecurrenceType.None);
Assert.Equal(DateTime.MaxValue, result);
}