@@ -24,6 +24,14 @@ public DeeplyNestedInclusionTests(TestFixture<TestStartup> fixture)
24
24
_fixture = fixture ;
25
25
}
26
26
27
+ private void ResetContext ( AppDbContext context )
28
+ {
29
+ context . TodoItems . RemoveRange ( context . TodoItems ) ;
30
+ context . TodoItemCollections . RemoveRange ( context . TodoItemCollections ) ;
31
+ context . People . RemoveRange ( context . People ) ;
32
+ context . PersonRoles . RemoveRange ( context . PersonRoles ) ;
33
+ }
34
+
27
35
[ Fact ]
28
36
public async Task Can_Include_Nested_Relationships ( )
29
37
{
@@ -72,8 +80,10 @@ public async Task Can_Include_Nested_HasMany_Relationships()
72
80
}
73
81
} ;
74
82
83
+
75
84
var context = _fixture . GetService < AppDbContext > ( ) ;
76
- context . TodoItems . RemoveRange ( context . TodoItems ) ;
85
+ ResetContext ( context ) ;
86
+
77
87
context . TodoItems . Add ( todoItem ) ;
78
88
await context . SaveChangesAsync ( ) ;
79
89
@@ -109,7 +119,8 @@ public async Task Can_Include_Nested_HasMany_Relationships_BelongsTo()
109
119
} ;
110
120
111
121
var context = _fixture . GetService < AppDbContext > ( ) ;
112
- context . TodoItems . RemoveRange ( context . TodoItems ) ;
122
+ ResetContext ( context ) ;
123
+
113
124
context . TodoItems . Add ( todoItem ) ;
114
125
await context . SaveChangesAsync ( ) ;
115
126
@@ -147,7 +158,8 @@ public async Task Can_Include_Nested_Relationships_With_Multiple_Paths()
147
158
} ;
148
159
149
160
var context = _fixture . GetService < AppDbContext > ( ) ;
150
- context . TodoItems . RemoveRange ( context . TodoItems ) ;
161
+ ResetContext ( context ) ;
162
+
151
163
context . TodoItems . Add ( todoItem ) ;
152
164
await context . SaveChangesAsync ( ) ;
153
165
@@ -163,5 +175,59 @@ public async Task Can_Include_Nested_Relationships_With_Multiple_Paths()
163
175
164
176
Assert . Equal ( 7 , included . Count ) ; // 1 collection, 3 todos, 2 owners, 1 role
165
177
}
178
+
179
+ [ Fact ]
180
+ public async Task Included_Resources_Are_Correct ( )
181
+ {
182
+ // arrange
183
+ var role = new PersonRole ( ) ;
184
+ var asignee = new Person { Role = role } ;
185
+ var collectionOwner = new Person ( ) ;
186
+ var someOtherOwner = new Person ( ) ;
187
+ var collection = new TodoItemCollection { Owner = collectionOwner } ;
188
+ var todoItem1 = new TodoItem { Collection = collection , Assignee = asignee } ;
189
+ var todoItem2 = new TodoItem { Collection = collection , Assignee = asignee } ;
190
+ var todoItem3 = new TodoItem { Collection = collection , Owner = someOtherOwner } ;
191
+ var todoItem4 = new TodoItem { Collection = collection , Owner = asignee } ;
192
+
193
+
194
+ string route =
195
+ "/api/v1/todo-items/" + todoItem1 . Id + "?include=" +
196
+ "collection.owner," +
197
+ "asignee.role," +
198
+ "asignee.assigned-todo-items" ;
199
+
200
+
201
+ var context = _fixture . GetService < AppDbContext > ( ) ;
202
+ ResetContext ( context ) ;
203
+
204
+ context . TodoItems . Add ( todoItem1 ) ;
205
+ context . TodoItems . Add ( todoItem2 ) ;
206
+ context . TodoItems . Add ( todoItem3 ) ;
207
+ context . TodoItems . Add ( todoItem4 ) ;
208
+ context . PersonRoles . Add ( role ) ;
209
+ context . People . Add ( asignee ) ;
210
+ context . People . Add ( collectionOwner ) ;
211
+ context . People . Add ( someOtherOwner ) ;
212
+ context . TodoItemCollections . Add ( collection ) ;
213
+
214
+
215
+ await context . SaveChangesAsync ( ) ;
216
+
217
+ // act
218
+ var response = await _fixture . Client . GetAsync ( route ) ;
219
+
220
+ // assert
221
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
222
+
223
+ var body = await response . Content . ReadAsStringAsync ( ) ;
224
+ var documents = JsonConvert . DeserializeObject < Document > ( body ) ;
225
+ var included = documents . Included ;
226
+
227
+ // 1 collection, 1 owner,
228
+ // 1 asignee, 1 asignee role,
229
+ // 2 assigned todo items
230
+ Assert . Equal ( 6 , included . Count ) ;
231
+ }
166
232
}
167
233
}
0 commit comments