add unit test

rune
Lucas Delanier 2 years ago
commit 62387e36f5

@ -7,6 +7,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.6" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

@ -3,8 +3,6 @@ using DTO;
using Microsoft.AspNetCore.Mvc;
using Model;
using StubLib;
using System;
using System.Xml.Linq;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
@ -14,60 +12,51 @@ namespace APILOL.Controllers
[ApiController]
public class ChampionsController : ControllerBase
{
IChampionsManager managerChampions;
public ChampionsController(IDataManager manager)
{
managerChampions = manager.ChampionsMgr;
}
IChampionsManager dataManager = new StubData().ChampionsMgr;
// GET: api/<ChampionController>
[HttpGet]
public async Task<IActionResult> Get()
{
IEnumerable<ChampionDTO> championDTOs = (await managerChampions.GetItems(0, await managerChampions.GetNbItems())).Select(x => x.ToDTO());
if (championDTOs != null)
{
return Ok(championDTOs);
}
return NotFound();
var champions = await dataManager.GetItems(0, await dataManager.GetNbItems());
IEnumerable<ChampionDTO> items = champions.Select(c => c.ToDto());
return Ok(items);
}
// GET api/<ChampionController>/5
[HttpGet("{name}")]
public async Task<IActionResult> GetAsync(String name)
public async Task<IActionResult> Get(string name)
{
IEnumerable<Champion> champ = await managerChampions.GetItemsByName(name, 0, await managerChampions.GetNbItems());
if (champ.Count() != 0)
if (dataManager.GetNbItemsByName(name) != null)
{
return Ok(champ.First().ToDTO());
return Ok(dataManager.GetItemsByName(name, 0, await dataManager.GetNbItems()));
}
return NotFound();
}
// POST api/<ChampionController>
[HttpPost]
public async Task<IActionResult> Post([FromBody] ChampionDTO champion)
public async Task<IActionResult> Post([FromBody] ChampionDTO championDTO)
{
return CreatedAtAction(nameof(Get), await managerChampions.AddItem(champion.ToModel()));
return CreatedAtAction(nameof(Get),(await dataManager.AddItem(championDTO.ToModel())).ToDto);
}
// PUT api/<ChampionController>/5
[HttpPut("{name}")]
public async void Put(String name, [FromBody] ChampionDTO champion)
public void Put(string name, [FromBody] ChampionDTO championDTO)
{
}
// DELETE api/<ChampionController>/5
[HttpDelete("{id}")]
public async void Delete(int id)
[HttpDelete("{name}")]
public void Delete(string name)
{
}
}
}
/*
var champion = new Champion("");
var dto = ChampionMapper.ToDto(champion);
*/

@ -1,20 +1,22 @@
using DTO;
using Model;
using System.Buffers.Text;
using System.Xml.Linq;
namespace APILOL.Mapper
{
public static class ChampionMapper
{
public static ChampionDTO ToDTO(this Champion champ)
public static ChampionDTO ToDto(this Champion champion)
{
return new ChampionDTO() { Name = champ.Name, Bio = champ.Bio, Icon = champ.Icon, Image = champ.Image.Base64 };
return new ChampionDTO()
{
Name = champion.Name,
Bio = champion.Bio,
};
}
public static Champion ToModel(this ChampionDTO champ)
public static Champion ToModel(this ChampionDTO champion)
{
return new Champion(champ.Name,icon: champ.Icon, image: champ.Icon, bio: champ.Bio);
return new Champion(champion.Name);
}
}
}

@ -4,13 +4,5 @@
{
public string Name { get; set; }
public string Bio { get; set; }
public string Icon { get; set; }
public string Image { get; set; }
}
}

@ -6,4 +6,14 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.6" />
</ItemGroup>
</Project>

@ -0,0 +1,12 @@
using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkLOL
{
class ChampionContext : DbContext
{
public DbSet<ChampionEntity> Champions { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite($"Data Source=DBLOL.db");
}
}

@ -0,0 +1,20 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace EntityFrameworkLOL
{
class ChampionEntity
{
[Key]
public string Name { get; set; }
public string Bio { get; set; }
public string Icon { get; set; }
}
}

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

@ -0,0 +1,42 @@
// <auto-generated />
using EntityFrameworkLOL;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace EntityFrameworkLOL.Migrations
{
[DbContext(typeof(ChampionContext))]
[Migration("20230201163336_MigrationWallah")]
partial class MigrationWallah
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.2");
modelBuilder.Entity("EntityFrameworkLOL.ChampionEntity", b =>
{
b.Property<string>("Name")
.HasColumnType("TEXT");
b.Property<string>("Bio")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Icon")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Name");
b.ToTable("Champions");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EntityFrameworkLOL.Migrations
{
/// <inheritdoc />
public partial class MigrationWallah : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Champions",
columns: table => new
{
Name = table.Column<string>(type: "TEXT", nullable: false),
Bio = table.Column<string>(type: "TEXT", nullable: false),
Icon = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Champions", x => x.Name);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Champions");
}
}
}

@ -0,0 +1,39 @@
// <auto-generated />
using EntityFrameworkLOL;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace EntityFrameworkLOL.Migrations
{
[DbContext(typeof(ChampionContext))]
partial class ChampionContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.2");
modelBuilder.Entity("EntityFrameworkLOL.ChampionEntity", b =>
{
b.Property<string>("Name")
.HasColumnType("TEXT");
b.Property<string>("Bio")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Icon")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Name");
b.ToTable("Champions");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

@ -17,7 +17,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubLib", "StubLib\StubLib.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APILOL", "APILOL\APILOL.csproj", "{88538F30-9D26-4A70-88FD-12BA05576E39}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DTO", "DTO\DTO.csproj", "{2124E1DB-1E6D-4FCE-9F34-53457C374394}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DTO", "DTO\DTO.csproj", "{1434DEF6-0575-4C3D-BF14-AF29A444BC74}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkLOL", "EntityFrameworkLOL\EntityFrameworkLOL.csproj", "{61D807B0-FA1A-439D-9810-9F31A0C47034}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUnitaire", "TestUnitaire\TestUnitaire.csproj", "{D24FBC48-F9C3-45CA-8D51-A851559C307F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,10 +49,18 @@ Global
{88538F30-9D26-4A70-88FD-12BA05576E39}.Debug|Any CPU.Build.0 = Debug|Any CPU
{88538F30-9D26-4A70-88FD-12BA05576E39}.Release|Any CPU.ActiveCfg = Release|Any CPU
{88538F30-9D26-4A70-88FD-12BA05576E39}.Release|Any CPU.Build.0 = Release|Any CPU
{2124E1DB-1E6D-4FCE-9F34-53457C374394}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2124E1DB-1E6D-4FCE-9F34-53457C374394}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2124E1DB-1E6D-4FCE-9F34-53457C374394}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2124E1DB-1E6D-4FCE-9F34-53457C374394}.Release|Any CPU.Build.0 = Release|Any CPU
{1434DEF6-0575-4C3D-BF14-AF29A444BC74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1434DEF6-0575-4C3D-BF14-AF29A444BC74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1434DEF6-0575-4C3D-BF14-AF29A444BC74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1434DEF6-0575-4C3D-BF14-AF29A444BC74}.Release|Any CPU.Build.0 = Release|Any CPU
{61D807B0-FA1A-439D-9810-9F31A0C47034}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{61D807B0-FA1A-439D-9810-9F31A0C47034}.Debug|Any CPU.Build.0 = Debug|Any CPU
{61D807B0-FA1A-439D-9810-9F31A0C47034}.Release|Any CPU.ActiveCfg = Release|Any CPU
{61D807B0-FA1A-439D-9810-9F31A0C47034}.Release|Any CPU.Build.0 = Release|Any CPU
{D24FBC48-F9C3-45CA-8D51-A851559C307F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D24FBC48-F9C3-45CA-8D51-A851559C307F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D24FBC48-F9C3-45CA-8D51-A851559C307F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D24FBC48-F9C3-45CA-8D51-A851559C307F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -56,6 +68,7 @@ Global
GlobalSection(NestedProjects) = preSolution
{1889FA6E-B7C6-416E-8628-9449FB9070B9} = {C76D0C23-1FFA-4963-93CD-E12BD643F030}
{B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB} = {2C607793-B163-4731-A4D1-AFE8A7C4C170}
{D24FBC48-F9C3-45CA-8D51-A851559C307F} = {C76D0C23-1FFA-4963-93CD-E12BD643F030}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {92F3083D-793F-4552-8A9A-0AD6534159C9}

@ -12,6 +12,15 @@
<ItemGroup>
<Folder Include="enums\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

@ -2,7 +2,8 @@
public interface IGenericDataManager<T>
{
Task<int> GetNbItems();
Task<IEnumerable<T>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false);
Task<IEnumerable<T>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false);
Task<int> GetNbItemsByName(string substring);
Task<IEnumerable<T>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false);
Task<T> UpdateItem(T oldItem, T newItem);

@ -6,4 +6,14 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.6" />
</ItemGroup>
</Project>

@ -60,6 +60,7 @@ namespace StubLib
collection.Add(newItem!);
return Task.FromResult<T?>(newItem);
}
}
}

@ -25,12 +25,18 @@ namespace StubLib
public Task<Champion?> AddItem(Champion? item)
=> parent.champions.AddItem(item);
public Task<bool> DeleteItem(Champion? item)
=> parent.champions.DeleteItem(item);
public Task<int> GetNbItems()
=> Task.FromResult(parent.champions.Count);
public Champion GetLast()
=> parent.champions.Last();
public Task<IEnumerable<Champion?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
=> parent.champions.GetItemsWithFilterAndOrdering(
c => true,

@ -6,6 +6,16 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\APILOL\APILOL.csproj" />
<ProjectReference Include="..\StubLib\StubLib.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,62 @@
using APILOL.Controllers;
using APILOL.Mapper;
using DTO;
using Microsoft.AspNetCore.Mvc;
using StubLib;
namespace TestUnitaire
{
[TestClass]
public class UnitTestChampion
{
private readonly ChampionsController controller;
private readonly StubData stub;
public UnitTestChampion()
{
stub = new StubData();
controller = new ChampionsController();
}
[TestMethod]
public async Task TestGet()
{
var champions = await controller.Get();
var resultObject = champions as OkObjectResult;
Assert.IsNotNull(resultObject);
var resultType = resultObject?.Value as IEnumerable<ChampionDTO>;
Assert.IsNotNull(resultType);
Assert.AreEqual(resultType.Count(), await stub.ChampionsMgr.GetNbItems());
}
[TestMethod]
public async Task TestPost()
{
var ChampionDtoToTest = new ChampionDTO { Bio= "This champion is a legendary Fox", Name="Foxane"};
var champions = await controller.Post(ChampionDtoToTest);
var resultObject = champions as OkObjectResult;
Assert.IsNotNull(resultObject);
var resultType = resultObject?.Value as IEnumerable<ChampionDTO>;
Assert.IsNotNull(resultType);
Assert.AreEqual(resultType.Last(), stub.ChampionsMgr.GetItems(-1, await stub.ChampionsMgr.GetNbItems()));
}
[TestMethod]
public void TestPut()
{
}
[TestMethod]
public void TestDelete()
{
}
}
}

@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;

@ -15,6 +15,13 @@
<None Remove="Microsoft.Extensions.DependencyInjection" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.6" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
</ItemGroup>
</Project>

Loading…
Cancel
Save