From 1cc2fdba95e6fbb94fd2ec0d5221d70bb74e90c1 Mon Sep 17 00:00:00 2001 From: etudiant Date: Tue, 31 Jan 2023 07:53:18 +0100 Subject: [PATCH 1/2] testpartiGet --- .../BowlingAPITest/BowlingAPITest.csproj | 5 + .../BowlingAPITest/TestPartiControlleur.cs | 93 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 Sources/Tests/BowlingAPITest/TestPartiControlleur.cs diff --git a/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj b/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj index 0a87957..125c01c 100644 --- a/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj +++ b/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj @@ -22,11 +22,13 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all + + @@ -35,4 +37,7 @@ + + + diff --git a/Sources/Tests/BowlingAPITest/TestPartiControlleur.cs b/Sources/Tests/BowlingAPITest/TestPartiControlleur.cs new file mode 100644 index 0000000..7847146 --- /dev/null +++ b/Sources/Tests/BowlingAPITest/TestPartiControlleur.cs @@ -0,0 +1,93 @@ +using DTOs; +using FluentAssertions; + +namespace BowlingAPITest +{ + public class TestPartiControlleur + { + [Fact] + public async void Get_ShouldReturnOkResult() + { + // Arrange + var partie1 = new PartieDTO { Score=1 }; + var partie2 = new PartieDTO { Score = 2 }; + var parti = GetTestItems(); + var mockService = new Mock(); + mockService.Setup(service => service.GetAll()).ReturnsAsync(parti); + var controller = new PartieController(mockService.Object); + + // Act + var result = await controller.Get() as OkObjectResult; + var value = result.Value as List; + + // Assert + result.Should().NotBeNull(); + value.Should().NotBeNull(); + result.StatusCode.Should().Be(200); + value.Should().BeEquivalentTo(parti); + + } + [Fact] + public async void Get_ShouldReturnAllItems() + { + // Arrange + var testItems = GetTestItems(); + var mockService = new Mock(); + mockService.Setup(service => service.GetAll()).ReturnsAsync(testItems); + var controller = new PartieController(mockService.Object); + + // Act + var result = await controller.Get(); + + // Assert + var okResult = result as OkObjectResult; + var items = Assert.IsType>(okResult.Value); + Assert.Equal(2, items.Count); + } + + private IEnumerable GetTestItems() + { + + var testItems = new List(); + testItems.Add(new PartieDTO { Score = 1 }); + testItems.Add(new PartieDTO { Score = 2 }); + return testItems; + } + [Fact] + public async Task Get_With_Invalid_Name_Should_Return_BadRequest() + { + // Arrange + var PartieController = new PartieController(null); + + // Act + var result = await PartieController.Get(null); + + // Assert + result.Should().BeOfType(); + var badRequestResult = result as BadRequestObjectResult; + badRequestResult.Value.Should().Be("Le score de la partie est obligatoire"); + } + + [Fact] + public async Task Get_With_Valid_Name_Should_Return_Ok_With_partie() + { + // Arrange + var parti = new PartieDTO { Id = 1, Score = 1 }; + var partiservicemock = new Mock(); + partiservicemock.Setup(x => x.GetDataWithName("John Doe")).ReturnsAsync(parti); + var particontrolleur = new PartieController(partiservicemock.Object); + + // Act + var result = await particontrolleur.Get("John Doe"); + + // Assert + result.Should().BeOfType(); + var okResult = result as OkObjectResult; + okResult.Value.Should().BeEquivalentTo(parti); + } + + } + + +} + From 527ac0e8e0efa89101bf1910b9c9418bf4603a76 Mon Sep 17 00:00:00 2001 From: etudiant Date: Wed, 1 Feb 2023 12:22:26 +0100 Subject: [PATCH 2/2] B2 --- .../Controllers/PartieController.cs | 9 ++ Sources/DTOs/PartieDTO.cs | 3 +- .../BowlingAPITest/TestPartiControlleur.cs | 105 +++++++++++++++++- 3 files changed, 115 insertions(+), 2 deletions(-) diff --git a/Sources/BowlingApi/Controllers/PartieController.cs b/Sources/BowlingApi/Controllers/PartieController.cs index 4d9702f..f700311 100644 --- a/Sources/BowlingApi/Controllers/PartieController.cs +++ b/Sources/BowlingApi/Controllers/PartieController.cs @@ -6,6 +6,7 @@ using BowlingEF.Entities; using BowlingLib.Model; using BowlingService; using BowlingService.Interfaces; +using DTOs; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -127,7 +128,15 @@ namespace BowlingApi.Controllers } + public Task Put(int? score, PartieDTO partie) + { + throw new NotImplementedException(); + } + public Task Put(long id, PartieDTO parti) + { + throw new NotImplementedException(); + } } } diff --git a/Sources/DTOs/PartieDTO.cs b/Sources/DTOs/PartieDTO.cs index 62334dd..b42e18b 100644 --- a/Sources/DTOs/PartieDTO.cs +++ b/Sources/DTOs/PartieDTO.cs @@ -15,7 +15,8 @@ namespace DTOs public class PartieDTO { #region Properties - + [Key] + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long Id { get; set; } public JoueurDTO Joueur { get; set; } diff --git a/Sources/Tests/BowlingAPITest/TestPartiControlleur.cs b/Sources/Tests/BowlingAPITest/TestPartiControlleur.cs index 7847146..62b90f5 100644 --- a/Sources/Tests/BowlingAPITest/TestPartiControlleur.cs +++ b/Sources/Tests/BowlingAPITest/TestPartiControlleur.cs @@ -86,8 +86,111 @@ namespace BowlingAPITest okResult.Value.Should().BeEquivalentTo(parti); } + //post + [Fact] + public async Task Put_With_Invalid_Joueur_Should_Return_BadRequest() + { + // Arrange + var joueurController = new JoueurController(null); + + // Act + var result = await joueurController.Put(null, null); + + // Assert + result.Should().BeOfType>(); + var actionResult = result as ActionResult; + actionResult.Result.Should().BeOfType(); + var badRequestResult = actionResult.Result as BadRequestObjectResult; + badRequestResult.Value.Should().Be("Le joueur est obligatoire"); + } + + [Fact] + public async Task Put_With_Valid_parti_Should_Return_Ok_With_Joueur() + { + // Arrange + var parti = new PartieDTO { Id = 1, Score = 1 }; + var partiServiceMock = new Mock(); + partiServiceMock.Setup(x => x.Update(parti)).ReturnsAsync(true); + var partiControlleur = new PartieController(partiServiceMock.Object); + + // Act + var result = await partiControlleur.Put(parti.Id, parti); + + // Assert + result.Should().BeOfType>(); + var actionResult = result as ActionResult; + actionResult.Result.Should().BeOfType(); + } + + //test Get_ShouldReturnNotFound + [Fact] + public async Task Get_ShouldReturnNotFound() + { + // Arrange + var mockService = new Mock(); + mockService.Setup(service => service.GetAll()).ReturnsAsync((List)null); + var controller = new PartieController(mockService.Object); + + // Act + var result = await controller.Get(); + + // Assert + result.Should().BeOfType(); + } + + [Fact] + public async Task Get_White_Name_ShouldReturnNotFound() + { + // Arrange + + var partie1 = new PartieDTO { Score = 1 }; + var mockService = new Mock(); + mockService.Setup(service => service.GetDataWithName("Jane Smith")).ReturnsAsync(partie1); + var controller = new PartieController(mockService.Object); + + // Act + var result = await controller.Get("John Doe"); + + // Assert + result.Should().BeOfType(); + } + [Fact] + public async Task Get_ShouldReturnInternalServerError() + { + // Arrange + var mockService = new Mock(); + mockService.Setup(service => service.GetAll()).ThrowsAsync(new Exception()); + var controller = new PartieController(mockService.Object); + + // Act + var result = await controller.Get() as ObjectResult; + + // Assert + result.Should().BeOfType(); + result.StatusCode.Should().Be(500); + } + [Fact] + public async Task Post_With_Invalid_Joueur_Should_Return_BadRequest() + { + // Arrange + var joueurController = new JoueurController(null); + + // Act + var result = await joueurController.Post(null); + + // Assert + result.Should().BeOfType>(); + var actionResult = result as ActionResult; + actionResult.Result.Should().BeOfType(); + var badRequestResult = actionResult.Result as BadRequestObjectResult; + badRequestResult.Value.Should().Be("La partie est obligatoire"); + } + + + + } - + }