140 lines
4.4 KiB
C#
140 lines
4.4 KiB
C#
using Xunit;
|
|
using Moq;
|
|
using EmployeeManagmentBusinessLogic.BusinessLogic;
|
|
using EmployeeManagmentContracts.ViewModels;
|
|
using EmployeeManagmentContracts.SearchModels;
|
|
using EmployeeManagmentContracts.StoragesContracts;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace EmployeeManagmentTests.Unit
|
|
{
|
|
public class PhisicalPersonLogicTests
|
|
{
|
|
private readonly Mock<IPhisicalPersonStorage> _mockStorage;
|
|
private readonly Mock<ILogger<PhisicalPersonLogic>> _mockLogger;
|
|
private readonly PhisicalPersonLogic _logic;
|
|
|
|
public PhisicalPersonLogicTests()
|
|
{
|
|
_mockStorage = new Mock<IPhisicalPersonStorage>();
|
|
_mockLogger = new Mock<ILogger<PhisicalPersonLogic>>();
|
|
_logic = new PhisicalPersonLogic(_mockLogger.Object, _mockStorage.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetFullList_ShouldReturnListOfPhisicalPersons()
|
|
{
|
|
// Arrange
|
|
var expectedList = new List<PhisicalPersonViewModel>
|
|
{
|
|
new PhisicalPersonViewModel { Id = 1, Name = "John", Surname = "Doe" },
|
|
new PhisicalPersonViewModel { Id = 2, Name = "Jane", Surname = "Smith" }
|
|
};
|
|
|
|
_mockStorage.Setup(x => x.GetFullList()).Returns(expectedList);
|
|
|
|
// Act
|
|
var result = _logic.GetFullList();
|
|
|
|
// Assert
|
|
Assert.Equal(expectedList.Count, result.Count);
|
|
Assert.Equal(expectedList[0].Name, result[0].Name);
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public void GetFilteredList_ShouldReturnFilteredResults()
|
|
{
|
|
// Arrange
|
|
var model = new PhisicalPersonSearchModel { Surname = "Doe" };
|
|
var expectedList = new List<PhisicalPersonViewModel>
|
|
{
|
|
new PhisicalPersonViewModel { Id = 1, Name = "John", Surname = "Doe" }
|
|
};
|
|
|
|
_mockStorage.Setup(x => x.GetFilteredList(model)).Returns(expectedList);
|
|
|
|
// Act
|
|
var result = _logic.GetFilteredList(model);
|
|
|
|
// Assert
|
|
Assert.Single(result);
|
|
Assert.Equal("Doe", result[0].Surname);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetFilteredList_ShouldReturnEmptyList_IfNoMatch()
|
|
{
|
|
// Arrange
|
|
var model = new PhisicalPersonSearchModel { Surname = "Unknown" };
|
|
_mockStorage.Setup(x => x.GetFilteredList(model)).Returns(new List<PhisicalPersonViewModel>());
|
|
|
|
// Act
|
|
var result = _logic.GetFilteredList(model);
|
|
|
|
// Assert
|
|
Assert.Empty(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void Insert_ShouldThrowException_IfNameOrSurnameIsEmpty()
|
|
{
|
|
// Arrange
|
|
var invalidModel = new PhisicalPersonViewModel { Name = "", Surname = "" };
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ArgumentException>(() => _logic.Insert(invalidModel));
|
|
}
|
|
|
|
[Fact]
|
|
public void Insert_ShouldCallStorageInsert_IfDataIsValid()
|
|
{
|
|
// Arrange
|
|
var validModel = new PhisicalPersonViewModel { Name = "John", Surname = "Doe" };
|
|
|
|
// Act
|
|
_logic.Insert(validModel);
|
|
|
|
// Assert
|
|
_mockStorage.Verify(x => x.Insert(validModel), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public void Update_ShouldThrowException_IfElementNotFound()
|
|
{
|
|
// Arrange
|
|
var model = new PhisicalPersonViewModel { Id = 1, Name = "John", Surname = "Doe" };
|
|
_mockStorage.Setup(x => x.GetElement(model.Id)).Returns((PhisicalPersonViewModel?)null);
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ArgumentException>(() => _logic.Update(model));
|
|
}
|
|
|
|
[Fact]
|
|
public void Delete_ShouldThrowException_IfElementNotFound()
|
|
{
|
|
// Arrange
|
|
var id = 1;
|
|
_mockStorage.Setup(x => x.GetElement(id)).Returns((PhisicalPersonViewModel?)null);
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ArgumentException>(() => _logic.Delete(id));
|
|
}
|
|
|
|
[Fact]
|
|
public void Delete_ShouldCallStorageDelete_IfElementExists()
|
|
{
|
|
// Arrange
|
|
var id = 1;
|
|
var model = new PhisicalPersonViewModel { Id = id };
|
|
_mockStorage.Setup(x => x.GetElement(id)).Returns(model);
|
|
|
|
// Act
|
|
_logic.Delete(id);
|
|
|
|
// Assert
|
|
_mockStorage.Verify(x => x.Delete(id), Times.Once);
|
|
}
|
|
}
|
|
}
|