From 72808c98bab42661d0c5802a0f9ae0834e9f20e9 Mon Sep 17 00:00:00 2001 From: masapountz Date: Sat, 16 Mar 2024 10:33:33 +0100 Subject: [PATCH] Ajout des TU GetLessons pour LessonController --- API_SQLuedo/Dto/LessonDTO.cs | 36 ++++++- .../Extensions/LessonsDto.EqualityComparer.cs | 22 ++++ API_SQLuedo/TestAPI/LessonUnitTest.cs | 101 ++++++++++++++++++ API_SQLuedo/TestAPI/ParagraphsUnitTest.cs | 30 +----- 4 files changed, 159 insertions(+), 30 deletions(-) create mode 100644 API_SQLuedo/TestAPI/Extensions/LessonsDto.EqualityComparer.cs create mode 100644 API_SQLuedo/TestAPI/LessonUnitTest.cs diff --git a/API_SQLuedo/Dto/LessonDTO.cs b/API_SQLuedo/Dto/LessonDTO.cs index ab8008f..1f87e92 100644 --- a/API_SQLuedo/Dto/LessonDTO.cs +++ b/API_SQLuedo/Dto/LessonDTO.cs @@ -1,6 +1,6 @@ namespace Dto; -public class LessonDTO +public class LessonDTO : IEquatable { public int Id { get; } public string? Title { get; set; } @@ -26,4 +26,38 @@ public class LessonDTO LastPublisher = lastPublisher; LastEdit = lastEdit; } + + public override string ToString() + { + return $"{Id}\t{Title}\t{LastPublisher}\t{LastEdit}"; + } + + public override bool Equals(object right) + { + if (object.ReferenceEquals(right, null)) + { + return false; + } + + if (object.ReferenceEquals(this, right)) + { + return true; + } + + if (this.GetType() != right.GetType()) + { + return false; + } + + return this.Equals(right as LessonDTO); + } + public bool Equals(LessonDTO other) + { + return (this.Id == other.Id); + } + + public override int GetHashCode() + { + return Id; + } } \ No newline at end of file diff --git a/API_SQLuedo/TestAPI/Extensions/LessonsDto.EqualityComparer.cs b/API_SQLuedo/TestAPI/Extensions/LessonsDto.EqualityComparer.cs new file mode 100644 index 0000000..c042f13 --- /dev/null +++ b/API_SQLuedo/TestAPI/Extensions/LessonsDto.EqualityComparer.cs @@ -0,0 +1,22 @@ +using Dto; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TestAPI.Extensions +{ + class LessonIdEqualityComparer : EqualityComparer + { + public override bool Equals(LessonDTO x, LessonDTO y) + { + return x.Id == y.Id; + } + + public override int GetHashCode(LessonDTO obj) + { + return obj.Id; + } + } +} diff --git a/API_SQLuedo/TestAPI/LessonUnitTest.cs b/API_SQLuedo/TestAPI/LessonUnitTest.cs new file mode 100644 index 0000000..e2d9b10 --- /dev/null +++ b/API_SQLuedo/TestAPI/LessonUnitTest.cs @@ -0,0 +1,101 @@ +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> lessonService; + + public LessonUnitTest() + { + lessonService = new Mock>(); + } + + [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()); + + 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, new LessonIdEqualityComparer())); + + } + + } + + [Fact] + public void GetLessonsListFail() + { + lessonService.Setup(x => x.GetLessons(1, 4, 0)) + .Returns(new List()); + var LessonsController = new LessonsController(lessonService.Object, new NullLogger()); + + var lessonsResult = LessonsController.GetLessons(2, 3, 0); + + if (lessonsResult is StatusCodeResult statusCodeResult && statusCodeResult.StatusCode == 204) + + { + + Assert.IsNotType(lessonsResult); + } + + } + + + + + + + + + + + + + + + + + + + + + + + private List GetLessonsData() + { + List lessonsData = new List(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; + } + + + + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestAPI/ParagraphsUnitTest.cs b/API_SQLuedo/TestAPI/ParagraphsUnitTest.cs index 162d102..f3379c4 100644 --- a/API_SQLuedo/TestAPI/ParagraphsUnitTest.cs +++ b/API_SQLuedo/TestAPI/ParagraphsUnitTest.cs @@ -278,35 +278,7 @@ namespace TestAPI Assert.Equal(400, BDObjectResult.StatusCode); } - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - + } private List GetParagraphsData() { List paragraphsData = new List(4)