From 8dcb73d17cc624c7062bead53ab63a81773c13c2 Mon Sep 17 00:00:00 2001 From: "nathan.boileau" Date: Tue, 7 Mar 2023 18:41:53 +0100 Subject: [PATCH] Unit Test sur PUT + DELETE --- Sources/TestUnitaire/TestAPILol.cs | 74 ++++++++++++++++--- .../apiLOL/Controllers/ControllerChampions.cs | 12 +-- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/Sources/TestUnitaire/TestAPILol.cs b/Sources/TestUnitaire/TestAPILol.cs index b057c89..1419976 100644 --- a/Sources/TestUnitaire/TestAPILol.cs +++ b/Sources/TestUnitaire/TestAPILol.cs @@ -10,16 +10,16 @@ namespace TestUnitaire public class TestAPILol { [Theory] - [InlineData("Beatrice", "sdfsdfd")] - [InlineData("Maurice", "Ahri est un champion de League of Legends")] - [InlineData("Loupiotte", "Akali est un champion de League of Legends")] - public async Task TestPostChampion(string name, string bio) + [InlineData("Beatrice", "sdfsdfd", "icon.png")] + [InlineData("Maurice", "Ahri est un champion de League of Legends", "icon.png")] + [InlineData("Loupiotte", "Akali est un champion de League of Legends", "icon.png")] + public async Task TestPostChampion(string name, string bio, string icon) { // Arrange var data = new StubData(); var logger = new NullLogger(); var controller = new ControllerChampions(data, logger); - var champDTO = new ChampionDTO(name, bio); + var champDTO = new ChampionDTO(name, bio, icon); // Act var nbInListBefore = data.ChampionsMgr.GetNbItems().Result; @@ -34,16 +34,16 @@ namespace TestUnitaire [Theory] - [InlineData("Beatrice", "Aatrox est un champion de League of Legends")] - [InlineData("Maurice", "Ahri est un champion de League of Legends")] - [InlineData("Loupiotte", "Akali est un champion de League of Legends")] - public async Task TestGetChampion(string name, string bio) + [InlineData("Beatrice", "Aatrox est un champion de League of Legends", "icon.png")] + [InlineData("Maurice", "Ahri est un champion de League of Legends", "icon.png")] + [InlineData("Loupiotte", "Akali est un champion de League of Legends", "icon.png")] + public async Task TestGetChampion(string name, string bio, string icon) { // Arrange var data = new StubData(); var logger = new NullLogger(); var controller = new ControllerChampions(data, logger); - var champDTO = new ChampionDTO(name, bio); + var champDTO = new ChampionDTO(name, bio, icon); // Act // Call method POST to add a champion @@ -52,9 +52,61 @@ namespace TestUnitaire var resultGet = await controller.GetChampion(name); // Assert - // IS the champion added to the list, number of champions in the list + 1 Assert.Equal(name, champDTO.Name); Assert.Equal(bio, champDTO.Bio); } + + [Theory] + [InlineData("Beatrice", "Nouvelle bio")] + [InlineData("Maurice", "Nouvelle bio")] + [InlineData("Loupiotte", "Nouvelle bio")] + public async Task TestPutChampion(string name, string bio) + { + // Method that change the bio of a champion. Make a test to check if the bio is changed + // Arrange + var data = new StubData(); + var logger = new NullLogger(); + var controller = new ControllerChampions(data, logger); + + // Act + // Add a champion + var champDTO = new ChampionDTO(name, "Ancienne bio", "icon.png"); + var resultPost = await controller.Post(champDTO); + + // Call method PUT to change the bio of a champion + var resultPut = await controller.Put(name, bio); + var champion = (await data.ChampionsMgr.GetItemsByName(name, 0, 1)).First(); + var bioOfChampion = champion.Bio; + + // Assert + // Does the bio of the champion is changed + Assert.Equal(bio, bioOfChampion); + } + + + [Theory] + [InlineData("Beatrice")] + [InlineData("Maurice")] + [InlineData("Loupiotte")] + public async Task TestDeleteChampion(string name) + { + // Method that delete a champion. Make a test to check if the champion is deleted + // Arrange + var data = new StubData(); + var logger = new NullLogger(); + var controller = new ControllerChampions(data, logger); + + // Act + // Add a champion + var champDTO = new ChampionDTO(name, "Ancienne bio", "icon.png"); + var resultPost = await controller.Post(champDTO); + + // Call method DELETE to delete the champion + var resultDelete = await controller.Delete(name); + + // Assert + // Does the type of the result is a OkObjectResult + Assert.IsType(resultDelete); + } } } \ No newline at end of file diff --git a/Sources/apiLOL/Controllers/ControllerChampions.cs b/Sources/apiLOL/Controllers/ControllerChampions.cs index 80aad2b..0b1a496 100644 --- a/Sources/apiLOL/Controllers/ControllerChampions.cs +++ b/Sources/apiLOL/Controllers/ControllerChampions.cs @@ -26,7 +26,7 @@ namespace apiLOL.Controllers // GET: api/ [HttpGet] - public async Task Get([FromQuery]int index = 0, int count = 10, [FromQuery]string name = "") + public async Task Get([FromQuery]int index = 0, int count = 10, string? name = "") { //FromQuery permet de filtrer dans la collection de champions en fonction du nom @@ -34,11 +34,7 @@ namespace apiLOL.Controllers int nbChampions = await data.ChampionsMgr.GetNbItems(); _logger.LogInformation($"Nombre de champions : {nbChampions}"); - Task> champs; - if(name.Equals(name)) - champs = (Task>)(await data.ChampionsMgr.GetItems(index, count)).Select(Model => Model.ToDTO()); - else - champs = (Task>)(await data.ChampionsMgr.GetItemsByName(name, index, count)).Select(Model => Model.ToDTO()); + var champs = (await data.ChampionsMgr.GetItems(index, count)).Select(Model => Model.ToDTO()); var page = new ChampionPageDTO { @@ -66,7 +62,7 @@ namespace apiLOL.Controllers { _logger.LogInformation($"erreur methode Get de ControllerChampions: {ex}"); return BadRequest("erreur de nom de champion"); - } + } } @@ -75,8 +71,6 @@ namespace apiLOL.Controllers public async Task Post(ChampionDTO champDTO) { _logger.LogInformation($"methode Post de ControllerChampions appelée avec le paramètre {champDTO.Name}"); - - try { Champion tmp = champDTO.ToModel();