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

@ -51,7 +51,7 @@ namespace ApiLol.Controllers
dtos = (await _manager.RunesMgr.GetItemsByName(pageRequest.name, pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending)) dtos = (await _manager.RunesMgr.GetItemsByName(pageRequest.name, pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending))
.Select(x => x.ToDto()); .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) catch (Exception error)

@ -50,7 +50,7 @@ namespace ApiLol.Controllers
dtos = (await _manager.SkinsMgr.GetItemsByName(pageRequest.name, pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending)) dtos = (await _manager.SkinsMgr.GetItemsByName(pageRequest.name, pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending))
.Select(x => x.ToDtoC()); .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) catch (Exception error)

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

@ -81,7 +81,7 @@ namespace ApiLol.Controllers.v2
dtos = (await _manager.ChampionsMgr.GetItemsByName(pageRequest.name, pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending)) dtos = (await _manager.ChampionsMgr.GetItemsByName(pageRequest.name, pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending))
.Select(x => x.ToDto()); .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) 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 namespace ApiTests
{ {
[TestClass] [TestClass]
public class ChampionsControllerTest public class ChampionsControllerTestV2
{ {
private readonly StubData stub; private readonly StubData stub;
private readonly ChampionsController champs; private readonly ChampionsController champs;
public ChampionsControllerTest() public ChampionsControllerTestV2()
{ {
stub = new StubData(); stub = new StubData();
champs = new ChampionsController(stub, new NullLogger<ChampionsController>()); 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] [TestMethod]
public async Task TestPostChampion() 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
{
}
}

@ -22,7 +22,7 @@ namespace ApiTests
skins = new SkinsController(stub, new NullLogger<SkinsController>()); skins = new SkinsController(stub, new NullLogger<SkinsController>());
} }
/* [TestMethod] [TestMethod]
public async Task TestGetSkins() public async Task TestGetSkins()
{ {
//Arrange //Arrange
@ -35,71 +35,85 @@ namespace ApiTests
var objectResult = skin as OkObjectResult; var objectResult = skin as OkObjectResult;
Assert.IsNotNull(objectResult); Assert.IsNotNull(objectResult);
var skinsResult = objectResult?.Value as IEnumerable<SkinDtoC>; var skinsResult = objectResult.Value as PageResponse<SkinDtoC>;
Assert.IsNotNull(skinsResult); Assert.IsNotNull(skinsResult);
Assert.AreEqual(skinsResult.Count(), total); var result = skinsResult.Data as IEnumerable<SkinDtoC>;
Assert.IsNotNull(result);
Assert.AreEqual(result.Count(), total);
Assert.AreEqual(total, skinsResult.total);
}*/ }
/* [TestMethod] [TestMethod]
public async Task TestPostSkin() public async Task TestPostSkin()
{ {
//Arange //Arange
var SkinDto = new SkinDtoC var SkinDto = new SkinDtoC
{ {
Name = "Project", Name = "Project",
ChampionName = "Aatrox" Description = "Test",
Icon = "",
Image = new LargeImageDto(),
Price = 900,
ChampionName = "aatrox"
}; };
//Act //Act
var total = await stub.SkinsMgr.GetNbItems();
var skinsResult = await skins.Post(SkinDto); var skinsResult = await skins.Post(SkinDto);
//Assert //Assert
var objectResult = skinsResult as CreatedAtActionResult; var objectResult = skinsResult as CreatedAtActionResult;
Assert.IsNotNull(objectResult); Assert.IsNotNull(objectResult);
var champions = objectResult?.Value as Ski; var isSkinDto = objectResult?.Value as SkinDtoC;
Assert.IsNotNull(champions); Assert.IsNotNull(isSkinDto);
}*/ Assert.AreEqual(total+1, await stub.SkinsMgr.GetNbItems());
}
/* [TestMethod] [TestMethod]
public async Task TestPutSkin() public async Task TestPutSkin()
{ {
//Arange //Arange
var ChampionDto = new ChampionDto var SkinDto = new SkinDtoC
{ {
Name = "Sylas", Name = "Project",
Bio = "Good", Description = "Test",
Class = ChampionClassDto.Tank,
Icon = "", Icon = "",
Image = new LargeImageDto() { Base64 = "" }, Image = new LargeImageDto(),
Skins = new List<SkinDto>() Price = 900,
ChampionName = "aatrox"
}; };
var ChampionDtoPut = new ChampionDto var SkinDtoPut = new SkinDtoC
{ {
Name = "Sylas", Name = "Project",
Bio = "Bad", Description = "ForTestTest",
Class = ChampionClassDto.Tank,
Icon = "", Icon = "",
Image = new LargeImageDto() { Base64 = "" }, Image = new LargeImageDto(),
Skins = new List<SkinDto>() Price = 850,
ChampionName = "aatrox"
}; };
//Act //Act
await champs.Post(ChampionDto); await skins.Post(SkinDto);
var championsResult = await champs.Put(ChampionDto.Name, ChampionDtoPut); var skinsResult = await skins.Put(SkinDto.Name, SkinDtoPut);
//Assert //Assert
var objectResult = championsResult as OkObjectResult; var objectResult = skinsResult as OkObjectResult;
Assert.IsNotNull(objectResult); Assert.IsNotNull(objectResult);
var champions = objectResult?.Value as ChampionDto; var skin = objectResult?.Value as SkinDtoC;
Assert.IsNotNull(champions); Assert.IsNotNull(skin);
Assert.AreNotEqual(ChampionDto.Bio, champions.Bio); Assert.AreNotEqual(SkinDto.Description, skin.Description);
Assert.AreEqual(ChampionDtoPut.Bio, champions.Bio); Assert.AreNotEqual(SkinDto.Price, skin.Price);
Assert.AreEqual(SkinDtoPut.Description, skin.Description);
Assert.AreEqual(SkinDtoPut.Price, skin.Price);
} }
@ -110,17 +124,17 @@ namespace ApiTests
//Act //Act
var total = await stub.ChampionsMgr.GetNbItems(); var total = await stub.SkinsMgr.GetNbItems();
var championsResult = await champs.Delete("Akali"); var skinsResult = await skins.Delete("Stinger");
//Assert //Assert
var objectResult = championsResult as OkObjectResult; var objectResult = skinsResult as OkObjectResult;
Assert.IsNotNull(objectResult); Assert.IsNotNull(objectResult);
Assert.AreEqual(objectResult.Value, true); Assert.AreEqual(objectResult.Value, true);
Assert.AreNotEqual(await stub.ChampionsMgr.GetNbItems(), total); 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