From 1ebe7b3b32bda2b825fdf81ccc32da67d0b12ba2 Mon Sep 17 00:00:00 2001 From: "arthur.valin" Date: Tue, 14 Mar 2023 16:23:25 +0100 Subject: [PATCH] Adding Skills to Champion tests --- .../Sources/Business/DbData.Champions.cs | 6 +-- .../Sources/EntityMappers/ChampionMapper.cs | 25 +++++++---- .../Sources/EntityMappers/SkillMapper.cs | 33 ++++++++++++++ .../Sources/EntityMappers/SkinMapper.cs | 2 +- .../Sources/TestEF/TestChampions.cs | 44 ++++++++++++++----- .../Sources/TestEF/TestSkins.cs | 1 + 6 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 EntityFramework_LoL/Sources/EntityMappers/SkillMapper.cs diff --git a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs index 90eadc1..7e54d83 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs @@ -19,7 +19,7 @@ namespace Business { try { - await parent.DbContext.champions.AddAsync(item.ToEntity()); + await parent.DbContext.champions.AddAsync(item.ToEntity(parent.DbContext)); parent.DbContext.SaveChanges(); } catch(OperationCanceledException){} @@ -100,7 +100,7 @@ namespace Business public async Task> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.champions.GetItemsWithFilterAndOrdering( + return parent.DbContext.champions.Include("Skills").GetItemsWithFilterAndOrdering( c => skill != null && c.Skills.Any(s => s.Name.Equals(skill)), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); @@ -151,7 +151,7 @@ namespace Business var toUpdate = parent.DbContext.champions.Find(oldItem.Name); try { - toUpdate = newItem.ToEntity(); + toUpdate = newItem.ToEntity(parent.DbContext); parent.DbContext.SaveChanges(); }catch (DbUpdateException){} diff --git a/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs b/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs index fe54594..9dabc8e 100644 --- a/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs +++ b/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs @@ -1,20 +1,29 @@ using Entities; +using EntityMappers; using Model; namespace EntityMapper { public static class ChampionMapper { - public static ChampionEntity ToEntity(this Champion item) + public static ChampionEntity ToEntity(this Champion item, LolDbContext context) { - return new() + + ChampionEntity championEntity = context.champions.Find(item.Name); + if (championEntity == null) { - Name = item.Name, - Bio = item.Bio, - Icon = item.Icon, - Class = item.Class, - Image = new() { Base64 = item.Image.Base64 }, - }; + championEntity = new() + { + Name = item.Name, + Bio = item.Bio, + Icon = item.Icon, + Class = item.Class, + Image = new() { Base64 = item.Image.Base64 }, + }; + championEntity.Skills = item.Skills.Select(x => x.ToEntity(championEntity, context)).ToList(); + } + return championEntity; + } public static Champion ToModel(this ChampionEntity entity) diff --git a/EntityFramework_LoL/Sources/EntityMappers/SkillMapper.cs b/EntityFramework_LoL/Sources/EntityMappers/SkillMapper.cs new file mode 100644 index 0000000..5f03970 --- /dev/null +++ b/EntityFramework_LoL/Sources/EntityMappers/SkillMapper.cs @@ -0,0 +1,33 @@ +using Entities; +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EntityMappers +{ + public static class SkillMapper + { + public static SkillEntity ToEntity(this Skill item, ChampionEntity champion, LolDbContext context) + { + var skillEntity = context.skills.Find(item.Name); + if (skillEntity == null) { + return new() + { + Name = item.Name, + Description = item.Description, + SkillType = item.Type, + Champions = new List() { champion } + }; + } + skillEntity!.Champions?.Add(champion); + return skillEntity; + } + + + public static Skill ToModel(this SkillEntity entity) + => new(entity.Name, entity.SkillType,entity.Description); + } +} diff --git a/EntityFramework_LoL/Sources/EntityMappers/SkinMapper.cs b/EntityFramework_LoL/Sources/EntityMappers/SkinMapper.cs index 96bf2e0..c595ec2 100644 --- a/EntityFramework_LoL/Sources/EntityMappers/SkinMapper.cs +++ b/EntityFramework_LoL/Sources/EntityMappers/SkinMapper.cs @@ -11,7 +11,7 @@ namespace EntityMapper return new() { Name = item.Name, - Champion = context?.champions.Find(item.Champion.Name) ?? item.Champion.ToEntity(), + Champion = context?.champions.Find(item.Champion.Name) ?? item.Champion.ToEntity(context), ChampionForeignKey = item.Champion.Name, Description = item.Description, Icon = item.Icon, diff --git a/EntityFramework_LoL/Sources/TestEF/TestChampions.cs b/EntityFramework_LoL/Sources/TestEF/TestChampions.cs index 3b86a4a..3e73a07 100644 --- a/EntityFramework_LoL/Sources/TestEF/TestChampions.cs +++ b/EntityFramework_LoL/Sources/TestEF/TestChampions.cs @@ -4,6 +4,7 @@ using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Model; using Shared; +using Xunit.Abstractions; namespace TestEF { @@ -27,9 +28,14 @@ namespace TestEF context.Database.EnsureCreated(); - Champion batman = new("Batman", ChampionClass.Assassin); - Champion endeavor = new("Endeavor", ChampionClass.Tank); - Champion escanor = new("Escanor", ChampionClass.Fighter); + Champion batman = new("Batman", ChampionClass.Assassin, "icon_1", "image_1", "L'ombre de la nuit"); + batman.AddSkill(new("Bat-signal", SkillType.Basic, "Envoie le signal")); + + Champion endeavor = new("Endeavor", ChampionClass.Tank, "icon_2", "image_2", "Feu brûlant énernel"); + endeavor.AddSkill(new("Final flames", SkillType.Ultimate, "Dernière flamme d'un héro")); + + Champion escanor = new("Escanor", ChampionClass.Fighter, "icon_3", "image_3", "1, 2, 3, Soleil"); + escanor.AddSkill(new("Croissance solaire", SkillType.Passive, "Le soleil rends plus fort !")); await manager.AddItem(batman); await manager.AddItem(endeavor); @@ -48,6 +54,13 @@ namespace TestEF var items = await manager.GetItemsByName("Batman", 0, nbItems); Assert.Equal("Batman", items.First().Name); + + items = await manager.GetItemsBySkill("Croissance solaire", 0, nbItems); + Assert.Equal("Escanor", items.First().Name); + + items = await manager.GetItemsBySkill(new Skill("Final flames", SkillType.Ultimate, "Dernière flamme d'un héro"), + 0, nbItems); + Assert.Equal("Endeavor", items.First().Name); } } @@ -70,9 +83,9 @@ namespace TestEF context.Database.EnsureCreated(); - Champion batman = new("Batman", ChampionClass.Assassin); - Champion endeavor = new("Endeavor", ChampionClass.Tank); - Champion escanor = new("Escanor", ChampionClass.Fighter); + Champion batman = new("Batman", ChampionClass.Assassin, "icon_1", "image_1", "L'ombre de la nuit"); + Champion endeavor = new("Endeavor", ChampionClass.Tank, "icon_2", "image_2", "Feu brûlant énernel"); + Champion escanor = new("Escanor", ChampionClass.Fighter, "icon_3", "image_3", "1, 2, 3, Soleil"); await manager.AddItem(batman); await manager.AddItem(endeavor); @@ -96,7 +109,6 @@ namespace TestEF itemsByName = await manager.GetItemsByClass(ChampionClass.Tank, 0, 3); Assert.Equal(2, itemsByName.Count()); - context.SaveChanges(); } } @@ -119,9 +131,17 @@ namespace TestEF context.Database.EnsureCreated(); - Champion batman = new("Batman", ChampionClass.Assassin); - Champion endeavor = new("Endeavor", ChampionClass.Tank); - Champion escanor = new("Escanor", ChampionClass.Fighter); + + Champion batman = new("Batman", ChampionClass.Assassin, "icon_1", "image_1", "L'ombre de la nuit"); + batman.AddSkill(new("Charge", SkillType.Basic, "Coup de base")); + batman.AddSkill(new("Double Saut", SkillType.Basic, "")); + + Champion endeavor = new("Endeavor", ChampionClass.Tank, "icon_2", "image_2", "Feu brûlant énernel"); + endeavor.AddSkill(new("Charge", SkillType.Basic, "Coup de base")); + + Champion escanor = new("Escanor", ChampionClass.Fighter, "icon_3", "image_3", "1, 2, 3, Soleil"); + escanor.AddSkill(new("Charge", SkillType.Basic, "Coup de base")); + batman.AddSkill(new("Double Saut", SkillType.Basic, "")); await manager.AddItem(batman); await manager.AddItem(endeavor); @@ -139,6 +159,10 @@ namespace TestEF var itemsByName = await manager.DeleteItem(endeavor); Assert.Equal(2, await manager.GetNbItems()); + + Assert.Equal(1, await manager.GetNbItemsBySkill(new Skill("Double Saut", SkillType.Basic, ""))); + Assert.Equal(2, await manager.GetNbItemsBySkill(new Skill("Charge", SkillType.Basic, "Coup de base"))); + } } diff --git a/EntityFramework_LoL/Sources/TestEF/TestSkins.cs b/EntityFramework_LoL/Sources/TestEF/TestSkins.cs index abe5195..5d85e3c 100644 --- a/EntityFramework_LoL/Sources/TestEF/TestSkins.cs +++ b/EntityFramework_LoL/Sources/TestEF/TestSkins.cs @@ -57,6 +57,7 @@ namespace TestEF var nbItems = await manager.GetNbItems(); Assert.Equal(4, nbItems); + Assert.Equal(4, (await manager.GetItems(0, nbItems)).Count()); var items = await manager.GetItemsByName("Batman", 0, nbItems); Assert.Equal(2, items.Count());