TypedResults update

pull/140/head
Fiyaz Bin Hasan 2023-04-08 14:30:38 +06:00
parent f99c5f3253
commit 4346c43ec1
4 changed files with 93 additions and 51 deletions

View File

@ -15,10 +15,14 @@ public class TodoInMemoryTests
await using var context = new MockDb().CreateDbContext();
// Act
var notFoundResult = (NotFound)await TodoEndpointsV1.GetTodo(1, context);
var result = await TodoEndpointsV1.GetTodo(1, context);
//Assert
Assert.Equal(404, notFoundResult.StatusCode);
Assert.IsType<Results<Ok<Todo>, NotFound>>(result);
var notFoundResult = (NotFound) result.Result;
Assert.NotNull(notFoundResult);
}
// </snippet_>
@ -47,22 +51,24 @@ public class TodoInMemoryTests
await context.SaveChangesAsync();
// Act
var okResult = (Ok<List<Todo>>)await TodoEndpointsV1.GetAllTodos(context);
var result = await TodoEndpointsV1.GetAllTodos(context);
//Assert
Assert.Equal(200, okResult.StatusCode);
var foundTodos = Assert.IsAssignableFrom<List<Todo>>(okResult.Value);
Assert.IsType<Ok<List<Todo>>>(result);
Assert.NotEmpty(foundTodos);
Assert.Collection(foundTodos, todo1 =>
var okResult = (Ok<List<Todo>>)result;
Assert.NotNull(okResult.Value);
Assert.NotEmpty(okResult.Value);
Assert.Collection(okResult.Value, todo1 =>
{
Assert.Equal(1, todo1.Id);
Assert.Equal("Test title 1", todo1.Title);
Assert.Equal("Test description 1", todo1.Description);
Assert.False(todo1.IsDone);
}, todo2 =>
{
Assert.Equal(2, todo2.Id);
Assert.Equal("Test title 2", todo2.Title);
Assert.Equal("Test description 2", todo2.Description);
Assert.True(todo2.IsDone);
});
}
@ -85,12 +91,15 @@ public class TodoInMemoryTests
await context.SaveChangesAsync();
// Act
var okResult = (Ok<Todo>)await TodoEndpointsV1.GetTodo(1, context);
var result = await TodoEndpointsV1.GetTodo(1, context);
//Assert
Assert.Equal(200, okResult.StatusCode);
var foundTodo = Assert.IsAssignableFrom<Todo>(okResult.Value);
Assert.Equal(1, foundTodo.Id);
Assert.IsType<Results<Ok<Todo>, NotFound>>(result);
var okResult = (Ok<Todo>)result.Result;
Assert.NotNull(okResult.Value);
Assert.Equal(1, okResult.Value.Id);
}
// </snippet_1>
@ -109,12 +118,15 @@ public class TodoInMemoryTests
};
//Act
var createdResult = (Created<Todo>)await TodoEndpointsV1.CreateTodo(newTodo, context);
var result = await TodoEndpointsV1.CreateTodo(newTodo, context);
//Assert
Assert.Equal(201, createdResult.StatusCode);
Assert.IsType<Created<Todo>>(result);
var createdResult = (Created<Todo>) result;
Assert.NotNull(createdResult);
Assert.NotNull(createdResult.Location);
Assert.IsAssignableFrom<Todo>(createdResult.Value);
Assert.NotEmpty(context.Todos);
Assert.Collection(context.Todos, todo =>
@ -149,12 +161,15 @@ public class TodoInMemoryTests
};
//Act
var createdResult = (Created<Todo>)await TodoEndpointsV1.UpdateTodo(updatedTodo, context);
var result = await TodoEndpointsV1.UpdateTodo(updatedTodo, context);
//Assert
Assert.Equal(201, createdResult.StatusCode);
Assert.IsType<Results<Created<Todo>, NotFound>>(result);
var createdResult = (Created<Todo>)result.Result;
Assert.NotNull(createdResult);
Assert.NotNull(createdResult.Location);
Assert.IsAssignableFrom<Todo>(createdResult.Value);
var todoInDb = await context.Todos.FindAsync(1);
@ -181,10 +196,14 @@ public class TodoInMemoryTests
await context.SaveChangesAsync();
//Act
var noContentResult = (NoContent)await TodoEndpointsV1.DeleteTodo(existingTodo.Id, context);
var result = await TodoEndpointsV1.DeleteTodo(existingTodo.Id, context);
//Assert
Assert.Equal(204, noContentResult.StatusCode);
Assert.IsType<Results<NoContent, NotFound>>(result);
var noContentResult = (NoContent)result.Result;
Assert.NotNull(noContentResult);
Assert.Empty(context.Todos);
}
}

View File

@ -18,10 +18,14 @@ public class TodoMoqTests
.ReturnsAsync((Todo?)null);
// Act
var notFoundResult = (NotFound)await TodoEndpointsV2.GetTodo(1, mock.Object);
var result = await TodoEndpointsV2.GetTodo(1, mock.Object);
//Assert
Assert.Equal(404, notFoundResult.StatusCode);
Assert.IsType<Results<Ok<Todo>, NotFound>>(result);
var notFoundResult = (NotFound) result.Result;
Assert.NotNull(notFoundResult);
}
[Fact]
@ -47,14 +51,16 @@ public class TodoMoqTests
});
// Act
var okResult = (Ok<List<Todo>>)await TodoEndpointsV2.GetAllTodos(mock.Object);
var result = await TodoEndpointsV2.GetAllTodos(mock.Object);
//Assert
Assert.Equal(200, okResult.StatusCode);
var foundTodos = Assert.IsAssignableFrom<List<Todo>>(okResult.Value);
Assert.IsType<Ok<List<Todo>>>(result);
Assert.NotEmpty(foundTodos);
Assert.Collection(foundTodos, todo1 =>
var okResult = (Ok<List<Todo>>) result;
Assert.NotNull(okResult.Value);
Assert.NotEmpty(okResult.Value);
Assert.Collection(okResult.Value, todo1 =>
{
Assert.Equal(1, todo1.Id);
Assert.Equal("Test title 1", todo1.Title);
@ -90,14 +96,16 @@ public class TodoMoqTests
});
// Act
var okResult = (Ok<List<Todo>>)await TodoEndpointsV2.GetAllIncompletedTodos(mock.Object);
var result = await TodoEndpointsV2.GetAllIncompletedTodos(mock.Object);
//Assert
Assert.Equal(200, okResult.StatusCode);
var foundTodos = Assert.IsAssignableFrom<List<Todo>>(okResult.Value);
Assert.IsType<Ok<List<Todo>>>(result);
Assert.NotEmpty(foundTodos);
Assert.Collection(foundTodos, todo1 =>
var okResult = (Ok<List<Todo>>) result;
Assert.NotNull(okResult.Value);
Assert.NotEmpty(okResult.Value);
Assert.Collection(okResult.Value, todo1 =>
{
Assert.Equal(1, todo1.Id);
Assert.Equal("Test title 1", todo1.Title);
@ -125,12 +133,15 @@ public class TodoMoqTests
});
// Act
var okResult = (Ok<Todo>)await TodoEndpointsV2.GetTodo(1, mock.Object);
var result = await TodoEndpointsV2.GetTodo(1, mock.Object);
//Assert
Assert.Equal(200, okResult.StatusCode);
var foundTodo = Assert.IsAssignableFrom<Todo>(okResult.Value);
Assert.Equal(1, foundTodo.Id);
Assert.IsType<Results<Ok<Todo>, NotFound>>(result);
var okResult = (Ok<Todo>) result.Result;
Assert.NotNull(okResult.Value);
Assert.Equal(1, okResult.Value.Id);
}
[Fact]
@ -153,12 +164,15 @@ public class TodoMoqTests
.Returns(Task.CompletedTask);
//Act
var createdResult = (Created<Todo>)await TodoEndpointsV2.CreateTodo(newTodo, mock.Object);
var result = await TodoEndpointsV2.CreateTodo(newTodo, mock.Object);
//Assert
Assert.Equal(201, createdResult.StatusCode);
Assert.IsType<Created<Todo>>(result);
var createdResult = (Created<Todo>) result;
Assert.NotNull(createdResult);
Assert.NotNull(createdResult.Location);
Assert.IsAssignableFrom<Todo>(createdResult.Value);
Assert.NotEmpty(todos);
Assert.Collection(todos, todo =>
@ -197,12 +211,15 @@ public class TodoMoqTests
.Returns(Task.CompletedTask);
//Act
var createdResult = (Created<Todo>)await TodoEndpointsV2.UpdateTodo(updatedTodo, mock.Object);
var result = await TodoEndpointsV2.UpdateTodo(updatedTodo, mock.Object);
//Assert
Assert.Equal(201, createdResult.StatusCode);
Assert.IsType<Results<Created<Todo>, NotFound>>(result);
var createdResult = (Created<Todo>) result.Result;
Assert.NotNull(createdResult);
Assert.NotNull(createdResult.Location);
Assert.IsAssignableFrom<Todo>(createdResult.Value);
Assert.Equal("Updated test title", existingTodo.Title);
Assert.True(existingTodo.IsDone);
@ -231,10 +248,14 @@ public class TodoMoqTests
.Returns(Task.CompletedTask);
//Act
var noContentResult = (NoContent)await TodoEndpointsV2.DeleteTodo(existingTodo.Id, mock.Object);
var result = await TodoEndpointsV2.DeleteTodo(existingTodo.Id, mock.Object);
//Assert
Assert.Equal(204, noContentResult.StatusCode);
Assert.IsType<Results<NoContent, NotFound>>(result);
var noContentResult = (NoContent) result.Result;
Assert.NotNull(noContentResult);
Assert.Empty(todos);
}
}

View File

@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using WebMinRouteGroup.Data;
@ -38,7 +39,7 @@ public static class TodoEndpointsV1
}
// get todo by id
public static async Task<IResult> GetTodo(int id, TodoGroupDbContext database)
public static async Task<Results<Ok<Todo>, NotFound>> GetTodo(int id, TodoGroupDbContext database)
{
var todo = await database.Todos.FindAsync(id);
@ -67,7 +68,7 @@ public static class TodoEndpointsV1
}
// update todo
public static async Task<IResult> UpdateTodo(Todo todo, TodoGroupDbContext database)
public static async Task<Results<Created<Todo>, NotFound>> UpdateTodo(Todo todo, TodoGroupDbContext database)
{
var existingTodo = await database.Todos.FindAsync(todo.Id);
@ -86,7 +87,7 @@ public static class TodoEndpointsV1
}
// delete todo
public static async Task<IResult> DeleteTodo(int id, TodoGroupDbContext database)
public static async Task<Results<NoContent, NotFound>> DeleteTodo(int id, TodoGroupDbContext database)
{
var todo = await database.Todos.FindAsync(id);

View File

@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Http.HttpResults;
using WebMinRouteGroup.Data;
using WebMinRouteGroup.Services;
@ -46,7 +47,7 @@ public static class TodoEndpointsV2
}
// get todo by id
public static async Task<IResult> GetTodo(int id, ITodoService todoService)
public static async Task<Results<Ok<Todo>, NotFound>> GetTodo(int id, ITodoService todoService)
{
var todo = await todoService.Find(id);
@ -74,7 +75,7 @@ public static class TodoEndpointsV2
}
// update todo
public static async Task<IResult> UpdateTodo(Todo todo, ITodoService todoService)
public static async Task<Results<Created<Todo>, NotFound>> UpdateTodo(Todo todo, ITodoService todoService)
{
var existingTodo = await todoService.Find(todo.Id);
@ -93,7 +94,7 @@ public static class TodoEndpointsV2
}
// delete todo
public static async Task<IResult> DeleteTodo(int id, ITodoService todoService)
public static async Task<Results<NoContent, NotFound>> DeleteTodo(int id, ITodoService todoService)
{
var todo = await todoService.Find(id);