From cf107b961bdf8f8a6467e9536168d2f66ce0b767 Mon Sep 17 00:00:00 2001 From: "arthur.valin" Date: Mon, 13 Mar 2023 17:29:36 +0100 Subject: [PATCH] Adding simple tests on Champion Entity --- .../Entities.LolDatabase.db-shm | Bin 32768 -> 32768 bytes .../Sources/Business/DbData.Champions.cs | 8 +- .../Sources/Entities/LolDbContext.cs | 102 ----------- .../Sources/Entities/LolDbContextWithStub.cs | 121 +++++++++++++ .../Sources/LeagueOfLegends.sln | 12 +- .../Sources/TestEF/TestChampion.cs | 162 ++++++++++++++++++ .../Sources/TestEF/TestEF.csproj | 32 ++++ EntityFramework_LoL/Sources/TestEF/Usings.cs | 1 + 8 files changed, 332 insertions(+), 106 deletions(-) create mode 100644 EntityFramework_LoL/Sources/Entities/LolDbContextWithStub.cs create mode 100644 EntityFramework_LoL/Sources/TestEF/TestChampion.cs create mode 100644 EntityFramework_LoL/Sources/TestEF/TestEF.csproj create mode 100644 EntityFramework_LoL/Sources/TestEF/Usings.cs diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Entities.LolDatabase.db-shm b/EntityFramework_LoL/Sources/API_LoL_Project/Entities.LolDatabase.db-shm index fbf399190040d917ca62415bc97b9740e0ea5b4a..8bb59d4d11425ff8b59b54cae273e07d8ca5a958 100644 GIT binary patch delta 55 ucmZo@U}|V!;*@x#%K!!wIpqca+Ryxby}x4#vLGv1aAK@7qx{Ck`g#D-1QJdF delta 55 vcmZo@U}|V!;*@x#%K!q56FKDtxg2Kxe$(Hv1X++3EI2V%nUP^*V|_gUrL7Qm diff --git a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs index c768c6c..918a005 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs @@ -1,6 +1,7 @@ using EntityMapper; using Model; using Shared; +using System.Diagnostics; namespace Business { @@ -21,7 +22,8 @@ namespace Business public async Task DeleteItem(Champion? item) { - parent.DbContext.champions.Remove(item.ToEntity()); + var toDelete = parent.DbContext.champions.Where(c => c.Name==item.Name).First(); + parent.DbContext.champions.Remove(toDelete); return true; } @@ -127,8 +129,8 @@ namespace Business public async Task UpdateItem(Champion? oldItem, Champion? newItem) { - parent.DbContext.champions.Remove(oldItem.ToEntity()); - parent.DbContext.champions.Add(newItem.ToEntity()); + var toUpdate = parent.DbContext.champions.Where(c => c.Name == oldItem.Name).First(); + toUpdate = newItem.ToEntity(); return newItem; } } diff --git a/EntityFramework_LoL/Sources/Entities/LolDbContext.cs b/EntityFramework_LoL/Sources/Entities/LolDbContext.cs index c2bcad4..8772474 100644 --- a/EntityFramework_LoL/Sources/Entities/LolDbContext.cs +++ b/EntityFramework_LoL/Sources/Entities/LolDbContext.cs @@ -41,108 +41,6 @@ namespace Entities modelBuilder.Entity() .HasMany(x => x.runepages) .WithMany(x => x.champions); - - - - - modelBuilder.Entity().HasData(new List() - { - new() - { - Id = Guid.NewGuid(), - Base64 = "aaa" - } - }); - - modelBuilder.Entity().HasData(new List() { - new() - { - Name = "Dave", - Bio = "Le meilleur Jazzman de France", - Class = ChampionClass.Fighter, - - }, - new() - { - Name = "Armure", - Bio = "Solide", - Class = ChampionClass.Tank, - } - }); - - modelBuilder.Entity().HasData(new List() { - new() - { - Name = "Force", - Value = 50, - ChampionForeignKey = "Dave", - }, - new() - { - Name = "Défense", - Value = 75, - ChampionForeignKey = "Armure", - } - }); - - modelBuilder.Entity().HasData(new List() { - new SkinEntity - { - Name = "Dave de glace", - Description = "Enneigé", - Icon = "aaa", - ChampionForeignKey = "Dave", - Price=7.99F - }, - new SkinEntity - { - Name = "Armure Fullspeed", - Description = "Deja vu", - Icon = "aaa", - ChampionForeignKey = "Armure", - Price=9.99F - }, - }); - - modelBuilder.Entity().HasData(new List() { - new() - { - Name = "Boule de feu", - Description = "Fire!", - SkillType = SkillType.Basic - }, - new() - { - Name = "White Star", - Description = "Random damage", - SkillType = SkillType.Ultimate - } - }); - - modelBuilder.Entity().HasData(new List() - { - new() - { - Id = Guid.NewGuid(), - Name="Runepage_1" - } - }); - - modelBuilder.Entity().HasData(new List() { - new() - { - Name = "Bullseye", - Description = "Steady shot", - RuneFamily = RuneFamily.Precision - }, - new() - { - Name = "Alkatraz", - Description = "Lock effect", - RuneFamily = RuneFamily.Domination - } - }); - } } } diff --git a/EntityFramework_LoL/Sources/Entities/LolDbContextWithStub.cs b/EntityFramework_LoL/Sources/Entities/LolDbContextWithStub.cs new file mode 100644 index 0000000..204ad8b --- /dev/null +++ b/EntityFramework_LoL/Sources/Entities/LolDbContextWithStub.cs @@ -0,0 +1,121 @@ +using Microsoft.EntityFrameworkCore; +using Shared; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Entities +{ + internal class LolDbContextWithStub : LolDbContext + { + public LolDbContextWithStub(DbContextOptions configuration) : base(configuration) + { + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity().HasData(new List() + { + new() + { + Id = Guid.NewGuid(), + Base64 = "aaa" + } + }); + + modelBuilder.Entity().HasData(new List() { + new() + { + Name = "Dave", + Bio = "Le meilleur Jazzman de France", + Class = ChampionClass.Fighter, + + }, + new() + { + Name = "Armure", + Bio = "Solide", + Class = ChampionClass.Tank, + } + }); + + modelBuilder.Entity().HasData(new List() { + new() + { + Name = "Force", + Value = 50, + ChampionForeignKey = "Dave", + }, + new() + { + Name = "Défense", + Value = 75, + ChampionForeignKey = "Armure", + } + }); + + modelBuilder.Entity().HasData(new List() { + new SkinEntity + { + Name = "Dave de glace", + Description = "Enneigé", + Icon = "aaa", + ChampionForeignKey = "Dave", + Price=7.99F + }, + new SkinEntity + { + Name = "Armure Fullspeed", + Description = "Deja vu", + Icon = "aaa", + ChampionForeignKey = "Armure", + Price=9.99F + }, + }); + + modelBuilder.Entity().HasData(new List() { + new() + { + Name = "Boule de feu", + Description = "Fire!", + SkillType = SkillType.Basic + }, + new() + { + Name = "White Star", + Description = "Random damage", + SkillType = SkillType.Ultimate + } + }); + + modelBuilder.Entity().HasData(new List() + { + new() + { + Id = Guid.NewGuid(), + Name="Runepage_1" + } + }); + + modelBuilder.Entity().HasData(new List() { + new() + { + Name = "Bullseye", + Description = "Steady shot", + RuneFamily = RuneFamily.Precision + }, + new() + { + Name = "Alkatraz", + Description = "Lock effect", + RuneFamily = RuneFamily.Domination + } + }); + + } + } +} diff --git a/EntityFramework_LoL/Sources/LeagueOfLegends.sln b/EntityFramework_LoL/Sources/LeagueOfLegends.sln index 8f1d4ea..4d78dec 100644 --- a/EntityFramework_LoL/Sources/LeagueOfLegends.sln +++ b/EntityFramework_LoL/Sources/LeagueOfLegends.sln @@ -27,7 +27,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test_Api", "Test_Api\Test_A EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Business", "Business\Business.csproj", "{A447B0BE-62AE-4F66-B887-D1F3D46B0DB3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityMappers", "EntityMappers\EntityMappers.csproj", "{3A70A719-4F42-4CC3-846A-53437F3B4CC5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityMappers", "EntityMappers\EntityMappers.csproj", "{3A70A719-4F42-4CC3-846A-53437F3B4CC5}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{4008CA21-360B-4C39-8AC3-6E5B02552E22}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestEF", "TestEF\TestEF.csproj", "{059B4A61-E9D5-4C00-8178-C8714D781FBC}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -75,6 +79,10 @@ Global {3A70A719-4F42-4CC3-846A-53437F3B4CC5}.Debug|Any CPU.Build.0 = Debug|Any CPU {3A70A719-4F42-4CC3-846A-53437F3B4CC5}.Release|Any CPU.ActiveCfg = Release|Any CPU {3A70A719-4F42-4CC3-846A-53437F3B4CC5}.Release|Any CPU.Build.0 = Release|Any CPU + {059B4A61-E9D5-4C00-8178-C8714D781FBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {059B4A61-E9D5-4C00-8178-C8714D781FBC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {059B4A61-E9D5-4C00-8178-C8714D781FBC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {059B4A61-E9D5-4C00-8178-C8714D781FBC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -85,6 +93,8 @@ Global {C463E2E1-237A-4339-A4C4-6EA3BE7002AE} = {BC2FFCA4-3801-433F-A83E-B55345F3B31E} {A447B0BE-62AE-4F66-B887-D1F3D46B0DB3} = {BC2FFCA4-3801-433F-A83E-B55345F3B31E} {3A70A719-4F42-4CC3-846A-53437F3B4CC5} = {BC2FFCA4-3801-433F-A83E-B55345F3B31E} + {4008CA21-360B-4C39-8AC3-6E5B02552E22} = {BC2FFCA4-3801-433F-A83E-B55345F3B31E} + {059B4A61-E9D5-4C00-8178-C8714D781FBC} = {4008CA21-360B-4C39-8AC3-6E5B02552E22} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {92F3083D-793F-4552-8A9A-0AD6534159C9} diff --git a/EntityFramework_LoL/Sources/TestEF/TestChampion.cs b/EntityFramework_LoL/Sources/TestEF/TestChampion.cs new file mode 100644 index 0000000..cdc7fb4 --- /dev/null +++ b/EntityFramework_LoL/Sources/TestEF/TestChampion.cs @@ -0,0 +1,162 @@ +using Business; +using Entities; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Model; +using Shared; + +namespace TestEF +{ + public class TestChampion + { + + [Fact] + public async void Test_Add() + { + //connection must be opened to use In-memory database + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new LolDbContext(options)) + { + var manager = new DbData(context).ChampionsMgr; + + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + + Champion batman = new("Batman", ChampionClass.Assassin); + Champion endeavor = new("Endeavor", ChampionClass.Tank); + Champion escanor = new("Escanor", ChampionClass.Fighter); + + await manager.AddItem(batman); + await manager.AddItem(endeavor); + await manager.AddItem(escanor); + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new LolDbContext(options)) + { + var manager = new DbData(context).ChampionsMgr; + + context.Database.EnsureCreated(); + + var nbItems = await manager.GetNbItems(); + Assert.Equal(3, nbItems); + + var items = await manager.GetItemsByName("Batman", 0, nbItems); + Assert.Equal("Batman", items.First().Name); + } + + } + + [Fact] + public async void Test_Update() + { + //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 LolDbContext(options)) + { + var manager = new DbData(context).ChampionsMgr; + + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + + Champion batman = new("Batman", ChampionClass.Assassin); + Champion endeavor = new("Endeavor", ChampionClass.Tank); + Champion escanor = new("Escanor", ChampionClass.Fighter); + + await manager.AddItem(batman); + await manager.AddItem(endeavor); + await manager.AddItem(escanor); + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new LolDbContext(options)) + { + var manager = new DbData(context).ChampionsMgr; + + context.Database.EnsureCreated(); + + + var itemsByName = await manager.GetItemsByName("E", 0, 3); + Assert.Equal(2, itemsByName.Count()); + + var escanor = context.champions.Where(n => n.Name.Contains("Esc")).First(); + + escanor.Class = ChampionClass.Tank; + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new LolDbContext(options)) + { + var manager = new DbData(context).ChampionsMgr; + + context.Database.EnsureCreated(); + + var itemsByName = await manager.GetItemsByClass(ChampionClass.Tank, 0, 3); + + Assert.Equal(2, itemsByName.Count()); + } + } + + [Fact] + public async void Test_Delete() + { + //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 LolDbContext(options)) + { + var manager = new DbData(context).ChampionsMgr; + + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + + Champion batman = new("Batman", ChampionClass.Assassin); + Champion endeavor = new("Endeavor", ChampionClass.Tank); + Champion escanor = new("Escanor", ChampionClass.Fighter); + + await manager.AddItem(batman); + await manager.AddItem(endeavor); + await manager.AddItem(escanor); + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new LolDbContext(options)) + { + var manager = new DbData(context).ChampionsMgr; + + context.Database.EnsureCreated(); + + var endeavor = (await manager.GetItemsByName("Endeavor", 0, 3)).First(); + + var itemsByName = await manager.DeleteItem(endeavor); + Assert.Equal(2, await manager.GetNbItems()); + context.SaveChanges(); + } + + } + + } +} \ No newline at end of file diff --git a/EntityFramework_LoL/Sources/TestEF/TestEF.csproj b/EntityFramework_LoL/Sources/TestEF/TestEF.csproj new file mode 100644 index 0000000..e7bf20e --- /dev/null +++ b/EntityFramework_LoL/Sources/TestEF/TestEF.csproj @@ -0,0 +1,32 @@ + + + + net6.0 + enable + enable + + false + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + diff --git a/EntityFramework_LoL/Sources/TestEF/Usings.cs b/EntityFramework_LoL/Sources/TestEF/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/EntityFramework_LoL/Sources/TestEF/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file