Ajout des tests unitaires pour le UpdateNote et le SupprimerNote de NoteService

develop
Yanaël GRETTE 4 years ago
parent bcdd8faa83
commit f2b9db54e8
  1. 128
      EPAServeur.Tests/Services/NoteServiceTests.cs
  2. 9
      EPAServeur/Controllers/NotesApi.cs
  3. 2
      EPAServeur/IServices/INoteService.cs
  4. 8
      EPAServeur/Services/NoteService.cs

@ -370,14 +370,140 @@ namespace EPAServeur.Tests.Services
#region Test Supprimer Note #region Test Supprimer Note
[TestCase(5)]
[TestCase(7)]
[TestCase(8)]
public async Task SupprimerNote_Succes(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
await noteService.SupprimerNoteAsync(id);
Note note = await context.Note.FindAsync(id);
Assert.IsNull(note);
}
[TestCase(0)]
[TestCase(-1)]
[TestCase(-999)]
[TestCase(20)]
[TestCase(30)]
[TestCase(100)]
public void SupprimerNote_NoteNotFoundException(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
AsyncTestDelegate exception = () => noteService.SupprimerNoteAsync(id);
Assert.ThrowsAsync(typeof(NoteNotFoundException), exception);
}
#endregion #endregion
#region Tests UpdateNote #region Tests UpdateNote
[TestCase(1)]
[TestCase(2)]
[TestCase(6)]
[TestCase(9)]
public async Task UpdateNote_UpdateTitreEtTexte(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DetailsNoteDTO note = await noteService.GetNoteByIdAsync(id);
note.Titre = "Titre à jour " + id;
note.Texte= "Texte à jour " + id;
DetailsNoteDTO noteUpdate = await noteService.UpdateNoteAsync(id, note);
Assert.IsNotNull(noteUpdate);
Assert.AreEqual(note.Id, noteUpdate.Id);
Assert.AreEqual(note.IdAuteur, noteUpdate.IdAuteur);
Assert.AreEqual(note.Collaborateur.Id, noteUpdate.Collaborateur.Id);
Assert.AreEqual(note.DateCreation, noteUpdate.DateCreation);
Assert.AreEqual(note.Titre, noteUpdate.Titre);
Assert.AreEqual(note.Texte, noteUpdate.Texte);
Assert.AreNotEqual(note.DateMiseAjour, noteUpdate.DateMiseAjour);
}
[TestCase(1)]
[TestCase(2)]
[TestCase(6)]
[TestCase(9)]
public async Task UpdateNote_UpdateTexte(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DetailsNoteDTO note = await noteService.GetNoteByIdAsync(id);
note.Texte = "Texte à jour " + id;
DetailsNoteDTO noteUpdate = await noteService.UpdateNoteAsync(id, note);
Assert.IsNotNull(noteUpdate);
Assert.AreEqual(note.Id, noteUpdate.Id);
Assert.AreEqual(note.IdAuteur, noteUpdate.IdAuteur);
Assert.AreEqual(note.Collaborateur.Id, noteUpdate.Collaborateur.Id);
Assert.AreEqual(note.DateCreation, noteUpdate.DateCreation);
Assert.AreEqual(note.Titre, noteUpdate.Titre);
Assert.AreEqual(note.Texte, noteUpdate.Texte);
Assert.AreNotEqual(note.DateMiseAjour, noteUpdate.DateMiseAjour);
}
[TestCase(1)]
[TestCase(2)]
[TestCase(6)]
[TestCase(9)]
public async Task UpdateNote_UpdateTitre(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DetailsNoteDTO note = await noteService.GetNoteByIdAsync(id);
note.Titre = "Titre à jour " + id;
DetailsNoteDTO noteUpdate = await noteService.UpdateNoteAsync(id, note);
Assert.IsNotNull(noteUpdate);
Assert.AreEqual(note.Id, noteUpdate.Id);
Assert.AreEqual(note.IdAuteur, noteUpdate.IdAuteur);
Assert.AreEqual(note.Collaborateur.Id, noteUpdate.Collaborateur.Id);
Assert.AreEqual(note.DateCreation, noteUpdate.DateCreation);
Assert.AreEqual(note.Titre, noteUpdate.Titre);
Assert.AreEqual(note.Texte, noteUpdate.Texte);
Assert.AreNotEqual(note.DateMiseAjour, noteUpdate.DateMiseAjour);
}
[TestCase(1,2)]
[TestCase(1,3)]
[TestCase(2,4)]
[TestCase(2,5)]
[TestCase(9,6)]
[TestCase(9,7)]
public async Task UpdateNote_NoteIdImcompatibleExceptionId(long id, long mauvaisId)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DetailsNoteDTO note = await noteService.GetNoteByIdAsync(id);
note.Titre = "Titre à jour " + id;
note.Texte = "Texte à jour " + id;
AsyncTestDelegate exception = () => noteService.UpdateNoteAsync(mauvaisId, note);
Assert.ThrowsAsync(typeof(NoteIdImcompatibleException), exception);
}
[TestCase(1)]
[TestCase(2)]
[TestCase(6)]
[TestCase(9)]
public async Task UpdateNote_NoteIdImcompatibleExceptionDate(long id)
{
INoteService noteService = new NoteService(collaborateurApi, collaborateurService, context);
DetailsNoteDTO note = await noteService.GetNoteByIdAsync(id);
note.Titre = "Titre à jour " + id;
note.Texte = "Texte à jour " + id;
note.DateCreation = DateTime.Now;
AsyncTestDelegate exception = () => noteService.UpdateNoteAsync(id, note);
Assert.ThrowsAsync(typeof(NoteIdImcompatibleException), exception);
}
#endregion #endregion
#region Tests GetNoteByAuteur #region Tests GetNoteByAuteur
#endregion #endregion.
} }

@ -186,6 +186,15 @@ namespace IO.Swagger.Controllers
{ {
noteService.SupprimerNoteAsync(idNote); noteService.SupprimerNoteAsync(idNote);
} }
catch(NoteNotFoundException e)
{
ErreurDTO erreur = new ErreurDTO()
{
Code = 404,
Message = e.Message
};
return NotFound(erreur);
}
catch(DbUpdateException e) catch(DbUpdateException e)
{ {
logger.LogError(e.Message); logger.LogError(e.Message);

@ -11,7 +11,7 @@ namespace EPAServeur.IServices
public interface INoteService public interface INoteService
{ {
Task<DetailsNoteDTO> AjouterNoteAsync(DetailsNoteDTO note); Task<DetailsNoteDTO> AjouterNoteAsync(DetailsNoteDTO note);
void SupprimerNoteAsync(long? idNote); Task<bool> SupprimerNoteAsync(long? idNote);
Task<DetailsNoteDTO> GetNoteByIdAsync(long? idNote); Task<DetailsNoteDTO> GetNoteByIdAsync(long? idNote);
Task<IEnumerable<AffichageNoteDTO>> GetNotesByAuteurAsync(Guid? idAuteur, bool? asc, int? numPage, int? parPAge, string texte, string tri); Task<IEnumerable<AffichageNoteDTO>> GetNotesByAuteurAsync(Guid? idAuteur, bool? asc, int? numPage, int? parPAge, string texte, string tri);
Task<int> GetNotesByAuteurCountAsync(Guid? idAuteur, string texte); Task<int> GetNotesByAuteurCountAsync(Guid? idAuteur, string texte);

@ -76,14 +76,16 @@ namespace EPAServeur.Services
/// Supprimer une note en fonction de son Id de manière async /// Supprimer une note en fonction de son Id de manière async
/// </summary> /// </summary>
/// <param name="idNote">Id de la note à supprimer</param> /// <param name="idNote">Id de la note à supprimer</param>
public async void SupprimerNoteAsync(long? idNote) public async Task<bool> SupprimerNoteAsync(long? idNote)
{ {
Note note = await context.Note.FindAsync(idNote); Note note = await context.Note.FindAsync(idNote);
if (note != null) if (note == null)
{ {
throw new NoteNotFoundException("Impossible de supprimer la note car elle n'a pas été trouvée");
}
context.Remove(note); context.Remove(note);
await context.SaveChangesAsync(); await context.SaveChangesAsync();
} return true;
} }

Loading…
Cancel
Save