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.
315 lines
11 KiB
315 lines
11 KiB
using API.Controllers;
|
|
using Dto;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Moq;
|
|
using Shared;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using TestAPI.Extensions;
|
|
|
|
namespace TestAPI
|
|
{
|
|
public class LessonUnitTest
|
|
{
|
|
private readonly Mock<ILessonService<LessonDto>> lessonService;
|
|
|
|
public LessonUnitTest()
|
|
{
|
|
lessonService = new Mock<ILessonService<LessonDto>>();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetLessonsListSuccess()
|
|
{
|
|
var lessonList = GetLessonsData();
|
|
lessonService.Setup(x => x.GetLessons(1, 4, 0))
|
|
.Returns(lessonList);
|
|
var LessonsController = new LessonsController(lessonService.Object, new NullLogger<LessonsController>());
|
|
|
|
var lessonsResult = LessonsController.GetLessons(1, 4, 0);
|
|
|
|
if (lessonsResult is OkObjectResult okObjectResult)
|
|
{
|
|
var valeur = okObjectResult.Value;
|
|
|
|
Assert.NotNull(valeur);
|
|
Assert.Equal(GetLessonsData().ToString(), valeur.ToString());
|
|
Assert.True(lessonList.SequenceEqual(valeur as IEnumerable<LessonDto>, new LessonIdEqualityComparer()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[Fact]
|
|
public void GetLessonsListFail()
|
|
{
|
|
lessonService.Setup(x => x.GetLessons(1, 4, 0))
|
|
.Returns(new List<LessonDto>());
|
|
var LessonsController = new LessonsController(lessonService.Object, new NullLogger<LessonsController>());
|
|
|
|
var lessonsResult = LessonsController.GetLessons(2, 3, 0);
|
|
|
|
if (lessonsResult is StatusCodeResult statusCodeResult && statusCodeResult.StatusCode == 204)
|
|
|
|
{
|
|
|
|
Assert.IsNotType<OkObjectResult>(lessonsResult);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public void GetLessonIdSuccess()
|
|
{
|
|
var lessonList = GetLessonsData();
|
|
lessonService.Setup(x => x.GetLessonById(1))
|
|
.Returns(lessonList[0]);
|
|
var LessonsController = new LessonsController(lessonService.Object, new NullLogger<LessonsController>());
|
|
|
|
var lessonsResult = LessonsController.GetLessonById(1);
|
|
if (lessonsResult is OkObjectResult okObjectResult)
|
|
{
|
|
LessonDto valeur = okObjectResult.Value as LessonDto;
|
|
|
|
Assert.NotNull(valeur);
|
|
Assert.Equal("Le titre", valeur.Title);
|
|
Assert.Equal("Clément", valeur.LastPublisher);
|
|
Assert.Equal(new DateOnly(2024, 03, 10), valeur.LastEdit);
|
|
Assert.NotEqual(2, valeur.Id);
|
|
|
|
|
|
Assert.Equal(valeur.GetHashCode(), lessonList[0].GetHashCode());
|
|
Assert.True(valeur.Equals(lessonList[0]));
|
|
Assert.False(valeur.Equals(new object()));
|
|
Assert.True(valeur.Equals(valeur));
|
|
Assert.IsType<LessonDto>(valeur);
|
|
Assert.Contains(valeur, lessonList);
|
|
}
|
|
|
|
}
|
|
|
|
[Fact]
|
|
public void GetLessonIdFail()
|
|
{
|
|
var lessonList = GetLessonsData();
|
|
lessonService.Setup(x => x.GetLessonById(1))
|
|
.Returns(lessonList[0]);
|
|
var LessonsController = new LessonsController(lessonService.Object, new NullLogger<LessonsController>());
|
|
|
|
var lessonsResult = LessonsController.GetLessonById(100);
|
|
if (lessonsResult is NotFoundObjectResult NFObjectResult)
|
|
{
|
|
var valeur = NFObjectResult.Value;
|
|
|
|
Assert.NotNull(valeur);
|
|
Assert.IsNotType<LessonDto>(valeur);
|
|
Assert.DoesNotContain(valeur, lessonList);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public void GetLessonTitleSuccess()
|
|
{
|
|
var lessonList = GetLessonsData();
|
|
lessonService.Setup(x => x.GetLessonByTitle("Chiant la"))
|
|
.Returns(lessonList[2]);
|
|
var LessonsController = new LessonsController(lessonService.Object, new NullLogger<LessonsController>());
|
|
|
|
var lessonsResult = LessonsController.GetLessonByTitle("Chiant la");
|
|
if (lessonsResult is OkObjectResult okObjectResult)
|
|
{
|
|
LessonDto valeur = okObjectResult.Value as LessonDto;
|
|
|
|
Assert.NotNull(valeur);
|
|
Assert.Equal("Chiant la", valeur.Title);
|
|
Assert.Equal("Une personne", valeur.LastPublisher);
|
|
Assert.Equal(new DateOnly(2012, 12, 25), valeur.LastEdit);
|
|
Assert.Equal(3, valeur.Id);
|
|
Assert.IsType<LessonDto>(valeur);
|
|
Assert.Contains(valeur, lessonList);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public void GetLessonTitleFail()
|
|
{
|
|
var lessonList = GetLessonsData();
|
|
lessonService.Setup(x => x.GetLessonByTitle("Chiant la"))
|
|
.Returns(lessonList[2]);
|
|
var LessonsController = new LessonsController(lessonService.Object, new NullLogger<LessonsController>());
|
|
|
|
var lessonsResult = LessonsController.GetLessonByTitle("IUHIUHU");
|
|
|
|
if (lessonsResult is NotFoundObjectResult NFObjectResult)
|
|
{
|
|
var valeur = NFObjectResult.Value;
|
|
|
|
Assert.NotNull(valeur);
|
|
Assert.IsNotType<LessonDto>(valeur);
|
|
Assert.DoesNotContain(valeur, lessonList);
|
|
Assert.False(lessonList == valeur);
|
|
}
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public void DeleteLessonSuccess()
|
|
{
|
|
lessonService.Setup(x => x.DeleteLesson(1))
|
|
.Returns(true);
|
|
var LessonsController = new LessonsController(lessonService.Object, new NullLogger<LessonsController>());
|
|
|
|
var lessonsResult = LessonsController.DeleteLesson(1);
|
|
if (lessonsResult is OkObjectResult okObjectResult)
|
|
{
|
|
bool valeur = (bool)okObjectResult.Value;
|
|
|
|
Assert.True(valeur);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public void DeleteLessonFail()
|
|
{
|
|
lessonService.Setup(x => x.DeleteLesson(1))
|
|
.Returns(true);
|
|
var LessonsController = new LessonsController(lessonService.Object, new NullLogger<LessonsController>());
|
|
|
|
var lessonsResult = LessonsController.DeleteLesson(100);
|
|
if (lessonsResult is NotFoundObjectResult NFObjectResult)
|
|
{
|
|
Assert.Null(NFObjectResult.Value);
|
|
Assert.IsNotType<bool>(NFObjectResult.Value);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void CreateLessonSuccess()
|
|
{
|
|
lessonService.Setup(x => x.CreateLesson("Le nouveau titre", "Le nouvel éditeur", new DateOnly(2024,03,16)))
|
|
.Returns(new LessonDto("Le nouveau titre", "Le nouvel éditeur", new DateOnly(2024, 03, 16)));
|
|
var LessonsController = new LessonsController(lessonService.Object, new NullLogger<LessonsController>());
|
|
|
|
var lessonsResult = LessonsController.CreateLesson(new LessonDto("Le nouveau titre", "Le nouvel éditeur", new DateOnly(2024, 03, 16)));
|
|
if (lessonsResult is CreatedResult createdObjectResult)
|
|
{
|
|
LessonDto valeur = createdObjectResult.Value as LessonDto;
|
|
|
|
Assert.NotNull(valeur);
|
|
Assert.Equal("Le nouveau titre", valeur.Title);
|
|
Assert.Equal("Le nouvel éditeur", valeur.LastPublisher);
|
|
Assert.Equal(new DateOnly(2024, 03, 16), valeur.LastEdit);
|
|
}
|
|
|
|
}
|
|
|
|
[Fact]
|
|
public void CreateLessonFail()
|
|
{
|
|
lessonService.Setup(x => x.CreateLesson("Le nouveau titre", "Le nouvel éditeur", new DateOnly(2024, 03, 16)))
|
|
.Returns(new LessonDto("Le nouveau titre", "Le nouvel éditeur", new DateOnly(2024, 03, 16)));
|
|
var LessonsController = new LessonsController(lessonService.Object, new NullLogger<LessonsController>());
|
|
|
|
var lessonsResult = LessonsController.CreateLesson(new LessonDto(null, "Le nouvel éditeur", new DateOnly(2024, 03, 16)));
|
|
|
|
if (lessonsResult is BadRequestResult BDObjectResult)
|
|
{
|
|
|
|
Assert.Equal(400, BDObjectResult.StatusCode);
|
|
}
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public void UpdateLessonSuccess()
|
|
{
|
|
lessonService.Setup(x => x.UpdateLesson(1,new LessonDto(1,"Titre update","Le dernier publisher",new DateOnly(2022,02,02))))
|
|
.Returns(new LessonDto(1, "Titre update", "Le dernier publisher", new DateOnly(2022, 02, 02)));
|
|
var LessonsController = new LessonsController(lessonService.Object, new NullLogger<LessonsController>());
|
|
|
|
var lessonsResult = LessonsController.UpdateLesson(1, new LessonDto(1, "Titre update", "Le dernier publisher", new DateOnly(2022, 02, 02)));
|
|
if (lessonsResult is OkObjectResult okObjectResult)
|
|
{
|
|
LessonDto valeur = okObjectResult.Value as LessonDto;
|
|
|
|
Assert.NotNull(valeur);
|
|
Assert.Equal("Titre update", valeur.Title);
|
|
Assert.Equal("Le dernier publisher", valeur.LastPublisher);
|
|
Assert.Equal(new DateOnly(2022, 02, 02), valeur.LastEdit);
|
|
Assert.Equal(1, valeur.Id);
|
|
}
|
|
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateLessonFail()
|
|
{
|
|
lessonService.Setup(x => x.UpdateLesson(1, new LessonDto(1, "Titre update", "Le dernier publisher", new DateOnly(2022, 02, 02))))
|
|
.Returns(new LessonDto(1, "Titre update", "Le dernier publisher", new DateOnly(2022, 02, 02)));
|
|
var LessonsController = new LessonsController(lessonService.Object, new NullLogger<LessonsController>());
|
|
|
|
var lessonsResult = LessonsController.UpdateLesson(1, new LessonDto(2, "Titre update", "Le dernier publisher", new DateOnly(2022, 02, 02)));
|
|
|
|
if (lessonsResult is BadRequestResult BDObjectResult)
|
|
{
|
|
|
|
Assert.Equal(400, BDObjectResult.StatusCode);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<LessonDto> GetLessonsData()
|
|
{
|
|
List<LessonDto> lessonsData = new List<LessonDto>(4)
|
|
{
|
|
new (1,"Le titre", "Clément",new DateOnly(2024,03,10)),
|
|
new (2,"Pas titre", "Erwan",new DateOnly(2024,02,11)),
|
|
new (3,"Chiant la", "Une personne",new DateOnly(2012,12,25)),
|
|
new ("Les fleurs du mal", "Baudelaire",new DateOnly(1872,10,01)),
|
|
};
|
|
return lessonsData;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
} |