Unit Test sur PUT + DELETE

API2
Nathan BOILEAU 2 years ago
parent e9bde48eae
commit 8dcb73d17c

@ -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<ControllerChampions>();
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<ControllerChampions>();
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<ControllerChampions>();
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<ControllerChampions>();
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<OkObjectResult>(resultDelete);
}
}
}

@ -26,7 +26,7 @@ namespace apiLOL.Controllers
// GET: api/<ControllerLol>
[HttpGet]
public async Task<IActionResult> Get([FromQuery]int index = 0, int count = 10, [FromQuery]string name = "")
public async Task<IActionResult> 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<IEnumerable<Champion?>> champs;
if(name.Equals(name))
champs = (Task<IEnumerable<Champion?>>)(await data.ChampionsMgr.GetItems(index, count)).Select(Model => Model.ToDTO());
else
champs = (Task<IEnumerable<Champion?>>)(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<IActionResult> Post(ChampionDTO champDTO)
{
_logger.LogInformation($"methode Post de ControllerChampions appelée avec le paramètre {champDTO.Name}");
try
{
Champion tmp = champDTO.ToModel();

Loading…
Cancel
Save