You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
4.8 KiB
143 lines
4.8 KiB
using Moq;
|
|
using Infrastructure;
|
|
using Infrastructure.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Server.Dto.Request;
|
|
using Server.Services;
|
|
|
|
public class FormationServicesTests
|
|
{
|
|
private readonly Mock<AlumniDbContext> _mockContext;
|
|
private readonly FormationServices _formationServices;
|
|
|
|
public FormationServicesTests()
|
|
{
|
|
var formations = new List<FormationEntity>
|
|
{
|
|
new FormationEntity { Id = "1", Name = "Formation1", SchoolName = "School1", StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(30), IsCurrent = true },
|
|
new FormationEntity { Id = "2", Name = "Formation2", SchoolName = "School2", StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(30), IsCurrent = true }
|
|
}.AsQueryable();
|
|
|
|
var mockSet = new Mock<DbSet<FormationEntity>>();
|
|
mockSet.As<IQueryable<FormationEntity>>().Setup(m => m.Provider).Returns(formations.Provider);
|
|
mockSet.As<IQueryable<FormationEntity>>().Setup(m => m.Expression).Returns(formations.Expression);
|
|
mockSet.As<IQueryable<FormationEntity>>().Setup(m => m.ElementType).Returns(formations.ElementType);
|
|
mockSet.As<IQueryable<FormationEntity>>().Setup(m => m.GetEnumerator()).Returns(formations.GetEnumerator());
|
|
|
|
_mockContext = new Mock<AlumniDbContext>();
|
|
_mockContext.Setup(c => c.Formations).Returns(mockSet.Object);
|
|
|
|
_formationServices = new FormationServices(_mockContext.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetFormationById_ReturnsFormation_WhenIdExists()
|
|
{
|
|
var result = await _formationServices.GetFormationById("1");
|
|
Assert.NotNull(result);
|
|
Assert.Equal("1", result.Id);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetFormationById_ReturnsNull_WhenIdDoesNotExist()
|
|
{
|
|
var result = await _formationServices.GetFormationById("3");
|
|
Assert.Null(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetFormations_ReturnsFormations_WhenNameExists()
|
|
{
|
|
var result = await _formationServices.GetFormations("Formation1", 1, 1);
|
|
Assert.Single(result.Formations);
|
|
Assert.Equal("Formation1", result.Formations.First().Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetFormations_ReturnsEmpty_WhenNameDoesNotExist()
|
|
{
|
|
var result = await _formationServices.GetFormations("Formation3", 1, 1);
|
|
Assert.Empty(result.Formations);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateFormation_ReturnsUpdatedFormation_WhenIdExists()
|
|
{
|
|
var formation = new RequestFormationDto { Name = "UpdatedFormation", SchoolName = "UpdatedSchool", StartingDate = DateTime.Now, EndingDate = DateTime.Now.AddDays(30), CurrentFormation = false };
|
|
var result = await _formationServices.UpdateFormation("1", formation);
|
|
Assert.NotNull(result);
|
|
Assert.Equal("UpdatedFormation", result.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateFormation_ReturnsNull_WhenIdDoesNotExist()
|
|
{
|
|
var formation = new RequestFormationDto { Name = "UpdatedFormation", SchoolName = "UpdatedSchool", StartingDate = DateTime.Now, EndingDate = DateTime.Now.AddDays(30), CurrentFormation = false };
|
|
var result = await _formationServices.UpdateFormation("3", formation);
|
|
Assert.Null(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteFormation_ReturnsTrue_WhenIdExists()
|
|
{
|
|
var result = await _formationServices.DeleteFormation("1");
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteFormation_ReturnsFalse_WhenIdDoesNotExist()
|
|
{
|
|
var result = await _formationServices.DeleteFormation("3");
|
|
Assert.False(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetFormations_NoName_ReturnsFormations()
|
|
{
|
|
// Arrange
|
|
string name = null;
|
|
var page = 1;
|
|
var size = 5;
|
|
|
|
// Act
|
|
var result = await _formationServices.GetFormations(name, page, size);
|
|
|
|
// Assert
|
|
Assert.NotEmpty(result.Formations);
|
|
Assert.Equal(2, result.Formations.Count());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetFormations_MatchingName_ReturnsFilteredFormations()
|
|
{
|
|
// Arrange
|
|
string name = "Formation1";
|
|
var page = 1;
|
|
var size = 5;
|
|
|
|
// Act
|
|
var result = await _formationServices.GetFormations(name, page, size);
|
|
|
|
// Assert
|
|
Assert.Single(result.Formations);
|
|
Assert.Equal("Formation1", result.Formations.First().Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetFormations_PaginationWorksCorrectly()
|
|
{
|
|
// Arrange
|
|
string name = null;
|
|
var page = 2;
|
|
var size = 1;
|
|
|
|
// Act
|
|
var result = await _formationServices.GetFormations(name, page, size);
|
|
|
|
// Assert
|
|
Assert.NotEmpty(result.Formations);
|
|
Assert.Single(result.Formations);
|
|
Assert.Equal("Formation2", result.Formations.First().Name);
|
|
}
|
|
|
|
} |