From 1b7220890e92154dbad7a6170e59e8d0a553b40f Mon Sep 17 00:00:00 2001 From: Corentin R <76619184+Koroh63@users.noreply.github.com> Date: Thu, 2 Feb 2023 17:10:16 +0100 Subject: [PATCH] Ajout des tests non-fonctionnels ! --- .../Controllers/ChampionsController.cs | 16 ++++++--- Sources/API_LoL/Mapper/ChampionMapper.cs | 12 ++++--- Sources/API_LoL/Program.cs | 5 +++ Sources/ApiTests/ApiTests.csproj | 10 ------ Sources/ApiTests/Program.cs | 1 - Sources/Api_UT/Api_UT.csproj | 25 +++++++++++++ Sources/Api_UT/UnitTest1.cs | 33 ++++++++++++++++++ Sources/Api_UT/Usings.cs | 1 + Sources/DTO/ChampionDTO.cs | 11 +++++- Sources/EntityFramework/ChampionEntity.cs | 24 +++++++++++++ .../EntityFramework/EntityFramework.csproj | 20 +++++++++++ Sources/EntityFramework/LoLDbContext.cs | 19 ++++++++++ Sources/EntityFramework/Program.cs | 10 ++++++ Sources/EntityFramework/champion.db | Bin 0 -> 20480 bytes Sources/LeagueOfLegends.sln | 20 +++++++---- Sources/Model/Properties/launchSettings.json | 12 +++++++ .../Tests/ConsoleTests/ConsoleTests.csproj | 3 +- .../Properties/launchSettings.json | 12 +++++++ Sources/Tests/Tests.csproj | 18 ++++++++++ Sources/Tests/UnitTest1.cs | 10 ++++++ Sources/Tests/Usings.cs | 1 + 21 files changed, 233 insertions(+), 30 deletions(-) delete mode 100644 Sources/ApiTests/ApiTests.csproj delete mode 100644 Sources/ApiTests/Program.cs create mode 100644 Sources/Api_UT/Api_UT.csproj create mode 100644 Sources/Api_UT/UnitTest1.cs create mode 100644 Sources/Api_UT/Usings.cs create mode 100644 Sources/EntityFramework/ChampionEntity.cs create mode 100644 Sources/EntityFramework/EntityFramework.csproj create mode 100644 Sources/EntityFramework/LoLDbContext.cs create mode 100644 Sources/EntityFramework/Program.cs create mode 100644 Sources/EntityFramework/champion.db create mode 100644 Sources/Model/Properties/launchSettings.json create mode 100644 Sources/Tests/ConsoleTests/Properties/launchSettings.json create mode 100644 Sources/Tests/Tests.csproj create mode 100644 Sources/Tests/UnitTest1.cs create mode 100644 Sources/Tests/Usings.cs diff --git a/Sources/API_LoL/Controllers/ChampionsController.cs b/Sources/API_LoL/Controllers/ChampionsController.cs index 9461ad4..1d4efad 100644 --- a/Sources/API_LoL/Controllers/ChampionsController.cs +++ b/Sources/API_LoL/Controllers/ChampionsController.cs @@ -12,14 +12,19 @@ namespace API_LoL.Controllers [ApiController] public class ChampionsController : ControllerBase { - private StubData.ChampionsManager ChampionsManager { get; set; } = new StubData.ChampionsManager(new StubData()); + + public ChampionsController(IDataManager Manager) { + this.ChampionsManager= Manager.ChampionsMgr; + } + + private IChampionsManager ChampionsManager; // GET: api/ [HttpGet] public async Task Get() { var list = await ChampionsManager.GetItems(0,await ChampionsManager.GetNbItems()); - if (list.Count() == 0) { + if (list.Count() != 0) { return Ok(list.Select(champion => champion?.toDTO())); }else { return NoContent(); @@ -27,11 +32,12 @@ namespace API_LoL.Controllers } // GET api//5 + /* [HttpGet("{id}")] public string Get(String name) { return "value"; - } + }*/ // POST api/ [HttpPost] @@ -43,8 +49,8 @@ namespace API_LoL.Controllers } else { - //ChampionsManager.AddItem(champion) - return CreatedAtAction("Post",champion.Name); + await ChampionsManager.AddItem(champion.toChampion()); + return CreatedAtAction("Post",champion); } } diff --git a/Sources/API_LoL/Mapper/ChampionMapper.cs b/Sources/API_LoL/Mapper/ChampionMapper.cs index f091999..b380b7e 100644 --- a/Sources/API_LoL/Mapper/ChampionMapper.cs +++ b/Sources/API_LoL/Mapper/ChampionMapper.cs @@ -11,11 +11,13 @@ namespace DTO.Mapper { public static ChampionDTO toDTO(this Champion champion) { - return new ChampionDTO() - { - Name = champion.Name, - Bio = champion.Bio, - }; + return new ChampionDTO(champion.Name, champion.Bio, champion.Icon); + } + + public static Champion toChampion(this ChampionDTO champion) + { + return new Champion(champion.Name, ChampionClass.Unknown, champion.Icon, "", champion.Bio); + } } } diff --git a/Sources/API_LoL/Program.cs b/Sources/API_LoL/Program.cs index 48863a6..d4fb3b8 100644 --- a/Sources/API_LoL/Program.cs +++ b/Sources/API_LoL/Program.cs @@ -1,3 +1,6 @@ +using Model; +using StubLib; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -7,6 +10,8 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddScoped(); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/Sources/ApiTests/ApiTests.csproj b/Sources/ApiTests/ApiTests.csproj deleted file mode 100644 index 74abf5c..0000000 --- a/Sources/ApiTests/ApiTests.csproj +++ /dev/null @@ -1,10 +0,0 @@ - - - - Exe - net6.0 - enable - enable - - - diff --git a/Sources/ApiTests/Program.cs b/Sources/ApiTests/Program.cs deleted file mode 100644 index a5af879..0000000 --- a/Sources/ApiTests/Program.cs +++ /dev/null @@ -1 +0,0 @@ -// See https://aka.ms/new-console-template for more information diff --git a/Sources/Api_UT/Api_UT.csproj b/Sources/Api_UT/Api_UT.csproj new file mode 100644 index 0000000..089bff3 --- /dev/null +++ b/Sources/Api_UT/Api_UT.csproj @@ -0,0 +1,25 @@ + + + + net6.0 + enable + enable + + false + + + + + + + + + + + + + + + + + diff --git a/Sources/Api_UT/UnitTest1.cs b/Sources/Api_UT/UnitTest1.cs new file mode 100644 index 0000000..13a5577 --- /dev/null +++ b/Sources/Api_UT/UnitTest1.cs @@ -0,0 +1,33 @@ +using API_LoL.Controllers; +using DTO; +using Microsoft.AspNetCore.Mvc; +using Model; +using StubLib; + +namespace Api_UT +{ + [TestClass] + public class UnitTest1 + { + [TestMethod] + public async Task TestGet() + { + List list = new List {new ChampionDTO("Akali","",""), new ChampionDTO("Aatrox", "", ""), new ChampionDTO("Ahri", "", ""), new ChampionDTO("Akshan", "", ""), new ChampionDTO("Bard", "", ""), new ChampionDTO("Alistar", "", "") }; + ChampionsController api = new ChampionsController(new StubData()); + IActionResult a = await api.Get(); + Assert.IsNotNull(a); + Assert.AreEqual(list,((OkObjectResult)a).Value); + } + + [TestMethod] + public async Task TestPostValid() + { + ChampionsController api = new ChampionsController(new StubData()); + IActionResult a = await api.Post(new ChampionDTO("nom","bio","icon")); + Assert.IsNotNull(a); + ChampionDTO champ = new ChampionDTO("nom", "bio", "icon"); + Assert.AreEqual(champ,((CreatedAtActionResult)a).Value); + } + + } +} \ No newline at end of file diff --git a/Sources/Api_UT/Usings.cs b/Sources/Api_UT/Usings.cs new file mode 100644 index 0000000..ab67c7e --- /dev/null +++ b/Sources/Api_UT/Usings.cs @@ -0,0 +1 @@ +global using Microsoft.VisualStudio.TestTools.UnitTesting; \ No newline at end of file diff --git a/Sources/DTO/ChampionDTO.cs b/Sources/DTO/ChampionDTO.cs index 35c9515..c6bb27c 100644 --- a/Sources/DTO/ChampionDTO.cs +++ b/Sources/DTO/ChampionDTO.cs @@ -2,10 +2,19 @@ { public class ChampionDTO { + public ChampionDTO(string name, string bio, string icon) + { + Name = name; + Bio = bio; + Icon = icon; + } + public string Name { get; set; } public string Bio { get; set; } - + public string Icon { get; set; } + + } } \ No newline at end of file diff --git a/Sources/EntityFramework/ChampionEntity.cs b/Sources/EntityFramework/ChampionEntity.cs new file mode 100644 index 0000000..0520665 --- /dev/null +++ b/Sources/EntityFramework/ChampionEntity.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EntityFramework +{ + [Table("Champion")] + class ChampionEntity + { + [Key] + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public int Id { get; set; } + public string name { get; set; } + + + public ChampionEntity(string name) { + this.name = name; + } + } +} diff --git a/Sources/EntityFramework/EntityFramework.csproj b/Sources/EntityFramework/EntityFramework.csproj new file mode 100644 index 0000000..66899d0 --- /dev/null +++ b/Sources/EntityFramework/EntityFramework.csproj @@ -0,0 +1,20 @@ + + + + Exe + net6.0 + enable + enable + $(MSBuildProjectDirectory) + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/Sources/EntityFramework/LoLDbContext.cs b/Sources/EntityFramework/LoLDbContext.cs new file mode 100644 index 0000000..bc537f9 --- /dev/null +++ b/Sources/EntityFramework/LoLDbContext.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace EntityFramework +{ + class LoLDbContext : DbContext + { + public DbSet Champions { get; set; } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + base.OnConfiguring(optionsBuilder); + optionsBuilder.UseSqlite("Data Source=champion.db"); + } + } +} diff --git a/Sources/EntityFramework/Program.cs b/Sources/EntityFramework/Program.cs new file mode 100644 index 0000000..29ee9d8 --- /dev/null +++ b/Sources/EntityFramework/Program.cs @@ -0,0 +1,10 @@ +// See https://aka.ms/new-console-template for more information +using EntityFramework; + +Console.WriteLine("Hello, World!"); + +using( var context = new LoLDbContext()) +{ + context.Add(new ChampionEntity("test") ); + context.SaveChanges(); +} \ No newline at end of file diff --git a/Sources/EntityFramework/champion.db b/Sources/EntityFramework/champion.db new file mode 100644 index 0000000000000000000000000000000000000000..df42e8e6ad2fa6f0d00384f0f839b96c2de7b076 GIT binary patch literal 20480 zcmeI)!H&{E7zgl~4lFK)trye8gk(}Zu-TQgWwXSaNUD;h1=+4y4yLJ4U6KV{S`s}j zkKl9Y+4u0^Mc-l%dUT3q*^&}3>S6s)C^MaW{h04ZFC7vNpEU=e&tHuvLoehNa*I$( z9&%0y$;OB1=1mgsnRpy^^vZbMFiXnror3&>WC~x&)@S+S)_pNRg8&2|009U<00Izz z00bZafj=qG&TJ?{LAGxq*K+^+&zc(`>zC)*O>NwMR{puf&!{-q7C_ zS8kuHETS9{ur9z&IrZ)O2Z`h9c#O!dp<#79`+2r+> zedu&*VxFt*19y2LX^Z<)5U8>frbrE!bX+wEYrDQPb`)}iK$(x4b`MI0=Ij){M&BTp%Bf8lN;$ZTO$Y10S zqCJ)ffOMBxo6Ah;n*h=8|5W}?#0w1q5P$##AOHafKmY;|fB*y_0D + Exe @@ -8,6 +8,7 @@ + diff --git a/Sources/Tests/ConsoleTests/Properties/launchSettings.json b/Sources/Tests/ConsoleTests/Properties/launchSettings.json new file mode 100644 index 0000000..c8ab57c --- /dev/null +++ b/Sources/Tests/ConsoleTests/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "profiles": { + "ConsoleTests": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:49970;http://localhost:49971" + } + } +} \ No newline at end of file diff --git a/Sources/Tests/Tests.csproj b/Sources/Tests/Tests.csproj new file mode 100644 index 0000000..01bb70b --- /dev/null +++ b/Sources/Tests/Tests.csproj @@ -0,0 +1,18 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + diff --git a/Sources/Tests/UnitTest1.cs b/Sources/Tests/UnitTest1.cs new file mode 100644 index 0000000..872f935 --- /dev/null +++ b/Sources/Tests/UnitTest1.cs @@ -0,0 +1,10 @@ +namespace Tests; + +[TestClass] +public class UnitTest1 +{ + [TestMethod] + public void TestMethod1() + { + } +} \ No newline at end of file diff --git a/Sources/Tests/Usings.cs b/Sources/Tests/Usings.cs new file mode 100644 index 0000000..ab67c7e --- /dev/null +++ b/Sources/Tests/Usings.cs @@ -0,0 +1 @@ +global using Microsoft.VisualStudio.TestTools.UnitTesting; \ No newline at end of file