table skill et test unitaire sur tout
continuous-integration/drone/push Build is failing Details

master
Jolys Enzo 2 years ago
parent 7df9a92660
commit 3cd2ab54b5

@ -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<Skill> list = new List<Skill>()
{ 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();
}
}
}

@ -17,24 +17,26 @@ namespace EntityFramwork
modelBuilder.Entity<EntityChampions>().HasKey(a => a.Id);
modelBuilder.Entity<EntityChampions>().Property(a => a.Id)
.ValueGeneratedOnAdd();
modelBuilder.Entity<EntityChampions>().HasIndex(a => a.Name)
.IsUnique(true);
//Clé avec skins
modelBuilder.Entity<EntityChampions>()
.HasMany(e => e.Skins)
.WithOne(e => e.Champion)
.HasForeignKey(e => e.ChampionForeignKey);
.HasForeignKey(x => x.ChampionId);
//Clé avec skill
modelBuilder.Entity<EntityChampions>()
.HasMany(e => e.Skills)
.WithOne(e => e.Champions)
.HasForeignKey(x => x.ChampionId);
// -------------------------------------------------------------------------------//
//création de la table Skins
modelBuilder.Entity<EntitySkins>().HasKey(m => m.Id);
modelBuilder.Entity<EntitySkins>().Property(m => m.Id)
.ValueGeneratedOnAdd();
modelBuilder.Entity<EntityChampions>()
.HasMany(e => e.Skins)
.WithOne(e => e.Champion)
.HasForeignKey(e => e.ChampionForeignKey);
// -------------------------------------------------------------------------------//
//création de la table Images
modelBuilder.Entity<EntityLargeImage>().HasKey(c => c.Id);
@ -60,12 +62,18 @@ namespace EntityFramwork
modelBuilder.Entity<EntityRunes>().HasKey(c => c.Id);
modelBuilder.Entity<EntityRunes>().Property(c => c.Id)
.ValueGeneratedOnAdd();
// -------------------------------------------------------------------------------//
//création de la table Skills
modelBuilder.Entity<EntitySkill>().HasKey(c => c.Id);
modelBuilder.Entity<EntitySkill>().Property(c => c.Id)
.ValueGeneratedOnAdd();
}
public DbSet<EntityChampions> Champions { get; set; }
public DbSet<EntitySkins> Skins { get; set; }
public DbSet<EntityLargeImage> Images { get; set; }
public DbSet<EntityRunes> Runes { get; set; }
public DbSet<EntitySkill> Skills { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

@ -26,5 +26,8 @@ namespace DTO
public int ImageId { get; set; }
public EntityLargeImage Image { get; set; }
// ------------------- Skill ----------------------//
public List<EntitySkill> Skills { get; set; }
}
}

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

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

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

@ -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<BDDContext> options)
{
//connection must be opened to use In-memory database
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<BDDContext>()
.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<BDDContext>()
.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<BDDContext>()
.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);
}
}
}
}

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

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

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

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

@ -25,6 +25,7 @@
<ItemGroup>
<ProjectReference Include="..\EntityFramwork\EntityFramwork.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>

Loading…
Cancel
Save