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

143 lines
4.0 KiB
C#

using EmployeeManagmentBusinessLogic.BusinessLogic;
using EmployeeManagmentContracts.SearchModels;
using EmployeeManagmentContracts.ViewModels;
using EmployeeManagmentDataBaseImplement.Implements;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit;
namespace EmployeeManagmentTests.Unit
{
public class SalaryLogicTests
{
private readonly Mock<ILogger<SalaryLogic>> _loggerMock;
private readonly SalaryStorage _salaryStorage;
private readonly SalaryLogic _salaryLogic;
public SalaryLogicTests()
{
_loggerMock = new Mock<ILogger<SalaryLogic>>();
_salaryStorage = new SalaryStorage();
_salaryLogic = new SalaryLogic(_loggerMock.Object, _salaryStorage);
}
[Fact]
public void GetFullList_ShouldReturnAllSalaries()
{
// Arrange
var expectedCount = _salaryStorage.GetFullList().Count;
// Act
var result = _salaryLogic.GetFullList();
// Assert
Assert.NotNull(result);
Assert.Equal(expectedCount, result.Count);
}
[Fact]
public void GetFilteredList_ShouldReturnFilteredSalaries()
{
// Arrange
var filter = new SalarySearchModel
{
Date = DateTime.UtcNow.AddMonths(-1) // Используйте UTC
};
// Act
var result = _salaryLogic.GetFilteredList(filter);
// Assert
Assert.NotNull(result);
Assert.All(result, salary => Assert.True(salary.Date >= filter.Date));
}
[Fact]
public void GetElement_ShouldReturnCorrectSalary()
{
// Arrange
var salaries = _salaryLogic.GetFullList();
if (salaries.Count == 0)
{
Assert.True(false, "No salaries available for testing.");
}
var salaryId = salaries.First().Id;
// Act
var result = _salaryLogic.GetElement(salaryId);
// Assert
Assert.NotNull(result);
Assert.Equal(salaryId, result.Id);
}
[Fact]
public void Insert_ShouldAddSalary()
{
// Arrange
var newSalary = new SalaryViewModel
{
CountHours = 40,
PriceHour = 15,
Premium = 200,
Date = DateTime.UtcNow, // Используем UTC для даты
Passed = false,
EmployeeId = 1
};
var initialCount = _salaryLogic.GetFullList().Count;
// Act
_salaryLogic.Insert(newSalary);
var updatedCount = _salaryLogic.GetFullList().Count;
// Assert
Assert.Equal(initialCount + 1, updatedCount);
}
[Fact]
public void Update_ShouldModifySalary()
{
// Arrange
var salary = _salaryLogic.GetFullList().FirstOrDefault();
if (salary == null)
{
Assert.True(false, "No salaries available for testing.");
}
salary.PriceHour += 5;
// Act
_salaryLogic.Update(salary);
var updatedSalary = _salaryLogic.GetElement(salary.Id);
// Assert
Assert.NotNull(updatedSalary);
Assert.Equal(salary.PriceHour, updatedSalary.PriceHour);
}
[Fact]
public void Delete_ShouldRemoveSalary()
{
// Arrange
var salary = _salaryLogic.GetFullList().LastOrDefault();
if (salary == null)
{
Assert.True(false, "No salaries available for testing.");
}
var initialCount = _salaryLogic.GetFullList().Count;
// Act
_salaryLogic.Delete(salary.Id);
var updatedCount = _salaryLogic.GetFullList().Count;
// Assert
Assert.Equal(initialCount - 1, updatedCount);
}
}
}