-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodoControllerTests.cs
More file actions
250 lines (195 loc) · 8.72 KB
/
TodoControllerTests.cs
File metadata and controls
250 lines (195 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.FeatureManagement;
using Moq;
using WebAPI.Controllers;
using WebAPI.Features;
using WebAPI.Persistence;
using Assert = Xunit.Assert;
namespace WebTests;
public class TodoControllerTests
{
private readonly Mock<IFeatureManager> _mockFeatureManager = new();
private static DbContextOptions<TodoDbContext> GetDbContextOptions()
{
return new DbContextOptionsBuilder<TodoDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
}
[Fact]
public async Task GetById_FeatureEnabled_ReturnsTodo()
{
// Arrange
var options = GetDbContextOptions();
var testTodo = new Todo(Guid.NewGuid(), "Test Todo", "Test Description", false, DateTime.Today, null);
await using var context = new TodoDbContext(options);
context.Todos.Add(testTodo);
await context.SaveChangesAsync();
_mockFeatureManager.Setup(fm => fm.IsEnabledAsync(FeatureFlags.Read)).ReturnsAsync(true);
var controller = new TodoController(context, _mockFeatureManager.Object);
// Act
var result = await controller.GetById(testTodo.Id);
// Assert
var okResult = Assert.IsType<OkObjectResult>(result);
var returnedTodo = Assert.IsType<Todo>(okResult.Value);
Assert.Equal(testTodo.Id, returnedTodo.Id);
Assert.Equal(testTodo.Title, returnedTodo.Title);
Assert.Equal(testTodo.Description, returnedTodo.Description);
Assert.Equal(testTodo.Completed, returnedTodo.Completed);
Assert.Equal(testTodo.Created, returnedTodo.Created);
}
[Fact]
public async Task GetById_TodoNotFound_ReturnsNotFound()
{
// Arrange
var options = GetDbContextOptions();
var testTodoId = Guid.NewGuid();
await using var context = new TodoDbContext(options);
_mockFeatureManager.Setup(fm => fm.IsEnabledAsync(FeatureFlags.Read)).ReturnsAsync(true);
var controller = new TodoController(context, _mockFeatureManager.Object);
// Act
var result = await controller.GetById(testTodoId);
// Assert
var notFoundResult = Assert.IsType<NotFoundObjectResult>(result);
Assert.Equal(testTodoId, notFoundResult.Value);
}
[Fact]
public async Task GetById_FeatureDisabled_Returns501NotImplemented()
{
// Arrange
var options = GetDbContextOptions();
var testTodo = new Todo(Guid.NewGuid(), "Test Todo", "Test Description", false, DateTime.Today, null);
await using var context = new TodoDbContext(options);
context.Todos.Add(testTodo);
await context.SaveChangesAsync();
_mockFeatureManager.Setup(fm => fm.IsEnabledAsync(FeatureFlags.Read)).ReturnsAsync(false);
var controller = new TodoController(context, _mockFeatureManager.Object);
// Act
var result = await controller.GetById(testTodo.Id);
// Assert
var statusCodeResult = Assert.IsType<StatusCodeResult>(result);
Assert.Equal(StatusCodes.Status501NotImplemented, statusCodeResult.StatusCode);
}
[Fact]
public async Task GetAll_FeatureEnabled_ReturnsTodos()
{
// Arrange
var options = GetDbContextOptions();
var testTodos = new List<Todo>
{
new(Guid.NewGuid(), "Test Todo 1", "Test Description 1", false, DateTime.Today, null),
new(Guid.NewGuid(), "Test Todo 2", "Test Description 2", true, DateTime.Today, null)
};
await using var context = new TodoDbContext(options);
context.Todos.AddRange(testTodos);
await context.SaveChangesAsync();
_mockFeatureManager.Setup(fm => fm.IsEnabledAsync(FeatureFlags.Read)).ReturnsAsync(true);
var controller = new TodoController(context, _mockFeatureManager.Object);
// Act
var result = await controller.GetAll();
// Assert
var okResult = Assert.IsType<OkObjectResult>(result);
var todos = Assert.IsAssignableFrom<IEnumerable<Todo>>(okResult.Value);
Assert.Equal(2, todos.Count());
}
[Fact]
public async Task GetAll_FeatureDisabled_Returns501NotImplemented()
{
// Arrange
var options = GetDbContextOptions();
await using var context = new TodoDbContext(options);
_mockFeatureManager.Setup(fm => fm.IsEnabledAsync(FeatureFlags.Read)).ReturnsAsync(false);
var controller = new TodoController(context, _mockFeatureManager.Object);
// Act
var result = await controller.GetAll();
// Assert
var statusCodeResult = Assert.IsType<StatusCodeResult>(result);
Assert.Equal(StatusCodes.Status501NotImplemented, statusCodeResult.StatusCode);
}
[Fact]
public async Task Create_FeatureEnabled_CreatesTodo()
{
// Arrange
var options = GetDbContextOptions();
var testTodo = new Todo(Guid.NewGuid(), "Test Todo", "Test Description", false, DateTime.Today, null);
await using var context = new TodoDbContext(options);
_mockFeatureManager.Setup(fm => fm.IsEnabledAsync(FeatureFlags.Create)).ReturnsAsync(true);
var controller = new TodoController(context, _mockFeatureManager.Object);
// Act
var result = await controller.Create(testTodo);
// Assert
var okResult = Assert.IsType<OkObjectResult>(result);
var saveResult = Assert.IsType<int>(okResult.Value);
Assert.Equal(1, saveResult);
// Verify the Todo was added
var addedTodo = await context.Todos.FirstOrDefaultAsync(t => t.Title == testTodo.Title);
Assert.NotNull(addedTodo);
Assert.Equal(testTodo.Title, addedTodo.Title);
Assert.Equal(testTodo.Description, addedTodo.Description);
}
[Fact]
public async Task Create_FeatureDisabled_Returns501NotImplemented()
{
// Arrange
var options = GetDbContextOptions();
var testTodo = new Todo(Guid.NewGuid(), "Test Todo", "Test Description", false, DateTime.Today, null);
await using var context = new TodoDbContext(options);
_mockFeatureManager.Setup(fm => fm.IsEnabledAsync(FeatureFlags.Create)).ReturnsAsync(false);
var controller = new TodoController(context, _mockFeatureManager.Object);
// Act
var result = await controller.Create(testTodo);
// Assert
var statusCodeResult = Assert.IsType<StatusCodeResult>(result);
Assert.Equal(StatusCodes.Status501NotImplemented, statusCodeResult.StatusCode);
}
[Fact]
public async Task Delete_FeatureEnabled_ValidId_DeletesTodo()
{
// Arrange
var options = GetDbContextOptions();
var testTodo = new Todo(Guid.NewGuid(), "Test Todo", "Test Description", false, DateTime.Today, null);
await using var context = new TodoDbContext(options);
context.Todos.Add(testTodo);
await context.SaveChangesAsync();
_mockFeatureManager.Setup(fm => fm.IsEnabledAsync(FeatureFlags.Mutate)).ReturnsAsync(true);
var controller = new TodoController(context, _mockFeatureManager.Object);
// Act
var result = await controller.Delete(testTodo.Id);
// Assert
Assert.IsType<OkResult>(result);
// Verify the Todo was deleted
var deletedTodo = await context.Todos.FindAsync(testTodo.Id);
Assert.Null(deletedTodo);
}
[Fact]
public async Task Delete_FeatureEnabled_InvalidId_ReturnsNotFound()
{
// Arrange
var options = GetDbContextOptions();
var testTodoId = Guid.NewGuid();
await using var context = new TodoDbContext(options);
_mockFeatureManager.Setup(fm => fm.IsEnabledAsync(FeatureFlags.Mutate)).ReturnsAsync(true);
var controller = new TodoController(context, _mockFeatureManager.Object);
// Act
var result = await controller.Delete(testTodoId);
// Assert
var notFoundResult = Assert.IsType<NotFoundObjectResult>(result);
Assert.Equal(testTodoId, notFoundResult.Value);
}
[Fact]
public async Task Delete_FeatureDisabled_Returns501NotImplemented()
{
// Arrange
var options = GetDbContextOptions();
var testTodoId = Guid.NewGuid();
await using var context = new TodoDbContext(options);
_mockFeatureManager.Setup(fm => fm.IsEnabledAsync(FeatureFlags.Mutate)).ReturnsAsync(false);
var controller = new TodoController(context, _mockFeatureManager.Object);
// Act
var result = await controller.Delete(testTodoId);
// Assert
var statusCodeResult = Assert.IsType<StatusCodeResult>(result);
Assert.Equal(StatusCodes.Status501NotImplemented, statusCodeResult.StatusCode);
}
}