Skip to content

Commit

Permalink
Merge pull request #34 from Star-Academy/test/accounts
Browse files Browse the repository at this point in the history
Test/accounts
  • Loading branch information
mobinbr authored Sep 7, 2024
2 parents 9deb06e + c5d1f33 commit bbd6d18
Showing 1 changed file with 165 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
using Application.DTOs.Account;
using Application.Interfaces.Repositories;
using Application.Interfaces.Services;
using Application.Mappers;
using Application.Services.DomainService;
using Application.Services.SharedService;
using Domain.Entities;
using NSubstitute;

namespace test.Application.UnitTests.Services.DomainService;

public class AccountServiceTests
{
private readonly IAccountRepository _accountRepository;
private readonly AccountService _accountService;
private readonly ICsvReaderService _csvReaderService;

public AccountServiceTests()
{
_accountRepository = Substitute.For<IAccountRepository>();
_csvReaderService = Substitute.For<ICsvReaderService>();
_accountService = new AccountService(_accountRepository, _csvReaderService);
}

[Fact]
public async Task AddAccountsFromCsvAsync_WhenCsvIsValid_ReturnsOk()
{
// Arrange
var filePath = "dummy.csv";
var accountCsvModels = new List<AccountCsvModel>
{
new AccountCsvModel
{
AccountID = 1,
CardID = 100,
IBAN = "IBAN1",
AccountType = "Savings",
BranchTelephone = "1234567890",
BranchAdress = "Main Street",
BranchName = "Main Branch",
OwnerName = "Mobin",
OwnerLastName = "Barfi",
OwnerID = 101
},
new AccountCsvModel
{
AccountID = 2,
CardID = 200,
IBAN = "IBAN2",
AccountType = "Checking",
BranchTelephone = "0987654321",
BranchAdress = "Some Street",
BranchName = "Some Branch",
OwnerName = "Mohammad",
OwnerLastName = "Mohammadi",
OwnerID = 102
}
};
var existingAccountIds = new List<long> { 1 };

_csvReaderService.ReadFromCsv<AccountCsvModel>(filePath).Returns(accountCsvModels);
_accountRepository.GetAllIdsAsync().Returns(existingAccountIds);

// Act
var result = await _accountService.AddAccountsFromCsvAsync(filePath);

// Assert
Assert.True(result.Succeed);
await _accountRepository
.Received(1)
.CreateBulkAsync(Arg.Is<List<Account>>(a =>
a.Count == 1
&& a.First().AccountId == 2
&& a.First().OwnerName == "Mohammad"));
}

[Fact]
public async Task AddAccountsFromCsvAsync_WhenExceptionIsThrown_ReturnsFail()
{
// Arrange
var filePath = "dummy.csv";
_csvReaderService
.When(x => x.ReadFromCsv<AccountCsvModel>(filePath))
.Do(x => { throw new Exception("CSV read error"); });

// Act
var result = await _accountService.AddAccountsFromCsvAsync(filePath);

// Assert
Assert.False(result.Succeed);
Assert.Contains("An unexpected error occurred: CSV read error", result.Message);
}

[Fact]
public async Task GetAccountByIdAsync_WhenAccountExists_ReturnsAccount()
{
// Arrange
var accountId = 1;
var account = new Account { AccountId = accountId, OwnerName = "John Doe" };

_accountRepository.GetByIdAsync(accountId).Returns(account);

// Act
var result = await _accountService.GetAccountByIdAsync(accountId);

// Assert
Assert.NotNull(result);
Assert.Equal(accountId, result.Value!.AccountId);
Assert.Equal("John Doe", result.Value!.OwnerName);
}

[Fact]
public async Task GetAccountByIdAsync_WhenAccountDoesNotExist_ReturnsNull()
{
// Arrange
var accountId = 1;
_accountRepository.GetByIdAsync(accountId).Returns((Account?)null);

// Act
var result = await _accountService.GetAccountByIdAsync(accountId);

// Assert
Assert.False(result.Succeed);
Assert.Null(result.Value);
Assert.Equal("Account not found", result.Message);
}

[Fact]
public async Task GetAllAccountsAsync_WhenAccountsExist_ReturnsListOfAccounts()
{
// Arrange
var accounts = new List<Account>
{
new Account { AccountId = 1, OwnerName = "Mobin Barfi" },
new Account { AccountId = 2, OwnerName = "Mohammad Mohammadi" }
};

_accountRepository.GetAllAccounts().Returns(accounts);

// Act
var result = await _accountService.GetAllAccountsAsync();

// Assert
Assert.True(result.Succeed);
Assert.Equal(2, result.Value.Count);
Assert.Equal("Mobin Barfi", result.Value[0].OwnerName);
Assert.Equal("Mohammad Mohammadi", result.Value[1].OwnerName);
}

[Fact]
public async Task GetAllAccountsAsync_WhenExceptionIsThrown_ReturnsFail()
{
// Arrange
_accountRepository
.When(x => x.GetAllAccounts())
.Do(x => throw new Exception("Database error"));

// Act
var result = await _accountService.GetAllAccountsAsync();

// Assert
Assert.False(result.Succeed);
Assert.Equal("An unexpected error occurred: Database error", result.Message);
}
}

0 comments on commit bbd6d18

Please sign in to comment.