-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathFetchResourceTests.cs
More file actions
349 lines (272 loc) · 14.4 KB
/
FetchResourceTests.cs
File metadata and controls
349 lines (272 loc) · 14.4 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
using System.Net;
using DapperExample.Models;
using DapperExample.Repositories;
using FluentAssertions;
using JsonApiDotNetCore.Serialization.Objects;
using Microsoft.Extensions.DependencyInjection;
using TestBuildingBlocks;
using Xunit;
using Xunit.Abstractions;
namespace DapperTests.IntegrationTests.ReadWrite.Resources;
public sealed class FetchResourceTests : IClassFixture<DapperTestContext>
{
private readonly DapperTestContext _testContext;
private readonly TestFakers _fakers = new();
public FetchResourceTests(DapperTestContext testContext, ITestOutputHelper testOutputHelper)
{
testContext.SetTestOutputHelper(testOutputHelper);
_testContext = testContext;
}
[Fact]
public async Task Can_get_primary_resources()
{
// Arrange
var store = _testContext.Factory.Services.GetRequiredService<SqlCaptureStore>();
store.Clear();
List<TodoItem> todoItems = _fakers.TodoItem.GenerateList(2);
todoItems.ForEach(todoItem => todoItem.Owner = _fakers.Person.GenerateOne());
todoItems[0].Priority = TodoItemPriority.Low;
todoItems[1].Priority = TodoItemPriority.High;
await _testContext.RunOnDatabaseAsync(async dbContext =>
{
await _testContext.ClearAllTablesAsync(dbContext);
dbContext.TodoItems.AddRange(todoItems);
await dbContext.SaveChangesAsync();
});
const string route = "/todoItems";
// Act
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK);
responseDocument.Data.ManyValue.Should().HaveCount(2);
responseDocument.Data.ManyValue.Should().AllSatisfy(resource => resource.Type.Should().Be("todoItems"));
responseDocument.Data.ManyValue[0].Id.Should().Be(todoItems[1].StringId);
responseDocument.Data.ManyValue[0].Attributes.Should().ContainKey("description").WhoseValue.Should().Be(todoItems[1].Description);
responseDocument.Data.ManyValue[0].Attributes.Should().ContainKey("priority").WhoseValue.Should().Be(todoItems[1].Priority);
responseDocument.Data.ManyValue[0].Attributes.Should().ContainKey("durationInHours").WhoseValue.Should().Be(todoItems[1].DurationInHours);
responseDocument.Data.ManyValue[0].Attributes.Should().ContainKey("createdAt").WhoseValue.Should().Be(todoItems[1].CreatedAt);
responseDocument.Data.ManyValue[0].Attributes.Should().ContainKey("modifiedAt").WhoseValue.Should().Be(todoItems[1].LastModifiedAt);
responseDocument.Data.ManyValue[0].Relationships.Should().OnlyContainKeys("owner", "assignee", "tags");
responseDocument.Data.ManyValue[1].Id.Should().Be(todoItems[0].StringId);
responseDocument.Data.ManyValue[1].Attributes.Should().ContainKey("description").WhoseValue.Should().Be(todoItems[0].Description);
responseDocument.Data.ManyValue[1].Attributes.Should().ContainKey("priority").WhoseValue.Should().Be(todoItems[0].Priority);
responseDocument.Data.ManyValue[1].Attributes.Should().ContainKey("durationInHours").WhoseValue.Should().Be(todoItems[0].DurationInHours);
responseDocument.Data.ManyValue[1].Attributes.Should().ContainKey("createdAt").WhoseValue.Should().Be(todoItems[0].CreatedAt);
responseDocument.Data.ManyValue[1].Attributes.Should().ContainKey("modifiedAt").WhoseValue.Should().Be(todoItems[0].LastModifiedAt);
responseDocument.Data.ManyValue[1].Relationships.Should().OnlyContainKeys("owner", "assignee", "tags");
responseDocument.Meta.Should().ContainTotal(2);
store.SqlCommands.Should().HaveCount(2);
store.SqlCommands[0].With(command =>
{
command.Statement.Should().Be(_testContext.AdaptSql("""
SELECT COUNT(*)
FROM "TodoItems" AS t1
"""));
command.Parameters.Should().BeEmpty();
});
store.SqlCommands[1].With(command =>
{
command.Statement.Should().Be(_testContext.AdaptSql("""
SELECT t1."Id", t1."CreatedAt", t1."Description", t1."DurationInHours", t1."LastModifiedAt", t1."Priority"
FROM "TodoItems" AS t1
ORDER BY t1."Priority", t1."LastModifiedAt" DESC
"""));
command.Parameters.Should().BeEmpty();
});
}
[Fact]
public async Task Can_get_primary_resource_by_ID()
{
// Arrange
var store = _testContext.Factory.Services.GetRequiredService<SqlCaptureStore>();
store.Clear();
TodoItem todoItem = _fakers.TodoItem.GenerateOne();
todoItem.Owner = _fakers.Person.GenerateOne();
await _testContext.RunOnDatabaseAsync(async dbContext =>
{
dbContext.TodoItems.Add(todoItem);
await dbContext.SaveChangesAsync();
});
string route = $"/todoItems/{todoItem.StringId}";
// Act
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK);
responseDocument.Data.SingleValue.Should().NotBeNull();
responseDocument.Data.SingleValue.Type.Should().Be("todoItems");
responseDocument.Data.SingleValue.Id.Should().Be(todoItem.StringId);
responseDocument.Data.SingleValue.Attributes.Should().ContainKey("description").WhoseValue.Should().Be(todoItem.Description);
responseDocument.Data.SingleValue.Attributes.Should().ContainKey("priority").WhoseValue.Should().Be(todoItem.Priority);
responseDocument.Data.SingleValue.Attributes.Should().ContainKey("durationInHours").WhoseValue.Should().Be(todoItem.DurationInHours);
responseDocument.Data.SingleValue.Attributes.Should().ContainKey("createdAt").WhoseValue.Should().Be(todoItem.CreatedAt);
responseDocument.Data.SingleValue.Attributes.Should().ContainKey("modifiedAt").WhoseValue.Should().Be(todoItem.LastModifiedAt);
responseDocument.Data.SingleValue.Relationships.Should().OnlyContainKeys("owner", "assignee", "tags");
responseDocument.Meta.Should().BeNull();
store.SqlCommands.Should().HaveCount(1);
store.SqlCommands[0].With(command =>
{
command.Statement.Should().Be(_testContext.AdaptSql("""
SELECT t1."Id", t1."CreatedAt", t1."Description", t1."DurationInHours", t1."LastModifiedAt", t1."Priority"
FROM "TodoItems" AS t1
WHERE t1."Id" = @p1
"""));
command.Parameters.Should().HaveCount(1);
command.Parameters.Should().Contain("@p1", todoItem.Id);
});
}
[Fact]
public async Task Cannot_get_unknown_primary_resource_by_ID()
{
// Arrange
var store = _testContext.Factory.Services.GetRequiredService<SqlCaptureStore>();
store.Clear();
const long unknownTodoItemId = Unknown.TypedId.Int64;
string route = $"/todoItems/{unknownTodoItemId}";
// Act
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.NotFound);
responseDocument.Errors.Should().HaveCount(1);
ErrorObject error = responseDocument.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.NotFound);
error.Title.Should().Be("The requested resource does not exist.");
error.Detail.Should().Be($"Resource of type 'todoItems' with ID '{unknownTodoItemId}' does not exist.");
error.Source.Should().BeNull();
store.SqlCommands.Should().HaveCount(1);
store.SqlCommands[0].With(command =>
{
command.Statement.Should().Be(_testContext.AdaptSql("""
SELECT t1."Id", t1."CreatedAt", t1."Description", t1."DurationInHours", t1."LastModifiedAt", t1."Priority"
FROM "TodoItems" AS t1
WHERE t1."Id" = @p1
"""));
command.Parameters.Should().HaveCount(1);
command.Parameters.Should().Contain("@p1", unknownTodoItemId);
});
}
[Fact]
public async Task Can_get_secondary_ToMany_resources()
{
// Arrange
var store = _testContext.Factory.Services.GetRequiredService<SqlCaptureStore>();
store.Clear();
TodoItem todoItem = _fakers.TodoItem.GenerateOne();
todoItem.Owner = _fakers.Person.GenerateOne();
todoItem.Tags = _fakers.Tag.GenerateSet(2);
await _testContext.RunOnDatabaseAsync(async dbContext =>
{
await _testContext.ClearAllTablesAsync(dbContext);
dbContext.TodoItems.Add(todoItem);
await dbContext.SaveChangesAsync();
});
string route = $"/todoItems/{todoItem.StringId}/tags";
// Act
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK);
responseDocument.Data.ManyValue.Should().HaveCount(2);
responseDocument.Data.ManyValue.Should().AllSatisfy(resource => resource.Type.Should().Be("tags"));
responseDocument.Data.ManyValue[0].Id.Should().Be(todoItem.Tags.ElementAt(0).StringId);
responseDocument.Data.ManyValue[0].Attributes.Should().ContainKey("name").WhoseValue.Should().Be(todoItem.Tags.ElementAt(0).Name);
responseDocument.Data.ManyValue[0].Relationships.Should().OnlyContainKeys("todoItem", "color");
responseDocument.Data.ManyValue[1].Id.Should().Be(todoItem.Tags.ElementAt(1).StringId);
responseDocument.Data.ManyValue[1].Attributes.Should().ContainKey("name").WhoseValue.Should().Be(todoItem.Tags.ElementAt(1).Name);
responseDocument.Data.ManyValue[1].Relationships.Should().OnlyContainKeys("todoItem", "color");
responseDocument.Meta.Should().ContainTotal(2);
store.SqlCommands.Should().HaveCount(2);
store.SqlCommands[0].With(command =>
{
command.Statement.Should().Be(_testContext.AdaptSql("""
SELECT COUNT(*)
FROM "Tags" AS t1
LEFT JOIN "TodoItems" AS t2 ON t1."TodoItemId" = t2."Id"
WHERE t2."Id" = @p1
"""));
command.Parameters.Should().HaveCount(1);
command.Parameters.Should().Contain("@p1", todoItem.Id);
});
store.SqlCommands[1].With(command =>
{
command.Statement.Should().Be(_testContext.AdaptSql("""
SELECT t1."Id", t2."Id", t2."Name"
FROM "TodoItems" AS t1
LEFT JOIN "Tags" AS t2 ON t1."Id" = t2."TodoItemId"
WHERE t1."Id" = @p1
"""));
command.Parameters.Should().HaveCount(1);
command.Parameters.Should().Contain("@p1", todoItem.Id);
});
}
[Fact]
public async Task Can_get_secondary_ToOne_resource()
{
// Arrange
var store = _testContext.Factory.Services.GetRequiredService<SqlCaptureStore>();
store.Clear();
TodoItem todoItem = _fakers.TodoItem.GenerateOne();
todoItem.Owner = _fakers.Person.GenerateOne();
await _testContext.RunOnDatabaseAsync(async dbContext =>
{
dbContext.TodoItems.Add(todoItem);
await dbContext.SaveChangesAsync();
});
string route = $"/todoItems/{todoItem.StringId}/owner";
// Act
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK);
responseDocument.Data.SingleValue.Should().NotBeNull();
responseDocument.Data.SingleValue.Type.Should().Be("people");
responseDocument.Data.SingleValue.Id.Should().Be(todoItem.Owner.StringId);
responseDocument.Data.SingleValue.Attributes.Should().ContainKey("firstName").WhoseValue.Should().Be(todoItem.Owner.FirstName);
responseDocument.Data.SingleValue.Attributes.Should().ContainKey("lastName").WhoseValue.Should().Be(todoItem.Owner.LastName);
responseDocument.Data.SingleValue.Attributes.Should().ContainKey("displayName").WhoseValue.Should().Be(todoItem.Owner.DisplayName);
responseDocument.Data.SingleValue.Relationships.Should().OnlyContainKeys("account", "ownedTodoItems", "assignedTodoItems");
responseDocument.Meta.Should().BeNull();
store.SqlCommands.Should().HaveCount(1);
store.SqlCommands[0].With(command =>
{
command.Statement.Should().Be(_testContext.AdaptSql("""
SELECT t1."Id", t2."Id", t2."FirstName", t2."LastName"
FROM "TodoItems" AS t1
INNER JOIN "People" AS t2 ON t1."OwnerId" = t2."Id"
WHERE t1."Id" = @p1
"""));
command.Parameters.Should().HaveCount(1);
command.Parameters.Should().Contain("@p1", todoItem.Id);
});
}
[Fact]
public async Task Can_get_empty_secondary_ToOne_resource()
{
// Arrange
var store = _testContext.Factory.Services.GetRequiredService<SqlCaptureStore>();
store.Clear();
TodoItem todoItem = _fakers.TodoItem.GenerateOne();
todoItem.Owner = _fakers.Person.GenerateOne();
await _testContext.RunOnDatabaseAsync(async dbContext =>
{
dbContext.TodoItems.Add(todoItem);
await dbContext.SaveChangesAsync();
});
string route = $"/todoItems/{todoItem.StringId}/assignee";
// Act
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK);
responseDocument.Data.SingleValue.Should().BeNull();
responseDocument.Meta.Should().BeNull();
store.SqlCommands.Should().HaveCount(1);
store.SqlCommands[0].With(command =>
{
command.Statement.Should().Be(_testContext.AdaptSql("""
SELECT t1."Id", t2."Id", t2."FirstName", t2."LastName"
FROM "TodoItems" AS t1
LEFT JOIN "People" AS t2 ON t1."AssigneeId" = t2."Id"
WHERE t1."Id" = @p1
"""));
command.Parameters.Should().HaveCount(1);
command.Parameters.Should().Contain("@p1", todoItem.Id);
});
}
}