Skip to content

Commit

Permalink
Fix TodoListDependantEntititesService bug
Browse files Browse the repository at this point in the history
Removed unnecessary database roundtrip and fixed to-do list ID validation bug
  • Loading branch information
romandykyi committed Mar 3, 2024
1 parent 39a8722 commit 56defd7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,10 @@ public sealed class TodoListDependantEntitiesService<TEntity, TKey>(
public async Task<TDto?> GetByIdAsync<TDto>(string todoListId, TKey entityId)
where TDto : class
{
// Check if to-do list exists
if (!await _existenceChecker.ExistsAsync<TodoList, string>(todoListId))
return null;

// Get the model
var entity = await _repository.GetByIdAsync(entityId);
// Return null if model is null
if (entity == null) return null;
// Return null if model is null or has wrong to-do list ID
if (entity == null || entity.TodoListId != todoListId) return null;
// Map it to DTO and return
return entity.Adapt<TDto>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ public async Task GetByIdAsync_EntityExists_ReturnsCorrectEntity()
string todoListId = "ID";
TestTodoListDependantEntity entity = TestModels.CreateTestTodoListDependantEntity(todoListId);
entity.Id = 500;
WebApplicationFactory.EntityExistenceChecker
.ExistsAsync<TodoList, string>(todoListId)
.Returns(true);
WebApplicationFactory.TestTodoListDependantEntitiesRepository
.GetByIdAsync(entity.Id)
.Returns(entity);
Expand All @@ -90,17 +87,18 @@ public async Task GetByIdAsync_EntityExists_ReturnsCorrectEntity()
}

[Test]
public async Task GetByIdAsync_TodoListDoesNotExist_ReturnsNull()
public async Task GetByIdAsync_WrongTodoList_ReturnsNull()
{
// Arrange
string todoListId = "ID";
int entityId = 500;
WebApplicationFactory.EntityExistenceChecker
.ExistsAsync<TodoList, string>(todoListId)
.Returns(false);
TestTodoListDependantEntity entity = TestModels.CreateTestTodoListDependantEntity("WrongTodoListId");
entity.Id = 500;
WebApplicationFactory.TestTodoListDependantEntitiesRepository
.GetByIdAsync(entity.Id)
.Returns(entity);

// Act
var result = await _service.GetByIdAsync<TestTodoListDependantViewDto>(todoListId, entityId);
var result = await _service.GetByIdAsync<TestTodoListDependantViewDto>(todoListId, entity.Id);

// Assert
Assert.That(result, Is.Null);
Expand All @@ -112,9 +110,6 @@ public async Task GetByIdAsync_EntityDoesNotExist_ReturnsNull()
// Arrange
string todoListId = "ID";
int entityId = 500;
WebApplicationFactory.EntityExistenceChecker
.ExistsAsync<TodoList, string>(todoListId)
.Returns(true);
WebApplicationFactory.TestTodoListDependantEntitiesRepository
.GetByIdAsync(entityId)
.ReturnsNull();
Expand Down

0 comments on commit 56defd7

Please sign in to comment.