Add some Tests
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
8bfaa37705
commit
205919227c
@ -0,0 +1,29 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
|
||||
<PackageReference Include="xunit" Version="2.4.2"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\API_Mapping\API_Mapping.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1 @@
|
||||
global using Xunit;
|
@ -0,0 +1,111 @@
|
||||
using API.Controllers;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Model;
|
||||
using Moq;
|
||||
|
||||
namespace API_Unit_Test;
|
||||
|
||||
public class ArticleControllerTests
|
||||
{
|
||||
private readonly Mock<IDataManager> _mockDataManager;
|
||||
private readonly Mock<ILogger<ArticleController>> _mockLogger;
|
||||
private readonly ArticleController _controller;
|
||||
|
||||
public ArticleControllerTests()
|
||||
{
|
||||
_mockDataManager = new Mock<IDataManager>();
|
||||
_mockLogger = new Mock<ILogger<ArticleController>>();
|
||||
_controller = new ArticleController(_mockDataManager.Object, _mockLogger.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllArticles_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
_mockDataManager.Setup(dm => dm.ArticleService.GetAllArticles(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<ArticleOrderCriteria>()))
|
||||
.ReturnsAsync(new List<Article>());
|
||||
|
||||
// Act
|
||||
var result = await _controller.GetAllArticles();
|
||||
|
||||
// Assert
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetArticleById_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var testArticleId = 1;
|
||||
_mockDataManager.Setup(dm => dm.ArticleService.GetArticleById(testArticleId))
|
||||
.ReturnsAsync(new Article { Id = testArticleId });
|
||||
|
||||
// Act
|
||||
var result = await _controller.GetArticleById(testArticleId);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateArticle_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var article = new Article { Title = "Test" };
|
||||
_mockDataManager.Setup(dm => dm.ArticleService.CreateArticle(article))
|
||||
.ReturnsAsync(article);
|
||||
|
||||
// Act
|
||||
var result = await _controller.CreateArticle(article);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteArticle_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var testArticleId = 1;
|
||||
_mockDataManager.Setup(dm => dm.ArticleService.DeleteArticle(testArticleId))
|
||||
.ReturnsAsync(new Article { Id = testArticleId });
|
||||
|
||||
// Act
|
||||
var result = await _controller.DeleteArticle(testArticleId);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateArticle_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var testArticleId = 1;
|
||||
var article = new Article { Title = "Updated" };
|
||||
_mockDataManager.Setup(dm => dm.ArticleService.UpdateArticle(testArticleId, article))
|
||||
.ReturnsAsync(article);
|
||||
|
||||
// Act
|
||||
var result = await _controller.UpdateArticle(testArticleId, article);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllArticles_ThrowsException_ReturnsBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
_mockDataManager.Setup(dm => dm.ArticleService.GetAllArticles(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<ArticleOrderCriteria>()))
|
||||
.ThrowsAsync(new Exception("Test exception"));
|
||||
|
||||
// Act
|
||||
var result = await _controller.GetAllArticles();
|
||||
|
||||
// Assert
|
||||
Assert.IsType<BadRequestObjectResult>(result);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
using Web_API.Model;
|
||||
|
||||
namespace API_Unit_Test;
|
||||
|
||||
public class ArticleDTOTests
|
||||
{
|
||||
[Fact]
|
||||
public void ArticleDTOPropertiesTest()
|
||||
{
|
||||
// Arrange
|
||||
var articleDTO = new ArticleDTO();
|
||||
var testId = 1L;
|
||||
var testTitle = "Test Title";
|
||||
var testDescription = "Test Description";
|
||||
var testDatePublished = "2024-03-16";
|
||||
var testLectureTime = 5;
|
||||
var testAuthor = "Test Author";
|
||||
|
||||
// Act
|
||||
articleDTO.Id = testId;
|
||||
articleDTO.Title = testTitle;
|
||||
articleDTO.Description = testDescription;
|
||||
articleDTO.DatePublished = testDatePublished;
|
||||
articleDTO.LectureTime = testLectureTime;
|
||||
articleDTO.Author = testAuthor;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(testId, articleDTO.Id);
|
||||
Assert.Equal(testTitle, articleDTO.Title);
|
||||
Assert.Equal(testDescription, articleDTO.Description);
|
||||
Assert.Equal(testDatePublished, articleDTO.DatePublished);
|
||||
Assert.Equal(testLectureTime, articleDTO.LectureTime);
|
||||
Assert.Equal(testAuthor, articleDTO.Author);
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
using API_Mapping;
|
||||
using Model;
|
||||
using Web_API.Model;
|
||||
|
||||
namespace API_UnitTest_Mapper;
|
||||
|
||||
public class ArticleMapperTests
|
||||
{
|
||||
[Fact]
|
||||
public void ToDTOMapsCorrectly()
|
||||
{
|
||||
var article = new Article
|
||||
{
|
||||
Id = 1,
|
||||
Title = "Test Article",
|
||||
Description = "Test Description",
|
||||
DatePublished = "2021-01-01",
|
||||
LectureTime = 5,
|
||||
Author = "Test Author"
|
||||
};
|
||||
var dto = ArticleMapper.ToDTO(article);
|
||||
|
||||
Assert.NotNull(dto);
|
||||
Assert.Equal(article.Id, dto.Id);
|
||||
Assert.Equal(article.Title, dto.Title);
|
||||
Assert.Equal(article.Description, dto.Description);
|
||||
Assert.Equal(article.DatePublished, dto.DatePublished);
|
||||
Assert.Equal(article.LectureTime, dto.LectureTime);
|
||||
Assert.Equal(article.Author, dto.Author);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToModelMapsCorrectly()
|
||||
{
|
||||
var dto = new ArticleDTO
|
||||
{
|
||||
Id = 2,
|
||||
Title = "Another Test Article",
|
||||
Description = "Another Test Description",
|
||||
DatePublished = "2021-01-02",
|
||||
LectureTime = 10,
|
||||
Author = "Another Test Author"
|
||||
};
|
||||
|
||||
var article = ArticleMapper.ToModel(dto);
|
||||
|
||||
Assert.NotNull(article);
|
||||
Assert.Equal(dto.Id, article.Id);
|
||||
Assert.Equal(dto.Title, article.Title);
|
||||
Assert.Equal(dto.Description, article.Description);
|
||||
Assert.Equal(dto.DatePublished, article.DatePublished);
|
||||
Assert.Equal(dto.LectureTime, article.LectureTime);
|
||||
Assert.Equal(dto.Author, article.Author);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToDTONullArticleThrowsNullReferenceException()
|
||||
{
|
||||
Article article = null;
|
||||
|
||||
Assert.Throws<NullReferenceException>(() => ArticleMapper.ToDTO(article));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToModelNullArticleDTOThrowsNullReferenceException()
|
||||
{
|
||||
ArticleDTO dto = null;
|
||||
|
||||
Assert.Throws<NullReferenceException>(() => ArticleMapper.ToModel(dto));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
using DbContextLib;
|
||||
using DbDataManager;
|
||||
using Moq;
|
||||
|
||||
namespace API_Unit_Test;
|
||||
|
||||
public class DbManagerTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_WithoutParameters_InitializesServices()
|
||||
{
|
||||
// Arrange & Act
|
||||
var dbManager = new DbManager();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(dbManager.ArticleService);
|
||||
Assert.NotNull(dbManager.UserService);
|
||||
Assert.NotNull(dbManager.FormulaireService);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithLibraryContextParameter_InitializesServicesWithGivenContext()
|
||||
{
|
||||
// Arrange
|
||||
var context = new Mock<LibraryContext>().Object;
|
||||
|
||||
// Act
|
||||
var dbManager = new DbManager(context);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(dbManager.ArticleService);
|
||||
Assert.NotNull(dbManager.UserService);
|
||||
Assert.NotNull(dbManager.FormulaireService);
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
using DbDataManager;
|
||||
using Entities;
|
||||
using Model;
|
||||
|
||||
namespace API_Unit_Test;
|
||||
|
||||
public class ExtensionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void ArticleToEntityMapsCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
var article = new Article
|
||||
{
|
||||
Id = 1,
|
||||
Author = "Author",
|
||||
Description = "Description",
|
||||
Title = "Title",
|
||||
DatePublished = "2021-01-01",
|
||||
LectureTime = 10
|
||||
};
|
||||
|
||||
// Act
|
||||
var entity = article.ToEntity();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(entity);
|
||||
Assert.Equal(article.Id, entity.Id);
|
||||
Assert.Equal(article.Author, entity.Author);
|
||||
Assert.Equal(article.Description, entity.Description);
|
||||
Assert.Equal(article.Title, entity.Title);
|
||||
Assert.Equal(article.DatePublished, entity.DatePublished);
|
||||
Assert.Equal(article.LectureTime, entity.LectureTime);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ArticleEntityToModelMapsCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
var entity = new ArticleEntity
|
||||
{
|
||||
Id = 1,
|
||||
Author = "Author",
|
||||
Description = "Description",
|
||||
Title = "Title",
|
||||
DatePublished = "2021-01-01",
|
||||
LectureTime = 10
|
||||
};
|
||||
|
||||
// Act
|
||||
var model = entity.ToModel();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(model);
|
||||
Assert.Equal(entity.Id, model.Id);
|
||||
Assert.Equal(entity.Author, model.Author);
|
||||
Assert.Equal(entity.Description, model.Description);
|
||||
Assert.Equal(entity.Title, model.Title);
|
||||
Assert.Equal(entity.DatePublished, model.DatePublished);
|
||||
Assert.Equal(entity.LectureTime, model.LectureTime);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
using API_Mapping;
|
||||
using Model;
|
||||
using Web_API.Model;
|
||||
|
||||
namespace API_UnitTest_Mapper;
|
||||
|
||||
public class FormMapperTests
|
||||
{
|
||||
[Fact]
|
||||
public void ToDTOMapsCorrectly()
|
||||
{
|
||||
var formulaire = new Formulaire
|
||||
{
|
||||
Id = 1,
|
||||
Theme = "Test Theme",
|
||||
Date = "2021-01-01",
|
||||
Lien = "http://example.com",
|
||||
UserPseudo = "TestUser"
|
||||
};
|
||||
|
||||
var dto = FormulaireMapping.ToDTO(formulaire);
|
||||
|
||||
Assert.NotNull(dto);
|
||||
Assert.Equal(formulaire.Id, dto.Id);
|
||||
Assert.Equal(formulaire.Theme, dto.Theme);
|
||||
Assert.Equal(formulaire.Date, dto.Date);
|
||||
Assert.Equal(formulaire.Lien, dto.Lien);
|
||||
Assert.Equal(formulaire.UserPseudo, dto.UserPseudo);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToModelMapsCorrectly()
|
||||
{
|
||||
var dto = new FormulaireDTO
|
||||
{
|
||||
Id = 2,
|
||||
Theme = "Another Test Theme",
|
||||
Date = "2021-01-02",
|
||||
Lien = "http://anotherexample.com",
|
||||
UserPseudo = "AnotherTestUser"
|
||||
};
|
||||
|
||||
var formulaire = FormulaireMapping.ToModel(dto);
|
||||
|
||||
Assert.NotNull(formulaire);
|
||||
Assert.Equal(dto.Id, formulaire.Id);
|
||||
Assert.Equal(dto.Theme, formulaire.Theme);
|
||||
Assert.Equal(dto.Date, formulaire.Date);
|
||||
Assert.Equal(dto.Lien, formulaire.Lien);
|
||||
Assert.Equal(dto.UserPseudo, formulaire.UserPseudo);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToDTONullFormulaireThrowsNullReferenceException()
|
||||
{
|
||||
Formulaire formulaire = null;
|
||||
|
||||
Assert.Throws<NullReferenceException>(() => FormulaireMapping.ToDTO(formulaire));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToModelNullFormulaireDTOThrowsNullReferenceException()
|
||||
{
|
||||
FormulaireDTO dto = null;
|
||||
|
||||
Assert.Throws<NullReferenceException>(() => FormulaireMapping.ToModel(dto));
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
using API.Controllers;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Model;
|
||||
using Moq;
|
||||
|
||||
namespace API_Unit_Test;
|
||||
|
||||
public class FormulaireControllerTests
|
||||
{
|
||||
private readonly Mock<IDataManager> _mockDataManager;
|
||||
private readonly Mock<ILogger<FormulaireController>> _mockLogger;
|
||||
private readonly FormulaireController _controller;
|
||||
|
||||
public FormulaireControllerTests()
|
||||
{
|
||||
_mockDataManager = new Mock<IDataManager>();
|
||||
_mockLogger = new Mock<ILogger<FormulaireController>>();
|
||||
_controller = new FormulaireController(_mockDataManager.Object, _mockLogger.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllForm_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
_mockDataManager.Setup(dm => dm.FormulaireService.GetAllForm(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<FormOrderCriteria>()))
|
||||
.ReturnsAsync(new List<Formulaire>());
|
||||
|
||||
// Act
|
||||
var result = await _controller.GetAllForm();
|
||||
|
||||
// Assert
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetById_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var testFormId = 1L;
|
||||
_mockDataManager.Setup(dm => dm.FormulaireService.GetById(testFormId))
|
||||
.ReturnsAsync(new Formulaire { Id = testFormId });
|
||||
|
||||
// Act
|
||||
var result = await _controller.GetById(testFormId);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateForm_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var form = new Formulaire { Theme = "Test" };
|
||||
_mockDataManager.Setup(dm => dm.FormulaireService.CreateForm(form))
|
||||
.ReturnsAsync(form);
|
||||
|
||||
// Act
|
||||
var result = await _controller.CreateForm(form);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteForm_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var testFormId = 1L;
|
||||
_mockDataManager.Setup(dm => dm.FormulaireService.DeleteForm(testFormId))
|
||||
.ReturnsAsync(new Formulaire { Id = testFormId });
|
||||
|
||||
// Act
|
||||
var result = await _controller.DeleteForm(testFormId);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateForm_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var testFormId = 1L;
|
||||
var form = new Formulaire { Theme = "Updated" };
|
||||
_mockDataManager.Setup(dm => dm.FormulaireService.UpdateForm(testFormId, form))
|
||||
.ReturnsAsync(form);
|
||||
|
||||
// Act
|
||||
var result = await _controller.UpdateForm(testFormId, form);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllForm_ThrowsException_ReturnsBadRequest()
|
||||
{
|
||||
// Arrange
|
||||
_mockDataManager.Setup(dm => dm.FormulaireService.GetAllForm(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<FormOrderCriteria>()))
|
||||
.ThrowsAsync(new Exception("Test exception"));
|
||||
|
||||
// Act
|
||||
var result = await _controller.GetAllForm();
|
||||
|
||||
// Assert
|
||||
Assert.IsType<BadRequestObjectResult>(result);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
using Web_API.Model;
|
||||
|
||||
namespace API_Unit_Test;
|
||||
|
||||
public class FormulaireDTOTests
|
||||
{
|
||||
[Fact]
|
||||
public void PropertiesTest()
|
||||
{
|
||||
// Arrange
|
||||
var formulaire = new FormulaireDTO
|
||||
{
|
||||
Id = 1,
|
||||
Theme = "Test Theme",
|
||||
Date = "2024-03-16",
|
||||
Lien = "http://example.com",
|
||||
UserPseudo = "TestUser"
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
Assert.Equal(1, formulaire.Id);
|
||||
Assert.Equal("Test Theme", formulaire.Theme);
|
||||
Assert.Equal("2024-03-16", formulaire.Date);
|
||||
Assert.Equal("http://example.com", formulaire.Lien);
|
||||
Assert.Equal("TestUser", formulaire.UserPseudo);
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
using API.Controllers;
|
||||
using Entities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Model;
|
||||
using Moq;
|
||||
|
||||
namespace API_Unit_Test;
|
||||
|
||||
public class UserControllerTests
|
||||
{
|
||||
private readonly Mock<IDataManager> _mockDataManager;
|
||||
private readonly Mock<ILogger<UserController>> _mockLogger;
|
||||
private readonly UserController _controller;
|
||||
|
||||
public UserControllerTests()
|
||||
{
|
||||
_mockDataManager = new Mock<IDataManager>();
|
||||
_mockLogger = new Mock<ILogger<UserController>>();
|
||||
_controller = new UserController(_mockDataManager.Object, _mockLogger.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAll_ReturnsOk()
|
||||
{
|
||||
_mockDataManager.Setup(dm => dm.UserService.GetAll(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<UserOrderCriteria>()))
|
||||
.ReturnsAsync(new List<User>());
|
||||
|
||||
var result = await _controller.GetAll();
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByPseudo_ReturnsOk()
|
||||
{
|
||||
var pseudo = "testUser";
|
||||
_mockDataManager.Setup(dm => dm.UserService.GetByPseudo(pseudo))
|
||||
.ReturnsAsync(new User { Pseudo = pseudo });
|
||||
|
||||
var result = await _controller.GetByPseudo(pseudo);
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Create_ReturnsOk()
|
||||
{
|
||||
var user = new User { Pseudo = "newUser" };
|
||||
_mockDataManager.Setup(dm => dm.UserService.Create(user))
|
||||
.ReturnsAsync(user);
|
||||
|
||||
var result = await _controller.Create(user);
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Update_ReturnsOk()
|
||||
{
|
||||
var pseudo = "existingUser";
|
||||
var user = new User { Pseudo = pseudo };
|
||||
_mockDataManager.Setup(dm => dm.UserService.Update(user, pseudo))
|
||||
.ReturnsAsync(user);
|
||||
|
||||
var result = await _controller.Update(user, pseudo);
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Delete_ReturnsOk()
|
||||
{
|
||||
var pseudo = "deleteUser";
|
||||
_mockDataManager.Setup(dm => dm.UserService.Delete(pseudo))
|
||||
.ReturnsAsync(new User { Pseudo = pseudo });
|
||||
|
||||
var result = await _controller.Delete(pseudo);
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task Create_ThrowsException_ReturnsBadRequest()
|
||||
{
|
||||
var user = new User { Pseudo = "errorUser" };
|
||||
_mockDataManager.Setup(dm => dm.UserService.Create(user))
|
||||
.ThrowsAsync(new Exception("Test exception"));
|
||||
|
||||
var result = await _controller.Create(user);
|
||||
|
||||
Assert.IsType<BadRequestObjectResult>(result);
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
using Web_API.Model;
|
||||
|
||||
namespace API_Unit_Test;
|
||||
|
||||
public class UserDTOTests
|
||||
{
|
||||
[Fact]
|
||||
public void UserDTOPropertiesTest()
|
||||
{
|
||||
// Arrange
|
||||
var user = new UserDTO
|
||||
{
|
||||
Pseudo = "user1",
|
||||
Mdp = "password",
|
||||
Nom = "Doe",
|
||||
Prenom = "John",
|
||||
Mail = "john.doe@example.com",
|
||||
Role = "Admin"
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
Assert.Equal("user1", user.Pseudo);
|
||||
Assert.Equal("password", user.Mdp);
|
||||
Assert.Equal("Doe", user.Nom);
|
||||
Assert.Equal("John", user.Prenom);
|
||||
Assert.Equal("john.doe@example.com", user.Mail);
|
||||
Assert.Equal("Admin", user.Role);
|
||||
}
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
using API_Mapping;
|
||||
using Model;
|
||||
using Web_API.Model;
|
||||
|
||||
namespace API_UnitTest_Mapper;
|
||||
|
||||
public class UserMappingTests
|
||||
{
|
||||
[Fact]
|
||||
public void ToDTOMapsCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
var user = new User
|
||||
{
|
||||
Pseudo = "testUser",
|
||||
Mdp = "testPassword",
|
||||
Nom = "Doe",
|
||||
Prenom = "John",
|
||||
Mail = "john.doe@example.com",
|
||||
Role = "User"
|
||||
};
|
||||
|
||||
// Act
|
||||
var dto = UserMapping.ToDTO(user);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(dto);
|
||||
Assert.Equal(user.Pseudo, dto.Pseudo);
|
||||
Assert.Equal(user.Mdp, dto.Mdp);
|
||||
Assert.Equal(user.Nom, dto.Nom);
|
||||
Assert.Equal(user.Prenom, dto.Prenom);
|
||||
Assert.Equal(user.Mail, dto.Mail);
|
||||
Assert.Equal(user.Role, dto.Role);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToModelMapsCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
var dto = new UserDTO
|
||||
{
|
||||
Pseudo = "anotherTestUser",
|
||||
Mdp = "anotherTestPassword",
|
||||
Nom = "Smith",
|
||||
Prenom = "Jane",
|
||||
Mail = "jane.smith@example.com",
|
||||
Role = "Admin"
|
||||
};
|
||||
|
||||
// Act
|
||||
var user = UserMapping.ToModel(dto);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(user);
|
||||
Assert.Equal(dto.Pseudo, user.Pseudo);
|
||||
Assert.Equal(dto.Mdp, user.Mdp);
|
||||
Assert.Equal(dto.Nom, user.Nom);
|
||||
Assert.Equal(dto.Prenom, user.Prenom);
|
||||
Assert.Equal(dto.Mail, user.Mail);
|
||||
Assert.Equal(dto.Role, user.Role);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToDTONullUserThrowsNullReferenceException()
|
||||
{
|
||||
// Arrange
|
||||
User user = null;
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<NullReferenceException>(() => UserMapping.ToDTO(user));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToModelNullUserDTOThrowsNullReferenceException()
|
||||
{
|
||||
// Arrange
|
||||
UserDTO dto = null;
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<NullReferenceException>(() => UserMapping.ToModel(dto));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToDTOMapsCorrectlyWithEmptyUser()
|
||||
{
|
||||
// Arrange
|
||||
var user = new User();
|
||||
|
||||
// Act
|
||||
var dto = UserMapping.ToDTO(user);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(dto);
|
||||
Assert.Equal(user.Pseudo, dto.Pseudo);
|
||||
Assert.Equal(user.Mdp, dto.Mdp);
|
||||
Assert.Equal(user.Nom, dto.Nom);
|
||||
Assert.Equal(user.Prenom, dto.Prenom);
|
||||
Assert.Equal(user.Mail, dto.Mail);
|
||||
Assert.Equal(user.Role, dto.Role);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToModelMapsCorrectlyWithEmptyUserDTO()
|
||||
{
|
||||
// Arrange
|
||||
var dto = new UserDTO();
|
||||
|
||||
// Act
|
||||
var user = UserMapping.ToModel(dto);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(user);
|
||||
Assert.Equal(dto.Pseudo, user.Pseudo);
|
||||
Assert.Equal(dto.Mdp, user.Mdp);
|
||||
Assert.Equal(dto.Nom, user.Nom);
|
||||
Assert.Equal(dto.Prenom, user.Prenom);
|
||||
Assert.Equal(dto.Mail, user.Mail);
|
||||
Assert.Equal(dto.Role, user.Role);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue