@ -84,9 +84,8 @@ namespace TestEF
}
}
}
}
[Fact]
[Fact]
public async Task Get Champion_Test( )
public async Task Update Champion_Test( )
{
{
// connection must be opened to use In-memory database
// connection must be opened to use In-memory database
var connection = new SqliteConnection ( "DataSource =:memory:" ) ;
var connection = new SqliteConnection ( "DataSource =:memory:" ) ;
@ -94,24 +93,62 @@ namespace TestEF
var options = new DbContextOptionsBuilder < SQLiteContext > ( ) . UseSqlite ( connection ) . Options ;
var options = new DbContextOptionsBuilder < SQLiteContext > ( ) . UseSqlite ( connection ) . Options ;
using ( var context = new S tubEFChampions ( options ) )
using ( var context = new S QLiteContext ( options ) )
{
{
//context.Database.OpenConnection();
await context . Database . EnsureCreatedAsync ( ) ; // pour créer la base si elle n'existe pas déjà
await context . Database . EnsureCreatedAsync ( ) ; // pour créer la base si elle n'existe pas déjà
var champs = context . Champions . SingleOrDefault ( c = > c . Name = = "Akali" ) ;
// Arrange
var champion = new EFChampion
{
Name = "Champion 1" ,
Bio = "Bio Champion 1" ,
Icon = "aatrox_icon.jpg" ,
Class = ChampionClass . Assassin ,
Assert . NotNull ( champs ) ;
Characteristics = new List < EFCharacteristics > {
Assert . Equal ( "Akali" , champs . Name ) ;
new EFCharacteristics { Name = "Attack Damage" , Value = 60 } ,
}
new EFCharacteristics { Name = "Armor" , Value = 38 } ,
}
new EFCharacteristics { Name = "Magic Resist" , Value = 32 }
}
} ,
Skills = ImmutableHashSet . Create < EFSkill > (
new EFSkill { Name = "Skill 1" , Description = "Desc Skill 1" } ,
new EFSkill { Name = "Skill 2" , Description = "Desc Skill 2" }
) ,
Skins = new ReadOnlyCollection < EFSkin > ( new List < EFSkin > {
new EFSkin
{
Name = "Mecha Kingdoms Aatrox" ,
Description = "In a distant cyberfuture, the champion Aatrox becomes a symbol of ultimate power and glory..." ,
Icon = "aatrox_mecha_icon.jpg" ,
Price = 1350 ,
NameChampion = "Aatrox" ,
ImageId = Guid . NewGuid ( ) ,
Image = new EFLargeImage { Id = Guid . NewGuid ( ) , Base64 = "aatrox_mecha_splash.jpg" }
} } ) ,
Image = new EFLargeImage { Id = Guid . NewGuid ( ) , Base64 = "CheminDeMonChampion" }
} ;
context . Add ( champion ) ;
await context . SaveChangesAsync ( ) ;
champion . Bio = "New Bio" ; // Updating the bio of the champion
// Act
await context . SaveChangesAsync ( ) ;
var updatedChampion = await context . Champions . FirstOrDefaultAsync ( c = > c . Name = = "Champion 1" ) ;
// Assert
Assert . NotNull ( updatedChampion ) ;
Assert . Equal ( "New Bio" , updatedChampion . Bio ) ;
}
}
/ *
[Fact]
[Fact]
public void Modify_Test ( )
public async Task DeleteChampion _Test( )
{
{
// connection must be opened to use In-memory database
// connection must be opened to use In-memory database
var connection = new SqliteConnection ( "DataSource =:memory:" ) ;
var connection = new SqliteConnection ( "DataSource =:memory:" ) ;
@ -119,94 +156,130 @@ namespace TestEF
var options = new DbContextOptionsBuilder < SQLiteContext > ( ) . UseSqlite ( connection ) . Options ;
var options = new DbContextOptionsBuilder < SQLiteContext > ( ) . UseSqlite ( connection ) . Options ;
//prepares the database with one instance of the context
using ( var context = new SQLiteContext ( options ) )
using ( var context = new StubEFChampions ( options ) )
{
{
await context . Database . EnsureCreatedAsync ( ) ; //pour créer la base si elle n'existe pas déjà
//context.Database.OpenConnection();
context . Database . EnsureCreated ( ) ;
/ *
Nounours chewie = new Nounours { Nom = "Chewbacca" } ;
Nounours yoda = new Nounours { Nom = "Yoda" } ;
Nounours ewok = new Nounours { Nom = "Ewok" } ;
context . Nounours . Add ( chewie ) ;
context . Nounours . Add ( yoda ) ;
context . Nounours . Add ( ewok ) ;
context . SaveChanges ( ) ;
} * /
//uses another instance of the context to do the tests
/ * using ( var context = new StubEFChampions ( options ) )
{
context . Database . EnsureCreated ( ) ;
string nameToFind = "ew" ;
Assert . Equal ( 2 , context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . Count ( ) ) ;
nameToFind = "wo" ;
Assert . Equal ( 1 , context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . Count ( ) ) ;
var ewok = context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . First ( ) ;
ewok . Name = "Wicket" ;
context . SaveChanges ( ) ;
}
//uses another instance of the context to do the tests
// Arrange
using ( var context = new StubEFChampions ( options ) )
var champion = new EFChampion
{
{
context . Database . EnsureCreated ( ) ;
Name = "ChampionToDelete" ,
Bio = "Bio Champion to delete" ,
Icon = "delete_icon.jpg" ,
Class = ChampionClass . Fighter ,
string nameToFind = "ew" ;
Characteristics = new List < EFCharacteristics > {
Assert . Equal ( 1 , context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . Count ( ) ) ;
new EFCharacteristics { Name = "Attack Damage" , Value = 70 } ,
nameToFind = "wick" ;
new EFCharacteristics { Name = "Armor" , Value = 42 } ,
Assert . Equal ( 1 , context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . Count ( ) ) ;
new EFCharacteristics { Name = "Magic Resist" , Value = 28 }
}
} ,
} * /
/ *
Skills = ImmutableHashSet . Create < EFSkill > (
[SetUp]
new EFSkill { Name = "Skill 1" , Description = "Desc Skill 1" }
public void Setup ( )
) ,
Skins = new ReadOnlyCollection < EFSkin > ( new List < EFSkin > {
new EFSkin
{
{
Name = "Delete Skin" ,
Description = "Delete skin description" ,
Icon = "delete_skin_icon.jpg" ,
Price = 975 ,
NameChampion = "ChampionToDelete" ,
ImageId = Guid . NewGuid ( ) ,
Image = new EFLargeImage { Id = Guid . NewGuid ( ) , Base64 = "delete_skin_splash.jpg" }
}
}
} ) ,
Image = new EFLargeImage { Id = Guid . NewGuid ( ) , Base64 = "CheminDeMonChampionToDelete" }
} ;
[Test]
context . Add ( champion ) ;
public void TestGET ( )
await context . SaveChangesAsync ( ) ;
{
//Arrange
// Act
// Act
var championResult = ChampionController . get ( ) ;
context . Remove ( champion ) ;
await context . SaveChangesAsync ( ) ;
// Assert
// Assert
var objectResult = championResult as OkObjectResult ;
var deletedChampion = await context . Champions . FirstOrDefaultAsync ( c = > c . Name = = "ChampionToDelete" ) ;
//vérifie que c’ est un ok 200 et un objet
Assert . Null ( deletedChampion ) ;
Assert . IsNotNull ( objectResult ) ;
}
}
var champions = objectResult ? . Value as IEnumerable < ChampionDto > ;
[Fact]
Assert . IsNotNull ( champions ) ;
public async Task GetChampion_Test ( )
{
// connection must be opened to use In-memory database
var connection = new SqliteConnection ( "DataSource =:memory:" ) ;
connection . Open ( ) ;
Assert . AreEqual ( champions . Count ( ) , ( await StubData . championsMgr . getItems ( 0 , 5 ) ) . Count ( ) ) ;
var options = new DbContextOptionsBuilder < SQLiteContext > ( ) . UseSqlite ( connection ) . Options ;
}
[Test]
using ( var context = new SQLiteContext ( options ) )
public void TestPOST ( )
{
{
await context . Database . EnsureCreatedAsync ( ) ; // pour créer la base si elle n'existe pas déjà
// Arrange
// Arrange
var championDto = new ChampionDto ( )
var championToAdd = new EFChampion
{
Name = "ChampionToGet" ,
Bio = "Bio Champion to get" ,
Icon = "get_icon.jpg" ,
Class = ChampionClass . Marksman ,
Characteristics = new List < EFCharacteristics > {
new EFCharacteristics { Name = "Attack Damage" , Value = 80 } ,
new EFCharacteristics { Name = "Armor" , Value = 32 } ,
new EFCharacteristics { Name = "Magic Resist" , Value = 24 }
} ,
Skills = ImmutableHashSet . Create < EFSkill > (
new EFSkill { Name = "Skill 1" , Description = "Desc Skill 1" } ,
new EFSkill { Name = "Skill 2" , Description = "Desc Skill 2" }
) ,
Skins = new ReadOnlyCollection < EFSkin > ( new List < EFSkin > {
new EFSkin
{
{
Name = "Darius"
Name = "Get Skin" ,
Description = "Get skin description" ,
Icon = "get_skin_icon.jpg" ,
Price = 520 ,
NameChampion = "ChampionToGet" ,
ImageId = Guid . NewGuid ( ) ,
Image = new EFLargeImage { Id = Guid . NewGuid ( ) , Base64 = "get_skin_splash.jpg" }
}
} ) ,
Image = new EFLargeImage { Id = Guid . NewGuid ( ) , Base64 = "CheminDeMonChampionToGet" }
} ;
} ;
context . Add ( championToAdd ) ;
await context . SaveChangesAsync ( ) ;
// Act
// Act
var championResult = await ChampionController . post ( championDto ) ;
var championToGet = await context . Champions . Include ( c = > c . Characteristics )
. Include ( c = > c . Skills ) . Include ( c = > c . Skins ) . FirstOrDefaultAsync ( c = > c . Name = = "ChampionToGet" ) ;
// Assert
// Assert
var objectResult = championResult as CreatedAtActionResult ;
Assert . NotNull ( championToGet ) ;
Assert . IsNotNull ( objectResult ) ;
Assert . Equal ( "Bio Champion to get" , championToGet . Bio ) ;
Assert . Equal ( "get_icon.jpg" , championToGet . Icon ) ;
var champions = objectResult ? . Value as ChampionDto ;
Assert . NotNull ( championToGet . Characteristics ) ;
Assert . IsNotNull ( champions ) ;
Assert . Equal ( 3 , championToGet . Characteristics . Count ) ;
Assert . Equal ( ChampionClass . Marksman , championToGet . Class ) ;
Assert . Single ( championToGet . Skins ) ;
if ( championToGet . Skins . Any ( ) )
{
Assert . Equal ( "Get Skin" , championToGet . Skins [ 0 ] . Name ) ;
}
Assert . Equal ( 2 , championToGet . Skills . Count ) ;
Assert . Contains ( championToGet . Skills , s = > s . Name = = "Skill 1" ) ;
Assert . Contains ( championToGet . Skills , s = > s . Name = = "Skill 2" ) ;
Assert . Equal ( "CheminDeMonChampionToGet" , championToGet . Image . Base64 ) ;
}
}
}
}
} * /
}
}