Adding simple tests on Champion Entity
continuous-integration/drone/push Build is failing Details

pull/6/head
Arthur VALIN 2 years ago
parent 527c0e71f5
commit cf107b961b

@ -1,6 +1,7 @@
using EntityMapper;
using Model;
using Shared;
using System.Diagnostics;
namespace Business
{
@ -21,7 +22,8 @@ namespace Business
public async Task<bool> 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<Champion?> 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;
}
}

@ -41,108 +41,6 @@ namespace Entities
modelBuilder.Entity<ChampionEntity>()
.HasMany(x => x.runepages)
.WithMany(x => x.champions);
modelBuilder.Entity<LargeImageEntity>().HasData(new List<LargeImageEntity>()
{
new()
{
Id = Guid.NewGuid(),
Base64 = "aaa"
}
});
modelBuilder.Entity<ChampionEntity>().HasData(new List<ChampionEntity>() {
new()
{
Name = "Dave",
Bio = "Le meilleur Jazzman de France",
Class = ChampionClass.Fighter,
},
new()
{
Name = "Armure",
Bio = "Solide",
Class = ChampionClass.Tank,
}
});
modelBuilder.Entity<CharacteristicEntity>().HasData(new List<CharacteristicEntity>() {
new()
{
Name = "Force",
Value = 50,
ChampionForeignKey = "Dave",
},
new()
{
Name = "Défense",
Value = 75,
ChampionForeignKey = "Armure",
}
});
modelBuilder.Entity<SkinEntity>().HasData(new List<SkinEntity>() {
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<SkillEntity>().HasData(new List<SkillEntity>() {
new()
{
Name = "Boule de feu",
Description = "Fire!",
SkillType = SkillType.Basic
},
new()
{
Name = "White Star",
Description = "Random damage",
SkillType = SkillType.Ultimate
}
});
modelBuilder.Entity<RunePageEntity>().HasData(new List<RunePageEntity>()
{
new()
{
Id = Guid.NewGuid(),
Name="Runepage_1"
}
});
modelBuilder.Entity<RuneEntity>().HasData(new List<RuneEntity>() {
new()
{
Name = "Bullseye",
Description = "Steady shot",
RuneFamily = RuneFamily.Precision
},
new()
{
Name = "Alkatraz",
Description = "Lock effect",
RuneFamily = RuneFamily.Domination
}
});
}
}
}

@ -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<LargeImageEntity>().HasData(new List<LargeImageEntity>()
{
new()
{
Id = Guid.NewGuid(),
Base64 = "aaa"
}
});
modelBuilder.Entity<ChampionEntity>().HasData(new List<ChampionEntity>() {
new()
{
Name = "Dave",
Bio = "Le meilleur Jazzman de France",
Class = ChampionClass.Fighter,
},
new()
{
Name = "Armure",
Bio = "Solide",
Class = ChampionClass.Tank,
}
});
modelBuilder.Entity<CharacteristicEntity>().HasData(new List<CharacteristicEntity>() {
new()
{
Name = "Force",
Value = 50,
ChampionForeignKey = "Dave",
},
new()
{
Name = "Défense",
Value = 75,
ChampionForeignKey = "Armure",
}
});
modelBuilder.Entity<SkinEntity>().HasData(new List<SkinEntity>() {
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<SkillEntity>().HasData(new List<SkillEntity>() {
new()
{
Name = "Boule de feu",
Description = "Fire!",
SkillType = SkillType.Basic
},
new()
{
Name = "White Star",
Description = "Random damage",
SkillType = SkillType.Ultimate
}
});
modelBuilder.Entity<RunePageEntity>().HasData(new List<RunePageEntity>()
{
new()
{
Id = Guid.NewGuid(),
Name="Runepage_1"
}
});
modelBuilder.Entity<RuneEntity>().HasData(new List<RuneEntity>() {
new()
{
Name = "Bullseye",
Description = "Steady shot",
RuneFamily = RuneFamily.Precision
},
new()
{
Name = "Alkatraz",
Description = "Lock effect",
RuneFamily = RuneFamily.Domination
}
});
}
}
}

@ -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}

@ -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<LolDbContext>()
.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<LolDbContext>()
.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<LolDbContext>()
.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();
}
}
}
}

@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Business\Business.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>
Loading…
Cancel
Save