-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from crunchloop/chore/add-csharpier
chore: Add csharpier
- Loading branch information
Showing
9 changed files
with
197 additions
and
189 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,125 @@ | ||
using TodoApi.Controllers; | ||
using TodoApi.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using TodoApi.Controllers; | ||
using TodoApi.Models; | ||
|
||
namespace TodoApi.Tests; | ||
|
||
#nullable disable | ||
public class TodoListsControllerTests | ||
{ | ||
private DbContextOptions<TodoContext> DatabaseContextOptions() | ||
{ | ||
return new DbContextOptionsBuilder<TodoContext>() | ||
.UseInMemoryDatabase(Guid.NewGuid().ToString()) | ||
.Options; | ||
} | ||
|
||
private void PopulateDatabaseContext(TodoContext context) | ||
{ | ||
context.TodoList.Add(new TodoList { Id = 1, Name = "Task 1" }); | ||
context.TodoList.Add(new TodoList { Id = 2, Name = "Task 2" }); | ||
context.SaveChanges(); | ||
} | ||
|
||
[Fact] | ||
public async Task GetTodoList_WhenCalled_ReturnsTodoListList() | ||
{ | ||
using (var context = new TodoContext(DatabaseContextOptions())) | ||
private DbContextOptions<TodoContext> DatabaseContextOptions() | ||
{ | ||
return new DbContextOptionsBuilder<TodoContext>() | ||
.UseInMemoryDatabase(Guid.NewGuid().ToString()) | ||
.Options; | ||
} | ||
|
||
private void PopulateDatabaseContext(TodoContext context) | ||
{ | ||
context.TodoList.Add(new TodoList { Id = 1, Name = "Task 1" }); | ||
context.TodoList.Add(new TodoList { Id = 2, Name = "Task 2" }); | ||
context.SaveChanges(); | ||
} | ||
|
||
[Fact] | ||
public async Task GetTodoList_WhenCalled_ReturnsTodoListList() | ||
{ | ||
PopulateDatabaseContext(context); | ||
using (var context = new TodoContext(DatabaseContextOptions())) | ||
{ | ||
PopulateDatabaseContext(context); | ||
|
||
var controller = new TodoListsController(context); | ||
var controller = new TodoListsController(context); | ||
|
||
var result = await controller.GetTodoLists(); | ||
var result = await controller.GetTodoLists(); | ||
|
||
Assert.IsType<OkObjectResult>(result.Result); | ||
Assert.Equal( | ||
2, | ||
((result.Result as OkObjectResult).Value as IList<TodoList>).Count | ||
); | ||
Assert.IsType<OkObjectResult>(result.Result); | ||
Assert.Equal(2, ((result.Result as OkObjectResult).Value as IList<TodoList>).Count); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task GetTodoList_WhenCalled_ReturnsTodoListById() | ||
{ | ||
using (var context = new TodoContext(DatabaseContextOptions())) | ||
[Fact] | ||
public async Task GetTodoList_WhenCalled_ReturnsTodoListById() | ||
{ | ||
PopulateDatabaseContext(context); | ||
using (var context = new TodoContext(DatabaseContextOptions())) | ||
{ | ||
PopulateDatabaseContext(context); | ||
|
||
var controller = new TodoListsController(context); | ||
var controller = new TodoListsController(context); | ||
|
||
var result = await controller.GetTodoList(1); | ||
var result = await controller.GetTodoList(1); | ||
|
||
Assert.IsType<OkObjectResult>(result.Result); | ||
Assert.Equal( | ||
1, | ||
((result.Result as OkObjectResult).Value as TodoList).Id | ||
); | ||
Assert.IsType<OkObjectResult>(result.Result); | ||
Assert.Equal(1, ((result.Result as OkObjectResult).Value as TodoList).Id); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task PutTodoList_WhenTodoListDoesntExist_ReturnsBadRequest() | ||
{ | ||
using (var context = new TodoContext(DatabaseContextOptions())) | ||
[Fact] | ||
public async Task PutTodoList_WhenTodoListDoesntExist_ReturnsBadRequest() | ||
{ | ||
PopulateDatabaseContext(context); | ||
using (var context = new TodoContext(DatabaseContextOptions())) | ||
{ | ||
PopulateDatabaseContext(context); | ||
|
||
var controller = new TodoListsController(context); | ||
var controller = new TodoListsController(context); | ||
|
||
var result = await controller.PutTodoList(3, new Dtos.UpdateTodoList { Name = "Task 3" }); | ||
var result = await controller.PutTodoList( | ||
3, | ||
new Dtos.UpdateTodoList { Name = "Task 3" } | ||
); | ||
|
||
Assert.IsType<NotFoundResult>(result); | ||
Assert.IsType<NotFoundResult>(result); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task PutTodoList_WhenCalled_UpdatesTheTodoList() | ||
{ | ||
using (var context = new TodoContext(DatabaseContextOptions())) | ||
[Fact] | ||
public async Task PutTodoList_WhenCalled_UpdatesTheTodoList() | ||
{ | ||
PopulateDatabaseContext(context); | ||
using (var context = new TodoContext(DatabaseContextOptions())) | ||
{ | ||
PopulateDatabaseContext(context); | ||
|
||
var controller = new TodoListsController(context); | ||
var controller = new TodoListsController(context); | ||
|
||
var todoList = await context.TodoList.Where(x => x.Id == 2).FirstAsync(); | ||
var result = await controller.PutTodoList(todoList.Id, new Dtos.UpdateTodoList { Name = "Changed Task 2" }); | ||
var todoList = await context.TodoList.Where(x => x.Id == 2).FirstAsync(); | ||
var result = await controller.PutTodoList( | ||
todoList.Id, | ||
new Dtos.UpdateTodoList { Name = "Changed Task 2" } | ||
); | ||
|
||
Assert.IsType<OkObjectResult>(result); | ||
Assert.IsType<OkObjectResult>(result); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task PostTodoList_WhenCalled_CreatesTodoList() | ||
{ | ||
using (var context = new TodoContext(DatabaseContextOptions())) | ||
[Fact] | ||
public async Task PostTodoList_WhenCalled_CreatesTodoList() | ||
{ | ||
PopulateDatabaseContext(context); | ||
using (var context = new TodoContext(DatabaseContextOptions())) | ||
{ | ||
PopulateDatabaseContext(context); | ||
|
||
var controller = new TodoListsController(context); | ||
var controller = new TodoListsController(context); | ||
|
||
var result = await controller.PostTodoList(new Dtos.CreateTodoList { Name = "Task 3" }); | ||
var result = await controller.PostTodoList(new Dtos.CreateTodoList { Name = "Task 3" }); | ||
|
||
Assert.IsType<CreatedAtActionResult>(result.Result); | ||
Assert.Equal( | ||
3, | ||
context.TodoList.Count() | ||
); | ||
Assert.IsType<CreatedAtActionResult>(result.Result); | ||
Assert.Equal(3, context.TodoList.Count()); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task DeleteTodoList_WhenCalled_RemovesTodoList() | ||
{ | ||
using (var context = new TodoContext(DatabaseContextOptions())) | ||
[Fact] | ||
public async Task DeleteTodoList_WhenCalled_RemovesTodoList() | ||
{ | ||
PopulateDatabaseContext(context); | ||
using (var context = new TodoContext(DatabaseContextOptions())) | ||
{ | ||
PopulateDatabaseContext(context); | ||
|
||
var controller = new TodoListsController(context); | ||
var controller = new TodoListsController(context); | ||
|
||
var result = await controller.DeleteTodoList(2); | ||
var result = await controller.DeleteTodoList(2); | ||
|
||
Assert.IsType<NoContentResult>(result); | ||
Assert.Equal( | ||
1, | ||
context.TodoList.Count() | ||
); | ||
Assert.IsType<NoContentResult>(result); | ||
Assert.Equal(1, context.TodoList.Count()); | ||
} | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
global using Xunit; | ||
global using Xunit; |
Oops, something went wrong.