Ajout des test unitaires pour les nouveaux controller et services
continuous-integration/drone/push Build is passing Details

deploiement^2
Johnny RATTON 1 year ago
parent 08bdb01b18
commit 18e7df02f3

@ -26,7 +26,7 @@ public class InquiryTableController : Controller
[HttpGet("database/{id:int}")] [HttpGet("database/{id:int}")]
[ProducesResponseType(typeof(string), 200)] [ProducesResponseType(typeof(string), 200)]
[ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 404)]
public IActionResult GetSolutionByInquiryById(int id) public IActionResult GetDatabaseNameByInquiryById(int id)
{ {
try try
{ {

@ -50,7 +50,7 @@ public class NotepadController : Controller
{ {
try try
{ {
if (notepad.InquiryId == null || notepad.UserId == null || notepad.Notes == null) if (notepad.InquiryId < 0 || notepad.UserId < 0 || notepad.Notes == null)
{ {
return BadRequest(); return BadRequest();
} }
@ -76,7 +76,7 @@ public class NotepadController : Controller
{ {
try try
{ {
if (notepad.InquiryId == null || notepad.UserId == null || notepad.Notes == null) if (notepad.InquiryId < 0 || notepad.UserId < 0 || notepad.Notes == null)
{ {
return BadRequest(); return BadRequest();
} }

@ -0,0 +1,51 @@
using API.Controllers;
using Dto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Shared;
namespace TestAPI;
public class InquiryTableUnitTest
{
private readonly Mock<IInquiryTableService<InquiryTableDto>> _inquiryTableService;
public InquiryTableUnitTest()
{
_inquiryTableService = new Mock<IInquiryTableService<InquiryTableDto>>();
}
[Fact]
public void GetDatabaseNameFromInquiryId_Success()
{
var database = "Inquiry1";
_inquiryTableService.Setup(x => x.GetDatabaseNameByInquiryId(42))
.Returns(database);
var inquiryTableController =
new InquiryTableController(_inquiryTableService.Object, new NullLogger<InquiryTableController>());
var inquiryTableResult = inquiryTableController.GetDatabaseNameByInquiryById(42);
if (inquiryTableResult is OkObjectResult okObjectResult)
{
var valeur = okObjectResult.Value;
Assert.NotNull(valeur);
Assert.Equal(database, ((KeyValuePair<string,string>)valeur).Value);
}
}
[Fact]
public void GetDatabaseNameFromInquiryId_Throws_ArgumentException()
{
_inquiryTableService.Setup(x => x.GetDatabaseNameByInquiryId(42))
.Throws<ArgumentException>();
var inquiryTableController =
new InquiryTableController(_inquiryTableService.Object, new NullLogger<InquiryTableController>());
var inquiryTableResult = inquiryTableController.GetDatabaseNameByInquiryById(42);
Assert.NotNull(inquiryTableResult);
Assert.Equal(typeof(NotFoundResult), inquiryTableResult.GetType());
}
}

@ -0,0 +1,183 @@
using API.Controllers;
using Dto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Shared;
using TestAPI.Extensions;
namespace TestAPI;
public class NotepadUnitTest
{
private readonly Mock<INotepadService<NotepadDto>> _notepadService;
public NotepadUnitTest()
{
_notepadService = new Mock<INotepadService<NotepadDto>>();
}
[Fact]
public void GetNotepadFromUserAndInquiryId()
{
var notepad = new NotepadDto(42, 42, "These are notes example.");
_notepadService.Setup(x => x.GetNotepadFromUserAndInquiryId(42, 42))
.Returns(notepad);
var notepadController =
new NotepadController(_notepadService.Object, new NullLogger<NotepadController>());
var notepadResult = notepadController.GetNotepadByUserAndInquiryById(42,42);
if (notepadResult is OkObjectResult okObjectResult)
{
var valeur = okObjectResult.Value;
Assert.NotNull(valeur);
Assert.Equal(notepad, valeur);
}
}
[Fact]
public void GetNotepadFromUserAndInquiryId_ThrowingArgumentException()
{
var notepad = new NotepadDto(42, 42, "These are notes example.");
_notepadService.Setup(x => x.GetNotepadFromUserAndInquiryId(42, 10))
.Throws<ArgumentException>();
var notepadController =
new NotepadController(_notepadService.Object, new NullLogger<NotepadController>());
var notepadResult = notepadController.GetNotepadByUserAndInquiryById(42,10);
Assert.NotNull(notepadResult);
Assert.Equal(typeof(NotFoundResult),notepadResult.GetType());
}
[Fact]
public void SetNotepadFromUserAndInquiryId_Success()
{
_notepadService.Setup(x => x.SetNotepadFromUserAndInquiryId(42, 42,"These are notes example."));
var notepadController =
new NotepadController(_notepadService.Object, new NullLogger<NotepadController>());
var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, "These are notes example."));
Assert.NotNull(notepadResult);
Assert.Equal(typeof(OkResult),notepadResult.GetType());
Assert.Equal(typeof(OkObjectResult) , notepadController.GetNotepadByUserAndInquiryById(42,42).GetType());
}
[Fact]
public void SetNotepadFromUserAndInquiryId_Negative_UserId()
{
_notepadService.Setup(x => x.SetNotepadFromUserAndInquiryId(-42, 42,"These are notes example.")).Throws<ArgumentException>();
var notepadController =
new NotepadController(_notepadService.Object, new NullLogger<NotepadController>());
var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(-42,42, "These are notes example."));
Assert.NotNull(notepadResult);
Assert.Equal(typeof(BadRequestResult),notepadResult.GetType());
}
[Fact]
public void SetNotepadFromUserAndInquiryId_Negative_InquiryId()
{
_notepadService.Setup(x => x.SetNotepadFromUserAndInquiryId(42, -42,"These are notes example.")).Throws<ArgumentException>();
var notepadController =
new NotepadController(_notepadService.Object, new NullLogger<NotepadController>());
var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,-42, "These are notes example."));
Assert.NotNull(notepadResult);
Assert.Equal(typeof(BadRequestResult),notepadResult.GetType());
}
[Fact]
public void SetNotepadFromUserAndInquiryId_Null_Notes()
{
_notepadService.Setup(x => x.SetNotepadFromUserAndInquiryId(42, 42,"These are notes example.")).Throws<ArgumentException>();
var notepadController =
new NotepadController(_notepadService.Object, new NullLogger<NotepadController>());
var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, null));
Assert.NotNull(notepadResult);
Assert.Equal(typeof(BadRequestResult),notepadResult.GetType());
}
[Fact]
public void SetNotepadFromUserAndInquiryId_Throws_ArgumentException()
{
var notepad = new NotepadDto(42, 42, "These are notes example.");
_notepadService.Setup(x => x.SetNotepadFromUserAndInquiryId(42, 42,"These are notes example.")).Throws<ArgumentException>();
var notepadController =
new NotepadController(_notepadService.Object, new NullLogger<NotepadController>());
var notepadResult = notepadController.SetNotepadByUserAndInquiryById(notepad);
Assert.NotNull(notepadResult);
Assert.Equal(typeof(NotFoundResult),notepadResult.GetType());
}
[Fact]
public void UpdateNotepadFromUserAndInquiryId_Success()
{
_notepadService.Setup(x => x.UpdateNotepadFromUserAndInquiryId(42, 42,"These are the new notes"));
var notepadController =
new NotepadController(_notepadService.Object, new NullLogger<NotepadController>());
var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, "These are notes example."));
Assert.NotNull(notepadResult);
Assert.Equal(typeof(OkResult),notepadResult.GetType());
Assert.Equal(typeof(OkObjectResult) , notepadController.GetNotepadByUserAndInquiryById(42,42).GetType());
var updateResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, "These are the new notes"));
Assert.NotNull(updateResult);
Assert.Equal(typeof(OkResult),updateResult.GetType());
}
[Fact]
public void UpdateNotepadFromUserAndInquiryId_Negative_UserId()
{
_notepadService.Setup(x => x.UpdateNotepadFromUserAndInquiryId(42, 42,"These are the new notes"));
var notepadController =
new NotepadController(_notepadService.Object, new NullLogger<NotepadController>());
var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, "These are notes example."));
var updateResult = notepadController.UpdateNotepadByUserAndInquiryById(new NotepadDto(-42,42, "These are the new notes"));
Assert.NotNull(updateResult);
Assert.Equal(typeof(BadRequestResult),updateResult.GetType());
}
[Fact]
public void UpdateNotepadFromUserAndInquiryId_Negative_InquiryId()
{
_notepadService.Setup(x => x.UpdateNotepadFromUserAndInquiryId(42, -42,"These are the new notes"));
var notepadController =
new NotepadController(_notepadService.Object, new NullLogger<NotepadController>());
var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, "These are notes example."));
var updateResult = notepadController.UpdateNotepadByUserAndInquiryById(new NotepadDto(42,-42, "These are the new notes"));
Assert.NotNull(updateResult);
Assert.Equal(typeof(BadRequestResult),updateResult.GetType());
}
[Fact]
public void UpdateNotepadFromUserAndInquiryId_Null_Notes()
{
_notepadService.Setup(x => x.UpdateNotepadFromUserAndInquiryId(42, 42,null)).Throws<ArgumentException>();
var notepadController =
new NotepadController(_notepadService.Object, new NullLogger<NotepadController>());
var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, "These are notes example."));
var updateResult = notepadController.UpdateNotepadByUserAndInquiryById(new NotepadDto(42,42, null));
Assert.NotNull(updateResult);
Assert.Equal(typeof(BadRequestResult),updateResult.GetType());
}
[Fact]
public void UpdateNotepadFromUserAndInquiryId_Throws_ArgumentException()
{
_notepadService.Setup(x => x.UpdateNotepadFromUserAndInquiryId(42, 10,"These are the new notes")).Throws<ArgumentException>();
var notepadController =
new NotepadController(_notepadService.Object, new NullLogger<NotepadController>());
var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, "These are notes example."));
var updateResult = notepadController.UpdateNotepadByUserAndInquiryById(new NotepadDto(42,10, "These are the new notes"));
Assert.NotNull(updateResult);
Assert.Equal(typeof(NotFoundResult),updateResult.GetType());
}
}

@ -0,0 +1,52 @@
using API.Controllers;
using Dto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Shared;
namespace TestAPI;
public class SolutionUnitTest
{
private readonly Mock<ISolutionService<SolutionDto>> _solutionService;
public SolutionUnitTest()
{
_solutionService = new Mock<ISolutionService<SolutionDto>>();
}
[Fact]
public void GetSolutionFromInquiryId_Success()
{
var solution = new SolutionDto(42,"Maxime","Sapountzis","La cuisine","Le couteau", "L'explication");
_solutionService.Setup(x => x.GetSolutionByInquiryId(42))
.Returns(solution);
var solutionController =
new SolutionController(_solutionService.Object, new NullLogger<SolutionController>());
var solutionResult = solutionController.GetSolutionByInquiryById(42);
if (solutionResult is OkObjectResult okObjectResult)
{
var valeur = okObjectResult.Value;
Assert.NotNull(valeur);
Assert.Equal(solution, valeur);
}
}
[Fact]
public void GetSolutionFromInquiryId_Throws_ArgumentException()
{
var solution = new SolutionDto(42,"Maxime","Sapountzis","La cuisine","Le couteau", "L'explication");
_solutionService.Setup(x => x.GetSolutionByInquiryId(42))
.Throws<ArgumentException>();
var solutionController =
new SolutionController(_solutionService.Object, new NullLogger<SolutionController>());
var solutionResult = solutionController.GetSolutionByInquiryById(42);
Assert.NotNull(solutionResult);
Assert.Equal(typeof(NotFoundResult), solutionResult.GetType());
}
}

@ -0,0 +1,62 @@
using DbContextLib;
using DbDataManager.Service;
using Entities;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
namespace TestEF.Service;
public class TestInquiryTableDataService
{
private readonly UserDbContext _dbContext;
private readonly InquiryTableDataService _inquiryTableDataService;
public TestInquiryTableDataService()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<UserDbContext>()
.UseSqlite(connection)
.Options;
_dbContext = new UserDbContext(options);
_inquiryTableDataService = new InquiryTableDataService(_dbContext);
}
[Fact]
public void GetDatabaseFromInquiryId_Success()
{
_dbContext.Inquiries.Add(new InquiryEntity
{ Id = 42, Title = "Titre", Description = "Description", IsUser = false });
var inquiryTable = new InquiryTableEntity()
{
OwnerId = 42,
DatabaseName = "Database",
ConnectionInfo = "ConnectionString"
};
_dbContext.InquiryTables.Add(inquiryTable);
_dbContext.SaveChanges();
var result = _inquiryTableDataService.GetDatabaseNameByInquiryId(42);
Assert.Equal("Database",result);
}
[Fact]
public void GetSolution_NotFound()
{
_dbContext.Inquiries.Add(new InquiryEntity
{ Id = 42, Title = "Titre", Description = "Description", IsUser = false });
var inquiryTable = new InquiryTableEntity()
{
OwnerId = 42,
DatabaseName = "Database",
ConnectionInfo = "ConnectionString"
};
_dbContext.InquiryTables.Add(inquiryTable);
_dbContext.SaveChanges();
Assert.Throws<ArgumentException>(() =>
{
_inquiryTableDataService.GetDatabaseNameByInquiryId(10);
});
}
}

@ -0,0 +1,169 @@
using DbContextLib;
using DbDataManager.Service;
using Entities;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Model.OrderCriteria;
namespace TestEF.Service;
public class TestNotepadDataService
{
private readonly UserDbContext _dbContext;
private readonly NotepadDataService _notepadDataService;
public TestNotepadDataService()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<UserDbContext>()
.UseSqlite(connection)
.Options;
_dbContext = new UserDbContext(options);
_notepadDataService = new NotepadDataService(_dbContext);
}
[Fact]
public void GetNotepad_Success()
{
_dbContext.Inquiries.Add(new InquiryEntity
{ Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false });
_dbContext.Users.Add(new UserEntity
{ Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true });
var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "These are some notes" };
_dbContext.Notepads.Add(notepad);
_dbContext.SaveChanges();
var result = _notepadDataService.GetNotepadFromUserAndInquiryId(42, 42);
Assert.Equal(notepad,result);
}
[Fact]
public void GetNotepad_Fail_UserNotFound()
{
_dbContext.Inquiries.Add(new InquiryEntity
{ Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false });
_dbContext.Users.Add(new UserEntity
{ Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true });
var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "These are some notes" };
_dbContext.Notepads.Add(notepad);
_dbContext.SaveChanges();
Assert.Throws<ArgumentException>(() =>
{
_notepadDataService.GetNotepadFromUserAndInquiryId(10, 42);
});
}
[Fact]
public void GetNotepad_Fail_InquiryNotFound()
{
_dbContext.Inquiries.Add(new InquiryEntity
{ Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false });
_dbContext.Users.Add(new UserEntity
{ Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true });
var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "These are some notes" };
_dbContext.Notepads.Add(notepad);
_dbContext.SaveChanges();
Assert.Throws<ArgumentException>(() =>
{
_notepadDataService.GetNotepadFromUserAndInquiryId(42, 10);
});
}
[Fact]
public void GetNotepad_Fail_NotepadNotFound()
{
_dbContext.Inquiries.Add(new InquiryEntity
{ Id = 1, Title = "Titre", Description = "Descritpion", IsUser = false });
_dbContext.Users.Add(new UserEntity
{ Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges();
Assert.Throws<ArgumentException>(() =>
{
_notepadDataService.GetNotepadFromUserAndInquiryId(42, 1);
});
}
[Fact]
public void SetNotepad_Success()
{
_dbContext.Inquiries.Add(new InquiryEntity
{ Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false });
_dbContext.Users.Add(new UserEntity
{ Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true });
_dbContext.SaveChanges();
var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "Some notes again" };
_notepadDataService.SetNotepadFromUserAndInquiryId(42,42, "Some notes again");
_dbContext.SaveChanges();
var result = _notepadDataService.GetNotepadFromUserAndInquiryId(42, 42);
Assert.Equal(notepad.UserId,result.UserId);
Assert.Equal(notepad.InquiryId,result.InquiryId);
Assert.Equal(notepad.Notes,result.Notes);
}
[Fact]
public void SetNotepad_Fail_UserNotFound()
{
_dbContext.Inquiries.Add(new InquiryEntity
{ Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false });
_dbContext.Users.Add(new UserEntity
{ Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true });
var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "These are some notes" };
_dbContext.SaveChanges();
Assert.Throws<ArgumentException>(() =>
{
_notepadDataService.SetNotepadFromUserAndInquiryId(10, 42, "Some notes");
});
}
[Fact]
public void SetNotepad_Fail_InquiryNotFound()
{
_dbContext.Inquiries.Add(new InquiryEntity
{ Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false });
_dbContext.Users.Add(new UserEntity
{ Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true });
var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "These are some notes" };
_dbContext.SaveChanges();
Assert.Throws<ArgumentException>(() =>
{
_notepadDataService.SetNotepadFromUserAndInquiryId(42, 10, "Some notes");
});
}
[Fact]
public void SetNotepad_Fail_NotepadAlreadyExists()
{
_dbContext.Inquiries.Add(new InquiryEntity
{ Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false });
_dbContext.Users.Add(new UserEntity
{ Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true });
var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "Some notes" };
_dbContext.Notepads.Add(notepad);
_dbContext.SaveChanges();
Assert.Throws<ArgumentException>(() =>
{
_notepadDataService.SetNotepadFromUserAndInquiryId(42, 42, "Some notes");
});
}
[Fact]
public void UpdateNotepad_Success()
{
_dbContext.Inquiries.Add(new InquiryEntity
{ Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false });
_dbContext.Users.Add(new UserEntity
{ Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true });
var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "Some notes" };
_dbContext.Notepads.Add(notepad);
_dbContext.SaveChanges();
_notepadDataService.UpdateNotepadFromUserAndInquiryId(42,42, "New Notes");
_dbContext.SaveChanges();
Assert.Equal("New Notes", _notepadDataService.GetNotepadFromUserAndInquiryId(42,42).Notes);
}
}

@ -0,0 +1,75 @@
using DbContextLib;
using DbDataManager.Service;
using Entities;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
namespace TestEF.Service;
public class TestSolutionDataService
{
private readonly UserDbContext _dbContext;
private readonly SolutionDataService _solutionDataService;
public TestSolutionDataService()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<UserDbContext>()
.UseSqlite(connection)
.Options;
_dbContext = new UserDbContext(options);
_solutionDataService = new SolutionDataService(_dbContext);
}
[Fact]
public void GetSolution_Success()
{
_dbContext.Inquiries.Add(new InquiryEntity
{ Id = 42, Title = "Titre", Description = "Description", IsUser = false });
var solution = new SolutionEntity
{
MurdererFirstName = "Maxime",
MurdererLastName = "Sapountzis",
MurderPlace = "La cuisine",
MurderWeapon = "Le couteau",
Explaination = "Parce que",
OwnerId = 42,
};
_dbContext.Solutions.Add(solution);
_dbContext.SaveChanges();
var result = _solutionDataService.GetSolutionByInquiryId(42);
Assert.Equal(solution.MurdererFirstName,result.MurdererFirstName);
Assert.Equal(solution.MurdererLastName,result.MurdererLastName);
Assert.Equal(solution.MurderPlace,result.MurderPlace);
Assert.Equal(solution.MurderWeapon,result.MurderWeapon);
Assert.Equal(solution.Explaination,result.Explaination);
Assert.Equal(solution.OwnerId,result.OwnerId);
}
[Fact]
public void GetSolution_NotFound()
{
_dbContext.Inquiries.Add(new InquiryEntity
{ Id = 42, Title = "Titre", Description = "Description", IsUser = false });
var solution = new SolutionEntity
{
MurdererFirstName = "Maxime",
MurdererLastName = "Sapountzis",
MurderPlace = "La cuisine",
MurderWeapon = "Le couteau",
Explaination = "Parce que",
OwnerId = 42,
};
_dbContext.Solutions.Add(solution);
_dbContext.SaveChanges();
Assert.Throws<ArgumentException>(() =>
{
_solutionDataService.GetSolutionByInquiryId(10);
});
}
}
Loading…
Cancel
Save