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.
232 lines
5.9 KiB
C#
232 lines
5.9 KiB
C#
using MoneyMap.Models;
|
|
using MoneyMap.Services;
|
|
using MoneyMap.Tests.TestHelpers;
|
|
|
|
namespace MoneyMap.Tests.Services;
|
|
|
|
public class CardServiceTests
|
|
{
|
|
[Fact]
|
|
public async Task GetAllCardsWithStatsAsync_ReturnsCardsWithTransactionCounts()
|
|
{
|
|
// Arrange
|
|
using var context = DbContextHelper.CreateInMemoryContext();
|
|
var service = new CardService(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
|
|
{
|
|
Id = 1,
|
|
AccountId = 1,
|
|
Issuer = "VISA",
|
|
Last4 = "1111",
|
|
Owner = "John Doe"
|
|
};
|
|
var card2 = new Card
|
|
{
|
|
Id = 2,
|
|
AccountId = 1,
|
|
Issuer = "Mastercard",
|
|
Last4 = "2222",
|
|
Owner = "Jane Smith"
|
|
};
|
|
context.Cards.AddRange(card1, card2);
|
|
|
|
var transaction = new Transaction
|
|
{
|
|
Date = DateTime.Now,
|
|
Amount = -50.00m,
|
|
Name = "Test",
|
|
Memo = "Test",
|
|
AccountId = 1,
|
|
CardId = 1
|
|
};
|
|
context.Transactions.Add(transaction);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await service.GetAllCardsWithStatsAsync();
|
|
|
|
// Assert
|
|
Assert.Equal(2, result.Count);
|
|
var card1Stats = result.First(c => c.Card.Id == 1);
|
|
Assert.Equal(1, card1Stats.TransactionCount);
|
|
var card2Stats = result.First(c => c.Card.Id == 2);
|
|
Assert.Equal(0, card2Stats.TransactionCount);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CanDeleteCardAsync_ReturnsFalse_WhenCardHasTransactions()
|
|
{
|
|
// Arrange
|
|
using var context = DbContextHelper.CreateInMemoryContext();
|
|
var service = new CardService(context);
|
|
|
|
var account = new Account
|
|
{
|
|
Id = 1,
|
|
Institution = "Test Bank",
|
|
AccountType = AccountType.Checking,
|
|
Last4 = "1234",
|
|
Owner = "Test Owner"
|
|
};
|
|
context.Accounts.Add(account);
|
|
|
|
var card = new Card
|
|
{
|
|
Id = 1,
|
|
AccountId = 1,
|
|
Issuer = "VISA",
|
|
Last4 = "9999",
|
|
Owner = "Test Owner"
|
|
};
|
|
context.Cards.Add(card);
|
|
|
|
var transaction = new Transaction
|
|
{
|
|
Date = DateTime.Now,
|
|
Amount = -50.00m,
|
|
Name = "Test",
|
|
Memo = "Test",
|
|
AccountId = 1,
|
|
CardId = 1
|
|
};
|
|
context.Transactions.Add(transaction);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await service.CanDeleteCardAsync(1);
|
|
|
|
// Assert
|
|
Assert.False(result.CanDelete);
|
|
Assert.Contains("transaction", result.Reason, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CanDeleteCardAsync_ReturnsTrue_WhenCardHasNoTransactions()
|
|
{
|
|
// Arrange
|
|
using var context = DbContextHelper.CreateInMemoryContext();
|
|
var service = new CardService(context);
|
|
|
|
var account = new Account
|
|
{
|
|
Id = 1,
|
|
Institution = "Test Bank",
|
|
AccountType = AccountType.Checking,
|
|
Last4 = "1234",
|
|
Owner = "Test Owner"
|
|
};
|
|
context.Accounts.Add(account);
|
|
|
|
var card = new Card
|
|
{
|
|
Id = 1,
|
|
AccountId = 1,
|
|
Issuer = "VISA",
|
|
Last4 = "9999",
|
|
Owner = "Test Owner"
|
|
};
|
|
context.Cards.Add(card);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await service.CanDeleteCardAsync(1);
|
|
|
|
// Assert
|
|
Assert.True(result.CanDelete);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteCardAsync_SuccessfullyDeletesCard_WhenNoTransactions()
|
|
{
|
|
// Arrange
|
|
using var context = DbContextHelper.CreateInMemoryContext();
|
|
var service = new CardService(context);
|
|
|
|
var account = new Account
|
|
{
|
|
Id = 1,
|
|
Institution = "Test Bank",
|
|
AccountType = AccountType.Checking,
|
|
Last4 = "1234",
|
|
Owner = "Test Owner"
|
|
};
|
|
context.Accounts.Add(account);
|
|
|
|
var card = new Card
|
|
{
|
|
Id = 1,
|
|
AccountId = 1,
|
|
Issuer = "VISA",
|
|
Last4 = "9999",
|
|
Owner = "Test Owner"
|
|
};
|
|
context.Cards.Add(card);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await service.DeleteCardAsync(1);
|
|
|
|
// Assert
|
|
Assert.True(result.Success);
|
|
Assert.Empty(context.Cards);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteCardAsync_FailsToDelete_WhenCardHasTransactions()
|
|
{
|
|
// Arrange
|
|
using var context = DbContextHelper.CreateInMemoryContext();
|
|
var service = new CardService(context);
|
|
|
|
var account = new Account
|
|
{
|
|
Id = 1,
|
|
Institution = "Test Bank",
|
|
AccountType = AccountType.Checking,
|
|
Last4 = "1234",
|
|
Owner = "Test Owner"
|
|
};
|
|
context.Accounts.Add(account);
|
|
|
|
var card = new Card
|
|
{
|
|
Id = 1,
|
|
AccountId = 1,
|
|
Issuer = "VISA",
|
|
Last4 = "9999",
|
|
Owner = "Test Owner"
|
|
};
|
|
context.Cards.Add(card);
|
|
|
|
var transaction = new Transaction
|
|
{
|
|
Date = DateTime.Now,
|
|
Amount = -50.00m,
|
|
Name = "Test",
|
|
Memo = "Test",
|
|
AccountId = 1,
|
|
CardId = 1
|
|
};
|
|
context.Transactions.Add(transaction);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await service.DeleteCardAsync(1);
|
|
|
|
// Assert
|
|
Assert.False(result.Success);
|
|
Assert.Single(context.Cards);
|
|
}
|
|
}
|