Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test/accounts #34

Merged
merged 4 commits into from
Sep 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

Check failure on line 16 in test/Application.UnitTests/Services/DomainService/AccountServiceTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The type or namespace name 'ICsvReaderService' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 16 in test/Application.UnitTests/Services/DomainService/AccountServiceTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The type or namespace name 'ICsvReaderService' could not be found (are you missing a using directive or an assembly reference?)

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);
}
}
Loading