Removed unused using statements from page models and test files: - Removed unused Microsoft.EntityFrameworkCore and MoneyMap.Data imports - Removed redundant Xunit usings (already declared as global) Fixes CS8019 and CS8933 hidden diagnostics.
221 lines
6.3 KiB
C#
221 lines
6.3 KiB
C#
using MoneyMap.Models;
|
|
using MoneyMap.Services;
|
|
using MoneyMap.Tests.TestHelpers;
|
|
|
|
namespace MoneyMap.Tests.Services;
|
|
|
|
public class ReferenceDataServiceTests
|
|
{
|
|
[Fact]
|
|
public async Task GetAvailableCategoriesAsync_ReturnsDistinctSortedCategories()
|
|
{
|
|
// Arrange
|
|
using var context = DbContextHelper.CreateInMemoryContext();
|
|
var service = new ReferenceDataService(context);
|
|
|
|
var account = new Account
|
|
{
|
|
Id = 1,
|
|
Institution = "Test Bank",
|
|
AccountType = AccountType.Checking,
|
|
Last4 = "1234",
|
|
Owner = "Test Owner"
|
|
};
|
|
context.Accounts.Add(account);
|
|
|
|
var transaction1 = new Transaction
|
|
{
|
|
Date = DateTime.Now,
|
|
Amount = -50.00m,
|
|
Name = "Test",
|
|
Memo = "Test",
|
|
AccountId = 1,
|
|
Category = "Groceries"
|
|
};
|
|
var transaction2 = new Transaction
|
|
{
|
|
Date = DateTime.Now,
|
|
Amount = -30.00m,
|
|
Name = "Test",
|
|
Memo = "Test",
|
|
AccountId = 1,
|
|
Category = "Gas"
|
|
};
|
|
var transaction3 = new Transaction
|
|
{
|
|
Date = DateTime.Now,
|
|
Amount = -20.00m,
|
|
Name = "Test",
|
|
Memo = "Test",
|
|
AccountId = 1,
|
|
Category = "Groceries" // Duplicate
|
|
};
|
|
context.Transactions.AddRange(transaction1, transaction2, transaction3);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await service.GetAvailableCategoriesAsync();
|
|
|
|
// Assert
|
|
Assert.Equal(2, result.Count);
|
|
Assert.Contains("Groceries", result);
|
|
Assert.Contains("Gas", result);
|
|
Assert.Equal("Gas", result[0]); // Alphabetically sorted
|
|
Assert.Equal("Groceries", result[1]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAvailableCategoriesAsync_ExcludesEmptyCategories()
|
|
{
|
|
// Arrange
|
|
using var context = DbContextHelper.CreateInMemoryContext();
|
|
var service = new ReferenceDataService(context);
|
|
|
|
var account = new Account
|
|
{
|
|
Id = 1,
|
|
Institution = "Test Bank",
|
|
AccountType = AccountType.Checking,
|
|
Last4 = "1234",
|
|
Owner = "Test Owner"
|
|
};
|
|
context.Accounts.Add(account);
|
|
|
|
var transaction1 = new Transaction
|
|
{
|
|
Date = DateTime.Now,
|
|
Amount = -50.00m,
|
|
Name = "Test",
|
|
Memo = "Test",
|
|
AccountId = 1,
|
|
Category = "Groceries"
|
|
};
|
|
var transaction2 = new Transaction
|
|
{
|
|
Date = DateTime.Now,
|
|
Amount = -30.00m,
|
|
Name = "Test",
|
|
Memo = "Test",
|
|
AccountId = 1,
|
|
Category = "" // Empty
|
|
};
|
|
var transaction3 = new Transaction
|
|
{
|
|
Date = DateTime.Now,
|
|
Amount = -20.00m,
|
|
Name = "Test",
|
|
Memo = "Test",
|
|
AccountId = 1,
|
|
Category = " " // Whitespace
|
|
};
|
|
context.Transactions.AddRange(transaction1, transaction2, transaction3);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await service.GetAvailableCategoriesAsync();
|
|
|
|
// Assert
|
|
Assert.Single(result);
|
|
Assert.Equal("Groceries", result[0]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAvailableMerchantsAsync_ReturnsSortedMerchants()
|
|
{
|
|
// Arrange
|
|
using var context = DbContextHelper.CreateInMemoryContext();
|
|
var service = new ReferenceDataService(context);
|
|
|
|
var merchant1 = new Merchant { Name = "Walmart" };
|
|
var merchant2 = new Merchant { Name = "Amazon" };
|
|
var merchant3 = new Merchant { Name = "Target" };
|
|
context.Merchants.AddRange(merchant1, merchant2, merchant3);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await service.GetAvailableMerchantsAsync();
|
|
|
|
// Assert
|
|
Assert.Equal(3, result.Count);
|
|
Assert.Equal("Amazon", result[0].Name); // Alphabetically sorted
|
|
Assert.Equal("Target", result[1].Name);
|
|
Assert.Equal("Walmart", result[2].Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAvailableCardsAsync_ReturnsSortedCards()
|
|
{
|
|
// Arrange
|
|
using var context = DbContextHelper.CreateInMemoryContext();
|
|
var service = new ReferenceDataService(context);
|
|
|
|
var account = new Account
|
|
{
|
|
Id = 1,
|
|
Institution = "Test Bank",
|
|
AccountType = AccountType.Checking,
|
|
Last4 = "1234",
|
|
Owner = "Test Owner"
|
|
};
|
|
context.Accounts.Add(account);
|
|
|
|
var card1 = new Card
|
|
{
|
|
AccountId = 1,
|
|
Issuer = "VISA",
|
|
Last4 = "3333",
|
|
Owner = "Bob"
|
|
};
|
|
var card2 = new Card
|
|
{
|
|
AccountId = 1,
|
|
Issuer = "Mastercard",
|
|
Last4 = "1111",
|
|
Owner = "Alice"
|
|
};
|
|
context.Cards.AddRange(card1, card2);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await service.GetAvailableCardsAsync();
|
|
|
|
// Assert
|
|
Assert.Equal(2, result.Count);
|
|
Assert.Equal("Alice", result[0].Owner); // Sorted by owner
|
|
Assert.Equal("Bob", result[1].Owner);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAvailableAccountsAsync_ReturnsSortedAccounts()
|
|
{
|
|
// Arrange
|
|
using var context = DbContextHelper.CreateInMemoryContext();
|
|
var service = new ReferenceDataService(context);
|
|
|
|
var account1 = new Account
|
|
{
|
|
Institution = "Wells Fargo",
|
|
AccountType = AccountType.Checking,
|
|
Last4 = "5678",
|
|
Owner = "Test"
|
|
};
|
|
var account2 = new Account
|
|
{
|
|
Institution = "Bank of America",
|
|
AccountType = AccountType.Savings,
|
|
Last4 = "1234",
|
|
Owner = "Test"
|
|
};
|
|
context.Accounts.AddRange(account1, account2);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await service.GetAvailableAccountsAsync();
|
|
|
|
// Assert
|
|
Assert.Equal(2, result.Count);
|
|
Assert.Equal("Bank of America", result[0].Institution); // Sorted by institution
|
|
Assert.Equal("Wells Fargo", result[1].Institution);
|
|
}
|
|
}
|