Skip to content

Commit b02b3ec

Browse files
committed
I think I found a bug in the current implementation?
1 parent c7887b8 commit b02b3ec

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/DeeplyNestedInclusionTests.cs

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using JsonApiDotNetCoreExample;
88
using JsonApiDotNetCoreExample.Data;
99
using JsonApiDotNetCoreExample.Models;
10+
using JsonApiDotNetCoreExampleTests.Helpers.Extensions;
1011
using Microsoft.AspNetCore.Hosting;
1112
using Newtonsoft.Json;
1213
using Xunit;
@@ -181,22 +182,14 @@ public async Task Included_Resources_Are_Correct()
181182
{
182183
// arrange
183184
var role = new PersonRole();
184-
var asignee = new Person { Role = role };
185+
var assignee = new Person { Role = role };
185186
var collectionOwner = new Person();
186187
var someOtherOwner = new Person();
187188
var collection = new TodoItemCollection { Owner = collectionOwner };
188-
var todoItem1 = new TodoItem { Collection = collection, Assignee = asignee };
189-
var todoItem2 = new TodoItem { Collection = collection, Assignee = asignee };
189+
var todoItem1 = new TodoItem { Collection = collection, Assignee = assignee };
190+
var todoItem2 = new TodoItem { Collection = collection, Assignee = assignee };
190191
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-
192+
var todoItem4 = new TodoItem { Collection = collection, Owner = assignee };
200193

201194
var context = _fixture.GetService<AppDbContext>();
202195
ResetContext(context);
@@ -206,14 +199,20 @@ public async Task Included_Resources_Are_Correct()
206199
context.TodoItems.Add(todoItem3);
207200
context.TodoItems.Add(todoItem4);
208201
context.PersonRoles.Add(role);
209-
context.People.Add(asignee);
202+
context.People.Add(assignee);
210203
context.People.Add(collectionOwner);
211204
context.People.Add(someOtherOwner);
212205
context.TodoItemCollections.Add(collection);
213206

214207

215208
await context.SaveChangesAsync();
216209

210+
string route =
211+
"/api/v1/todo-items/" + todoItem1.Id + "?include=" +
212+
"collection.owner," +
213+
"assignee.role," +
214+
"assignee.assigned-todo-items";
215+
217216
// act
218217
var response = await _fixture.Client.GetAsync(route);
219218

@@ -225,9 +224,35 @@ public async Task Included_Resources_Are_Correct()
225224
var included = documents.Included;
226225

227226
// 1 collection, 1 owner,
228-
// 1 asignee, 1 asignee role,
229-
// 2 assigned todo items
227+
// 1 assignee, 1 assignee role,
228+
// 2 assigned todo items (including the primary resource)
230229
Assert.Equal(6, included.Count);
230+
231+
232+
var collectionDocument = included.FindResource("todo-item-collections", collection.Id);
233+
var ownerDocument = included.FindResource("people", collectionOwner.Id);
234+
var assigneeDocument = included.FindResource("people", assignee.Id);
235+
var roleDocument = included.FindResource("person-roles", role.Id);
236+
var assignedTodo1 = included.FindResource("todo-items", todoItem1.Id);
237+
var assignedTodo2 = included.FindResource("todo-items", todoItem2.Id);
238+
239+
Assert.NotNull(assignedTodo1);
240+
Assert.Equal(todoItem1.Id.ToString(), assignedTodo1.Id);
241+
242+
Assert.NotNull(assignedTodo2);
243+
Assert.Equal(todoItem2.Id.ToString(), assignedTodo2.Id);
244+
245+
Assert.NotNull(collectionDocument);
246+
Assert.Equal(collection.Id.ToString(), collectionDocument.Id);
247+
248+
Assert.NotNull(ownerDocument);
249+
Assert.Equal(collectionOwner.Id.ToString(), ownerDocument.Id);
250+
251+
Assert.NotNull(assigneeDocument);
252+
Assert.Equal(assignee.Id.ToString(), assigneeDocument.Id);
253+
254+
Assert.NotNull(roleDocument);
255+
Assert.Equal(role.Id.ToString(), roleDocument.Id);
231256
}
232257
}
233258
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using JsonApiDotNetCore.Models;
6+
using Microsoft.EntityFrameworkCore.Internal;
7+
using Microsoft.EntityFrameworkCore.Query;
8+
using Microsoft.EntityFrameworkCore.Query.Internal;
9+
using Microsoft.EntityFrameworkCore.Storage;
10+
using Database = Microsoft.EntityFrameworkCore.Storage.Database;
11+
12+
namespace JsonApiDotNetCoreExampleTests.Helpers.Extensions
13+
{
14+
public static class DocumentExtensions
15+
{
16+
public static DocumentData FindResource<TId>(this List<DocumentData> included, string type, TId id)
17+
{
18+
var document = included.Where(documentData => (
19+
documentData.Type == type
20+
&& documentData.Id == id.ToString()
21+
)).FirstOrDefault();
22+
23+
return document;
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)