PIbd-42_Kashin_M.I_CPO_Cour.../EmployeeManagmentTests/Unit/EmployeeLogicTests.cs

139 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 EmployeeLogicTests
{
private readonly Mock<IEmployeeStorage> _mockStorage;
private readonly Mock<ILogger<EmployeeLogic>> _mockLogger;
private readonly EmployeeLogic _logic;
public EmployeeLogicTests()
{
_mockStorage = new Mock<IEmployeeStorage>();
_mockLogger = new Mock<ILogger<EmployeeLogic>>();
_logic = new EmployeeLogic(_mockLogger.Object, _mockStorage.Object);
}
[Fact]
public void GetFullList_ShouldReturnListOfEmployees()
{
// Arrange
var expectedList = new List<EmployeeViewModel>
{
new EmployeeViewModel { Id = 1, NameJob = "Developer", StartJob = DateTime.Now.AddYears(-1) },
new EmployeeViewModel { Id = 2, NameJob = "Manager", StartJob = DateTime.Now.AddYears(-2) }
};
_mockStorage.Setup(x => x.GetFullList()).Returns(expectedList);
// Act
var result = _logic.GetFullList();
// Assert
Assert.Equal(expectedList.Count, result.Count);
Assert.Equal(expectedList[0].NameJob, result[0].NameJob);
}
[Fact]
public void GetFilteredList_ShouldReturnFilteredResults()
{
// Arrange
var model = new EmployeeSearchModel { NameJob = "Developer" };
var expectedList = new List<EmployeeViewModel>
{
new EmployeeViewModel { Id = 1, NameJob = "Developer", StartJob = DateTime.Now.AddYears(-1) }
};
_mockStorage.Setup(x => x.GetFilteredList(model)).Returns(expectedList);
// Act
var result = _logic.GetFilteredList(model);
// Assert
Assert.Single(result);
Assert.Equal("Developer", result[0].NameJob);
}
[Fact]
public void GetFilteredList_ShouldReturnEmptyList_IfNoMatch()
{
// Arrange
var model = new EmployeeSearchModel { NameJob = "Unknown" };
_mockStorage.Setup(x => x.GetFilteredList(model)).Returns(new List<EmployeeViewModel>());
// Act
var result = _logic.GetFilteredList(model);
// Assert
Assert.Empty(result);
}
[Fact]
public void Insert_ShouldThrowException_IfNameJobIsEmpty()
{
// Arrange
var invalidModel = new EmployeeViewModel { NameJob = "" };
// Act & Assert
Assert.Throws<ArgumentException>(() => _logic.Insert(invalidModel));
}
[Fact]
public void Insert_ShouldCallStorageInsert_IfDataIsValid()
{
// Arrange
var validModel = new EmployeeViewModel { NameJob = "Developer", StartJob = DateTime.Now };
// Act
_logic.Insert(validModel);
// Assert
_mockStorage.Verify(x => x.Insert(validModel), Times.Once);
}
[Fact]
public void Update_ShouldThrowException_IfElementNotFound()
{
// Arrange
var model = new EmployeeViewModel { Id = 1, NameJob = "Developer" };
_mockStorage.Setup(x => x.GetElement(model.Id)).Returns((EmployeeViewModel?)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((EmployeeViewModel?)null);
// Act & Assert
Assert.Throws<ArgumentException>(() => _logic.Delete(id));
}
[Fact]
public void Delete_ShouldCallStorageDelete_IfElementExists()
{
// Arrange
var id = 1;
var model = new EmployeeViewModel { Id = id };
_mockStorage.Setup(x => x.GetElement(id)).Returns(model);
// Act
_logic.Delete(id);
// Assert
_mockStorage.Verify(x => x.Delete(id), Times.Once);
}
}
}