Fix skipped test and add test for duplicate links

This commit is contained in:
Mateo Murphy 2015-03-24 19:46:30 -04:00
parent db788a5f68
commit 5e560ddbef

View File

@ -39,7 +39,7 @@ module ActiveModel
@bio2.author = @author2 @bio2.author = @author2
end end
def test_include_multiple_posts_and_linked def test_include_multiple_posts_and_linked_array
serializer = ArraySerializer.new([@first_post, @second_post]) serializer = ArraySerializer.new([@first_post, @second_post])
adapter = ActiveModel::Serializer::Adapter::JsonApi.new( adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
serializer, serializer,
@ -51,64 +51,12 @@ module ActiveModel
) )
expected = { expected = {
linked: { data: [
comments: [
{
id: "1",
body: "ZOMG A COMMENT",
links: {
post: { linkage: { type: "posts", id: "1" } },
author: { linkage: nil }
}
}, {
id: "2",
body: "ZOMG ANOTHER COMMENT",
links: {
post: { linkage: { type: "posts", id: "1" } },
author: { linkage: nil }
}
}
],
authors: [
{
id: "1",
name: "Steve K.",
links: {
posts: { linkage: [ { type: "posts", id: "1" }, { type: "posts", id: "3" } ] },
roles: { linkage: [] },
bio: { linkage: { type: "bios", id: "1" } }
}
}, {
id: "2",
name: "Tenderlove",
links: {
posts: { linkage: [ { type: "posts", id:"2" } ] },
roles: { linkage: [] },
bio: { linkage: { type: "bios", id: "2" } }
}
}
],
bios: [
{
id: "1",
content: "AMS Contributor",
links: {
author: { linkage: { type: "authors", id: "1" } }
}
}, {
id: "2",
content: "Rails Contributor",
links: {
author: { linkage: { type: "authors", id: "2" } }
}
}
]
},
posts: [
{ {
id: "10", id: "10",
title: "Hello!!", title: "Hello!!",
body: "Hello, world!!", body: "Hello, world!!",
type: "posts",
links: { links: {
comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] }, comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
blog: { linkage: { type: "blogs", id: "999" } }, blog: { linkage: { type: "blogs", id: "999" } },
@ -116,15 +64,67 @@ module ActiveModel
} }
}, },
{ {
id: "2", id: "20",
title: "New Post", title: "New Post",
body: "Body", body: "Body",
type: "posts",
links: { links: {
comments: { linkage: [] }, comments: { linkage: [] },
blog: { linkage: { type: "blogs", id: "999" } }, blog: { linkage: { type: "blogs", id: "999" } },
author: { linkage: { type: "authors", id: "2" } } author: { linkage: { type: "authors", id: "2" } }
} }
} }
],
included: [
{
id: "1",
body: "ZOMG A COMMENT",
type: "comments",
links: {
post: { linkage: { type: "posts", id: "10" } },
author: { linkage: nil }
}
}, {
id: "2",
body: "ZOMG ANOTHER COMMENT",
type: "comments",
links: {
post: { linkage: { type: "posts", id: "10" } },
author: { linkage: nil }
}
}, {
id: "1",
name: "Steve K.",
type: "authors",
links: {
posts: { linkage: [ { type: "posts", id: "10" }, { type: "posts", id: "30" } ] },
roles: { linkage: [] },
bio: { linkage: { type: "bios", id: "1" } }
}
}, {
id: "1",
content: "AMS Contributor",
type: "bios",
links: {
author: { linkage: { type: "authors", id: "1" } }
}
}, {
id: "2",
name: "Tenderlove",
type: "authors",
links: {
posts: { linkage: [ { type: "posts", id:"20" } ] },
roles: { linkage: [] },
bio: { linkage: { type: "bios", id: "2" } }
}
}, {
id: "2",
content: "Rails Contributor",
type: "bios",
links: {
author: { linkage: { type: "authors", id: "2" } }
}
}
] ]
} }
assert_equal expected, adapter.serializable_hash assert_equal expected, adapter.serializable_hash
@ -195,6 +195,36 @@ module ActiveModel
} }
assert_equal expected, links assert_equal expected, links
end end
def test_multiple_references_to_same_resource
serializer = ArraySerializer.new([@first_comment, @second_comment])
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
serializer,
include: ['post']
)
expected = [
{
id: "10",
title: "Hello!!",
body: "Hello, world!!",
type: "posts",
links: {
comments: {
linkage: [{type: "comments", id: "1"}, {type: "comments", id: "2"}]
},
blog: {
linkage: {type: "blogs", id: "999"}
},
author: {
linkage: {type: "authors", id: "1"}
}
}
}
]
assert_equal expected, adapter.serializable_hash[:included]
end
end end
end end
end end