Skip to content

Commit 5e560dd

Browse files
committed
Fix skipped test and add test for duplicate links
1 parent db788a5 commit 5e560dd

File tree

1 file changed

+86
-56
lines changed

1 file changed

+86
-56
lines changed

test/adapter/json_api/linked_test.rb

Lines changed: 86 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def setup
3939
@bio2.author = @author2
4040
end
4141

42-
def test_include_multiple_posts_and_linked
42+
def test_include_multiple_posts_and_linked_array
4343
serializer = ArraySerializer.new([@first_post, @second_post])
4444
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
4545
serializer,
@@ -51,80 +51,80 @@ def test_include_multiple_posts_and_linked
5151
)
5252

5353
expected = {
54-
linked: {
55-
comments: [
56-
{
57-
id: "1",
58-
body: "ZOMG A COMMENT",
59-
links: {
60-
post: { linkage: { type: "posts", id: "1" } },
61-
author: { linkage: nil }
62-
}
63-
}, {
64-
id: "2",
65-
body: "ZOMG ANOTHER COMMENT",
66-
links: {
67-
post: { linkage: { type: "posts", id: "1" } },
68-
author: { linkage: nil }
69-
}
70-
}
71-
],
72-
authors: [
73-
{
74-
id: "1",
75-
name: "Steve K.",
76-
links: {
77-
posts: { linkage: [ { type: "posts", id: "1" }, { type: "posts", id: "3" } ] },
78-
roles: { linkage: [] },
79-
bio: { linkage: { type: "bios", id: "1" } }
80-
}
81-
}, {
82-
id: "2",
83-
name: "Tenderlove",
84-
links: {
85-
posts: { linkage: [ { type: "posts", id:"2" } ] },
86-
roles: { linkage: [] },
87-
bio: { linkage: { type: "bios", id: "2" } }
88-
}
89-
}
90-
],
91-
bios: [
92-
{
93-
id: "1",
94-
content: "AMS Contributor",
95-
links: {
96-
author: { linkage: { type: "authors", id: "1" } }
97-
}
98-
}, {
99-
id: "2",
100-
content: "Rails Contributor",
101-
links: {
102-
author: { linkage: { type: "authors", id: "2" } }
103-
}
104-
}
105-
]
106-
},
107-
posts: [
54+
data: [
10855
{
10956
id: "10",
11057
title: "Hello!!",
11158
body: "Hello, world!!",
59+
type: "posts",
11260
links: {
11361
comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
11462
blog: { linkage: { type: "blogs", id: "999" } },
11563
author: { linkage: { type: "authors", id: "1" } }
11664
}
11765
},
11866
{
119-
id: "2",
67+
id: "20",
12068
title: "New Post",
12169
body: "Body",
70+
type: "posts",
12271
links: {
12372
comments: { linkage: [] },
12473
blog: { linkage: { type: "blogs", id: "999" } },
12574
author: { linkage: { type: "authors", id: "2" } }
12675
}
12776
}
77+
],
78+
included: [
79+
{
80+
id: "1",
81+
body: "ZOMG A COMMENT",
82+
type: "comments",
83+
links: {
84+
post: { linkage: { type: "posts", id: "10" } },
85+
author: { linkage: nil }
86+
}
87+
}, {
88+
id: "2",
89+
body: "ZOMG ANOTHER COMMENT",
90+
type: "comments",
91+
links: {
92+
post: { linkage: { type: "posts", id: "10" } },
93+
author: { linkage: nil }
94+
}
95+
}, {
96+
id: "1",
97+
name: "Steve K.",
98+
type: "authors",
99+
links: {
100+
posts: { linkage: [ { type: "posts", id: "10" }, { type: "posts", id: "30" } ] },
101+
roles: { linkage: [] },
102+
bio: { linkage: { type: "bios", id: "1" } }
103+
}
104+
}, {
105+
id: "1",
106+
content: "AMS Contributor",
107+
type: "bios",
108+
links: {
109+
author: { linkage: { type: "authors", id: "1" } }
110+
}
111+
}, {
112+
id: "2",
113+
name: "Tenderlove",
114+
type: "authors",
115+
links: {
116+
posts: { linkage: [ { type: "posts", id:"20" } ] },
117+
roles: { linkage: [] },
118+
bio: { linkage: { type: "bios", id: "2" } }
119+
}
120+
}, {
121+
id: "2",
122+
content: "Rails Contributor",
123+
type: "bios",
124+
links: {
125+
author: { linkage: { type: "authors", id: "2" } }
126+
}
127+
}
128128
]
129129
}
130130
assert_equal expected, adapter.serializable_hash
@@ -195,6 +195,36 @@ def test_ignore_model_namespace_for_linked_resource_type
195195
}
196196
assert_equal expected, links
197197
end
198+
199+
def test_multiple_references_to_same_resource
200+
serializer = ArraySerializer.new([@first_comment, @second_comment])
201+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
202+
serializer,
203+
include: ['post']
204+
)
205+
206+
expected = [
207+
{
208+
id: "10",
209+
title: "Hello!!",
210+
body: "Hello, world!!",
211+
type: "posts",
212+
links: {
213+
comments: {
214+
linkage: [{type: "comments", id: "1"}, {type: "comments", id: "2"}]
215+
},
216+
blog: {
217+
linkage: {type: "blogs", id: "999"}
218+
},
219+
author: {
220+
linkage: {type: "authors", id: "1"}
221+
}
222+
}
223+
}
224+
]
225+
226+
assert_equal expected, adapter.serializable_hash[:included]
227+
end
198228
end
199229
end
200230
end

0 commit comments

Comments
 (0)