More API UT and create a pageResponse DTO 🔋
continuous-integration/drone/push Build is passing Details

UT_EF_Manager
Emre KARTAL 2 years ago
parent d71c2d9138
commit b1ac2fccd3

@ -1,28 +1,28 @@
using ApiLol.Mapper;
using DTO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using Model;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace ApiLol.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RunesController : ControllerBase
namespace ApiLol.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RunesController : ControllerBase
{
private readonly IDataManager _manager;
private readonly ILogger<RunesController> _logger;
public RunesController(IDataManager dataManager, ILogger<RunesController> logger)
{
_logger = logger;
this._manager = dataManager;
}
// GET: api/<RunesController>
private readonly IDataManager _manager;
private readonly ILogger<RunesController> _logger;
public RunesController(IDataManager dataManager, ILogger<RunesController> logger)
{
_logger = logger;
this._manager = dataManager;
}
// GET: api/<RunesController>
[HttpGet]
public async Task<IActionResult> Get([FromQuery] PageRequest pageRequest)
{
@ -51,7 +51,7 @@ namespace ApiLol.Controllers
dtos = (await _manager.RunesMgr.GetItemsByName(pageRequest.name, pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending))
.Select(x => x.ToDto());
}
return Ok(new { Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal });
return Ok(new PageResponse<RuneDto>{ Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal });
}
catch (Exception error)
@ -59,11 +59,11 @@ namespace ApiLol.Controllers
_logger.LogError(error.Message);
return BadRequest(error.Message);
}
}
// GET api/<RunesController>/5
[HttpGet("{name}")]
public async Task<IActionResult> Get(string name)
}
// GET api/<RunesController>/5
[HttpGet("{name}")]
public async Task<IActionResult> Get(string name)
{
_logger.LogInformation("method {Action} - RUNE call with {name}", nameof(Get), name);
try
@ -81,12 +81,12 @@ namespace ApiLol.Controllers
{
_logger.LogError(error.Message);
return BadRequest(error.Message);
}
}
// POST api/<RunesController>
}
}
// POST api/<RunesController>
[HttpPost]
public async Task<IActionResult> Post([FromBody] RuneDto rune)
public async Task<IActionResult> Post([FromBody] RuneDto rune)
{
_logger.LogInformation("method {Action} - RUNE call with {item}", nameof(Post), rune);
try
@ -103,7 +103,7 @@ namespace ApiLol.Controllers
{
_logger.LogError(error.Message);
return BadRequest(error.Message);
}
}
}
// PUT api/<RunesController>/5
@ -135,11 +135,11 @@ namespace ApiLol.Controllers
_logger.LogError(error.Message);
return BadRequest(error.Message);
}
}
// DELETE api/<RunesController>/5
}
// DELETE api/<RunesController>/5
[HttpDelete("{name}")]
public async Task<IActionResult> Delete(string name)
public async Task<IActionResult> Delete(string name)
{
_logger.LogInformation("method {Action} - RUNE call with {name}", nameof(Delete), name);
try
@ -156,7 +156,7 @@ namespace ApiLol.Controllers
{
_logger.LogError(error.Message);
return BadRequest(error.Message);
}
}
}
[HttpGet("/countRunes")]
@ -174,4 +174,4 @@ namespace ApiLol.Controllers
}
}
}
}
}

@ -50,7 +50,7 @@ namespace ApiLol.Controllers
dtos = (await _manager.SkinsMgr.GetItemsByName(pageRequest.name, pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending))
.Select(x => x.ToDtoC());
}
return Ok(new { Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal });
return Ok(new PageResponse<SkinDtoC> { Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal });
}
catch (Exception error)

@ -68,7 +68,7 @@ namespace ApiLol.Controllers.v1
}
[HttpGet("/countChampions")]
public async Task<ActionResult<int>> GetCountChampions()
public async Task<ActionResult> GetCountChampions()
{
try
{

@ -81,7 +81,7 @@ namespace ApiLol.Controllers.v2
dtos = (await _manager.ChampionsMgr.GetItemsByName(pageRequest.name, pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending))
.Select(x => x.ToDto());
}
return Ok(new { Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal });
return Ok(new PageResponse<ChampionDto>{ Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal });
}
catch (Exception error)
{

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTO
{
public class PageResponse<T>
{
public IEnumerable<T> Data { get; set; }
public int index { get; set; }
public int count { get; set; }
public int total { get; set; }
}
}

@ -0,0 +1,171 @@
using ApiLol.Controllers.v1;
using DTO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Abstractions;
using StubLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ApiTests
{
[TestClass]
public class ChampionsControllerTestV1
{
private readonly StubData stub;
private readonly ChampionsController champs;
public ChampionsControllerTestV1()
{
stub = new StubData();
champs = new ChampionsController(stub, new NullLogger<ChampionsController>());
}
[TestMethod]
public async Task TestGetChampions()
{
//Arrange
//Act
var total = await stub.ChampionsMgr.GetNbItems();
var champion = await champs.Get(new PageRequest(){ index = 0, count = total });
//Assert
var objectResult = champion as OkObjectResult;
Assert.IsNotNull(objectResult);
var champions = objectResult?.Value as IEnumerable<ChampionDto>;
Assert.IsNotNull(champions);
Assert.AreEqual(champions.Count(), total);
}
[TestMethod]
public async Task TestPostChampion()
{
//Arange
var ChampionDto = new ChampionDto
{
Name = "Sylas",
Bio = "Good",
Class = ChampionClassDto.Tank,
Icon = "",
Image = new LargeImageDto() { Base64 = "" },
Skins = new List<SkinDto>()
};
//Act
var championsResult = await champs.Post(ChampionDto);
//Assert
var objectResult = championsResult as CreatedAtActionResult;
Assert.IsNotNull(objectResult);
var champions = objectResult?.Value as ChampionDto;
Assert.IsNotNull(champions);
}
[TestMethod]
public async Task TestCountChampion()
{
//Arange
var ChampionDto = new ChampionDto
{
Name = "Sylas",
Bio = "Good",
Class = ChampionClassDto.Tank,
Icon = "",
Image = new LargeImageDto() { Base64 = "" },
Skins = new List<SkinDto>()
};
//Act
var oldTotal = await stub.ChampionsMgr.GetNbItems();
var oldResult = await champs.GetCountChampions();
await champs.Post(ChampionDto);
var objectResult = oldResult as OkObjectResult;
Assert.IsNotNull(objectResult);
var newTotal = await stub.ChampionsMgr.GetNbItems();
var newResult = await champs.GetCountChampions();
//Assert
var objectResultOld = oldResult as OkObjectResult;
Assert.IsNotNull(objectResultOld);
var objectResultNew = newResult as OkObjectResult;
Assert.IsNotNull(objectResultNew);
Assert.AreEqual(objectResultOld.Value, oldTotal);
Assert.AreNotEqual(objectResultOld.Value, newTotal);
Assert.AreEqual(objectResultNew.Value, newTotal);
Assert.AreNotEqual(objectResultNew.Value, oldTotal);
}
[TestMethod]
public async Task TestPutChampion()
{
//Arange
var ChampionDto = new ChampionDto
{
Name = "Sylas",
Bio = "Good",
Class = ChampionClassDto.Tank,
Icon = "",
Image = new LargeImageDto() { Base64 = "" },
Skins = new List<SkinDto>()
};
var ChampionDtoPut = new ChampionDto
{
Name = "Sylas",
Bio = "Bad",
Class = ChampionClassDto.Tank,
Icon = "",
Image = new LargeImageDto() { Base64 = "" },
Skins = new List<SkinDto>()
};
//Act
await champs.Post(ChampionDto);
var championsResult = await champs.Put(ChampionDto.Name, ChampionDtoPut);
//Assert
var objectResult = championsResult as OkObjectResult;
Assert.IsNotNull(objectResult);
var champions = objectResult?.Value as ChampionDto;
Assert.IsNotNull(champions);
Assert.AreNotEqual(ChampionDto.Bio, champions.Bio);
Assert.AreEqual(ChampionDtoPut.Bio, champions.Bio);
}
[TestMethod]
public async Task TestDeleteChampion()
{
//Arange
//Act
var total = await stub.ChampionsMgr.GetNbItems();
var championsResult = await champs.Delete("Akali");
//Assert
var objectResult = championsResult as OkObjectResult;
Assert.IsNotNull(objectResult);
Assert.AreEqual(objectResult.Value, true);
Assert.AreNotEqual(await stub.ChampionsMgr.GetNbItems(), total);
}
}
}

@ -9,11 +9,11 @@ using StubLib;
namespace ApiTests
{
[TestClass]
public class ChampionsControllerTest
public class ChampionsControllerTestV2
{
private readonly StubData stub;
private readonly ChampionsController champs;
public ChampionsControllerTest()
public ChampionsControllerTestV2()
{
stub = new StubData();
champs = new ChampionsController(stub, new NullLogger<ChampionsController>());
@ -39,6 +39,29 @@ namespace ApiTests
}
[TestMethod]
public async Task TestGetV3Champions()
{
//Arrange
//Act
var total = await stub.ChampionsMgr.GetNbItems();
var champion = await champs.GetV3(new PageRequest());
//Assert
var objectResult = champion as OkObjectResult;
Assert.IsNotNull(objectResult);
var skinsResult = objectResult?.Value as PageResponse<ChampionDto>;
Assert.IsNotNull(skinsResult);
var result = skinsResult?.Data as IEnumerable<ChampionDto>;
Assert.IsNotNull(result);
Assert.AreEqual(result.Count(), total);
}
[TestMethod]
public async Task TestPostChampion()
{

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ApiTests
{
[TestClass]
public class RunesControllerTest
{
}
}

@ -1,126 +1,140 @@
using ApiLol.Controllers;
using DTO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Abstractions;
using StubLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ApiTests
{
[TestClass]
public class SkinsControllerTest
{
private readonly StubData stub;
private readonly SkinsController skins;
public SkinsControllerTest()
{
stub = new StubData();
skins = new SkinsController(stub, new NullLogger<SkinsController>());
}
/* [TestMethod]
public async Task TestGetSkins()
{
//Arrange
//Act
var total = await stub.SkinsMgr.GetNbItems();
var skin = await skins.Get(new PageRequest());
//Assert
var objectResult = skin as OkObjectResult;
Assert.IsNotNull(objectResult);
var skinsResult = objectResult?.Value as IEnumerable<SkinDtoC>;
Assert.IsNotNull(skinsResult);
Assert.AreEqual(skinsResult.Count(), total);
}*/
/* [TestMethod]
public async Task TestPostSkin()
{
//Arange
var SkinDto = new SkinDtoC
{
Name = "Project",
ChampionName = "Aatrox"
};
//Act
var skinsResult = await skins.Post(SkinDto);
//Assert
var objectResult = skinsResult as CreatedAtActionResult;
Assert.IsNotNull(objectResult);
var champions = objectResult?.Value as Ski;
Assert.IsNotNull(champions);
}*/
/* [TestMethod]
public async Task TestPutSkin()
{
//Arange
var ChampionDto = new ChampionDto
{
Name = "Sylas",
Bio = "Good",
Class = ChampionClassDto.Tank,
Icon = "",
Image = new LargeImageDto() { Base64 = "" },
Skins = new List<SkinDto>()
};
var ChampionDtoPut = new ChampionDto
{
Name = "Sylas",
Bio = "Bad",
Class = ChampionClassDto.Tank,
Icon = "",
Image = new LargeImageDto() { Base64 = "" },
Skins = new List<SkinDto>()
};
//Act
await champs.Post(ChampionDto);
var championsResult = await champs.Put(ChampionDto.Name, ChampionDtoPut);
//Assert
var objectResult = championsResult as OkObjectResult;
Assert.IsNotNull(objectResult);
var champions = objectResult?.Value as ChampionDto;
Assert.IsNotNull(champions);
Assert.AreNotEqual(ChampionDto.Bio, champions.Bio);
Assert.AreEqual(ChampionDtoPut.Bio, champions.Bio);
}
[TestMethod]
public async Task TestDeleteChampion()
{
//Arange
//Act
var total = await stub.ChampionsMgr.GetNbItems();
var championsResult = await champs.Delete("Akali");
//Assert
var objectResult = championsResult as OkObjectResult;
Assert.IsNotNull(objectResult);
Assert.AreEqual(objectResult.Value, true);
Assert.AreNotEqual(await stub.ChampionsMgr.GetNbItems(), total);
}*/
}
}
using ApiLol.Controllers;
using DTO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Abstractions;
using StubLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ApiTests
{
[TestClass]
public class SkinsControllerTest
{
private readonly StubData stub;
private readonly SkinsController skins;
public SkinsControllerTest()
{
stub = new StubData();
skins = new SkinsController(stub, new NullLogger<SkinsController>());
}
[TestMethod]
public async Task TestGetSkins()
{
//Arrange
//Act
var total = await stub.SkinsMgr.GetNbItems();
var skin = await skins.Get(new PageRequest());
//Assert
var objectResult = skin as OkObjectResult;
Assert.IsNotNull(objectResult);
var skinsResult = objectResult.Value as PageResponse<SkinDtoC>;
Assert.IsNotNull(skinsResult);
var result = skinsResult.Data as IEnumerable<SkinDtoC>;
Assert.IsNotNull(result);
Assert.AreEqual(result.Count(), total);
Assert.AreEqual(total, skinsResult.total);
}
[TestMethod]
public async Task TestPostSkin()
{
//Arange
var SkinDto = new SkinDtoC
{
Name = "Project",
Description = "Test",
Icon = "",
Image = new LargeImageDto(),
Price = 900,
ChampionName = "aatrox"
};
//Act
var total = await stub.SkinsMgr.GetNbItems();
var skinsResult = await skins.Post(SkinDto);
//Assert
var objectResult = skinsResult as CreatedAtActionResult;
Assert.IsNotNull(objectResult);
var isSkinDto = objectResult?.Value as SkinDtoC;
Assert.IsNotNull(isSkinDto);
Assert.AreEqual(total+1, await stub.SkinsMgr.GetNbItems());
}
[TestMethod]
public async Task TestPutSkin()
{
//Arange
var SkinDto = new SkinDtoC
{
Name = "Project",
Description = "Test",
Icon = "",
Image = new LargeImageDto(),
Price = 900,
ChampionName = "aatrox"
};
var SkinDtoPut = new SkinDtoC
{
Name = "Project",
Description = "ForTestTest",
Icon = "",
Image = new LargeImageDto(),
Price = 850,
ChampionName = "aatrox"
};
//Act
await skins.Post(SkinDto);
var skinsResult = await skins.Put(SkinDto.Name, SkinDtoPut);
//Assert
var objectResult = skinsResult as OkObjectResult;
Assert.IsNotNull(objectResult);
var skin = objectResult?.Value as SkinDtoC;
Assert.IsNotNull(skin);
Assert.AreNotEqual(SkinDto.Description, skin.Description);
Assert.AreNotEqual(SkinDto.Price, skin.Price);
Assert.AreEqual(SkinDtoPut.Description, skin.Description);
Assert.AreEqual(SkinDtoPut.Price, skin.Price);
}
[TestMethod]
public async Task TestDeleteChampion()
{
//Arange
//Act
var total = await stub.SkinsMgr.GetNbItems();
var skinsResult = await skins.Delete("Stinger");
//Assert
var objectResult = skinsResult as OkObjectResult;
Assert.IsNotNull(objectResult);
Assert.AreEqual(objectResult.Value, true);
Assert.AreNotEqual(await stub.SkinsMgr.GetNbItems(), total);
}
}
}

@ -124,5 +124,26 @@ namespace UT_EF
}
}
[Fact]
public void Test_DataSeeder()
{
var options = new DbContextOptionsBuilder<LolDbContext>()
.UseInMemoryDatabase(databaseName: "DataSeeder_Test_Champion_database")
.Options;
using (var context = new LolDbContext(options))
{
DataSeeder.SeedData(context);
string nameToFind = "hecarim";
ChampionClassEntity type = ChampionClassEntity.Assassin;
Assert.Equal(1, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count());
Assert.Equal(1, context.Champions.Where(c => c.Class == type).Count());
Assert.Equal(3, context.Champions.Count());
}
}
}
}
Loading…
Cancel
Save