From 3cd2ab54b57bdb749162baafa18d1e09b78d0f4a Mon Sep 17 00:00:00 2001 From: Jolys Enzo Date: Tue, 14 Feb 2023 19:33:28 +0100 Subject: [PATCH] table skill et test unitaire sur tout --- Sources/Client/StubEntityInit.cs | 25 +++ Sources/EntityFramwork/BDD-APILOL.db-shm | Bin 0 -> 32768 bytes Sources/EntityFramwork/BDD-APILOL.db-wal | 0 Sources/EntityFramwork/BDDContext.cs | 20 ++- Sources/EntityFramwork/EntityChampions.cs | 3 + Sources/EntityFramwork/EntitySkill.cs | 8 +- Sources/EntityFramwork/EntitySkins.cs | 3 +- Sources/EntityFramwork/Factories/Factories.cs | 6 +- Sources/TestUnitaireBDD/TestChampions.cs | 101 ++++++++---- Sources/TestUnitaireBDD/TestImage.cs | 115 ++++++++++++++ Sources/TestUnitaireBDD/TestRunes.cs | 149 ++++++++++++++++++ Sources/TestUnitaireBDD/TestSkill.cs | 120 ++++++++++++++ Sources/TestUnitaireBDD/TestSkins.cs | 133 ++++++++++++++++ .../TestUnitaireBDD/TestUnitaireBDD.csproj | 1 + 14 files changed, 645 insertions(+), 39 deletions(-) create mode 100644 Sources/EntityFramwork/BDD-APILOL.db-shm create mode 100644 Sources/EntityFramwork/BDD-APILOL.db-wal create mode 100644 Sources/TestUnitaireBDD/TestImage.cs create mode 100644 Sources/TestUnitaireBDD/TestRunes.cs create mode 100644 Sources/TestUnitaireBDD/TestSkill.cs create mode 100644 Sources/TestUnitaireBDD/TestSkins.cs diff --git a/Sources/Client/StubEntityInit.cs b/Sources/Client/StubEntityInit.cs index 172b20b..d310b4e 100644 --- a/Sources/Client/StubEntityInit.cs +++ b/Sources/Client/StubEntityInit.cs @@ -10,6 +10,7 @@ using System.Text; using System.Threading.Tasks; using Api_lol.Factories; using Api_lol.Controllers; +using System.Data; namespace Client { @@ -87,6 +88,28 @@ namespace Client } } + private void addSkill() + { + Factories factories = new Factories(); + + List list = new List() + { new Skill( "toto",SkillType.Unknown), + new Skill("tata",SkillType.Unknown), + new Skill("tutu",SkillType.Unknown)}; + + EntitySkill skill1 = facto.SkillModeleToEntity(list[0],1); + EntitySkill skill2 = facto.SkillModeleToEntity(list[1],1); + EntitySkill skill3 = facto.SkillModeleToEntity(list[2],1); + + using (BDDContext db = new BDDContext()) + { + db.Add(skill1); + db.Add(skill2); + db.Add(skill3); + db.SaveChanges(); + } + + } public void Init() { @@ -98,6 +121,8 @@ namespace Client AddAllSkins(); //Rune AddAllRunes(); + //skill + addSkill(); } } } diff --git a/Sources/EntityFramwork/BDD-APILOL.db-shm b/Sources/EntityFramwork/BDD-APILOL.db-shm new file mode 100644 index 0000000000000000000000000000000000000000..fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10 GIT binary patch literal 32768 zcmeIuAr62r3().HasKey(a => a.Id); modelBuilder.Entity().Property(a => a.Id) .ValueGeneratedOnAdd(); + modelBuilder.Entity().HasIndex(a => a.Name) .IsUnique(true); //Clé avec skins modelBuilder.Entity() .HasMany(e => e.Skins) .WithOne(e => e.Champion) - .HasForeignKey(e => e.ChampionForeignKey); + .HasForeignKey(x => x.ChampionId); + //Clé avec skill + modelBuilder.Entity() + .HasMany(e => e.Skills) + .WithOne(e => e.Champions) + .HasForeignKey(x => x.ChampionId); + // -------------------------------------------------------------------------------// //création de la table Skins modelBuilder.Entity().HasKey(m => m.Id); modelBuilder.Entity().Property(m => m.Id) .ValueGeneratedOnAdd(); - modelBuilder.Entity() - .HasMany(e => e.Skins) - .WithOne(e => e.Champion) - .HasForeignKey(e => e.ChampionForeignKey); - // -------------------------------------------------------------------------------// //création de la table Images modelBuilder.Entity().HasKey(c => c.Id); @@ -60,12 +62,18 @@ namespace EntityFramwork modelBuilder.Entity().HasKey(c => c.Id); modelBuilder.Entity().Property(c => c.Id) .ValueGeneratedOnAdd(); + // -------------------------------------------------------------------------------// + //création de la table Skills + modelBuilder.Entity().HasKey(c => c.Id); + modelBuilder.Entity().Property(c => c.Id) + .ValueGeneratedOnAdd(); } public DbSet Champions { get; set; } public DbSet Skins { get; set; } public DbSet Images { get; set; } public DbSet Runes { get; set; } + public DbSet Skills { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { diff --git a/Sources/EntityFramwork/EntityChampions.cs b/Sources/EntityFramwork/EntityChampions.cs index f1c9886..98a83c6 100644 --- a/Sources/EntityFramwork/EntityChampions.cs +++ b/Sources/EntityFramwork/EntityChampions.cs @@ -26,5 +26,8 @@ namespace DTO public int ImageId { get; set; } public EntityLargeImage Image { get; set; } + // ------------------- Skill ----------------------// + public List Skills { get; set; } + } } diff --git a/Sources/EntityFramwork/EntitySkill.cs b/Sources/EntityFramwork/EntitySkill.cs index 2d540c3..322c6c2 100644 --- a/Sources/EntityFramwork/EntitySkill.cs +++ b/Sources/EntityFramwork/EntitySkill.cs @@ -1,4 +1,5 @@ -using System; +using DTO; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -12,5 +13,10 @@ namespace EntityFramwork public string Name { get; set; } public string Description { get; set; } public string Type { get; set; } + + // ------------ Champion ------------ + public int ChampionId { get; set; } + public EntityChampions Champions { get; set; } + } } diff --git a/Sources/EntityFramwork/EntitySkins.cs b/Sources/EntityFramwork/EntitySkins.cs index 4cf9ef6..f0633df 100644 --- a/Sources/EntityFramwork/EntitySkins.cs +++ b/Sources/EntityFramwork/EntitySkins.cs @@ -20,8 +20,7 @@ namespace EntityFramwork public float Price { get; set; } // ----------- Champion ----------- // - public int ChampionForeignKey { get; set; } - [ForeignKey("ChampionForeignKey")] + public int ChampionId { get; set; } public EntityChampions Champion { get; set; } // ----------- Image ------------ // diff --git a/Sources/EntityFramwork/Factories/Factories.cs b/Sources/EntityFramwork/Factories/Factories.cs index e4a7aa9..001b25e 100644 --- a/Sources/EntityFramwork/Factories/Factories.cs +++ b/Sources/EntityFramwork/Factories/Factories.cs @@ -24,11 +24,12 @@ namespace EntityFramwork.Factories entity.Price = skin.Price; entity.Icon = skin.Icon; entity.Description = skin.Description; - entity.ChampionForeignKey = id; + entity.ChampionId = id; return entity; } + public EntityRunes RuneModelToEntity(Rune rune) { EntityRunes entity = new EntityRunes(); @@ -41,13 +42,14 @@ namespace EntityFramwork.Factories return entity; } - public EntitySkill SkillModeleToEntity(Skill skill) + public EntitySkill SkillModeleToEntity(Skill skill,int championId) { EntitySkill entity = new EntitySkill(); entity.Name = skill.Name; entity.Description = skill.Description; entity.Type = skill.Type.ToString(); + entity.ChampionId = championId; return entity; } diff --git a/Sources/TestUnitaireBDD/TestChampions.cs b/Sources/TestUnitaireBDD/TestChampions.cs index ab851ea..7e70984 100644 --- a/Sources/TestUnitaireBDD/TestChampions.cs +++ b/Sources/TestUnitaireBDD/TestChampions.cs @@ -2,37 +2,68 @@ using DTO; using EntityFramwork; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; +using Model; namespace TestUnitaireBDD { public class TestChampions { - [Fact] - public void Add_Test() + private void Add_Champion_Image(DbContextOptions options) { - //connection must be opened to use In-memory database - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; + //Image 1 + Random aleatoire = new Random(); + EntityLargeImage tmpImage = new EntityLargeImage(); + tmpImage.Base64 = "Inconnu"; + tmpImage.Id = aleatoire.Next(); + + //Image 2 + EntityLargeImage tmpImage2 = new EntityLargeImage(); + tmpImage2.Base64 = "Inconnu"; + tmpImage2.Id = aleatoire.Next(); + + //Image 2 + EntityLargeImage tmpImage3 = new EntityLargeImage(); + tmpImage3.Base64 = "Inconnu"; + tmpImage3.Id = aleatoire.Next(); //prepares the database with one instance of the context using (var context = new BDDContext(options)) { - //context.Database.OpenConnection(); + //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" }; + EntityChampions chewie = new EntityChampions { Name = "Chewbacca", Bio = "Inconnu", Icon = "Inconnu", Classe = ChampionClass.Assassin.ToString() }; + EntityChampions yoda = new EntityChampions { Name = "Yoda", Bio = "Inconnu", Icon = "Inconnu", Classe = ChampionClass.Assassin.ToString() }; + EntityChampions ewok = new EntityChampions { Name = "Ewok", Bio = "Inconnu", Icon = "Inconnu", Classe = ChampionClass.Assassin.ToString() }; + context.Add(tmpImage); + context.Add(tmpImage2); + context.Add(tmpImage3); + chewie.ImageId = tmpImage.Id; + yoda.ImageId = tmpImage2.Id; + ewok.ImageId = tmpImage3.Id; context.Add(chewie); context.Add(yoda); context.Add(ewok); context.SaveChanges(); } + } + + + + [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; + + Add_Champion_Image(options); //uses another instance of the context to do the tests using (var context = new BDDContext(options)) @@ -55,21 +86,7 @@ namespace TestUnitaireBDD .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(); - } + Add_Champion_Image(options); //uses another instance of the context to do the tests using (var context = new BDDContext(options)) @@ -96,5 +113,33 @@ namespace TestUnitaireBDD Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); } } + + [Fact] + public void Delete_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; + + Add_Champion_Image(options); + + using (var context = new BDDContext(options)) + { + EntityChampions tmpChamp = context.Champions.OrderBy(e => e.Name).Include(e => e.Image).First(); + + context.Remove(tmpChamp); + context.SaveChanges(); + } + + using (var context = new BDDContext(options)) + { + Assert.Equal(2, context.Champions.Count()); + Assert.Equal("Yoda",context.Champions.First().Name); + } + } } } \ No newline at end of file diff --git a/Sources/TestUnitaireBDD/TestImage.cs b/Sources/TestUnitaireBDD/TestImage.cs new file mode 100644 index 0000000..09e87d2 --- /dev/null +++ b/Sources/TestUnitaireBDD/TestImage.cs @@ -0,0 +1,115 @@ +using DTO; +using EntityFramwork; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TestUnitaireBDD +{ + public class TestImage + { + private void Add_Image(DbContextOptions options) + { + EntityLargeImage tmpImage = new EntityLargeImage(); + tmpImage.Id = 1; + tmpImage.Base64 = "Inconnu"; + + EntityLargeImage tmpImage2 = new EntityLargeImage(); + tmpImage2.Id = 2; + tmpImage2.Base64 = "Inconnu"; + + EntityLargeImage tmpImage3 = new EntityLargeImage(); + tmpImage3.Id = 3; + tmpImage3.Base64 = "Inconnu"; + + //prepares the database with one instance of the context + using (var context = new BDDContext(options)) + { + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + + context.Add(tmpImage); + context.Add(tmpImage2); + context.Add(tmpImage3); + context.SaveChanges(); + } + } + + [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; + + Add_Image(options); + + using (var context = new BDDContext(options)) + { + Assert.Equal(3,context.Images.Count()); + Assert.Equal(1,context.Images.First().Id); + } + } + + [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; + + Add_Image(options); + + using (var context = new BDDContext(options)) + { + var image = context.Images.Where(e => e.Id == 3).First(); + image.Base64 = "Modify"; + context.SaveChanges(); + } + + using (var context = new BDDContext(options)) + { + var image = context.Images.Where(e => e.Base64 == "Modify").First(); + Assert.NotNull(image); + } + } + + [Fact] + public void Delete_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; + + Add_Image(options); + + using (var context = new BDDContext(options)) + { + var image = context.Images.Where(e => e.Id == 3).First(); + context.Remove(image); + context.SaveChanges(); + } + using (var context = new BDDContext(options)) + { + Assert.Equal(2,context.Images.Count()); + } + } + } +} diff --git a/Sources/TestUnitaireBDD/TestRunes.cs b/Sources/TestUnitaireBDD/TestRunes.cs new file mode 100644 index 0000000..887ee08 --- /dev/null +++ b/Sources/TestUnitaireBDD/TestRunes.cs @@ -0,0 +1,149 @@ +using DTO; +using EntityFramwork; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TestUnitaireBDD +{ + public class TestRunes + { + + private void Add_Champion_Rune(DbContextOptions options) + { + //Image 1 + Random aleatoire = new Random(); + EntityLargeImage tmpImage = new EntityLargeImage(); + tmpImage.Base64 = "Inconnu"; + tmpImage.Id = aleatoire.Next(); + + //Image 2 + EntityLargeImage tmpImage2 = new EntityLargeImage(); + tmpImage2.Base64 = "Inconnu"; + tmpImage2.Id = aleatoire.Next(); + + //Image 2 + EntityLargeImage tmpImage3 = new EntityLargeImage(); + tmpImage3.Base64 = "Inconnu"; + tmpImage3.Id = aleatoire.Next(); + + //prepares the database with one instance of the context + using (var context = new BDDContext(options)) + { + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + + EntityRunes chewie = new EntityRunes { Name = "Chewbacca", Icon = "Inconnu",Description = "Inconnu",Family = RuneFamily.Unknown.ToString()}; + EntityRunes yoda = new EntityRunes { Name = "Yoda",Icon = "Inconnu", Description = "Inconnu", Family = RuneFamily.Unknown.ToString() }; + EntityRunes ewok = new EntityRunes { Name = "Ewok", Icon = "Inconnu", Description = "Inconnu", Family = RuneFamily.Unknown.ToString() }; + + context.Add(tmpImage); + context.Add(tmpImage2); + context.Add(tmpImage3); + chewie.ImageId = tmpImage.Id; + yoda.ImageId = tmpImage2.Id; + ewok.ImageId = tmpImage3.Id; + context.Add(chewie); + context.Add(yoda); + context.Add(ewok); + context.SaveChanges(); + } + } + + + [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; + + Add_Champion_Rune(options); + + //uses another instance of the context to do the tests + using (var context = new BDDContext(options)) + { + context.Database.EnsureCreated(); + + Assert.Equal(3, context.Runes.Count()); + Assert.Equal("Chewbacca", context.Runes.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; + + Add_Champion_Rune(options); + + //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.Runes.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); + nameToFind = "wo"; + Assert.Equal(1, context.Runes.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); + var ewok = context.Runes.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.Runes.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); + nameToFind = "wick"; + Assert.Equal(1, context.Runes.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); + } + } + + [Fact] + public void Delete_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; + + Add_Champion_Rune(options); + + using (var context = new BDDContext(options)) + { + EntityRunes tmpChamp = context.Runes.OrderBy(e => e.Name).Include(e => e.Image).First(); + + context.Remove(tmpChamp); + context.SaveChanges(); + } + + using (var context = new BDDContext(options)) + { + Assert.Equal(2, context.Runes.Count()); + Assert.Equal("Yoda", context.Runes.First().Name); + } + } + } +} diff --git a/Sources/TestUnitaireBDD/TestSkill.cs b/Sources/TestUnitaireBDD/TestSkill.cs new file mode 100644 index 0000000..9cc0afa --- /dev/null +++ b/Sources/TestUnitaireBDD/TestSkill.cs @@ -0,0 +1,120 @@ +using DTO; +using EntityFramwork; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TestUnitaireBDD +{ + public class TestSkill + { + private void Add_Skill(DbContextOptions options) + { + //Image Champ + EntityLargeImage imageChamp = new EntityLargeImage(); + imageChamp.Base64 = "Inconnu"; + imageChamp.Id = 1; + + //Champion + EntityChampions champ = new EntityChampions { Name = "Chewbacca", Bio = "Inconnu", Icon = "Inconnu", Classe = ChampionClass.Assassin.ToString() }; + champ.ImageId = imageChamp.Id; + champ.Id = 1; + + //prepares the database with one instance of the context + using (var context = new BDDContext(options)) + { + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + context.Add(imageChamp); + context.Add(champ); + EntitySkill skill = new EntitySkill { Name = "toto", Description = "Inconnu", Type = SkillType.Unknown.ToString(),ChampionId = champ.Id,Id = 1 }; + context.Add(skill); + + context.SaveChanges(); + } + } + + [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; + + Add_Skill(options); + + //uses another instance of the context to do the tests + using (var context = new BDDContext(options)) + { + context.Database.EnsureCreated(); + + Assert.Equal(1, context.Skills.Count()); + Assert.Equal("toto", context.Skills.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; + + Add_Skill(options); + + //uses another instance of the context to do the tests + using (var context = new BDDContext(options)) + { + var tmpSkill = context.Skills.First(); + tmpSkill.Name = "totoModify"; + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new BDDContext(options)) + { + Assert.Equal("totoModify", context.Skills.First().Name); + } + } + + [Fact] + public void Delete_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; + + Add_Skill(options); + + using (var context = new BDDContext(options)) + { + var tmp = context.Skills.First(); + + context.Remove(tmp); + context.SaveChanges(); + } + + using (var context = new BDDContext(options)) + { + Assert.Equal(0, context.Skills.Count()); + } + } + } +} diff --git a/Sources/TestUnitaireBDD/TestSkins.cs b/Sources/TestUnitaireBDD/TestSkins.cs new file mode 100644 index 0000000..b400bd6 --- /dev/null +++ b/Sources/TestUnitaireBDD/TestSkins.cs @@ -0,0 +1,133 @@ +using DTO; +using EntityFramwork; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TestUnitaireBDD +{ + public class TestSkins + { + private void Add_Skin(DbContextOptions options) + { + //Image Champ + EntityLargeImage imageChamp = new EntityLargeImage(); + imageChamp.Base64 = "Inconnu"; + imageChamp.Id = 1; + + //Image skin + EntityLargeImage imageSkin = new EntityLargeImage(); + imageSkin.Base64 = "Inconnu"; + imageSkin.Id = 2; + + + //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", Classe = ChampionClass.Assassin.ToString() }; + EntitySkins skin = new EntitySkins { Description = "Inconnu", Icon = "Inconnu", Price = 1350 }; + + context.Add(imageChamp); + context.Add(imageSkin); + + chewie.ImageId = imageChamp.Id; + context.Add(chewie); + + skin.ImageId = 2; + skin.ChampionId = 1; + + context.Add(skin); + + context.SaveChanges(); + } + } + + [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; + + Add_Skin(options); + + //uses another instance of the context to do the tests + using (var context = new BDDContext(options)) + { + context.Database.EnsureCreated(); + + Assert.Equal(1, context.Skins.Count()); + Assert.Equal(1350, context.Skins.First().Price); + } + } + + [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; + + Add_Skin(options); + + //uses another instance of the context to do the tests + using (var context = new BDDContext(options)) + { + var tmpSkin = context.Skins.First(); + tmpSkin.Price = 100; + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new BDDContext(options)) + { + Assert.Equal(100,context.Skins.First().Price); + } + } + + [Fact] + public void Delete_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; + + Add_Skin(options); + + using (var context = new BDDContext(options)) + { + var tmp = context.Skins.First(); + + context.Remove(tmp); + context.SaveChanges(); + } + + using (var context = new BDDContext(options)) + { + Assert.Equal(0, context.Skins.Count()); + } + } + + + } +} diff --git a/Sources/TestUnitaireBDD/TestUnitaireBDD.csproj b/Sources/TestUnitaireBDD/TestUnitaireBDD.csproj index 4047a8d..585612a 100644 --- a/Sources/TestUnitaireBDD/TestUnitaireBDD.csproj +++ b/Sources/TestUnitaireBDD/TestUnitaireBDD.csproj @@ -25,6 +25,7 @@ +