Skip to content

Commit

Permalink
Merge pull request #11 from crunchloop/chore/add-csharpier
Browse files Browse the repository at this point in the history
chore: Add csharpier
  • Loading branch information
bilby91 authored Nov 26, 2024
2 parents a3fd35d + 9458e12 commit 125dde1
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 189 deletions.
7 changes: 7 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"dotnet-ef"
],
"rollForward": false
},
"csharpier": {
"version": "0.30.2",
"commands": [
"dotnet-csharpier"
],
"rollForward": false
}
}
}
12 changes: 10 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
name: ci
name: Continuous Integration

on: [push]

jobs:
build:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install tools
run: dotnet tool restore
- name: Run csharpier
run: dotnet csharpier --check .
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
170 changes: 82 additions & 88 deletions TodoApi.Tests/Controllers/TodoListsControllerTests.cs
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());
}
}
}
}
}
2 changes: 1 addition & 1 deletion TodoApi.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global using Xunit;
global using Xunit;
Loading

0 comments on commit 125dde1

Please sign in to comment.