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

145 lines
4.5 KiB
C#
Raw Normal View History

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 VacationLogicTests
{
private readonly Mock<ILogger<VacationLogic>> _loggerMock;
private readonly VacationStorage _vacationStorage;
private readonly VacationLogic _vacationLogic;
public VacationLogicTests()
{
_loggerMock = new Mock<ILogger<VacationLogic>>();
_vacationStorage = new VacationStorage();
_vacationLogic = new VacationLogic(_loggerMock.Object, _vacationStorage);
}
[Fact]
public void GetFullList_ShouldReturnAllVacations()
{
// Arrange
var expectedCount = _vacationStorage.GetFullList().Count;
// Act
var result = _vacationLogic.GetFullList();
// Assert
Assert.NotNull(result);
Assert.Equal(expectedCount, result.Count);
}
[Fact]
public void GetFilteredList_ShouldReturnFilteredVacations()
{
// Arrange
var filter = new VacationSearchModel
{
StartData = DateTime.UtcNow.AddMonths(-1),
EndData = DateTime.UtcNow
};
// Act
var result = _vacationLogic.GetFilteredList(filter);
// Assert
Assert.NotNull(result);
Assert.All(result, vacation =>
Assert.True(vacation.StartData >= filter.StartData && vacation.EndData <= filter.EndData)
);
}
[Fact]
public void GetElement_ShouldReturnCorrectVacation()
{
// Arrange
var vacations = _vacationLogic.GetFullList();
if (vacations.Count == 0)
{
Assert.True(false, "No vacations available for testing.");
}
var vacationId = vacations.First().Id;
// Act
var result = _vacationLogic.GetElement(vacationId);
// Assert
Assert.NotNull(result);
Assert.Equal(vacationId, result.Id);
}
[Fact]
public void Insert_ShouldAddVacation()
{
// Arrange
var newVacation = new VacationViewModel
{
StartData = DateTime.UtcNow.AddDays(1).ToUniversalTime(), // Преобразование в UTC
EndData = DateTime.UtcNow.AddDays(10).ToUniversalTime(), // Преобразование в UTC
Passed = false,
EmployeeId = 1 // ID существующего сотрудника
};
var initialCount = _vacationLogic.GetFullList().Count;
// Act
_vacationLogic.Insert(newVacation);
var updatedCount = _vacationLogic.GetFullList().Count;
// Assert
Assert.Equal(initialCount + 1, updatedCount);
}
[Fact]
public void Update_ShouldModifyVacation()
{
// Arrange
var vacation = _vacationLogic.GetFullList().FirstOrDefault();
if (vacation == null)
{
Assert.True(false, "No vacations available for testing.");
}
vacation.EndData = DateTime.UtcNow.AddDays(20).ToUniversalTime(); // Преобразование в UTC
// Act
_vacationLogic.Update(vacation);
var updatedVacation = _vacationLogic.GetElement(vacation.Id);
// Assert
Assert.NotNull(updatedVacation);
// Сравниваем с учетом допустимой погрешности в миллисекундах
Assert.Equal(vacation.EndData, updatedVacation.EndData, TimeSpan.FromMilliseconds(1));
}
[Fact]
public void Delete_ShouldRemoveVacation()
{
// Arrange
var vacation = _vacationLogic.GetFullList().LastOrDefault();
if (vacation == null)
{
Assert.True(false, "No vacations available for testing.");
}
var initialCount = _vacationLogic.GetFullList().Count;
// Act
_vacationLogic.Delete(vacation.Id);
var updatedCount = _vacationLogic.GetFullList().Count;
// Assert
Assert.Equal(initialCount - 1, updatedCount);
}
}
}