-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* L07 code * small updates * Fixed ToDoItemsRepository * Tests done according to Assignment * Small update
- Loading branch information
Showing
8 changed files
with
255 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
namespace ToDoList.Test.UnitTests; | ||
|
||
using NSubstitute; | ||
using Microsoft.AspNetCore.Mvc; | ||
using ToDoList.WebApi.Controllers; | ||
using ToDoList.Persistence.Repositories; | ||
using ToDoList.Domain.Models; | ||
using Microsoft.AspNetCore.Http; | ||
using NSubstitute.ExceptionExtensions; | ||
|
||
public class DeleteUnitTests | ||
{ | ||
[Fact] | ||
public void Delete_DeleteByIdValidItemId_ReturnsNoContent() | ||
Check warning on line 14 in ToDoList/tests/ToDoList.Test/UnitTests/DeleteTests.cs GitHub Actions / build
|
||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
repositoryMock.ReadById(Arg.Any<int>()).Returns(new ToDoItem { Name = "testItem", Description = "testDescription", IsCompleted = false }); | ||
var someId = 1; | ||
|
||
// Act | ||
var result = controller.DeleteById(someId); | ||
|
||
// Assert | ||
Assert.IsType<NoContentResult>(result); | ||
repositoryMock.Received(1).ReadById(someId); | ||
repositoryMock.Received(1).Delete(Arg.Any<ToDoItem>()); | ||
} | ||
|
||
[Fact] | ||
public void Delete_DeleteByIdInvalidItemId_ReturnsNotFound() | ||
Check warning on line 32 in ToDoList/tests/ToDoList.Test/UnitTests/DeleteTests.cs GitHub Actions / build
|
||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
repositoryMock.ReadById(Arg.Any<int>()).Returns(null as ToDoItem); | ||
var someId = 1; | ||
|
||
// Act | ||
var result = controller.DeleteById(someId); | ||
|
||
// Assert | ||
Assert.IsType<NotFoundResult>(result); | ||
repositoryMock.Received(1).ReadById(someId); | ||
repositoryMock.Received(0).Delete(Arg.Any<ToDoItem>()); // make sure nothing was deleted | ||
} | ||
|
||
[Fact] | ||
public void Delete_DeleteByIdExceptionOccurredDuringRepositoryReadById_ReturnsInternalServerError() | ||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
repositoryMock.ReadById(Arg.Any<int>()).Throws(new Exception()); | ||
var someId = 1; | ||
|
||
// Act | ||
var result = controller.DeleteById(someId); | ||
|
||
// Assert | ||
Assert.IsType<ObjectResult>(result); | ||
repositoryMock.Received(1).ReadById(someId); | ||
Assert.Equal(StatusCodes.Status500InternalServerError, ((ObjectResult)result).StatusCode); | ||
} | ||
|
||
[Fact] | ||
public void Delete_DeleteByIdExceptionOccurredDuringRepositoryDelete_ReturnsInternalServerError() | ||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
repositoryMock.ReadById(Arg.Any<int>()).Returns(new ToDoItem { Name = "testItem", Description = "testDescription", IsCompleted = false }); | ||
repositoryMock.When(r => r.Delete(Arg.Any<ToDoItem>())).Do(r => throw new Exception()); | ||
var someId = 1; | ||
|
||
// Act | ||
var result = controller.DeleteById(someId); | ||
|
||
// Assert | ||
Assert.IsType<ObjectResult>(result); | ||
repositoryMock.Received(1).ReadById(someId); | ||
repositoryMock.Received(1).Delete(Arg.Any<ToDoItem>()); | ||
Assert.Equal(StatusCodes.Status500InternalServerError, ((ObjectResult)result).StatusCode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
namespace ToDoList.Test.UnitTests; | ||
|
||
using NSubstitute; | ||
using Microsoft.AspNetCore.Mvc; | ||
using ToDoList.WebApi.Controllers; | ||
using ToDoList.Persistence.Repositories; | ||
using ToDoList.Domain.Models; | ||
using Microsoft.AspNetCore.Http; | ||
using NSubstitute.ExceptionExtensions; | ||
using NSubstitute.ReturnsExtensions; | ||
|
||
public class GetByIdUnitTests | ||
{ | ||
[Fact] | ||
public void Get_ReadByIdWhenSomeItemAvailable_ReturnsOk() | ||
Check warning on line 15 in ToDoList/tests/ToDoList.Test/UnitTests/GetByIdTests.cs GitHub Actions / build
|
||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
var someId = 1; | ||
repositoryMock.ReadById(someId).Returns(new ToDoItem { Name = "testItem", Description = "testDescription", IsCompleted = false }); | ||
|
||
// Act | ||
var result = controller.ReadById(someId); | ||
var resultResult = result.Result; | ||
|
||
// Assert | ||
Assert.IsType<OkObjectResult>(resultResult); | ||
repositoryMock.Received(1).ReadById(someId); | ||
} | ||
|
||
[Fact] | ||
public void Get_ReadByIdWhenItemIsNull_ReturnsNotFound() | ||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
var someId = 1; | ||
repositoryMock.ReadById(someId).ReturnsNull(); | ||
|
||
// Act | ||
var result = controller.ReadById(someId); | ||
var resultResult = result.Result; | ||
|
||
// Assert | ||
Assert.IsType<NotFoundResult>(resultResult); | ||
repositoryMock.Received(1).ReadById(someId); | ||
} | ||
|
||
[Fact] | ||
public void Get_ReadByIdUnhandledException_ReturnsInternalServerError() | ||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
var someId = 1; | ||
repositoryMock.ReadById(someId).Throws(new Exception()); | ||
|
||
// Act | ||
var result = controller.ReadById(someId); | ||
var resultResult = result.Result; | ||
|
||
// Assert | ||
Assert.IsType<ObjectResult>(resultResult); | ||
repositoryMock.Received(1).ReadById(someId); | ||
Assert.Equivalent(new StatusCodeResult(StatusCodes.Status500InternalServerError), resultResult); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
namespace ToDoList.Test.UnitTests; | ||
|
||
using NSubstitute; | ||
using Microsoft.AspNetCore.Mvc; | ||
using ToDoList.Domain.DTOs; | ||
using ToDoList.WebApi.Controllers; | ||
using ToDoList.Persistence.Repositories; | ||
using ToDoList.Domain.Models; | ||
using Microsoft.AspNetCore.Http; | ||
using NSubstitute.ReturnsExtensions; | ||
|
||
public class PutUnitTests | ||
{ | ||
[Fact] | ||
public void Put_UpdateByIdWhenItemUpdated_ReturnsNoContent() | ||
Check warning on line 15 in ToDoList/tests/ToDoList.Test/UnitTests/PutTests.cs GitHub Actions / build
|
||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
var request = new ToDoItemUpdateRequestDto( | ||
Name: "Jmeno", | ||
Description: "Popis", | ||
IsCompleted: false | ||
); | ||
var someId = 1; | ||
var readToDoItem = new ToDoItem { Name = "Jmeno", Description = "Popis", IsCompleted = false, ToDoItemId = someId }; | ||
repositoryMock.ReadById(someId).Returns(readToDoItem); | ||
|
||
// Act | ||
var result = controller.UpdateById(someId, request); | ||
|
||
// Assert | ||
Assert.IsType<NoContentResult>(result); | ||
repositoryMock.Received(1).ReadById(someId); | ||
repositoryMock.Received(1).Update(Arg.Any<ToDoItem>()); | ||
} | ||
|
||
[Fact] | ||
public void Put_UpdateByIdWhenIdNotFound_ReturnsNotFound() | ||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
var request = new ToDoItemUpdateRequestDto( | ||
Name: "Jmeno", | ||
Description: "Popis", | ||
IsCompleted: false | ||
); | ||
repositoryMock.ReadById(Arg.Any<int>()).ReturnsNull(); | ||
var someId = 1; | ||
|
||
// Act | ||
var result = controller.UpdateById(someId, request); | ||
|
||
// Assert | ||
Assert.IsType<NotFoundResult>(result); | ||
repositoryMock.Received(1).ReadById(someId); | ||
} | ||
|
||
[Fact] | ||
public void Put_UpdateByIdUnhandledException_ReturnsInternalServerError() | ||
{ | ||
// Arrange | ||
var repositoryMock = Substitute.For<IRepository<ToDoItem>>(); | ||
var controller = new ToDoItemsController(repositoryMock); | ||
var request = new ToDoItemUpdateRequestDto( | ||
Name: "Jmeno", | ||
Description: "Popis", | ||
IsCompleted: false | ||
); | ||
var someId = 1; | ||
var readToDoItem = new ToDoItem { Name = "Jmeno", Description = "Popis", IsCompleted = false, ToDoItemId = someId }; | ||
|
||
repositoryMock.ReadById(Arg.Any<int>()).Returns(readToDoItem); | ||
repositoryMock.When(r => r.Update(Arg.Any<ToDoItem>())).Do(r => throw new Exception()); | ||
|
||
// Act | ||
var result = controller.UpdateById(someId, request); | ||
|
||
// Assert | ||
Assert.IsType<ObjectResult>(result); | ||
Assert.Equivalent(new StatusCodeResult(StatusCodes.Status500InternalServerError), result); | ||
} | ||
} |