@ -6,92 +6,112 @@ namespace TestUnitaire
{
{
public class TestEfLol
public class TestEfLol
{
{
[Fact]
public async Task TestAddInMemory ( )
[Theory]
[InlineData("Zeus", "Dieu de la foudre", true)]
[InlineData("Hades", "Dieu des enfers", true)]
[InlineData("Aphrodite", "Déesse de l'amour", true)]
[ InlineData ( "AresAresAresAresAresAresAresAresAresAres" ,
"Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" +
"Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" +
"Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" , false ) ]
public async Task TestAddInMemory ( String name , String bio , bool expected )
{
{
// Arrange
var connection = new SqliteConnection ( "DataSource=:memory:" ) ;
var connection = new SqliteConnection ( "DataSource=:memory:" ) ;
connection . Open ( ) ;
connection . Open ( ) ;
var options = new DbContextOptionsBuilder < ChampionContext > ( )
var options = new DbContextOptionsBuilder < MyDb Context> ( )
. UseSqlite ( connection )
. UseSqlite ( connection )
. Options ;
. Options ;
// Act
using ( var context = new MyDbContext ( options ) )
using ( var context = new ChampionContext ( options ) )
{
{
await context . Database . EnsureCreatedAsync ( ) ;
await context . Database . EnsureCreatedAsync ( ) ;
var Zeus = new ChampionEntity
var Dieu = new ChampionEntity
{
{
Name = "Zeus" ,
Name = name ,
Bio = "Zeus is the king of the gods."
Bio = bio
} ;
var Poseidon = new ChampionEntity
{
Name = "Poseidon" ,
Bio = "Poseidon is the king of the sea."
} ;
} ;
ChampionEntity found = await context . Champions . SingleOrDefaultAsync ( c = > c . Name = = "Zeus" ) ;
ChampionEntity found = await context . Champions . SingleOrDefaultAsync ( c = > c . Name = = "Zeus" ) ;
Assert . Null ( found ) ;
Assert . Null ( found ) ;
await context . Champions . AddAsync ( Zeus ) ;
await context . Champions . AddAsync ( Dieu ) ;
await context . Champions . AddAsync ( Poseidon ) ;
await context . SaveChangesAsync ( ) ;
await context . SaveChangesAsync ( ) ;
found = await context . Champions . SingleOrDefaultAsync ( c = > c . Name = = "Zeus" ) ;
found = await context . Champions . SingleOrDefaultAsync ( c = > c . Name = = name ) ;
Assert . NotNull ( found ) ;
Assert . NotNull ( found ) ;
Assert . Equal ( 2 , await context . Champions . CountAsync ( ) ) ;
Assert . Equal ( 1 , await context . Champions . CountAsync ( ) ) ;
Assert . Equal ( "Zeus" , found . Name ) ;
Assert . Equal ( name , found . Name ) ;
// Test if the max length of the name is respected (30) and the max length of the bio is respected (256)
if ( expected )
{
Assert . True ( found . Name . Length < = 30 ) ;
Assert . True ( found . Bio . Length < = 256 ) ;
}
else
{
Assert . False ( found . Bio . Length < = 256 ) ;
Assert . False ( found . Name . Length < = 30 ) ;
}
}
}
}
}
[Fact]
[Fact]
public void ModifyTestInMemory ( )
public void ModifyTestInMemory ( )
{
{
var options = new DbContextOptionsBuilder < ChampionContext > ( )
var connection = new SqliteConnection ( "DataSource=:memory:" ) ;
. UseInMemoryDatabase ( databaseName : "Modify_Test_database" )
connection . Open ( ) ;
. Options ;
var options = new DbContextOptionsBuilder < MyDbContext > ( )
//prepares the database with one instance of the context
. UseSqlite ( connection )
. Options ;
using ( var context = new ChampionContext ( options ) )
{
//prepares the database with one instance of the context
ChampionEntity chewie = new ChampionEntity { Name = "Chewbacca" , Bio = "Zeus is the king of the gods." } ;
using ( var context = new MyDbContext ( options ) )
ChampionEntity yoda = new ChampionEntity { Name = "Yoda" , Bio = "Zeus is the king of the gods." } ;
{
ChampionEntity ewok = new ChampionEntity { Name = "Ewok" , Bio = "Zeus is the king of the gods." } ;
//context.Database.OpenConnection();
context . Database . EnsureCreated ( ) ;
context . Champions . Add ( chewie ) ;
context . Champions . Add ( yoda ) ;
ChampionEntity chewie = new ChampionEntity { Name = "Chewbacca" , Bio = "Zeus is the king of the gods." } ;
context . Champions . Add ( ewok ) ;
ChampionEntity yoda = new ChampionEntity { Name = "Yoda" , Bio = "Zeus is the king of the gods." } ;
context . SaveChanges ( ) ;
ChampionEntity ewok = new ChampionEntity { Name = "Ewok" , Bio = "Zeus is the king of the gods." } ;
}
//prepares the database with one instance of the context
context . Champions . Add ( chewie ) ;
using ( var context = new ChampionContext ( options ) )
context . Champions . Add ( yoda ) ;
{
context . Champions . Add ( ewok ) ;
string nameToFind = "ew" ;
context . SaveChanges ( ) ;
Assert . Equal ( 2 , context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . Count ( ) ) ;
}
nameToFind = "ewo" ;
Assert . Equal ( 1 , context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . Count ( ) ) ;
//uses another instance of the context to do the tests
var ewok = context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . First ( ) ;
using ( var context = new MyDbContext ( options ) )
ewok . Name = "Wicket" ;
{
context . SaveChanges ( ) ;
context . Database . EnsureCreated ( ) ;
}
string nameToFind = "ew" ;
//prepares the database with one instance of the context
Assert . Equal ( 2 , context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . Count ( ) ) ;
using ( var context = new ChampionContext ( options ) )
nameToFind = "wo" ;
{
Assert . Equal ( 1 , context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . Count ( ) ) ;
string nameToFind = "ew" ;
var ewok = context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . First ( ) ;
Assert . Equal ( 1 , context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . Count ( ) ) ;
ewok . Name = "Wicket" ;
nameToFind = "wick" ;
context . SaveChanges ( ) ;
Assert . Equal ( 1 , context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . Count ( ) ) ;
}
}
}
//uses another instance of the context to do the tests
}
using ( var context = new MyDbContext ( options ) )
{
context . Database . EnsureCreated ( ) ;
string nameToFind = "ew" ;
Assert . Equal ( 1 , context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . Count ( ) ) ;
nameToFind = "wick" ;
Assert . Equal ( 1 , context . Champions . Where ( n = > n . Name . ToLower ( ) . Contains ( nameToFind ) ) . Count ( ) ) ;
}
}
}
}
}