From 792f3140a65c19217ce29bd6ebac5c6c0cde9a45 Mon Sep 17 00:00:00 2001 From: Pierre Ferreira Date: Wed, 22 Feb 2023 17:12:02 +0100 Subject: [PATCH] :test_tube: ajout des tests d'ajout et de modification pour l'EF et ajout de l'enum de Classes de champion. :bento: --- Sources/Api_UT/EntityTest.cs | 46 ---------- Sources/DTO/DTO.csproj | 5 ++ Sources/DTO/Program.cs | 30 +++++++ Sources/EF_UT/EF_UT.csproj | 27 ++++++ Sources/EF_UT/EntityTest.cs | 87 +++++++++++++++++++ Sources/EF_UT/Usings.cs | 1 + Sources/EntityFramework/ChampionEntity.cs | 1 + Sources/EntityFramework/EnumChampionClass.cs | 19 ++++ Sources/EntityFramework/champion.db | Bin 20480 -> 20480 bytes Sources/LeagueOfLegends.sln | 7 ++ 10 files changed, 177 insertions(+), 46 deletions(-) delete mode 100644 Sources/Api_UT/EntityTest.cs create mode 100644 Sources/DTO/Program.cs create mode 100644 Sources/EF_UT/EF_UT.csproj create mode 100644 Sources/EF_UT/EntityTest.cs create mode 100644 Sources/EF_UT/Usings.cs create mode 100644 Sources/EntityFramework/EnumChampionClass.cs diff --git a/Sources/Api_UT/EntityTest.cs b/Sources/Api_UT/EntityTest.cs deleted file mode 100644 index 6de3a08..0000000 --- a/Sources/Api_UT/EntityTest.cs +++ /dev/null @@ -1,46 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using EntityFramework; -using System.Threading.Tasks; - - -namespace Api_UT -{ - [TestClass] - public class EntityTest - { - [TestMethod] - public void TestAdd() - { - var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(databaseName: "Add_Test_database").Options; - - - //prepares the database with one instance of the context - using (var context = new LoLDbContext(options)) - { - - ChampionEntity chewie = new ChampionEntity("Chewbacca"); - ChampionEntity yoda = new ChampionEntity("Yoda"); - ChampionEntity ewok = new ChampionEntity("Ewok"); - - - Console.WriteLine("Creates and inserts new Champion for tests"); - context.Add(chewie); - context.Add(yoda); - context.Add(ewok); - context.SaveChanges(); - } - - //prepares the database with one instance of the context - using (var context = new LoLDbContext(options)) - { - Assert.AreEqual(3, context.Champions.Count()); - Assert.AreEqual("Chewbacca", context.Champions.First().name); - } - } - } -} diff --git a/Sources/DTO/DTO.csproj b/Sources/DTO/DTO.csproj index 132c02c..dd77eab 100644 --- a/Sources/DTO/DTO.csproj +++ b/Sources/DTO/DTO.csproj @@ -6,4 +6,9 @@ enable + + + + + diff --git a/Sources/DTO/Program.cs b/Sources/DTO/Program.cs new file mode 100644 index 0000000..8133fe8 --- /dev/null +++ b/Sources/DTO/Program.cs @@ -0,0 +1,30 @@ +//using Model; +//using StubLib; + +//var builder = WebApplication.CreateBuilder(args); + +//// Add services to the container. + +//builder.Services.AddControllers(); +//// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +//builder.Services.AddEndpointsApiExplorer(); +//builder.Services.AddSwaggerGen(); + +//builder.Services.AddScoped(); + +//var app = builder.Build(); + +//// Configure the HTTP request pipeline. +//if (app.Environment.IsDevelopment()) +//{ +// app.UseSwagger(); +// app.UseSwaggerUI(); +//} + +//app.UseHttpsRedirection(); + +//app.UseAuthorization(); + +//app.MapControllers(); + +//app.Run(); diff --git a/Sources/EF_UT/EF_UT.csproj b/Sources/EF_UT/EF_UT.csproj new file mode 100644 index 0000000..eadf5a3 --- /dev/null +++ b/Sources/EF_UT/EF_UT.csproj @@ -0,0 +1,27 @@ + + + + net6.0 + enable + enable + + false + + + + + + + + + + + + + + + + + + + diff --git a/Sources/EF_UT/EntityTest.cs b/Sources/EF_UT/EntityTest.cs new file mode 100644 index 0000000..45716d8 --- /dev/null +++ b/Sources/EF_UT/EntityTest.cs @@ -0,0 +1,87 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using EntityFramework; +using System.Threading.Tasks; + + +namespace EF_UT +{ + [TestClass] + public class EntityTest + { + [TestMethod] + public void TestAdd() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "Add_Test_database").Options; + + + //prepares the database with one instance of the context + using (var context = new LoLDbContext(options)) + { + + ChampionEntity chewie = new ChampionEntity("Chewbacca"); + ChampionEntity yoda = new ChampionEntity("Yoda"); + ChampionEntity ewok = new ChampionEntity("Ewok"); + + + Console.WriteLine("Creates and inserts new Champion for tests"); + context.Add(chewie); + context.Add(yoda); + context.Add(ewok); + context.SaveChanges(); + } + + //prepares the database with one instance of the context + using (var context = new LoLDbContext(options)) + { + Assert.AreEqual(3, context.Champions.Count()); + Assert.AreEqual("Chewbacca", context.Champions.First().name); + } + } + [TestMethod] + public void TestUpdate() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "Modify_Test_database") + .Options; + + //prepares the database with one instance of the context + using (var context = new LoLDbContext(options)) + { + ChampionEntity chewie = new ChampionEntity ("Chewbacca"); + ChampionEntity yoda = new ChampionEntity ("Yoda"); + ChampionEntity ewok = new ChampionEntity("Ewok"); + + context.Add(chewie); + context.Add(yoda); + context.Add(ewok); + context.SaveChanges(); + } + + //prepares the database with one instance of the context + using (var context = new LoLDbContext(options)) + { + string nameToFind = "ew"; + Assert.AreEqual(2, context.Champions.Where(n => n.name.ToLower().Contains(nameToFind)).Count()); + nameToFind = "ewo"; + Assert.AreEqual(1, context.Champions.Where(n => n.name.ToLower().Contains(nameToFind)).Count()); + var ewok = context.Champions.Where(n => n.name.ToLower().Contains(nameToFind)).First(); + ewok.name = "Wicket"; + context.SaveChanges(); + } + + //prepares the database with one instance of the context + using (var context = new LoLDbContext(options)) + { + string nameToFind = "ew"; + Assert.AreEqual(1, context.Champions.Where(n => n.name.ToLower().Contains(nameToFind)).Count()); + nameToFind = "wick"; + Assert.AreEqual(1, context.Champions.Where(n => n.name.ToLower().Contains(nameToFind)).Count()); + } + } + } +} diff --git a/Sources/EF_UT/Usings.cs b/Sources/EF_UT/Usings.cs new file mode 100644 index 0000000..ab67c7e --- /dev/null +++ b/Sources/EF_UT/Usings.cs @@ -0,0 +1 @@ +global using Microsoft.VisualStudio.TestTools.UnitTesting; \ No newline at end of file diff --git a/Sources/EntityFramework/ChampionEntity.cs b/Sources/EntityFramework/ChampionEntity.cs index c4ce92c..36d33f6 100644 --- a/Sources/EntityFramework/ChampionEntity.cs +++ b/Sources/EntityFramework/ChampionEntity.cs @@ -15,6 +15,7 @@ namespace EntityFramework [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string name { get; set; } + public ChampionClass Class { get; set; } public ChampionEntity(string name) { diff --git a/Sources/EntityFramework/EnumChampionClass.cs b/Sources/EntityFramework/EnumChampionClass.cs new file mode 100644 index 0000000..b8a039f --- /dev/null +++ b/Sources/EntityFramework/EnumChampionClass.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EntityFramework +{ + public enum ChampionClass + { + Unknown, + Assassin, + Fighter, + Mage, + Marksman, + Support, + Tank + } +} diff --git a/Sources/EntityFramework/champion.db b/Sources/EntityFramework/champion.db index df42e8e6ad2fa6f0d00384f0f839b96c2de7b076..f3674046057692db08f3aecf495e04c250fe5ce4 100644 GIT binary patch delta 95 zcmZozz}T>Wae_1>`$QRMM)r*fOZ2&z`JXcIf9HS4|8%pU!F7IBc4lS<(UR2S5_TpS ngAvN$Wnf@nWae_1>>qHr6M%Il9OZ3?o`M)#pf8Q+V@Q{CE0FN*`BQt|&NosKkF9QPu bBmZXx{?AZ34qk3XW?4?>jKti6%=|n6I;|BI diff --git a/Sources/LeagueOfLegends.sln b/Sources/LeagueOfLegends.sln index 4deaa66..6415543 100644 --- a/Sources/LeagueOfLegends.sln +++ b/Sources/LeagueOfLegends.sln @@ -23,6 +23,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFramework", "EntityFr EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Api_UT", "Api_UT\Api_UT.csproj", "{20A1A7DC-1E93-4506-BD32-8597A5DADD7B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EF_UT", "EF_UT\EF_UT.csproj", "{74F469C3-A94A-4507-9DC7-7DBADCD18173}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -61,6 +63,10 @@ Global {20A1A7DC-1E93-4506-BD32-8597A5DADD7B}.Debug|Any CPU.Build.0 = Debug|Any CPU {20A1A7DC-1E93-4506-BD32-8597A5DADD7B}.Release|Any CPU.ActiveCfg = Release|Any CPU {20A1A7DC-1E93-4506-BD32-8597A5DADD7B}.Release|Any CPU.Build.0 = Release|Any CPU + {74F469C3-A94A-4507-9DC7-7DBADCD18173}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {74F469C3-A94A-4507-9DC7-7DBADCD18173}.Debug|Any CPU.Build.0 = Debug|Any CPU + {74F469C3-A94A-4507-9DC7-7DBADCD18173}.Release|Any CPU.ActiveCfg = Release|Any CPU + {74F469C3-A94A-4507-9DC7-7DBADCD18173}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -69,6 +75,7 @@ Global {1889FA6E-B7C6-416E-8628-9449FB9070B9} = {C76D0C23-1FFA-4963-93CD-E12BD643F030} {B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB} = {2C607793-B163-4731-A4D1-AFE8A7C4C170} {20A1A7DC-1E93-4506-BD32-8597A5DADD7B} = {C76D0C23-1FFA-4963-93CD-E12BD643F030} + {74F469C3-A94A-4507-9DC7-7DBADCD18173} = {C76D0C23-1FFA-4963-93CD-E12BD643F030} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {92F3083D-793F-4552-8A9A-0AD6534159C9}