test unitaire / Client

master
Jolys Enzo 2 years ago
parent 182792d689
commit 5723da8b69

@ -36,6 +36,7 @@ namespace Api_lol.Controllers
public IActionResult Post(DtoChampions champDTO)
{
Champion tmp = champDTO.DtoToModel();
data.ChampionsMgr.AddItem(tmp);
return Ok();
}
@ -45,9 +46,13 @@ namespace Api_lol.Controllers
[Route("{name}")]
public async Task<IActionResult> GetChampion(string name)
{
var champs = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).Where(i => i.Name == name).First();
DtoChampions result = champs.ModelToDto();
return Ok(champs);
Champion champion = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).First(i => i.Name == name);
if ( champion == null)
{
return BadRequest();
}
DtoChampions result = champion.ModelToDto();
return Ok(result);
}
}

@ -1,33 +0,0 @@
using Microsoft.AspNetCore.Mvc;
namespace Api_lol.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}

@ -1,13 +0,0 @@
namespace Api_lol
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}

@ -8,8 +8,11 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Api-lol\Api-lol.csproj" />
<ProjectReference Include="..\DTO\DTO.csproj" />
<ProjectReference Include="..\EntityFramwork\EntityFramwork.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
<ProjectReference Include="..\StubLib\StubLib.csproj" />
</ItemGroup>
</Project>

@ -11,23 +11,29 @@ namespace Client
public class ClientChampions
{
private readonly HttpClient httpClient_client;
private readonly string urlConstant = "Champions/";
private readonly string urlConstant = "https://localhost:7081/Champions/";
public ClientChampions(HttpClient httpClient_client)
public ClientChampions()
{
this.httpClient_client = httpClient_client;
this.httpClient_client = new HttpClient();
}
public async void Get()
public async Task<List<DtoChampions>> Get()
{
await tmp = httpClient_client.GetFromJsonAsync<List<DtoChampions>(urlConstant);
var tmp = await httpClient_client.GetFromJsonAsync<List<DtoChampions>>(urlConstant);
return tmp;
}
public void Post(DtoChampions champions)
public async Task Post(DtoChampions champions)
{
await httpClient_client.PostAsJsonAsync(urlConstant, champions);
}
public async Task<DtoChampions> GetChampion(string name)
{
var tmp = await httpClient_client.GetFromJsonAsync<DtoChampions>(urlConstant+name);
return tmp;
}
}
}

@ -1,2 +1,43 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
using Client;
using DTO;
using EntityFramwork;
using EntityFramwork.Factories;
using Model;
using StubLib;
/*
StubData tmp = new StubData();
var tmpListe = await tmp.ChampionsMgr.GetItemsByName("Akali", 0, 6);
Champion champ = tmpListe.First();
Factories facto = new Factories();
EntityChampions entity = facto.ModelToEntity(champ);
using ( BDDContext context = new BDDContext())
{
context.Add(entity);
context.SaveChanges();
}
*/
Console.WriteLine("Start");
Console.ReadLine();
ClientChampions client = new ClientChampions();
List<DtoChampions> champions = await client.Get();
DtoChampions champ = await client.GetChampion("Akali");
Console.WriteLine(champions.Count);
Console.WriteLine(champ.name);
DtoChampions dtoTest = new DtoChampions("toto");
await client.Post(dtoTest);
List<DtoChampions> champions2 = await client.Get();
Console.WriteLine(champions2.Count);
DtoChampions champ2 = await client.GetChampion("toto");
Console.WriteLine(champ2.name);
Console.WriteLine("End");
Console.ReadLine();

@ -8,7 +8,6 @@ namespace DTO
{
public class DtoChampions
{
public long id { get; set; }
public string name { get; set; }
public DtoChampions(string name)

@ -1,14 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTO
{
public class EntityChampions
{
public long id { get; set; }
public string name { get; set; }
}
}

@ -10,6 +10,7 @@ namespace EntityFramwork
public DbSet<EntityChampions> Champions { get; set; }
public DbSet<EntitySkins> Skins { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

@ -1,4 +1,5 @@
using System;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -8,7 +9,15 @@ namespace DTO
{
public class EntityChampions
{
public long id { get; set; }
public string name { get; set; }
public long Id { get; set; }
public string Name { get; set; }
public string Bio { get; set; }
public string Icon { get; set; }
//public List<Skin> Skins { get; set; }
}
}

@ -18,6 +18,11 @@
<ItemGroup>
<ProjectReference Include="..\DTO\DTO.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
</Project>

@ -0,0 +1,26 @@
using DTO;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityFramwork
{
public class EntitySkins
{
public long Id { get; set; }
public string Description { get; set; }
public string Icon { get; set; }
public float Price { get; set; }
[ForeignKey("ChampionsForeignKey")]
public EntityChampions EntityChampion { get; set; }
public long ChampionsForeignKey { get;set; }
}
}

@ -0,0 +1,20 @@
using DTO;
using Model;
namespace EntityFramwork.Factories
{
public class Factories
{
public EntityChampions ModelToEntity(Champion champ)
{
EntityChampions entity = new EntityChampions();
entity.Name = champ.Name;
entity.Bio = champ.Bio;
entity.Icon = champ.Icon;
//entity.Skins = new List<Skin>(champ.Skins);
return entity;
}
}
}

@ -1,39 +0,0 @@
// <auto-generated />
using EntityFramwork;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace EntityFramwork.Migrations
{
[DbContext(typeof(BDDContext))]
[Migration("20230130130012_monNomDeMigration")]
partial class monNomDeMigration
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.2");
modelBuilder.Entity("DTO.EntityChampions", b =>
{
b.Property<long>("id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("id");
b.ToTable("Champions");
});
#pragma warning restore 612, 618
}
}
}

@ -1,34 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EntityFramwork.Migrations
{
/// <inheritdoc />
public partial class monNomDeMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Champions",
columns: table => new
{
id = table.Column<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
name = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Champions", x => x.id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Champions");
}
}
}

@ -0,0 +1,69 @@
// <auto-generated />
using EntityFramwork;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace EntityFramwork.Migrations
{
[DbContext(typeof(BDDContext))]
[Migration("20230201131800_migrationTest")]
partial class migrationTest
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.2");
modelBuilder.Entity("DTO.EntityChampions", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Bio")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Icon")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Champions");
});
modelBuilder.Entity("EntityFramwork.EntitySkins", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Icon")
.IsRequired()
.HasColumnType("TEXT");
b.Property<float>("Price")
.HasColumnType("REAL");
b.HasKey("Id");
b.ToTable("Skins");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,54 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EntityFramwork.Migrations
{
/// <inheritdoc />
public partial class migrationTest : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Champions",
columns: table => new
{
Id = table.Column<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
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.Id);
});
migrationBuilder.CreateTable(
name: "Skins",
columns: table => new
{
Id = table.Column<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Description = table.Column<string>(type: "TEXT", nullable: false),
Icon = table.Column<string>(type: "TEXT", nullable: false),
Price = table.Column<float>(type: "REAL", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Skins", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Champions");
migrationBuilder.DropTable(
name: "Skins");
}
}
}

@ -18,18 +18,48 @@ namespace EntityFramwork.Migrations
modelBuilder.Entity("DTO.EntityChampions", b =>
{
b.Property<long>("id")
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("name")
b.Property<string>("Bio")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("id");
b.Property<string>("Icon")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Champions");
});
modelBuilder.Entity("EntityFramwork.EntitySkins", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Icon")
.IsRequired()
.HasColumnType("TEXT");
b.Property<float>("Price")
.HasColumnType("REAL");
b.HasKey("Id");
b.ToTable("Skins");
});
#pragma warning restore 612, 618
}
}

@ -21,7 +21,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DTO", "DTO\DTO.csproj", "{3
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFramwork", "EntityFramwork\EntityFramwork.csproj", "{2225E38B-8445-40D2-A2FE-9F90F7ACD463}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{B1BDE539-7DC3-4DC7-A908-36119E468FA8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{B1BDE539-7DC3-4DC7-A908-36119E468FA8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUnitaireApi", "TestUnitaireApi\TestUnitaireApi.csproj", "{1FB0F7BE-AEEE-42FD-BCE9-C11AE6923E6C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -61,6 +63,10 @@ Global
{B1BDE539-7DC3-4DC7-A908-36119E468FA8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B1BDE539-7DC3-4DC7-A908-36119E468FA8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B1BDE539-7DC3-4DC7-A908-36119E468FA8}.Release|Any CPU.Build.0 = Release|Any CPU
{1FB0F7BE-AEEE-42FD-BCE9-C11AE6923E6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1FB0F7BE-AEEE-42FD-BCE9-C11AE6923E6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FB0F7BE-AEEE-42FD-BCE9-C11AE6923E6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FB0F7BE-AEEE-42FD-BCE9-C11AE6923E6C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -68,6 +74,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}
{1FB0F7BE-AEEE-42FD-BCE9-C11AE6923E6C} = {C76D0C23-1FFA-4963-93CD-E12BD643F030}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {92F3083D-793F-4552-8A9A-0AD6534159C9}

@ -0,0 +1,42 @@
using Api_lol.Controllers;
using DTO;
using Microsoft.AspNetCore.Mvc;
using StubLib;
using System.Net.Http.Json;
using System.Reflection;
namespace TestUnitaireApi
{
public class TestChampions
{
StubData stubChamps;
Champions controlleurChampion;
public TestChampions()
{
this.stubChamps = new StubData();
this.controlleurChampion = new Champions(null,stubChamps);
}
[Fact]
public async Task TestGetChampions()
{
var liste = await controlleurChampion.Get();
OkObjectResult tmp = liste as OkObjectResult;
if (tmp == null)
{
Assert.Fail("Liste okObject == null");
return;
}
IEnumerable<DtoChampions> listeChamp = tmp.Value as IEnumerable<DtoChampions>;
if (listeChamp == null)
{
Assert.Fail("Liste champ == null");
return;
}
Assert.Equal(6,listeChamp.Count());
}
}
}

@ -0,0 +1,30 @@
<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.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Api-lol\Api-lol.csproj" />
<ProjectReference Include="..\DTO\DTO.csproj" />
<ProjectReference Include="..\StubLib\StubLib.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1 @@
global using Xunit;
Loading…
Cancel
Save