@ -4,34 +4,30 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Abstractions ;
using Microsoft.Extensions.Logging.Abstractions ;
using Moq ;
using Moq ;
using Shared ;
using Shared ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using TestAPI.Extensions ;
using TestAPI.Extensions ;
namespace TestAPI
namespace TestAPI ;
{
public class ParagraphsUnitTest
{
public class ParagraphsUnitTest
private readonly Mock < IParagraphService < ParagraphDto > > paragraphService ;
{
private readonly Mock < IParagraphService < ParagraphDto > > _paragraphService ;
public ParagraphsUnitTest ( )
public ParagraphsUnitTest ( )
{
{
paragraphService = new Mock < IParagraphService < ParagraphDto > > ( ) ;
_ paragraphService = new Mock < IParagraphService < ParagraphDto > > ( ) ;
}
}
[Fact]
[Fact]
public void GetParagraphsListSuccess ( )
public void GetParagraphsListSuccess ( )
{
{
var paragraphsList = GetParagraphsData ( ) ;
var paragraphsList = GetParagraphsData ( ) ;
paragraphService. Setup ( x = > x . GetParagraphs ( 1 , 4 , 0 ) )
_ paragraphService. Setup ( x = > x . GetParagraphs ( 1 , 4 , 0 ) )
. Returns ( paragraphsList ) ;
. Returns ( paragraphsList ) ;
var ParagraphsController = new ParagraphsController ( paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsController =
new ParagraphsController ( _paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsResult = P aragraphsController. GetParagraphs ( 1 , 4 , 0 ) ;
var paragraphsResult = p aragraphsController. GetParagraphs ( 1 , 4 , 0 ) ;
if ( paragraphsResult is OkObjectResult okObjectResult )
if ( paragraphsResult is OkObjectResult okObjectResult )
{
{
@ -39,41 +35,38 @@ namespace TestAPI
Assert . NotNull ( valeur ) ;
Assert . NotNull ( valeur ) ;
Assert . Equal ( GetParagraphsData ( ) . ToString ( ) , valeur . ToString ( ) ) ;
Assert . Equal ( GetParagraphsData ( ) . ToString ( ) , valeur . ToString ( ) ) ;
Assert . True ( paragraphsList . SequenceEqual ( valeur as IEnumerable < ParagraphDto > , new ParagraphIdEqualityComparer ( ) ) ) ;
Assert . True ( paragraphsList . SequenceEqual ( valeur as IEnumerable < ParagraphDto > ,
new ParagraphIdEqualityComparer ( ) ) ) ;
}
}
}
}
[Fact]
[Fact]
public void GetParagraphsListFail ( )
public void GetParagraphsListFail ( )
{
{
paragraphService. Setup ( x = > x . GetParagraphs ( 1 , 4 , 0 ) )
_ paragraphService. Setup ( x = > x . GetParagraphs ( 1 , 4 , 0 ) )
. Returns ( new List < ParagraphDto > ( ) ) ;
. Returns ( new List < ParagraphDto > ( ) ) ;
var ParagraphsController = new ParagraphsController ( paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsController =
new ParagraphsController ( _paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsResult = P aragraphsController. GetParagraphs ( 1 , 4 , 0 ) ;
var paragraphsResult = p aragraphsController. GetParagraphs ( 1 , 4 , 0 ) ;
if ( paragraphsResult is StatusCodeResult statusCodeResult & & statusCodeResult . StatusCode = = 204 )
if ( paragraphsResult is StatusCodeResult statusCodeResult & & statusCodeResult . StatusCode = = 204 )
{
{
Assert . IsNotType < OkObjectResult > ( paragraphsResult ) ;
Assert . IsNotType < OkObjectResult > ( paragraphsResult ) ;
}
}
}
}
[Fact]
[Fact]
public void GetParagraphIdSuccess ( )
public void GetParagraphIdSuccess ( )
{
{
var paragraphsList = GetParagraphsData ( ) ;
var paragraphsList = GetParagraphsData ( ) ;
paragraphService. Setup ( x = > x . GetParagraphById ( 1 ) )
_ paragraphService. Setup ( x = > x . GetParagraphById ( 1 ) )
. Returns ( paragraphsList [ 1 ] ) ;
. Returns ( paragraphsList [ 1 ] ) ;
var ParagraphsController = new ParagraphsController ( paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsController =
new ParagraphsController ( _paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsResult = P aragraphsController. GetParagraphById ( 1 ) ;
var paragraphsResult = p aragraphsController. GetParagraphById ( 1 ) ;
if ( paragraphsResult is OkObjectResult okObjectResult )
if ( paragraphsResult is OkObjectResult okObjectResult )
{
{
ParagraphDto valeur = okObjectResult . Value as ParagraphDto ;
ParagraphDto valeur = okObjectResult . Value as ParagraphDto ;
@ -94,39 +87,38 @@ namespace TestAPI
Assert . IsType < ParagraphDto > ( valeur ) ;
Assert . IsType < ParagraphDto > ( valeur ) ;
Assert . Contains ( valeur , paragraphsList ) ;
Assert . Contains ( valeur , paragraphsList ) ;
}
}
}
}
[Fact]
[Fact]
public void GetParagraphIdFail ( )
public void GetParagraphIdFail ( )
{
{
var paragraphsList = GetParagraphsData ( ) ;
var paragraphsList = GetParagraphsData ( ) ;
paragraphService. Setup ( x = > x . GetParagraphById ( 1 ) )
_ paragraphService. Setup ( x = > x . GetParagraphById ( 1 ) )
. Returns ( paragraphsList [ 1 ] ) ;
. Returns ( paragraphsList [ 1 ] ) ;
var ParagraphsController = new ParagraphsController ( paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsController =
new ParagraphsController ( _paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsResult = P aragraphsController. GetParagraphById ( 100 ) ;
var paragraphsResult = p aragraphsController. GetParagraphById ( 100 ) ;
if ( paragraphsResult is NotFoundObjectResult NF ObjectResult)
if ( paragraphsResult is NotFoundObjectResult nf ObjectResult)
{
{
var valeur = NF ObjectResult. Value ;
var valeur = nf ObjectResult. Value ;
Assert . NotNull ( valeur ) ;
Assert . NotNull ( valeur ) ;
Assert . IsNotType < ParagraphDto > ( valeur ) ;
Assert . IsNotType < ParagraphDto > ( valeur ) ;
Assert . DoesNotContain ( valeur , paragraphsList ) ;
Assert . DoesNotContain ( valeur , paragraphsList ) ;
}
}
}
}
[Fact]
[Fact]
public void GetParagraphTitleSuccess ( )
public void GetParagraphTitleSuccess ( )
{
{
var paragraphsList = GetParagraphsData ( ) ;
var paragraphsList = GetParagraphsData ( ) ;
paragraphService. Setup ( x = > x . GetParagraphByTitle ( "Title" ) )
_ paragraphService. Setup ( x = > x . GetParagraphByTitle ( "Title" ) )
. Returns ( paragraphsList [ 3 ] ) ;
. Returns ( paragraphsList [ 3 ] ) ;
var ParagraphsController = new ParagraphsController ( paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsController =
new ParagraphsController ( _paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsResult = P aragraphsController. GetParagraphByTitle ( "Title" ) ;
var paragraphsResult = p aragraphsController. GetParagraphByTitle ( "Title" ) ;
if ( paragraphsResult is OkObjectResult okObjectResult )
if ( paragraphsResult is OkObjectResult okObjectResult )
{
{
ParagraphDto valeur = okObjectResult . Value as ParagraphDto ;
ParagraphDto valeur = okObjectResult . Value as ParagraphDto ;
@ -141,23 +133,22 @@ namespace TestAPI
Assert . IsType < ParagraphDto > ( valeur ) ;
Assert . IsType < ParagraphDto > ( valeur ) ;
Assert . Contains ( valeur , paragraphsList ) ;
Assert . Contains ( valeur , paragraphsList ) ;
}
}
}
}
[Fact]
[Fact]
public void GetParagraphTitleFail ( )
public void GetParagraphTitleFail ( )
{
{
var paragraphsList = GetParagraphsData ( ) ;
var paragraphsList = GetParagraphsData ( ) ;
paragraphService. Setup ( x = > x . GetParagraphByTitle ( "Title" ) )
_ paragraphService. Setup ( x = > x . GetParagraphByTitle ( "Title" ) )
. Returns ( paragraphsList [ 3 ] ) ;
. Returns ( paragraphsList [ 3 ] ) ;
var ParagraphsController = new ParagraphsController ( paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsController =
new ParagraphsController ( _paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsResult = P aragraphsController. GetParagraphByTitle ( "IUHIUHU" ) ;
var paragraphsResult = p aragraphsController. GetParagraphByTitle ( "IUHIUHU" ) ;
if ( paragraphsResult is NotFoundObjectResult NF ObjectResult)
if ( paragraphsResult is NotFoundObjectResult nf ObjectResult)
{
{
var valeur = NF ObjectResult. Value ;
var valeur = nf ObjectResult. Value ;
Assert . NotNull ( valeur ) ;
Assert . NotNull ( valeur ) ;
Assert . IsNotType < ParagraphDto > ( valeur ) ;
Assert . IsNotType < ParagraphDto > ( valeur ) ;
@ -166,49 +157,51 @@ namespace TestAPI
}
}
}
}
[Fact]
[Fact]
public void DeleteParagraphSuccess ( )
public void DeleteParagraphSuccess ( )
{
{
paragraphService. Setup ( x = > x . DeleteParagraph ( 1 ) )
_ paragraphService. Setup ( x = > x . DeleteParagraph ( 1 ) )
. Returns ( true ) ;
. Returns ( true ) ;
var ParagraphsController = new ParagraphsController ( paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsController =
new ParagraphsController ( _paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsResult = P aragraphsController. DeleteParagraph ( 1 ) ;
var paragraphsResult = p aragraphsController. DeleteParagraph ( 1 ) ;
if ( paragraphsResult is OkObjectResult okObjectResult )
if ( paragraphsResult is OkObjectResult okObjectResult )
{
{
bool valeur = ( bool ) okObjectResult . Value ;
bool valeur = ( bool ) okObjectResult . Value ;
Assert . True ( valeur ) ;
Assert . True ( valeur ) ;
}
}
}
}
[Fact]
[Fact]
public void DeleteParagraphFail ( )
public void DeleteParagraphFail ( )
{
{
paragraphService. Setup ( x = > x . DeleteParagraph ( 1 ) )
_ paragraphService. Setup ( x = > x . DeleteParagraph ( 1 ) )
. Returns ( true ) ;
. Returns ( true ) ;
var ParagraphsController = new ParagraphsController ( paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsController =
new ParagraphsController ( _paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsResult = P aragraphsController. DeleteParagraph ( 100 ) ;
var paragraphsResult = p aragraphsController. DeleteParagraph ( 100 ) ;
if ( paragraphsResult is NotFoundObjectResult NF ObjectResult)
if ( paragraphsResult is NotFoundObjectResult nf ObjectResult)
{
{
Assert . Null ( NF ObjectResult. Value ) ;
Assert . Null ( nf ObjectResult. Value ) ;
Assert . IsNotType < bool > ( NF ObjectResult. Value ) ;
Assert . IsNotType < bool > ( nf ObjectResult. Value ) ;
}
}
}
}
[Fact]
[Fact]
public void CreateParagraphSuccess ( )
public void CreateParagraphSuccess ( )
{
{
paragraphService . Setup ( x = > x . CreateParagraph ( "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" , "Commentaires" , 2 ) )
_paragraphService . Setup ( x = > x . CreateParagraph ( "Le nouveau titre" , "Le nouveau content" , "Les infos" ,
. Returns ( new ParagraphDto ( "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" , "Commentaires" , 2 ) ) ;
"La requête requêtante" , "Commentaires" , 2 ) )
var ParagraphsController = new ParagraphsController ( paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
. Returns ( new ParagraphDto ( "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" ,
"Commentaires" , 2 ) ) ;
var paragraphsResult = ParagraphsController . CreateParagraph ( new ParagraphDto ( "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" , "Commentaires" , 2 ) ) ;
var paragraphsController =
new ParagraphsController ( _paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsResult = paragraphsController . CreateParagraph ( new ParagraphDto ( "Le nouveau titre" ,
"Le nouveau content" , "Les infos" , "La requête requêtante" , "Commentaires" , 2 ) ) ;
if ( paragraphsResult is CreatedResult createdObjectResult )
if ( paragraphsResult is CreatedResult createdObjectResult )
{
{
ParagraphDto valeur = createdObjectResult . Value as ParagraphDto ;
ParagraphDto valeur = createdObjectResult . Value as ParagraphDto ;
@ -221,34 +214,41 @@ namespace TestAPI
Assert . Equal ( "Commentaires" , valeur . Comment ) ;
Assert . Equal ( "Commentaires" , valeur . Comment ) ;
Assert . Equal ( 2 , valeur . LessonId ) ;
Assert . Equal ( 2 , valeur . LessonId ) ;
}
}
}
}
[Fact]
[Fact]
public void CreateParagraphFail ( )
public void CreateParagraphFail ( )
{
{
paragraphService . Setup ( x = > x . CreateParagraph ( "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" , "Commentaires" , 2 ) )
_paragraphService . Setup ( x = > x . CreateParagraph ( "Le nouveau titre" , "Le nouveau content" , "Les infos" ,
. Returns ( new ParagraphDto ( "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" , "Commentaires" , 2 ) ) ;
"La requête requêtante" , "Commentaires" , 2 ) )
var ParagraphsController = new ParagraphsController ( paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
. Returns ( new ParagraphDto ( "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" ,
"Commentaires" , 2 ) ) ;
var paragraphsController =
new ParagraphsController ( _paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsResult = ParagraphsController . CreateParagraph ( new ParagraphDto ( null , "Le nouveau content" , "Les infos" , "La requête requêtante" , "Commentaires" , 2 ) ) ;
var paragraphsResult = paragraphsController . CreateParagraph ( new ParagraphDto ( null , "Le nouveau content" ,
"Les infos" , "La requête requêtante" , "Commentaires" , 2 ) ) ;
if ( paragraphsResult is BadRequestResult BD ObjectResult)
if ( paragraphsResult is BadRequestResult bd ObjectResult)
{
{
Assert . Equal ( 400 , bdObjectResult . StatusCode ) ;
Assert . Equal ( 400 , BDObjectResult . StatusCode ) ;
}
}
}
}
[Fact]
[Fact]
public void UpdateParagraphSuccess ( )
public void UpdateParagraphSuccess ( )
{
{
paragraphService . Setup ( x = > x . UpdateParagraph ( 1 , new ParagraphDto ( 1 , "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" , "Commentaires" , 2 ) ) )
_paragraphService . Setup ( x = > x . UpdateParagraph ( 1 ,
. Returns ( new ParagraphDto ( 1 , "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" , "Commentaires" , 2 ) ) ;
new ParagraphDto ( 1 , "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" ,
var ParagraphsController = new ParagraphsController ( paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
"Commentaires" , 2 ) ) )
. Returns ( new ParagraphDto ( 1 , "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" ,
var paragraphsResult = ParagraphsController . UpdateParagraph ( 1 , new ParagraphDto ( 1 , "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" , "Commentaires" , 2 ) ) ;
"Commentaires" , 2 ) ) ;
var paragraphsController =
new ParagraphsController ( _paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsResult = paragraphsController . UpdateParagraph ( 1 ,
new ParagraphDto ( 1 , "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" ,
"Commentaires" , 2 ) ) ;
if ( paragraphsResult is OkObjectResult okObjectResult )
if ( paragraphsResult is OkObjectResult okObjectResult )
{
{
ParagraphDto valeur = okObjectResult . Value as ParagraphDto ;
ParagraphDto valeur = okObjectResult . Value as ParagraphDto ;
@ -261,41 +261,40 @@ namespace TestAPI
Assert . Equal ( "Commentaires" , valeur . Comment ) ;
Assert . Equal ( "Commentaires" , valeur . Comment ) ;
Assert . Equal ( 2 , valeur . LessonId ) ;
Assert . Equal ( 2 , valeur . LessonId ) ;
}
}
}
}
[Fact]
[Fact]
public void UpdateParagraphFail ( )
public void UpdateParagraphFail ( )
{
{
paragraphService . Setup ( x = > x . UpdateParagraph ( 1 , new ParagraphDto ( 1 , "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" , "Commentaires" , 2 ) ) )
_paragraphService . Setup ( x = > x . UpdateParagraph ( 1 ,
. Returns ( new ParagraphDto ( 1 , "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" , "Commentaires" , 2 ) ) ;
new ParagraphDto ( 1 , "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" ,
var ParagraphsController = new ParagraphsController ( paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
"Commentaires" , 2 ) ) )
. Returns ( new ParagraphDto ( 1 , "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" ,
var paragraphsResult = ParagraphsController . UpdateParagraph ( 1 , new ParagraphDto ( 2 , "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" , "Commentaires" , 2 ) ) ;
"Commentaires" , 2 ) ) ;
var paragraphsController =
if ( paragraphsResult is BadRequestResult BDObjectResult )
new ParagraphsController ( _paragraphService . Object , new NullLogger < ParagraphsController > ( ) ) ;
var paragraphsResult = paragraphsController . UpdateParagraph ( 1 ,
new ParagraphDto ( 2 , "Le nouveau titre" , "Le nouveau content" , "Les infos" , "La requête requêtante" ,
"Commentaires" , 2 ) ) ;
if ( paragraphsResult is BadRequestResult bdObjectResult )
{
{
Assert . Equal ( 400 , bdObjectResult . StatusCode ) ;
Assert . Equal ( 400 , BDObjectResult . StatusCode ) ;
}
}
}
}
private List < ParagraphDto > GetParagraphsData ( )
private List < ParagraphDto > GetParagraphsData ( )
{
{
List < ParagraphDto > paragraphsData = new List < ParagraphDto > ( 4 )
List < ParagraphDto > paragraphsData = new List < ParagraphDto > ( 4 )
{
{
new ( 0 , "Titre 1" , "Le contenu" , "Les infos de ce paragraphes sont " , "Select * from C#" , "Le commentaire" , 1 ) ,
new ( 0 , "Titre 1" , "Le contenu" , "Les infos de ce paragraphes sont " , "Select * from C#" , "Le commentaire" ,
new ( 1 , "Le titre" , "pas contenu" , "Pas d'infos ici" , "Delete * from Earth" , "Miam" , 2 ) ,
1 ) ,
new ( 2 , "Ceci n'est pas un titre" , "Certainement hmmm" , "OUOOOOO " , "Autocommit = {true}" , "First" , 1 ) ,
new ( 1 , "Le titre" , "pas contenu" , "Pas d'infos ici" , "Delete * from Earth" , "Miam" , 2 ) ,
new ( "Title" , "Content" , "Je ne parle pas anglais" , "Select select from select" , "Mais qui est ce commentaire" , 3 ) ,
new ( 2 , "Ceci n'est pas un titre" , "Certainement hmmm" , "OUOOOOO " , "Autocommit = {true}" , "First" , 1 ) ,
new ( "Title" , "Content" , "Je ne parle pas anglais" , "Select select from select" ,
"Mais qui est ce commentaire" , 3 ) ,
} ;
} ;
return paragraphsData ;
return paragraphsData ;
}
}
}
}
}