From 9ba8bb4c00aa9170971b2761a9ed9527a18693b3 Mon Sep 17 00:00:00 2001 From: Maxence Lanone Date: Sun, 26 Mar 2023 23:36:25 +0200 Subject: [PATCH] ajout test unit for api (not op) --- .../20230317075439_MyMigration.Designer.cs | 108 ---------- .../Migrations/20230317075439_MyMigration.cs | 82 ------- .../Migrations/LolContextModelSnapshot.cs | 105 --------- Sources/Tests/TestUnitaireLOL/TestAPI.cs | 40 ++++ Sources/Tests/TestUnitaireLOL/TestEf.cs | 200 ++++++++++++++---- .../TestUnitaireLOL/TestUnitaireLOL.csproj | 6 + .../Tests/TestUnitaireLOL/projet.Champions.db | Bin 0 -> 77824 bytes .../TestUnitaireLOL/projet.Champions.db-shm | Bin 0 -> 32768 bytes .../TestUnitaireLOL/projet.Champions.db-wal | 0 9 files changed, 205 insertions(+), 336 deletions(-) delete mode 100644 Sources/EntityFrameWorkLib/Migrations/20230317075439_MyMigration.Designer.cs delete mode 100644 Sources/EntityFrameWorkLib/Migrations/20230317075439_MyMigration.cs delete mode 100644 Sources/EntityFrameWorkLib/Migrations/LolContextModelSnapshot.cs create mode 100644 Sources/Tests/TestUnitaireLOL/TestAPI.cs create mode 100644 Sources/Tests/TestUnitaireLOL/projet.Champions.db create mode 100644 Sources/Tests/TestUnitaireLOL/projet.Champions.db-shm create mode 100644 Sources/Tests/TestUnitaireLOL/projet.Champions.db-wal diff --git a/Sources/EntityFrameWorkLib/Migrations/20230317075439_MyMigration.Designer.cs b/Sources/EntityFrameWorkLib/Migrations/20230317075439_MyMigration.Designer.cs deleted file mode 100644 index 026aaa2..0000000 --- a/Sources/EntityFrameWorkLib/Migrations/20230317075439_MyMigration.Designer.cs +++ /dev/null @@ -1,108 +0,0 @@ -// -using System; -using EntityFrameWorkLib; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace EntityFrameWorkLib.Migrations -{ - [DbContext(typeof(LolContext))] - [Migration("20230317075439_MyMigration")] - partial class MyMigration - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "7.0.4"); - - modelBuilder.Entity("EntityFrameWorkLib.ChampionEntity", b => - { - b.Property("UniqueId") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("Bio") - .IsRequired() - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("Class") - .HasColumnType("INTEGER"); - - b.Property("Icon") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("Name") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("UniqueId"); - - b.ToTable("Champions"); - }); - - modelBuilder.Entity("EntityFrameWorkLib.LargeImageEntity", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("Base64") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("championId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("championId") - .IsUnique(); - - b.ToTable("LargeImage"); - }); - - modelBuilder.Entity("EntityFrameWorkLib.SkillEntity", b => - { - b.Property("Name") - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("Description") - .IsRequired() - .HasMaxLength(500) - .HasColumnType("TEXT"); - - b.Property("SkillType") - .HasColumnType("INTEGER"); - - b.HasKey("Name"); - - b.ToTable("Skill"); - }); - - modelBuilder.Entity("EntityFrameWorkLib.LargeImageEntity", b => - { - b.HasOne("EntityFrameWorkLib.ChampionEntity", "champion") - .WithOne("LargeImageEntity") - .HasForeignKey("EntityFrameWorkLib.LargeImageEntity", "championId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("champion"); - }); - - modelBuilder.Entity("EntityFrameWorkLib.ChampionEntity", b => - { - b.Navigation("LargeImageEntity"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/Sources/EntityFrameWorkLib/Migrations/20230317075439_MyMigration.cs b/Sources/EntityFrameWorkLib/Migrations/20230317075439_MyMigration.cs deleted file mode 100644 index 0fd9526..0000000 --- a/Sources/EntityFrameWorkLib/Migrations/20230317075439_MyMigration.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace EntityFrameWorkLib.Migrations -{ - /// - public partial class MyMigration : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Champions", - columns: table => new - { - UniqueId = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - Name = table.Column(type: "TEXT", nullable: false), - Bio = table.Column(type: "TEXT", maxLength: 256, nullable: false), - Icon = table.Column(type: "TEXT", nullable: false), - Class = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Champions", x => x.UniqueId); - }); - - migrationBuilder.CreateTable( - name: "Skill", - columns: table => new - { - Name = table.Column(type: "TEXT", maxLength: 256, nullable: false), - Description = table.Column(type: "TEXT", maxLength: 500, nullable: false), - SkillType = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Skill", x => x.Name); - }); - - migrationBuilder.CreateTable( - name: "LargeImage", - columns: table => new - { - Id = table.Column(type: "TEXT", nullable: false), - Base64 = table.Column(type: "TEXT", nullable: false), - championId = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_LargeImage", x => x.Id); - table.ForeignKey( - name: "FK_LargeImage_Champions_championId", - column: x => x.championId, - principalTable: "Champions", - principalColumn: "UniqueId", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateIndex( - name: "IX_LargeImage_championId", - table: "LargeImage", - column: "championId", - unique: true); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "LargeImage"); - - migrationBuilder.DropTable( - name: "Skill"); - - migrationBuilder.DropTable( - name: "Champions"); - } - } -} diff --git a/Sources/EntityFrameWorkLib/Migrations/LolContextModelSnapshot.cs b/Sources/EntityFrameWorkLib/Migrations/LolContextModelSnapshot.cs deleted file mode 100644 index a9a3587..0000000 --- a/Sources/EntityFrameWorkLib/Migrations/LolContextModelSnapshot.cs +++ /dev/null @@ -1,105 +0,0 @@ -// -using System; -using EntityFrameWorkLib; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace EntityFrameWorkLib.Migrations -{ - [DbContext(typeof(LolContext))] - partial class LolContextModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "7.0.4"); - - modelBuilder.Entity("EntityFrameWorkLib.ChampionEntity", b => - { - b.Property("UniqueId") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("Bio") - .IsRequired() - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("Class") - .HasColumnType("INTEGER"); - - b.Property("Icon") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("Name") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("UniqueId"); - - b.ToTable("Champions"); - }); - - modelBuilder.Entity("EntityFrameWorkLib.LargeImageEntity", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("Base64") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("championId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("championId") - .IsUnique(); - - b.ToTable("LargeImage"); - }); - - modelBuilder.Entity("EntityFrameWorkLib.SkillEntity", b => - { - b.Property("Name") - .HasMaxLength(256) - .HasColumnType("TEXT"); - - b.Property("Description") - .IsRequired() - .HasMaxLength(500) - .HasColumnType("TEXT"); - - b.Property("SkillType") - .HasColumnType("INTEGER"); - - b.HasKey("Name"); - - b.ToTable("Skill"); - }); - - modelBuilder.Entity("EntityFrameWorkLib.LargeImageEntity", b => - { - b.HasOne("EntityFrameWorkLib.ChampionEntity", "champion") - .WithOne("LargeImageEntity") - .HasForeignKey("EntityFrameWorkLib.LargeImageEntity", "championId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("champion"); - }); - - modelBuilder.Entity("EntityFrameWorkLib.ChampionEntity", b => - { - b.Navigation("LargeImageEntity"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/Sources/Tests/TestUnitaireLOL/TestAPI.cs b/Sources/Tests/TestUnitaireLOL/TestAPI.cs new file mode 100644 index 0000000..22c7725 --- /dev/null +++ b/Sources/Tests/TestUnitaireLOL/TestAPI.cs @@ -0,0 +1,40 @@ +using System; +using Microsoft.Extensions.Logging.Abstractions; +using StubLib; +using WebApiLol; +using WebApiLol.Controllers; + +namespace TestUnitaireLOL +{ + public class TestAPI + { + [Theory] + [InlineData("Beatrice", "sdfsdfd", "icon.png")] + [InlineData("Maurice", "Ahri est un champion de League of Legends", "icon.png")] + [InlineData("Loupiotte", "Akali est un champion de League of Legends", "icon.png")] + public async Task TestPostChampion(string name, string bio, string icon) + { + // Arrange + var data = new StubData(); + var logger = new NullLogger(); + var controller = new ChampionController(data, logger); + var champDTO = new ChampionDTO() + { + Name = name, + Bio = bio, + Icon = icon + }; + + // Act + var nbInListBefore = data.ChampionsMgr.GetNbItems().Result; + var result = await controller.AddChampion(champDTO); + var nbInListAfter = data.ChampionsMgr.GetNbItems().Result; + + // Assert + // IS the champion added to the list, number of champions in the list + 1 + Assert.Equal(nbInListBefore + 1, nbInListAfter); + // Test le code de retour + } + } +} + diff --git a/Sources/Tests/TestUnitaireLOL/TestEf.cs b/Sources/Tests/TestUnitaireLOL/TestEf.cs index 90a7880..48dde60 100644 --- a/Sources/Tests/TestUnitaireLOL/TestEf.cs +++ b/Sources/Tests/TestUnitaireLOL/TestEf.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.ObjectModel; using EntityFrameWorkLib; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; @@ -7,52 +8,169 @@ using Shared; namespace TestUnitaireLOL { - public class TestsData + public class TestEf { - public IEnumerable Data_TestAddItem() + //[Theory] + + //[InlineData(0, "Zeus", "Dieu de la foudre", true)] + //[InlineData(10, "Hades", "Dieu des enfers", true)] + //[InlineData(1, "Aphrodite", "Déesse de l'amour", true)] + ////[InlineData(10, "AresAresAresAresAresAresAresAresAresAres", + //// "Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" + + //// "Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" + + //// "Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre", false)] + //public async Task TestAddChampionInMemory(int id, String name, String bio, bool expected) + //{ + // var connection = new SqliteConnection("DataSource=:memory:"); + // connection.Open(); + + // var options = new DbContextOptionsBuilder() + // .UseSqlite(connection) + // .Options; + + // using (var context = new LolContext(options)) + // { + // await context.Database.EnsureCreatedAsync(); + // SkinEntity black = new SkinEntity { Name = "Black", Description = "Black skin", Icon = "black.png", Price = 0.99f }; + // SkinEntity white = new SkinEntity { Name = "White", Description = "White skin", Icon = "white.png", Price = 150.99f }; + // SkinEntity green = new SkinEntity { Name = "Green", Description = "Green skin", Icon = "green.png", Price = 4.99f }; + + // RunePageEntity runePage1 = new RunePageEntity { Id = 1, Name = "runepage1" }; + + // var Dieu = new ChampionEntity + // { + // UniqueId = id, + // Name = name, + // Bio = bio, + // Skins = new Collection(new List { black, white, green }), + // ListRunePages = new Collection(new List { { runePage1 } }) + // }; + + // ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); + // Assert.Null(found); + + // await context.Champions.AddAsync(Dieu); + // await context.SaveChangesAsync(); + + // found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name); + // Assert.NotNull(found); + + // Assert.Equal(1, await context.Champions.CountAsync()); + // Assert.Equal(name, found.Name); + // Assert.Equal(3, found.Skins.Count); + // Assert.Equal(1, found.ListRunePages.Count); + + // // Test if the max length of the name is respected (30) and the max length of the bio is respected (256) + // if (expected) + // { + // Assert.True(found.Name.Length <= 30); + // Assert.True(found.Bio.Length <= 256); + // } + // else + // { + // Assert.False(found.Name.Length <= 30); + // Assert.False(found.Bio.Length <= 256); + // } + // } + //} + + [Fact] + public void TestModifyChampionInMemory() { - yield return new object[] + 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 LolContext(options)) + { + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + + ChampionEntity chewie = new ChampionEntity { Name = "Chewbacca",Icon="Icon1", Bio = "Zeus is the king of the gods." }; + ChampionEntity yoda = new ChampionEntity { Name = "Yoda",Icon="Icon2", Bio = "Zeus is the king of the gods." }; + ChampionEntity ewok = new ChampionEntity { Name = "Ewok",Icon="Icon3", Bio = "Zeus is the king of the gods." }; + + + context.Champions.Add(chewie); + context.Champions.Add(yoda); + context.Champions.Add(ewok); + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new LolContext(options)) { - 1, - new ChampionEntity[] + 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 LolContext(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()); + } + } + + [Theory] + + [InlineData(0, "black", "super Skin", "icon1.png", 190000000.2f, true)] + [InlineData(1, "White", "skin1", "icon1", 19, true)] + [InlineData(2, "Green", "skin", "icon1.jpg", -1, false)] + public async Task TestAddSkinToChampionInMemory(int id, String name, String description, String icon, float price, bool expected) + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new LolContext(options)) + { + await context.Database.EnsureCreatedAsync(); + + var Dieu = new ChampionEntity { - new ChampionEntity - { - Name = "jax", - Icon = "icon jax", - Bio = "test bio jax", - Class = ChampionClass.Fighter, - LargeImageEntity = new LargeImageEntity(){Base64 = "tevhdfvsdefefef"} - }, - new ChampionEntity - { - Name = "jax2", - Icon = "icon jax2", - Bio = "test bio jax2", - Class = ChampionClass.Assassin, - LargeImageEntity = new LargeImageEntity(){Base64 = "tevhdfvsdfefefe"} - }, - new ChampionEntity - { - Name = "jax3", - Icon = "icon jax3", - Bio = "test bio jax3", - Class = ChampionClass.Tank, - LargeImageEntity = new LargeImageEntity(){Base64 = "tevhdfvsdfkhefef"} - } - }, - new ChampionEntity[] + UniqueId = 0, + Name = "Zeus", + Bio = "Dieu de la foudre", + Skins = new Collection() + }; + + SkinEntity item = new SkinEntity { - new ChampionEntity() - { - Name = "jax4", - Icon = "icon jax4", - Bio = "test bio jax4", - Class = ChampionClass.Marksman, - LargeImageEntity = new LargeImageEntity(){Base64 = "tevhdfvsdfkhvfdfdf"} - } - } - }; + Name = name, + Description = description, + Icon = icon, + Price = price + }; + + Dieu.Skins.Add(item); + + ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); + Assert.Null(found); + + await context.Champions.AddAsync(Dieu); + await context.SaveChangesAsync(); + + found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name); + } } } diff --git a/Sources/Tests/TestUnitaireLOL/TestUnitaireLOL.csproj b/Sources/Tests/TestUnitaireLOL/TestUnitaireLOL.csproj index 0fc8bb0..8b4fdcb 100644 --- a/Sources/Tests/TestUnitaireLOL/TestUnitaireLOL.csproj +++ b/Sources/Tests/TestUnitaireLOL/TestUnitaireLOL.csproj @@ -20,6 +20,10 @@ all + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + @@ -28,5 +32,7 @@ + + diff --git a/Sources/Tests/TestUnitaireLOL/projet.Champions.db b/Sources/Tests/TestUnitaireLOL/projet.Champions.db new file mode 100644 index 0000000000000000000000000000000000000000..b980d537fbe1b83bdac29136aea094349c56b531 GIT binary patch literal 77824 zcmeI(%}*m`9Ki7zUOG@H>`Tm?lwlI%HoFpD*2Rt4#8L*FPzqa$**%!1mXU2NWw$Mf zBzh?8#-nF%o=x=VU*OrB2NO^J6?*fTr_*_9r?6a1cJgg#nf96I?elwH7zPd=7wR3y z_~Nv6QtcRP$_-^~O!?R_6h#>m&)eeJf6R)5iT)e$T@4+NIvi7$YHDWwALZ8g56avh z^FL?)o%<>CPUiQS?`IxO|28wF{h4~G{WAIM&8Cx&@N7Sq@( z8m7!>8h6b2a?R{FH#I%KpmyKwH=}gcblUsXW2fRC^_o$vo;d#Jw}zVGCk_GnZ0PuYPzngukxMh=M6`WBl(i?>M7?o3RW<{(uR1L?m#h(vh}2F z6!*mQuuw4ad&PrtDQ6eUhPl7f8|J35U$S>|rKiS@_4HmZ@21nPwd&71V(NQcXm>vbcUN2L>m(lrTib+(qwylzEV(-*&JGZ=c`qOdL#59eqy^>{b7u}9r zGR;KWwycs>%v%SBKNs7se1D3Y*>pCWN$HPtRgqC|zi5c9yHY*toXYp!05Gf%x4Sab z!Lkt?rK7Sh$eDdPtI1jW>R6WPcfdcGij)&sDSETkUoL_bY{cC4%Zx0x*POc> z_0#yEk6w)PjcQv v^&=6M5YaHF|miu;}D?P0?^S#+ojmL451GBrH2y6;cQP442M z42MjYFR5r4`WdgY$u$}gcdz?bZgR4!^7(VIoec_#{ULS5{?KSt-2I`^(7H?Gy{>&E zyCxSE`7#mhT7R{9?XGzlhL?ydyXNNHs-Dyv=dpd8xU6ro*2;?fHrYK?HGO?uy|889 zDiv#Mw|?BJx<5L!KdHAnr>*nY567Z?k3GjWD) z{wXJ&wZ~HWXX~RDsn}X7E3qGsi%YC2_cALZp6dKxiumGz00IagfB*srAbr2Z^_tTipk|_~L;80tg_000Iag zfB*srAb+%Ewd400IagfB*srAb#f1-SlCevIiM0tg_000IagfB*srAbmo$5I_I{1Q0*~0R#|0 i009ILNWK8q|H+RrJwyNj1Q0*~0R#|0009ILK;S<_XZyea literal 0 HcmV?d00001 diff --git a/Sources/Tests/TestUnitaireLOL/projet.Champions.db-shm b/Sources/Tests/TestUnitaireLOL/projet.Champions.db-shm new file mode 100644 index 0000000000000000000000000000000000000000..fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10 GIT binary patch literal 32768 zcmeIuAr62r3