From 2a1f585932eeace4b91953c4a40e102480f89edf Mon Sep 17 00:00:00 2001 From: Jolys Enzo Date: Tue, 7 Feb 2023 10:23:53 +0100 Subject: [PATCH] avancement --- Sources/Api-lol/Controllers/Champions.cs | 9 +- Sources/Api-lol/Controllers/Skins.cs | 61 +++++++++++ Sources/Api-lol/Factories/FactoSkins.cs | 13 +++ Sources/Client/Program.cs | 4 +- Sources/DTO/DtoSkins.cs | 18 ++++ Sources/EntityFramwork/BDD-APILOL.db | Bin 32768 -> 28672 bytes Sources/EntityFramwork/BDD-APILOL.db-shm | Bin 32768 -> 0 bytes Sources/EntityFramwork/BDD-APILOL.db-wal | Bin 16512 -> 0 bytes Sources/EntityFramwork/BDDContext.cs | 11 +- Sources/EntityFramwork/EntityLargeImage.cs | 15 +++ Sources/EntityFramwork/Factories/Factories.cs | 6 +- .../Migrations/20230204085418_TEST.cs | 22 ---- ...s => 20230206102420_testMigra.Designer.cs} | 10 +- .../Migrations/20230206102420_testMigra.cs | 66 ++++++++++++ .../Migrations/BDDContextModelSnapshot.cs | 6 +- Sources/LeagueOfLegends.sln | 9 +- Sources/Model/Champion.cs | 4 +- Sources/TestUnitaireBDD/TestChampions.cs | 100 ++++++++++++++++++ .../TestUnitaireBDD/TestUnitaireBDD.csproj | 30 ++++++ Sources/TestUnitaireBDD/Usings.cs | 1 + 20 files changed, 340 insertions(+), 45 deletions(-) create mode 100644 Sources/Api-lol/Controllers/Skins.cs create mode 100644 Sources/Api-lol/Factories/FactoSkins.cs create mode 100644 Sources/DTO/DtoSkins.cs delete mode 100644 Sources/EntityFramwork/BDD-APILOL.db-shm delete mode 100644 Sources/EntityFramwork/BDD-APILOL.db-wal create mode 100644 Sources/EntityFramwork/EntityLargeImage.cs delete mode 100644 Sources/EntityFramwork/Migrations/20230204085418_TEST.cs rename Sources/EntityFramwork/Migrations/{20230204085418_TEST.Designer.cs => 20230206102420_testMigra.Designer.cs} (91%) create mode 100644 Sources/EntityFramwork/Migrations/20230206102420_testMigra.cs create mode 100644 Sources/TestUnitaireBDD/TestChampions.cs create mode 100644 Sources/TestUnitaireBDD/TestUnitaireBDD.csproj create mode 100644 Sources/TestUnitaireBDD/Usings.cs diff --git a/Sources/Api-lol/Controllers/Champions.cs b/Sources/Api-lol/Controllers/Champions.cs index 9a2b8a0..850cc8d 100644 --- a/Sources/Api-lol/Controllers/Champions.cs +++ b/Sources/Api-lol/Controllers/Champions.cs @@ -33,11 +33,14 @@ namespace Api_lol.Controllers [HttpPost] - public IActionResult Post(DtoChampions champDTO) + public async Task Post(DtoChampions champDTO) { Champion tmp = champDTO.DtoToModel(); - data.ChampionsMgr.AddItem(tmp); - return Ok(); + var champion = await data.ChampionsMgr.AddItem(tmp); + //Convertir le resultat en dto + //Nom de la méthode, l'id du champion, et le champion + Console.WriteLine(champion.Name); + return CreatedAtAction(nameof(GetChampion),champion.Name,champion); } diff --git a/Sources/Api-lol/Controllers/Skins.cs b/Sources/Api-lol/Controllers/Skins.cs new file mode 100644 index 0000000..650364f --- /dev/null +++ b/Sources/Api-lol/Controllers/Skins.cs @@ -0,0 +1,61 @@ +using Api_lol.Factories; +using DTO; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Model; + +namespace Api_lol.Controllers +{ + [ApiController] + [Route("/Skins")] + public class Skins : Controller + { + private readonly IDataManager data; + + private readonly ILogger logger; + + public Skins(ILogger logger, IDataManager manager) + { + this.logger = logger; + data = manager; + } + + [HttpGet] + public async Task Get() + { + var champs = (await data.SkinsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).Select(Model => Model.ModelToDto()); + + return Ok(champs); + } + + [HttpGet] + [Route("{name}")] + public async Task GetSkinsByName(string name) + { + Skin skin = (await data.SkinsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).First(i => i.Name == name); + if (skin == null) + { + return BadRequest(); + } + DtoSkins result = skin.ModelToDto(); + return Ok(result); + } + + [HttpGet] + [Route("GetSkinsByChampionName/{name}")] + public async Task GetSkinsByChampionName(string name) + { + Champion champ = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).First(i => i.Name == name); + List skinsModele = new List(champ.Skins); + List skinsD = new + + + List skins = new List((List)champ.Skins.Select(Model => Model.ModelToDto)); + if (skins == null) + { + return BadRequest(); + } + return Ok(skins); + } + } +} diff --git a/Sources/Api-lol/Factories/FactoSkins.cs b/Sources/Api-lol/Factories/FactoSkins.cs new file mode 100644 index 0000000..d872813 --- /dev/null +++ b/Sources/Api-lol/Factories/FactoSkins.cs @@ -0,0 +1,13 @@ +using DTO; +using Model; + +namespace Api_lol.Factories +{ + public static class FactoSkins + { + public static DtoSkins ModelToDto(this Skin skin) + { + return new DtoSkins(skin.Name); + } + } +} diff --git a/Sources/Client/Program.cs b/Sources/Client/Program.cs index 5c8e5dc..eed9cc0 100644 --- a/Sources/Client/Program.cs +++ b/Sources/Client/Program.cs @@ -13,13 +13,11 @@ StubData tmp = new StubData(); var tmpListe = await tmp.ChampionsMgr.GetItemsByName("Akali", 0, 6); Champion champ = tmpListe.First(); Factories facto = new Factories(); -EntityChampions entity = facto.ChampionModelToEntity(champ); -List skins = new List(entity.Skins); using ( BDDContext db = new BDDContext()) { - db.Add(entity); + db.SaveChanges(); } diff --git a/Sources/DTO/DtoSkins.cs b/Sources/DTO/DtoSkins.cs new file mode 100644 index 0000000..d1a002e --- /dev/null +++ b/Sources/DTO/DtoSkins.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DTO +{ + public class DtoSkins + { + public string name { get; set; } + + public DtoSkins(string name) + { + this.name = name; + } + } +} \ No newline at end of file diff --git a/Sources/EntityFramwork/BDD-APILOL.db b/Sources/EntityFramwork/BDD-APILOL.db index 3121d98a05c775b949469c47f75f9aa6958ec602..4d1ae4c017172db3ae19a2fa3755eefd33596f2c 100644 GIT binary patch delta 383 zcmZo@U}|{4I6+#FiGhKE9f)CoWulI;Bol*Pyc9404+b{w6AXNp_}B7&;a$X|&3$6C zqCfz*MF44a zp`e6C+2nP6;&PrTN}5e2tnA|A;*72CC5cHnsbKA3f^BjscjjbYt_V&gnCX)Zcts}9 zYE*FZ4|4T%_fzn8ja1NpEAdR3{EN$U@^#)IuEuC)c5!)m#%9*Z-+AUtcHrS) zcFN43Y{(-%IiE|IiI;2hMLuIjHb(w?4E*;t3mRPE*HK_(wiP!rFfukUGB7hVFfuVR zh%ZSkF7eGwFG@7mGte{Q0tOZ%{}TrOCqNyy_+_P;Z8>pi;N8p^z$LJlJ3wGFx4~C_ LMy}0_2LJg15(8?5 delta 1002 zcmcJO&rj1}7{~kWWnI@_?;42>46|CJW3ntSEpuKt;OJms{0Njp!l4;jq0zQv+XbUx zlYm!94_-W|cT99AFa8gB(1RX4cs1OJCu3K@6+*n3^lke*&!~ z34)+b7Vw|Fds#I}5;>!v<1+-wEED`Ew9o$JzA($Y9^Zb~V&X+U1J}fK8tib5>dGV2 zd|)(8d)-)gXgT#WL995hjp>@CkKL(gk{qgeLp=6&*uhK1wW_^txlX<0)=X>FnKCy# zf$Q`3*(L8Lm(GRqvQ(O#*UA$!(v-F&4aygNYIFG#p8+35Q3N}SfxJLgaHERze(g<1 z`u5Xf>02ls>#!$rmyLNEYyAgxbZZsH#8e7wUGaW%FCj-~y;pp1RvOnzV~Z7CN~g1u zT)yWzvZQNwb!leSUyGGW2=HEW3pDyg40ZZEOZt8K1Tx;)N`iPo$3#s8t&DfTToyvp z3#^geA(LW4SueqgY$%r72fPaSD4HbDXY>|5M<$wV3yE83GZ}@ds9;6K3Le3?F;+$l zU0cu#Ih<3Z9yr=97V-sLFkGkBaP6wI5fIlQoH!+hhgF;($s3I|t9F0dTCG(Z{x1+< zHzKgy#gu$ORgJpok7Mc_I0)eMG3Yn*FFdRmw(HEe_PBYrCGC$TidG2p4Shf_&||dX z8=6C#B2>Wzexd_#w3|O?rROS~I4xf60?M%5B|-+e`XkribX-YwM)?oz?{QCp0_cc9 nN9adez+X}S02MkyPMwNCkqY*eZUamw0O@0gXm9uL+g$VycM%y< diff --git a/Sources/EntityFramwork/BDD-APILOL.db-shm b/Sources/EntityFramwork/BDD-APILOL.db-shm deleted file mode 100644 index 2e0d47f4781296cc9a7b2b2ff7de105a9572ed0b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 32768 zcmeI)F$w}P5CFjBJVZOo`-G3M($fBF;cRXFfW7zyD_eX0iVsnfLk~N#lVcW^4M|wo zdB9e;X~j3KSvlIuZm35FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly IK;Rz*o_Bd8Hvj+t diff --git a/Sources/EntityFramwork/BDD-APILOL.db-wal b/Sources/EntityFramwork/BDD-APILOL.db-wal deleted file mode 100644 index cf1dcaee1d772f7ad0644ee037343a9ac9abe1c5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16512 zcmeI&JxIeq6bJA$*QQpG6I2xJ;0KBb$|aW&7blfkp@P`0NT?dLNE57G)IoI7%|XP? z$x&Ry!A-%*Sp>nUqnqF?UL$Q93GUkeqaE(OM@WC&-aT_qIgrLGJ(AQeRpUN&65Ul2 zpD#OePsbZ0A0j0;=JzZ2kGak+xlJS$Ix5j;dPNVZO=rac0Rad=00Izz00bZa0SG_< z0uX3gfuur=ZjEW2X-sDm2G_WiomtFsbDWK9y)nWYs*Gv6SM`d0Q8MWlO~YiS option) : base(option) { } @@ -15,29 +16,31 @@ namespace EntityFramwork //création de la table Champion modelBuilder.Entity().HasKey(a => a.Id); modelBuilder.Entity().Property(a => a.Id) - .ValueGeneratedOnAdd(); + .ValueGeneratedOnAdd(); //création de la table Skins modelBuilder.Entity().HasKey(m => m.Id); modelBuilder.Entity().Property(m => m.Id) - .ValueGeneratedOnAdd(); + .ValueGeneratedOnAdd(); //modelBuilder.Entity().Property("ChampionForeignKey"); // Use the shadow property as a foreign key + /* modelBuilder.Entity() .HasOne(m => m.Champion) .WithMany(a => a.Skins) - .HasForeignKey("ChampionsForeignKey"); + .HasForeignKey("ChampionsForeignKey");*/ } public DbSet Champions { get; set; } public DbSet Skins { get; set; } + public DbSet Images { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { - optionsBuilder.UseSqlite($"Data Source=BDD-APILOL.db"); ; + optionsBuilder.UseSqlite($"Data Source=C:\\Users\\Jolys Enzo\\home\\BUT\\Projet\\LOL-API\\Sources\\EntityFramwork\\BDD-APILOL.db"); } } } diff --git a/Sources/EntityFramwork/EntityLargeImage.cs b/Sources/EntityFramwork/EntityLargeImage.cs new file mode 100644 index 0000000..a5f291c --- /dev/null +++ b/Sources/EntityFramwork/EntityLargeImage.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EntityFramwork +{ + public class EntityLargeImage + { + public long Id { get; set; } + + public string Base64 { get; set; } + } +} diff --git a/Sources/EntityFramwork/Factories/Factories.cs b/Sources/EntityFramwork/Factories/Factories.cs index dc491c6..6c9f980 100644 --- a/Sources/EntityFramwork/Factories/Factories.cs +++ b/Sources/EntityFramwork/Factories/Factories.cs @@ -14,7 +14,7 @@ namespace EntityFramwork.Factories entity.Icon = champ.Icon; if ( sansSkin == 0) { - entity.Skins = champ.Skins.Select(Model => this.SkinsModelToEntity(Model)).ToList(); + //entity.Skins = champ.Skins.Select(Model => this.SkinsModelToEntity(Model)).ToList(); } return entity; @@ -27,9 +27,11 @@ namespace EntityFramwork.Factories entity.Price = skin.Price; entity.Icon = skin.Icon; entity.Description= skin.Description; - entity.Champion = ChampionModelToEntity(skin.Champion,1); + //entity.Champion = ChampionModelToEntity(skin.Champion,1); return entity; } + + } } diff --git a/Sources/EntityFramwork/Migrations/20230204085418_TEST.cs b/Sources/EntityFramwork/Migrations/20230204085418_TEST.cs deleted file mode 100644 index 148e9bf..0000000 --- a/Sources/EntityFramwork/Migrations/20230204085418_TEST.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace EntityFramwork.Migrations -{ - /// - public partial class TEST : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - - } - } -} diff --git a/Sources/EntityFramwork/Migrations/20230204085418_TEST.Designer.cs b/Sources/EntityFramwork/Migrations/20230206102420_testMigra.Designer.cs similarity index 91% rename from Sources/EntityFramwork/Migrations/20230204085418_TEST.Designer.cs rename to Sources/EntityFramwork/Migrations/20230206102420_testMigra.Designer.cs index ec97c73..4831a4b 100644 --- a/Sources/EntityFramwork/Migrations/20230204085418_TEST.Designer.cs +++ b/Sources/EntityFramwork/Migrations/20230206102420_testMigra.Designer.cs @@ -10,8 +10,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace EntityFramwork.Migrations { [DbContext(typeof(BDDContext))] - [Migration("20230204085418_TEST")] - partial class TEST + [Migration("20230206102420_testMigra")] + partial class testMigra { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -48,7 +48,7 @@ namespace EntityFramwork.Migrations .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); - b.Property("ChampionsForeignKey") + b.Property("ChampionId") .HasColumnType("INTEGER"); b.Property("Description") @@ -64,7 +64,7 @@ namespace EntityFramwork.Migrations b.HasKey("Id"); - b.HasIndex("ChampionsForeignKey"); + b.HasIndex("ChampionId"); b.ToTable("Skins"); }); @@ -73,7 +73,7 @@ namespace EntityFramwork.Migrations { b.HasOne("DTO.EntityChampions", "Champion") .WithMany("Skins") - .HasForeignKey("ChampionsForeignKey") + .HasForeignKey("ChampionId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); diff --git a/Sources/EntityFramwork/Migrations/20230206102420_testMigra.cs b/Sources/EntityFramwork/Migrations/20230206102420_testMigra.cs new file mode 100644 index 0000000..f21f03f --- /dev/null +++ b/Sources/EntityFramwork/Migrations/20230206102420_testMigra.cs @@ -0,0 +1,66 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace EntityFramwork.Migrations +{ + /// + public partial class testMigra : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Champions", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(type: "TEXT", nullable: false), + Bio = table.Column(type: "TEXT", nullable: false), + Icon = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Champions", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Skins", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Description = table.Column(type: "TEXT", nullable: false), + Icon = table.Column(type: "TEXT", nullable: false), + Price = table.Column(type: "REAL", nullable: false), + ChampionId = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Skins", x => x.Id); + table.ForeignKey( + name: "FK_Skins_Champions_ChampionId", + column: x => x.ChampionId, + principalTable: "Champions", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Skins_ChampionId", + table: "Skins", + column: "ChampionId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Skins"); + + migrationBuilder.DropTable( + name: "Champions"); + } + } +} diff --git a/Sources/EntityFramwork/Migrations/BDDContextModelSnapshot.cs b/Sources/EntityFramwork/Migrations/BDDContextModelSnapshot.cs index dc6ff1d..85c3e43 100644 --- a/Sources/EntityFramwork/Migrations/BDDContextModelSnapshot.cs +++ b/Sources/EntityFramwork/Migrations/BDDContextModelSnapshot.cs @@ -45,7 +45,7 @@ namespace EntityFramwork.Migrations .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); - b.Property("ChampionsForeignKey") + b.Property("ChampionId") .HasColumnType("INTEGER"); b.Property("Description") @@ -61,7 +61,7 @@ namespace EntityFramwork.Migrations b.HasKey("Id"); - b.HasIndex("ChampionsForeignKey"); + b.HasIndex("ChampionId"); b.ToTable("Skins"); }); @@ -70,7 +70,7 @@ namespace EntityFramwork.Migrations { b.HasOne("DTO.EntityChampions", "Champion") .WithMany("Skins") - .HasForeignKey("ChampionsForeignKey") + .HasForeignKey("ChampionId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); diff --git a/Sources/LeagueOfLegends.sln b/Sources/LeagueOfLegends.sln index 4ee80bf..7f45f82 100644 --- a/Sources/LeagueOfLegends.sln +++ b/Sources/LeagueOfLegends.sln @@ -23,7 +23,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFramwork", "EntityFra EndProject 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}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUnitaireApi", "TestUnitaireApi\TestUnitaireApi.csproj", "{1FB0F7BE-AEEE-42FD-BCE9-C11AE6923E6C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUnitaireBDD", "TestUnitaireBDD\TestUnitaireBDD.csproj", "{138B858C-11AA-4915-B3D0-DAF11268A434}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -67,6 +69,10 @@ Global {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 + {138B858C-11AA-4915-B3D0-DAF11268A434}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {138B858C-11AA-4915-B3D0-DAF11268A434}.Debug|Any CPU.Build.0 = Debug|Any CPU + {138B858C-11AA-4915-B3D0-DAF11268A434}.Release|Any CPU.ActiveCfg = Release|Any CPU + {138B858C-11AA-4915-B3D0-DAF11268A434}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -75,6 +81,7 @@ Global {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} + {138B858C-11AA-4915-B3D0-DAF11268A434} = {C76D0C23-1FFA-4963-93CD-E12BD643F030} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {92F3083D-793F-4552-8A9A-0AD6534159C9} diff --git a/Sources/Model/Champion.cs b/Sources/Model/Champion.cs index 3a50658..9ec1933 100644 --- a/Sources/Model/Champion.cs +++ b/Sources/Model/Champion.cs @@ -28,7 +28,7 @@ public class Champion : IEquatable { if(value == null) { - bio = ""; + bio = "Bio inconnu"; return; } bio = value; @@ -42,7 +42,7 @@ public class Champion : IEquatable public LargeImage Image { get; set; } - public Champion(string name, ChampionClass champClass = ChampionClass.Unknown, string icon = "", string image = "", string bio = "") + public Champion(string name, ChampionClass champClass = ChampionClass.Unknown, string icon = "Icon inconnu !", string image = "", string bio = "Bio inconnu !") { Name = name; Class = champClass; diff --git a/Sources/TestUnitaireBDD/TestChampions.cs b/Sources/TestUnitaireBDD/TestChampions.cs new file mode 100644 index 0000000..ab851ea --- /dev/null +++ b/Sources/TestUnitaireBDD/TestChampions.cs @@ -0,0 +1,100 @@ +using DTO; +using EntityFramwork; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; + +namespace TestUnitaireBDD +{ + public class TestChampions + { + [Fact] + public void Add_Test() + { + //connection must be opened to use In-memory database + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + //prepares the database with one instance of the context + using (var context = new BDDContext(options)) + { + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + + EntityChampions chewie = new EntityChampions { Name = "Chewbacca",Bio ="Inconnu",Icon="Inconnu" }; + EntityChampions yoda = new EntityChampions { Name = "Yoda",Bio = "Inconnu", Icon = "Inconnu" }; + EntityChampions ewok = new EntityChampions { Name = "Ewok",Bio = "Inconnu", Icon = "Inconnu" }; + + context.Add(chewie); + context.Add(yoda); + context.Add(ewok); + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new BDDContext(options)) + { + context.Database.EnsureCreated(); + + Assert.Equal(3, context.Champions.Count()); + Assert.Equal("Chewbacca", context.Champions.First().Name); + } + } + + [Fact] + public void Modify_Test() + { + //connection must be opened to use In-memory database + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + //prepares the database with one instance of the context + using (var context = new BDDContext(options)) + { + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + + EntityChampions chewie = new EntityChampions { Name = "Chewbacca", Bio = "Inconnu", Icon = "Inconnu" }; + EntityChampions yoda = new EntityChampions { Name = "Yoda", Bio = "Inconnu", Icon = "Inconnu" }; + EntityChampions ewok = new EntityChampions { Name = "Ewok", Bio = "Inconnu", Icon = "Inconnu" }; + + context.Add(chewie); + context.Add(yoda); + context.Add(ewok); + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new BDDContext(options)) + { + context.Database.EnsureCreated(); + + string nameToFind = "ew"; + Assert.Equal(2, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); + nameToFind = "wo"; + Assert.Equal(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(); + } + + //uses another instance of the context to do the tests + using (var context = new BDDContext(options)) + { + context.Database.EnsureCreated(); + + string nameToFind = "ew"; + Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); + nameToFind = "wick"; + Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); + } + } + } +} \ No newline at end of file diff --git a/Sources/TestUnitaireBDD/TestUnitaireBDD.csproj b/Sources/TestUnitaireBDD/TestUnitaireBDD.csproj new file mode 100644 index 0000000..4047a8d --- /dev/null +++ b/Sources/TestUnitaireBDD/TestUnitaireBDD.csproj @@ -0,0 +1,30 @@ + + + + net6.0 + enable + enable + + false + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/Sources/TestUnitaireBDD/Usings.cs b/Sources/TestUnitaireBDD/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/Sources/TestUnitaireBDD/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file