Skip to content

Commit 4635c82

Browse files
spike83csantero
authored andcommitted
Feat: implementation of includes (#118)
* added basic IncludeExpressionExractor and introduced includes on EFDocumentMaterializer * added includes to FallbackDocumentBuilder * added tests for DefaultIncludeExpressionExtractor / fixed ugly typo * added includes in ToManyRelatedResourceDocument * removed obsolete method * includes on create and update * reverse deletion of GetDefaultSortExpressions * fixed typo * removed related to one and related to many methods from EntityFrameworkDocumentMaterializer because they are never used and replaced with EntityFrameworkToManyRelatedResourceDocumentMaterializer and EntityFrameworkToOneRelatedResourceDocumentMaterializer * a try for the GetIncludes method (renamed to GetNavigationPropertiesIncludes) * GetNavigationPropertiesIncludes added in EntityFrameworkToManyRelatedResourceDocumentMaterializer * bring back get default sort expression
1 parent 90a399d commit 4635c82

21 files changed

Lines changed: 641 additions & 80 deletions

JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Tests/CreatingResourcesTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,34 @@ public async Task Post_with_empty_id()
112112
}
113113
}
114114
}
115+
116+
117+
[TestMethod]
118+
[DeploymentItem(@"Data\Comment.csv", @"Data")]
119+
[DeploymentItem(@"Data\Post.csv", @"Data")]
120+
[DeploymentItem(@"Data\PostTagLink.csv", @"Data")]
121+
[DeploymentItem(@"Data\Tag.csv", @"Data")]
122+
[DeploymentItem(@"Data\User.csv", @"Data")]
123+
public async Task Post_with_empty_id_and_include()
124+
{
125+
using (var effortConnection = GetEffortConnection())
126+
{
127+
var response = await SubmitPost(effortConnection, "posts?include=author", @"Fixtures\CreatingResources\Requests\Post_with_empty_id_Request.json");
128+
129+
await AssertResponseContent(response, @"Fixtures\CreatingResources\Responses\Post_with_empty_id_and_include_author_Response.json", HttpStatusCode.OK);
130+
131+
using (var dbContext = new TestDbContext(effortConnection, false))
132+
{
133+
var allPosts = dbContext.Posts.ToArray();
134+
allPosts.Length.Should().Be(5);
135+
var actualPost = allPosts.First(t => t.Id == "230");
136+
actualPost.Id.Should().Be("230");
137+
actualPost.Title.Should().Be("New post");
138+
actualPost.Content.Should().Be("The server generated my ID");
139+
actualPost.Created.Should().Be(new DateTimeOffset(2015, 04, 13, 12, 09, 0, new TimeSpan(0, 3, 0, 0)));
140+
actualPost.AuthorId.Should().Be("401");
141+
}
142+
}
143+
}
115144
}
116145
}

JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Tests/FetchingResourcesTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@ public async Task Get_related_to_many()
9999
}
100100
}
101101

102+
103+
[TestMethod]
104+
[DeploymentItem(@"Data\Comment.csv", @"Data")]
105+
[DeploymentItem(@"Data\Post.csv", @"Data")]
106+
[DeploymentItem(@"Data\PostTagLink.csv", @"Data")]
107+
[DeploymentItem(@"Data\Tag.csv", @"Data")]
108+
[DeploymentItem(@"Data\User.csv", @"Data")]
109+
public async Task Get_related_to_many_included()
110+
{
111+
using (var effortConnection = GetEffortConnection())
112+
{
113+
var response = await SubmitGet(effortConnection, "posts/201/comments?include=author");
114+
115+
await AssertResponseContent(response, @"Fixtures\FetchingResources\Get_related_to_many_include_response.json", HttpStatusCode.OK);
116+
}
117+
}
118+
102119
[TestMethod]
103120
[DeploymentItem(@"Data\Comment.csv", @"Data")]
104121
[DeploymentItem(@"Data\Post.csv", @"Data")]
@@ -115,6 +132,23 @@ public async Task Get_related_to_one()
115132
}
116133
}
117134

135+
136+
[TestMethod]
137+
[DeploymentItem(@"Data\Comment.csv", @"Data")]
138+
[DeploymentItem(@"Data\Post.csv", @"Data")]
139+
[DeploymentItem(@"Data\PostTagLink.csv", @"Data")]
140+
[DeploymentItem(@"Data\Tag.csv", @"Data")]
141+
[DeploymentItem(@"Data\User.csv", @"Data")]
142+
public async Task Get_included_to_one()
143+
{
144+
using (var effortConnection = GetEffortConnection())
145+
{
146+
var response = await SubmitGet(effortConnection, "posts/201?include=author");
147+
148+
await AssertResponseContent(response, @"Fixtures\FetchingResources\Get_included_to_one_response.json", HttpStatusCode.OK);
149+
}
150+
}
151+
118152
[TestMethod]
119153
[DeploymentItem(@"Data\Comment.csv", @"Data")]
120154
[DeploymentItem(@"Data\Post.csv", @"Data")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"data": {
3+
"type": "posts",
4+
"id": "230",
5+
"attributes": {
6+
"content": "The server generated my ID",
7+
"created": "2015-04-13T09:09:00.0000000+00:00",
8+
"title": "New post"
9+
},
10+
"relationships": {
11+
"author": {
12+
"links": {
13+
"self": "https://www.example.com/posts/230/relationships/author",
14+
"related": "https://www.example.com/posts/230/author"
15+
},
16+
"data": {
17+
"type": "users",
18+
"id": "401"
19+
}
20+
},
21+
"comments": {
22+
"links": {
23+
"self": "https://www.example.com/posts/230/relationships/comments",
24+
"related": "https://www.example.com/posts/230/comments"
25+
}
26+
},
27+
"tags": {
28+
"links": {
29+
"self": "https://www.example.com/posts/230/relationships/tags",
30+
"related": "https://www.example.com/posts/230/tags"
31+
}
32+
}
33+
}
34+
},
35+
"included": [
36+
{
37+
"type": "users",
38+
"id": "401",
39+
"attributes": {
40+
"first-name": "Alice",
41+
"last-name": "Smith"
42+
},
43+
"relationships": {
44+
"comments": {
45+
"links": {
46+
"self": "https://www.example.com/users/401/relationships/comments",
47+
"related": "https://www.example.com/users/401/comments"
48+
}
49+
},
50+
"posts": {
51+
"links": {
52+
"self": "https://www.example.com/users/401/relationships/posts",
53+
"related": "https://www.example.com/users/401/posts"
54+
}
55+
},
56+
"user-groups": {
57+
"links": {
58+
"self": "https://www.example.com/users/401/relationships/user-groups",
59+
"related": "https://www.example.com/users/401/user-groups"
60+
}
61+
}
62+
}
63+
}
64+
]
65+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"data": {
3+
"type": "posts",
4+
"id": "201",
5+
"attributes": {
6+
"content": "Post 1 content",
7+
"created": "2015-01-31T14:00:00.0000000+00:00",
8+
"title": "Post 1"
9+
},
10+
"relationships": {
11+
"author": {
12+
"links": {
13+
"self": "https://www.example.com/posts/201/relationships/author",
14+
"related": "https://www.example.com/posts/201/author"
15+
},
16+
"data": {
17+
"type": "users",
18+
"id": "401"
19+
}
20+
},
21+
"comments": {
22+
"links": {
23+
"self": "https://www.example.com/posts/201/relationships/comments",
24+
"related": "https://www.example.com/posts/201/comments"
25+
}
26+
},
27+
"tags": {
28+
"links": {
29+
"self": "https://www.example.com/posts/201/relationships/tags",
30+
"related": "https://www.example.com/posts/201/tags"
31+
}
32+
}
33+
}
34+
},
35+
"included": [
36+
{
37+
"type": "users",
38+
"id": "401",
39+
"attributes": {
40+
"first-name": "Alice",
41+
"last-name": "Smith"
42+
},
43+
"relationships": {
44+
"comments": {
45+
"links": {
46+
"self": "https://www.example.com/users/401/relationships/comments",
47+
"related": "https://www.example.com/users/401/comments"
48+
}
49+
},
50+
"posts": {
51+
"links": {
52+
"self": "https://www.example.com/users/401/relationships/posts",
53+
"related": "https://www.example.com/users/401/posts"
54+
}
55+
},
56+
"user-groups": {
57+
"links": {
58+
"self": "https://www.example.com/users/401/relationships/user-groups",
59+
"related": "https://www.example.com/users/401/user-groups"
60+
}
61+
}
62+
}
63+
}
64+
]
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"data": [
3+
{
4+
"type": "comments",
5+
"id": "101",
6+
"attributes": {
7+
"created": "2015-01-31T14:30:00.0000000+00:00",
8+
"text": "Comment 1"
9+
},
10+
"relationships": {
11+
"author": {
12+
"links": {
13+
"self": "https://www.example.com/comments/101/relationships/author",
14+
"related": "https://www.example.com/comments/101/author"
15+
},
16+
"data": {
17+
"type": "users",
18+
"id": "403"
19+
}
20+
},
21+
"post": {
22+
"links": {
23+
"self": "https://www.example.com/comments/101/relationships/post",
24+
"related": "https://www.example.com/comments/101/post"
25+
}
26+
}
27+
}
28+
},
29+
{
30+
"type": "comments",
31+
"id": "102",
32+
"attributes": {
33+
"created": "2015-01-31T14:35:00.0000000+00:00",
34+
"text": "Comment 2"
35+
},
36+
"relationships": {
37+
"author": {
38+
"links": {
39+
"self": "https://www.example.com/comments/102/relationships/author",
40+
"related": "https://www.example.com/comments/102/author"
41+
},
42+
"data": {
43+
"type": "users",
44+
"id": "402"
45+
}
46+
},
47+
"post": {
48+
"links": {
49+
"self": "https://www.example.com/comments/102/relationships/post",
50+
"related": "https://www.example.com/comments/102/post"
51+
}
52+
}
53+
}
54+
},
55+
{
56+
"type": "comments",
57+
"id": "103",
58+
"attributes": {
59+
"created": "2015-01-31T14:41:00.0000000+00:00",
60+
"text": "Comment 3"
61+
},
62+
"relationships": {
63+
"author": {
64+
"links": {
65+
"self": "https://www.example.com/comments/103/relationships/author",
66+
"related": "https://www.example.com/comments/103/author"
67+
},
68+
"data": {
69+
"type": "users",
70+
"id": "403"
71+
}
72+
},
73+
"post": {
74+
"links": {
75+
"self": "https://www.example.com/comments/103/relationships/post",
76+
"related": "https://www.example.com/comments/103/post"
77+
}
78+
}
79+
}
80+
}
81+
],
82+
"included": [
83+
{
84+
"type": "users",
85+
"id": "403",
86+
"attributes": {
87+
"first-name": "Charlie",
88+
"last-name": "Michaels"
89+
},
90+
"relationships": {
91+
"comments": {
92+
"links": {
93+
"self": "https://www.example.com/users/403/relationships/comments",
94+
"related": "https://www.example.com/users/403/comments"
95+
}
96+
},
97+
"posts": {
98+
"links": {
99+
"self": "https://www.example.com/users/403/relationships/posts",
100+
"related": "https://www.example.com/users/403/posts"
101+
}
102+
},
103+
"user-groups": {
104+
"links": {
105+
"self": "https://www.example.com/users/403/relationships/user-groups",
106+
"related": "https://www.example.com/users/403/user-groups"
107+
}
108+
}
109+
}
110+
},
111+
{
112+
"type": "users",
113+
"id": "402",
114+
"attributes": {
115+
"first-name": "Bob",
116+
"last-name": "Jones"
117+
},
118+
"relationships": {
119+
"comments": {
120+
"links": {
121+
"self": "https://www.example.com/users/402/relationships/comments",
122+
"related": "https://www.example.com/users/402/comments"
123+
}
124+
},
125+
"posts": {
126+
"links": {
127+
"self": "https://www.example.com/users/402/relationships/posts",
128+
"related": "https://www.example.com/users/402/posts"
129+
}
130+
},
131+
"user-groups": {
132+
"links": {
133+
"self": "https://www.example.com/users/402/relationships/user-groups",
134+
"related": "https://www.example.com/users/402/user-groups"
135+
}
136+
}
137+
}
138+
}
139+
]
140+
}

0 commit comments

Comments
 (0)