Skip to content

Commit

Permalink
L07 (#6)
Browse files Browse the repository at this point in the history
* L07 code

* small updates

* Fixed ToDoItemsRepository

* Tests done according to Assignment

* Small update
  • Loading branch information
Hijtec authored Nov 12, 2024
1 parent 52a3759 commit bec55c1
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public interface IRepository<T> where T : class
public IEnumerable<T> ReadAll();
public T? ReadById(int id);
public void Update(T item);
public void DeleteById(int id);
public void Delete(T item);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ public void Create(ToDoItem item)
public ToDoItem? ReadById(int id) => context.ToDoItems.Find(id);
public void Update(ToDoItem item)
{
var foundItem = context.ToDoItems.Find(item.ToDoItemId) ?? throw new ArgumentOutOfRangeException($"ToDo item with ID {item.ToDoItemId} not found.");
context.Entry(foundItem).CurrentValues.SetValues(item);
context.Entry(item).CurrentValues.SetValues(item);
context.SaveChanges();
}

public void DeleteById(int id)
public void Delete(ToDoItem item)
{
var item = context.ToDoItems.Find(id) ?? throw new ArgumentOutOfRangeException($"ToDo item with ID {id} not found.");
context.ToDoItems.Remove(item);
context.SaveChanges();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public IActionResult DeleteById(int toDoItemId)
return NotFound(); //404
}

repository.DeleteById(toDoItemId);
repository.Delete(itemToDelete);
}
catch (Exception ex)
{
Expand Down
86 changes: 86 additions & 0 deletions ToDoList/tests/ToDoList.Test/UnitTests/DeleteTests.cs
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

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.UnitTests.DeleteUnitTests.Delete_DeleteByIdValidItemId_ReturnsNoContent() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.UnitTests.DeleteUnitTests.Delete_DeleteByIdInvalidItemId_ReturnsNotFound() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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);
}
}
69 changes: 69 additions & 0 deletions ToDoList/tests/ToDoList.Test/UnitTests/GetByIdTests.cs
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

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.UnitTests.GetByIdUnitTests.Get_ReadByIdWhenSomeItemAvailable_ReturnsOk() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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);
}

}
26 changes: 10 additions & 16 deletions ToDoList/tests/ToDoList.Test/UnitTests/GetTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
namespace ToDoList.Test.UnitTests;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using ToDoList.Domain.Models;
using ToDoList.Persistence.Repositories;
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 GetUnitTests
{
Expand All @@ -16,15 +17,7 @@ public void Get_ReadWhenSomeItemAvailable_ReturnsOk()
// Arrange
var repositoryMock = Substitute.For<IRepository<ToDoItem>>();
var controller = new ToDoItemsController(repositoryMock);
repositoryMock.ReadAll().Returns(
[
new ToDoItem{
Name = "testName",
Description = "testDescription",
IsCompleted = false
}
]
);
repositoryMock.ReadAll().Returns([new ToDoItem { Name = "testItem", Description = "testDescription", IsCompleted = false }]);

// Act
var result = controller.Read();
Expand All @@ -41,8 +34,7 @@ public void Get_ReadWhenNoItemAvailable_ReturnsNotFound()
// Arrange
var repositoryMock = Substitute.For<IRepository<ToDoItem>>();
var controller = new ToDoItemsController(repositoryMock);
// repositoryMock.ReadAll().ReturnsNull();
repositoryMock.ReadAll().Returns(null as IEnumerable<ToDoItem>);
repositoryMock.ReadAll().ReturnsNull();

// Act
var result = controller.Read();
Expand All @@ -59,6 +51,7 @@ public void Get_ReadUnhandledException_ReturnsInternalServerError()
// Arrange
var repositoryMock = Substitute.For<IRepository<ToDoItem>>();
var controller = new ToDoItemsController(repositoryMock);
//repositoryMock.When(r => r.ReadAll()).Do(r => throw new Exception());
repositoryMock.ReadAll().Throws(new Exception());

// Act
Expand All @@ -70,4 +63,5 @@ public void Get_ReadUnhandledException_ReturnsInternalServerError()
repositoryMock.Received(1).ReadAll();
Assert.Equivalent(new StatusCodeResult(StatusCodes.Status500InternalServerError), resultResult);
}

}
2 changes: 2 additions & 0 deletions ToDoList/tests/ToDoList.Test/UnitTests/PostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public void Post_CreateValidRequest_ReturnsCreatedAtAction()
// Assert
Assert.IsType<CreatedAtActionResult>(resultResult);
repositoryMock.Received(1).Create(Arg.Any<ToDoItem>());

// These asserts are optional
Assert.NotNull(value);

Assert.Equal(request.Description, value.Description);
Expand Down
84 changes: 84 additions & 0 deletions ToDoList/tests/ToDoList.Test/UnitTests/PutTests.cs
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

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from member name ToDoList.Test.UnitTests.PutUnitTests.Put_UpdateByIdWhenItemUpdated_ReturnsNoContent() (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
// 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);
}
}

0 comments on commit bec55c1

Please sign in to comment.