forked from json-api-dotnet/JsonApiDotNetCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeeplyNestedInclusionTests.cs
316 lines (251 loc) · 11.4 KB
/
DeeplyNestedInclusionTests.cs
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
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Bogus;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCoreExample;
using JsonApiDotNetCoreExample.Data;
using JsonApiDotNetCoreExample.Models;
using JsonApiDotNetCoreExampleTests.Helpers.Extensions;
using Microsoft.AspNetCore.Hosting;
using Newtonsoft.Json;
using Xunit;
using Person = JsonApiDotNetCoreExample.Models.Person;
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
{
[Collection("WebHostCollection")]
public class DeeplyNestedInclusionTests
{
private TestFixture<TestStartup> _fixture;
public DeeplyNestedInclusionTests(TestFixture<TestStartup> fixture)
{
_fixture = fixture;
}
private void ResetContext(AppDbContext context)
{
context.TodoItems.RemoveRange(context.TodoItems);
context.TodoItemCollections.RemoveRange(context.TodoItemCollections);
context.People.RemoveRange(context.People);
context.PersonRoles.RemoveRange(context.PersonRoles);
}
[Fact]
public async Task Can_Include_Nested_Relationships()
{
// arrange
const string route = "/api/v1/todo-items?include=collection.owner";
var todoItem = new TodoItem {
Collection = new TodoItemCollection {
Owner = new Person()
}
};
var context = _fixture.GetService<AppDbContext>();
context.TodoItems.RemoveRange(context.TodoItems);
context.TodoItems.Add(todoItem);
await context.SaveChangesAsync();
// act
var response = await _fixture.Client.GetAsync(route);
// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.Content.ReadAsStringAsync();
var todoItems = _fixture.DeSerializer.DeserializeList<TodoItem>(body);
var responseTodoItem = Assert.Single(todoItems);
Assert.NotNull(responseTodoItem);
Assert.NotNull(responseTodoItem.Collection);
Assert.NotNull(responseTodoItem.Collection.Owner);
}
[Fact]
public async Task Can_Include_Nested_HasMany_Relationships()
{
// arrange
const string route = "/api/v1/todo-items?include=collection.todo-items";
var todoItem = new TodoItem {
Collection = new TodoItemCollection {
Owner = new Person(),
TodoItems = new List<TodoItem> {
new TodoItem(),
new TodoItem()
}
}
};
var context = _fixture.GetService<AppDbContext>();
ResetContext(context);
context.TodoItems.Add(todoItem);
await context.SaveChangesAsync();
// act
var response = await _fixture.Client.GetAsync(route);
// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.Content.ReadAsStringAsync();
var documents = JsonConvert.DeserializeObject<Documents>(body);
var included = documents.Included;
Assert.Equal(4, included.Count);
Assert.Equal(3, included.CountOfType("todo-items"));
Assert.Equal(1, included.CountOfType("todo-collections"));
}
[Fact]
public async Task Can_Include_Nested_HasMany_Relationships_BelongsTo()
{
// arrange
const string route = "/api/v1/todo-items?include=collection.todo-items.owner";
var todoItem = new TodoItem {
Collection = new TodoItemCollection {
Owner = new Person(),
TodoItems = new List<TodoItem> {
new TodoItem {
Owner = new Person()
},
new TodoItem()
}
}
};
var context = _fixture.GetService<AppDbContext>();
ResetContext(context);
context.TodoItems.Add(todoItem);
await context.SaveChangesAsync();
// act
var response = await _fixture.Client.GetAsync(route);
// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.Content.ReadAsStringAsync();
var documents = JsonConvert.DeserializeObject<Documents>(body);
var included = documents.Included;
Assert.Equal(5, included.Count);
Assert.Equal(3, included.CountOfType("todo-items"));
Assert.Equal(1, included.CountOfType("people"));
Assert.Equal(1, included.CountOfType("todo-collections"));
}
[Fact]
public async Task Can_Include_Nested_Relationships_With_Multiple_Paths()
{
// arrange
const string route = "/api/v1/todo-items?include=collection.owner.role,collection.todo-items.owner";
var todoItem = new TodoItem {
Collection = new TodoItemCollection {
Owner = new Person {
Role = new PersonRole()
},
TodoItems = new List<TodoItem> {
new TodoItem {
Owner = new Person()
},
new TodoItem()
}
}
};
var context = _fixture.GetService<AppDbContext>();
ResetContext(context);
context.TodoItems.Add(todoItem);
await context.SaveChangesAsync();
// act
var response = await _fixture.Client.GetAsync(route);
// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.Content.ReadAsStringAsync();
var documents = JsonConvert.DeserializeObject<Documents>(body);
var included = documents.Included;
Assert.Equal(7, included.Count);
Assert.Equal(3, included.CountOfType("todo-items"));
Assert.Equal(2, included.CountOfType("people"));
Assert.Equal(1, included.CountOfType("person-roles"));
Assert.Equal(1, included.CountOfType("todo-collections"));
}
[Fact]
public async Task Included_Resources_Are_Correct()
{
// arrange
var role = new PersonRole();
var assignee = new Person { Role = role };
var collectionOwner = new Person();
var someOtherOwner = new Person();
var collection = new TodoItemCollection { Owner = collectionOwner };
var todoItem1 = new TodoItem { Collection = collection, Assignee = assignee };
var todoItem2 = new TodoItem { Collection = collection, Assignee = assignee };
var todoItem3 = new TodoItem { Collection = collection, Owner = someOtherOwner };
var todoItem4 = new TodoItem { Collection = collection, Owner = assignee };
var context = _fixture.GetService<AppDbContext>();
ResetContext(context);
context.TodoItems.Add(todoItem1);
context.TodoItems.Add(todoItem2);
context.TodoItems.Add(todoItem3);
context.TodoItems.Add(todoItem4);
context.PersonRoles.Add(role);
context.People.Add(assignee);
context.People.Add(collectionOwner);
context.People.Add(someOtherOwner);
context.TodoItemCollections.Add(collection);
await context.SaveChangesAsync();
string route =
"/api/v1/todo-items/" + todoItem1.Id + "?include=" +
"collection.owner," +
"assignee.role," +
"assignee.assigned-todo-items";
// act
var response = await _fixture.Client.GetAsync(route);
// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.Content.ReadAsStringAsync();
var documents = JsonConvert.DeserializeObject<Document>(body);
var included = documents.Included;
// 1 collection, 1 owner,
// 1 assignee, 1 assignee role,
// 2 assigned todo items (including the primary resource)
Assert.Equal(6, included.Count);
var collectionDocument = included.FindResource("todo-collections", collection.Id);
var ownerDocument = included.FindResource("people", collectionOwner.Id);
var assigneeDocument = included.FindResource("people", assignee.Id);
var roleDocument = included.FindResource("person-roles", role.Id);
var assignedTodo1 = included.FindResource("todo-items", todoItem1.Id);
var assignedTodo2 = included.FindResource("todo-items", todoItem2.Id);
Assert.NotNull(assignedTodo1);
Assert.Equal(todoItem1.Id.ToString(), assignedTodo1.Id);
Assert.NotNull(assignedTodo2);
Assert.Equal(todoItem2.Id.ToString(), assignedTodo2.Id);
Assert.NotNull(collectionDocument);
Assert.Equal(collection.Id.ToString(), collectionDocument.Id);
Assert.NotNull(ownerDocument);
Assert.Equal(collectionOwner.Id.ToString(), ownerDocument.Id);
Assert.NotNull(assigneeDocument);
Assert.Equal(assignee.Id.ToString(), assigneeDocument.Id);
Assert.NotNull(roleDocument);
Assert.Equal(role.Id.ToString(), roleDocument.Id);
}
[Fact]
public async Task Can_Include_Doubly_HasMany_Relationships()
{
// arrange
var person = new Person {
TodoItemCollections = new List<TodoItemCollection> {
new TodoItemCollection {
TodoItems = new List<TodoItem> {
new TodoItem(),
new TodoItem()
}
},
new TodoItemCollection {
TodoItems = new List<TodoItem> {
new TodoItem(),
new TodoItem(),
new TodoItem()
}
}
}
};
var context = _fixture.GetService<AppDbContext>();
ResetContext(context);
context.People.Add(person);
await context.SaveChangesAsync();
string route = "/api/v1/people/" + person.Id + "?include=todo-collections.todo-items";
// act
var response = await _fixture.Client.GetAsync(route);
// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.Content.ReadAsStringAsync();
var documents = JsonConvert.DeserializeObject<Document>(body);
var included = documents.Included;
Assert.Equal(7, included.Count);
Assert.Equal(5, included.CountOfType("todo-items"));
Assert.Equal(2, included.CountOfType("todo-collections"));
}
}
}