mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Merge remote-tracking branch 'upstream/master' into improve-tests
This commit is contained in:
@@ -4,9 +4,9 @@ module ActiveModel
|
||||
class Adapter
|
||||
class FragmentCacheTest < Minitest::Test
|
||||
def setup
|
||||
@spam = Spam::UnrelatedLink.new(id: "spam-id-1")
|
||||
@spam = Spam::UnrelatedLink.new(id: 'spam-id-1')
|
||||
@author = Author.new(name: 'Joao M. D. Moura')
|
||||
@role = Role.new(name: 'Great Author', description:nil)
|
||||
@role = Role.new(name: 'Great Author', description: nil)
|
||||
@role.author = [@author]
|
||||
@role_serializer = RoleSerializer.new(@role)
|
||||
@spam_serializer = Spam::UnrelatedLinkSerializer.new(@spam)
|
||||
|
||||
@@ -14,7 +14,7 @@ module ActiveModel
|
||||
@comment.post = @post
|
||||
@comment.author = nil
|
||||
@anonymous_post.author = nil
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
||||
@post.blog = @blog
|
||||
@anonymous_post.blog = nil
|
||||
|
||||
@@ -24,21 +24,21 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_includes_post
|
||||
assert_equal({id: 42, title: 'New Post', body: 'Body'}, @adapter.serializable_hash[:comment][:post])
|
||||
assert_equal({ id: 42, title: 'New Post', body: 'Body' }, @adapter.serializable_hash[:comment][:post])
|
||||
end
|
||||
|
||||
def test_include_nil_author
|
||||
serializer = PostSerializer.new(@anonymous_post)
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||
|
||||
assert_equal({post: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: {id: 999, name: "Custom blog"}, author: nil}}, adapter.serializable_hash)
|
||||
assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], blog: { id: 999, name: 'Custom blog' }, author: nil } }, adapter.serializable_hash)
|
||||
end
|
||||
|
||||
def test_include_nil_author_with_specified_serializer
|
||||
serializer = PostPreviewSerializer.new(@anonymous_post)
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||
|
||||
assert_equal({post: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}}, adapter.serializable_hash)
|
||||
assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], author: nil } }, adapter.serializable_hash)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,7 +13,7 @@ module ActiveModel
|
||||
@second_post.comments = []
|
||||
@first_post.author = @author
|
||||
@second_post.author = @author
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
||||
@first_post.blog = @blog
|
||||
@second_post.blog = nil
|
||||
|
||||
@@ -21,16 +21,16 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_with_serializer_option
|
||||
@blog.special_attribute = "Special"
|
||||
@blog.special_attribute = 'Special'
|
||||
@blog.articles = [@first_post, @second_post]
|
||||
serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||
|
||||
expected = {blogs:[{
|
||||
expected = { blogs: [{
|
||||
id: 1,
|
||||
special_attribute: "Special",
|
||||
articles: [{id: 1,title: "Hello!!", body: "Hello, world!!"}, {id: 2, title: "New Post", body: "Body"}]
|
||||
}]}
|
||||
special_attribute: 'Special',
|
||||
articles: [{ id: 1, title: 'Hello!!', body: 'Hello, world!!' }, { id: 2, title: 'New Post', body: 'Body' }]
|
||||
}] }
|
||||
assert_equal expected, adapter.serializable_hash
|
||||
end
|
||||
|
||||
@@ -39,32 +39,32 @@ module ActiveModel
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||
|
||||
expected = { posts: [{
|
||||
title: "Hello!!",
|
||||
body: "Hello, world!!",
|
||||
title: 'Hello!!',
|
||||
body: 'Hello, world!!',
|
||||
id: 1,
|
||||
comments: [],
|
||||
author: {
|
||||
id: 1,
|
||||
name: "Steve K."
|
||||
name: 'Steve K.'
|
||||
},
|
||||
blog: {
|
||||
id: 999,
|
||||
name: "Custom blog"
|
||||
name: 'Custom blog'
|
||||
}
|
||||
}, {
|
||||
title: "New Post",
|
||||
body: "Body",
|
||||
title: 'New Post',
|
||||
body: 'Body',
|
||||
id: 2,
|
||||
comments: [],
|
||||
author: {
|
||||
id: 1,
|
||||
name: "Steve K."
|
||||
name: 'Steve K.'
|
||||
},
|
||||
blog: {
|
||||
id: 999,
|
||||
name: "Custom blog"
|
||||
name: 'Custom blog'
|
||||
}
|
||||
}]}
|
||||
}] }
|
||||
assert_equal expected, adapter.serializable_hash
|
||||
end
|
||||
|
||||
|
||||
@@ -15,9 +15,9 @@ module ActiveModel
|
||||
@post.author = @author
|
||||
@first_comment.post = @post
|
||||
@second_comment.post = @post
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
||||
@post.blog = @blog
|
||||
@tag = Tag.new(id: 1, name: "#hash_tag")
|
||||
@tag = Tag.new(id: 1, name: '#hash_tag')
|
||||
@post.tags = [@tag]
|
||||
end
|
||||
|
||||
@@ -25,8 +25,8 @@ module ActiveModel
|
||||
serializer = PostSerializer.new(@post)
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||
assert_equal([
|
||||
{id: 1, body: 'ZOMG A COMMENT'},
|
||||
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
|
||||
{ id: 1, body: 'ZOMG A COMMENT' },
|
||||
{ id: 2, body: 'ZOMG ANOTHER COMMENT' }
|
||||
], adapter.serializable_hash[:post][:comments])
|
||||
end
|
||||
|
||||
@@ -36,7 +36,7 @@ module ActiveModel
|
||||
assert_equal({
|
||||
id: 42,
|
||||
tags: [
|
||||
{"attributes"=>{"id"=>1, "name"=>"#hash_tag"}}
|
||||
{ 'attributes' => { 'id' => 1, 'name' => '#hash_tag' } }
|
||||
]
|
||||
}.to_json, adapter.serializable_hash[:post].to_json)
|
||||
end
|
||||
|
||||
@@ -21,7 +21,7 @@ module ActiveModel
|
||||
@comment.author = nil
|
||||
@post.author = @author
|
||||
@anonymous_post.author = nil
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
||||
@blog.writer = @author
|
||||
@blog.articles = [@post, @anonymous_post]
|
||||
@author.posts = []
|
||||
@@ -32,7 +32,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_includes_post_id
|
||||
expected = { data: { type: "posts", id: "42" } }
|
||||
expected = { data: { type: 'posts', id: '42' } }
|
||||
|
||||
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:post])
|
||||
end
|
||||
@@ -40,33 +40,33 @@ module ActiveModel
|
||||
def test_includes_linked_post
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post')
|
||||
expected = [{
|
||||
id: "42",
|
||||
type: "posts",
|
||||
id: '42',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: 'New Post',
|
||||
body: 'Body',
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [ { type: "comments", id: "1" } ] },
|
||||
blog: { data: { type: "blogs", id: "999" } },
|
||||
author: { data: { type: "authors", id: "1" } }
|
||||
comments: { data: [{ type: 'comments', id: '1' }] },
|
||||
blog: { data: { type: 'blogs', id: '999' } },
|
||||
author: { data: { type: 'authors', id: '1' } }
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:included]
|
||||
end
|
||||
|
||||
def test_limiting_linked_post_fields
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post', fields: {post: [:title]})
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post', fields: { post: [:title] })
|
||||
expected = [{
|
||||
id: "42",
|
||||
type: "posts",
|
||||
id: '42',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: 'New Post'
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [ { type: "comments", id: "1" } ] },
|
||||
blog: { data: { type: "blogs", id: "999" } },
|
||||
author: { data: { type: "authors", id: "1" } }
|
||||
comments: { data: [{ type: 'comments', id: '1' }] },
|
||||
blog: { data: { type: 'blogs', id: '999' } },
|
||||
author: { data: { type: 'authors', id: '1' } }
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:included]
|
||||
@@ -76,7 +76,7 @@ module ActiveModel
|
||||
serializer = PostSerializer.new(@anonymous_post)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||
|
||||
assert_equal({comments: { data: [] }, blog: { data: { type: "blogs", id: "999" } }, author: { data: nil }}, adapter.serializable_hash[:data][:relationships])
|
||||
assert_equal({ comments: { data: [] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: nil } }, adapter.serializable_hash[:data][:relationships])
|
||||
end
|
||||
|
||||
def test_include_type_for_association_when_different_than_name
|
||||
@@ -86,19 +86,19 @@ module ActiveModel
|
||||
expected = {
|
||||
writer: {
|
||||
data: {
|
||||
type: "authors",
|
||||
id: "1"
|
||||
type: 'authors',
|
||||
id: '1'
|
||||
}
|
||||
},
|
||||
articles: {
|
||||
data: [
|
||||
{
|
||||
type: "posts",
|
||||
id: "42"
|
||||
type: 'posts',
|
||||
id: '42'
|
||||
},
|
||||
{
|
||||
type: "posts",
|
||||
id: "43"
|
||||
type: 'posts',
|
||||
id: '43'
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -108,42 +108,42 @@ module ActiveModel
|
||||
|
||||
def test_include_linked_resources_with_type_name
|
||||
serializer = BlogSerializer.new(@blog)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: ['writer', 'articles'])
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: %w(writer articles))
|
||||
linked = adapter.serializable_hash[:included]
|
||||
expected = [
|
||||
{
|
||||
id: "1",
|
||||
type: "authors",
|
||||
id: '1',
|
||||
type: 'authors',
|
||||
attributes: {
|
||||
name: "Steve K."
|
||||
name: 'Steve K.'
|
||||
},
|
||||
relationships: {
|
||||
posts: { data: [] },
|
||||
roles: { data: [] },
|
||||
bio: { data: nil }
|
||||
}
|
||||
},{
|
||||
id: "42",
|
||||
type: "posts",
|
||||
}, {
|
||||
id: '42',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: "New Post",
|
||||
body: "Body"
|
||||
title: 'New Post',
|
||||
body: 'Body'
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [ { type: "comments", id: "1" } ] },
|
||||
blog: { data: { type: "blogs", id: "999" } },
|
||||
author: { data: { type: "authors", id: "1" } }
|
||||
comments: { data: [{ type: 'comments', id: '1' }] },
|
||||
blog: { data: { type: 'blogs', id: '999' } },
|
||||
author: { data: { type: 'authors', id: '1' } }
|
||||
}
|
||||
}, {
|
||||
id: "43",
|
||||
type: "posts",
|
||||
id: '43',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: "Hello!!",
|
||||
body: "Hello, world!!"
|
||||
title: 'Hello!!',
|
||||
body: 'Hello, world!!'
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [] },
|
||||
blog: { data: { type: "blogs", id: "999" } },
|
||||
blog: { data: { type: 'blogs', id: '999' } },
|
||||
author: { data: nil }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,29 +27,29 @@ module ActiveModel
|
||||
def test_include_multiple_posts
|
||||
expected = [
|
||||
{
|
||||
id: "1",
|
||||
type: "posts",
|
||||
id: '1',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: "Hello!!",
|
||||
body: "Hello, world!!"
|
||||
title: 'Hello!!',
|
||||
body: 'Hello, world!!'
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [] },
|
||||
blog: { data: { type: "blogs", id: "999" } },
|
||||
author: { data: { type: "authors", id: "1" } }
|
||||
blog: { data: { type: 'blogs', id: '999' } },
|
||||
author: { data: { type: 'authors', id: '1' } }
|
||||
}
|
||||
},
|
||||
{
|
||||
id: "2",
|
||||
type: "posts",
|
||||
id: '2',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: "New Post",
|
||||
body: "Body"
|
||||
title: 'New Post',
|
||||
body: 'Body'
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [] },
|
||||
blog: { data: { type: "blogs", id: "999" } },
|
||||
author: { data: { type: "authors", id: "1" } }
|
||||
blog: { data: { type: 'blogs', id: '999' } },
|
||||
author: { data: { type: 'authors', id: '1' } }
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -62,33 +62,32 @@ module ActiveModel
|
||||
|
||||
expected = [
|
||||
{
|
||||
id: "1",
|
||||
type: "posts",
|
||||
id: '1',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: "Hello!!"
|
||||
title: 'Hello!!'
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [] },
|
||||
blog: { data: { type: "blogs", id: "999" } },
|
||||
author: { data: { type: "authors", id: "1" } }
|
||||
blog: { data: { type: 'blogs', id: '999' } },
|
||||
author: { data: { type: 'authors', id: '1' } }
|
||||
}
|
||||
},
|
||||
{
|
||||
id: "2",
|
||||
type: "posts",
|
||||
id: '2',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: "New Post"
|
||||
title: 'New Post'
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [] },
|
||||
blog: { data: { type: "blogs", id: "999" } },
|
||||
author: { data: { type: "authors", id: "1" } }
|
||||
blog: { data: { type: 'blogs', id: '999' } },
|
||||
author: { data: { type: 'authors', id: '1' } }
|
||||
}
|
||||
}
|
||||
]
|
||||
assert_equal(expected, @adapter.serializable_hash[:data])
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -27,8 +27,8 @@ module ActiveModel
|
||||
def test_includes_comment_ids
|
||||
expected = {
|
||||
data: [
|
||||
{ type: "posts", id: "1"},
|
||||
{ type: "posts", id: "2"}
|
||||
{ type: 'posts', id: '1' },
|
||||
{ type: 'posts', id: '2' }
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ module ActiveModel
|
||||
@serializer = PostPreviewSerializer.new(@post)
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
|
||||
@serializer,
|
||||
include: ['comments', 'author']
|
||||
include: %w(comments author)
|
||||
)
|
||||
end
|
||||
|
||||
@@ -58,9 +58,9 @@ module ActiveModel
|
||||
},
|
||||
{
|
||||
id: @author.id.to_s,
|
||||
type: "authors",
|
||||
type: 'authors',
|
||||
relationships: {
|
||||
posts: { data: [ {type: "posts", id: @post.id.to_s } ] }
|
||||
posts: { data: [{ type: 'posts', id: @post.id.to_s }] }
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -70,7 +70,7 @@ module ActiveModel
|
||||
|
||||
def test_includes_author_id
|
||||
expected = {
|
||||
data: { type: "authors", id: @author.id.to_s }
|
||||
data: { type: 'authors', id: @author.id.to_s }
|
||||
}
|
||||
|
||||
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
|
||||
|
||||
@@ -22,12 +22,12 @@ module ActiveModel
|
||||
@second_comment.post = @post
|
||||
@post.author = @author
|
||||
@post_without_comments.author = nil
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
||||
@blog.writer = @author
|
||||
@blog.articles = [@post]
|
||||
@post.blog = @blog
|
||||
@post_without_comments.blog = nil
|
||||
@tag = Tag.new(id: 1, name: "#hash_tag")
|
||||
@tag = Tag.new(id: 1, name: '#hash_tag')
|
||||
@post.tags = [@tag]
|
||||
@serializer = PostSerializer.new(@post)
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
|
||||
@@ -36,7 +36,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_includes_comment_ids
|
||||
expected = { data: [ { type: "comments", id: "1" }, { type: "comments", id: "2" } ] }
|
||||
expected = { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] }
|
||||
|
||||
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
|
||||
end
|
||||
@@ -44,23 +44,23 @@ module ActiveModel
|
||||
def test_includes_linked_comments
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments')
|
||||
expected = [{
|
||||
id: "1",
|
||||
type: "comments",
|
||||
id: '1',
|
||||
type: 'comments',
|
||||
attributes: {
|
||||
body: 'ZOMG A COMMENT'
|
||||
},
|
||||
relationships: {
|
||||
post: { data: { type: "posts", id: "1" } },
|
||||
post: { data: { type: 'posts', id: '1' } },
|
||||
author: { data: nil }
|
||||
}
|
||||
}, {
|
||||
id: "2",
|
||||
type: "comments",
|
||||
id: '2',
|
||||
type: 'comments',
|
||||
attributes: {
|
||||
body: 'ZOMG ANOTHER COMMENT'
|
||||
},
|
||||
relationships: {
|
||||
post: { data: { type: "posts", id: "1" } },
|
||||
post: { data: { type: 'posts', id: '1' } },
|
||||
author: { data: nil }
|
||||
}
|
||||
}]
|
||||
@@ -68,19 +68,19 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_limit_fields_of_linked_comments
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:id]})
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: { comment: [:id] })
|
||||
expected = [{
|
||||
id: "1",
|
||||
type: "comments",
|
||||
id: '1',
|
||||
type: 'comments',
|
||||
relationships: {
|
||||
post: { data: { type: "posts", id: "1" } },
|
||||
post: { data: { type: 'posts', id: '1' } },
|
||||
author: { data: nil }
|
||||
}
|
||||
}, {
|
||||
id: "2",
|
||||
type: "comments",
|
||||
id: '2',
|
||||
type: 'comments',
|
||||
relationships: {
|
||||
post: { data: { type: "posts", id: "1" } },
|
||||
post: { data: { type: 'posts', id: '1' } },
|
||||
author: { data: nil }
|
||||
}
|
||||
}]
|
||||
@@ -101,8 +101,8 @@ module ActiveModel
|
||||
|
||||
expected = {
|
||||
data: [{
|
||||
type: "posts",
|
||||
id: "1"
|
||||
type: 'posts',
|
||||
id: '1'
|
||||
}]
|
||||
}
|
||||
assert_equal expected, actual
|
||||
@@ -114,10 +114,10 @@ module ActiveModel
|
||||
|
||||
assert_equal({
|
||||
data: {
|
||||
id: "1",
|
||||
type: "posts",
|
||||
id: '1',
|
||||
type: 'posts',
|
||||
relationships: {
|
||||
tags: { data: [@tag.as_json]}
|
||||
tags: { data: [@tag.as_json] }
|
||||
}
|
||||
}
|
||||
}, adapter.serializable_hash)
|
||||
@@ -129,11 +129,11 @@ module ActiveModel
|
||||
|
||||
assert_equal({
|
||||
data: {
|
||||
id: "1",
|
||||
type: "virtual_values",
|
||||
id: '1',
|
||||
type: 'virtual_values',
|
||||
relationships: {
|
||||
maker: {data: {id: 1}},
|
||||
reviews: {data: [{id: 1}, {id: 2}]}
|
||||
maker: { data: { id: 1 } },
|
||||
reviews: { data: [{ id: 1 }, { id: 2 }] }
|
||||
}
|
||||
}
|
||||
}, adapter.serializable_hash)
|
||||
|
||||
@@ -19,7 +19,7 @@ module ActiveModel
|
||||
@comment.author = nil
|
||||
@post.author = @author
|
||||
@anonymous_post.author = nil
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
||||
@blog.writer = @author
|
||||
@blog.articles = [@post, @anonymous_post]
|
||||
@author.posts = []
|
||||
@@ -32,7 +32,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_includes_bio_id
|
||||
expected = { data: { type: "bios", id: "43" } }
|
||||
expected = { data: { type: 'bios', id: '43' } }
|
||||
|
||||
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:bio])
|
||||
end
|
||||
@@ -42,14 +42,14 @@ module ActiveModel
|
||||
|
||||
expected = [
|
||||
{
|
||||
id: "43",
|
||||
type: "bios",
|
||||
id: '43',
|
||||
type: 'bios',
|
||||
attributes: {
|
||||
content:"AMS Contributor",
|
||||
content: 'AMS Contributor',
|
||||
rating: nil
|
||||
},
|
||||
relationships: {
|
||||
author: { data: { type: "authors", id: "1" } }
|
||||
author: { data: { type: 'authors', id: '1' } }
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -63,11 +63,11 @@ module ActiveModel
|
||||
|
||||
expected = {
|
||||
data: {
|
||||
id: "1",
|
||||
type: "virtual_values",
|
||||
id: '1',
|
||||
type: 'virtual_values',
|
||||
relationships: {
|
||||
maker: {data: {id: 1}},
|
||||
reviews: {data: [{id: 1}, {id: 2}]}
|
||||
maker: { data: { id: 1 } },
|
||||
reviews: { data: [{ id: 1 }, { id: 2 }] }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,9 +14,8 @@ module ActiveModel
|
||||
@first_comment.post = @post
|
||||
@second_comment.post = @post
|
||||
@post.author = @author
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
||||
@post.blog = @blog
|
||||
|
||||
end
|
||||
|
||||
def test_custom_keys
|
||||
@@ -25,11 +24,11 @@ module ActiveModel
|
||||
|
||||
assert_equal({
|
||||
reviews: { data: [
|
||||
{type: "comments", id: "1"},
|
||||
{type: "comments", id: "2"}
|
||||
]},
|
||||
writer: { data: {type: "authors", id: "1"} },
|
||||
site: { data: {type: "blogs", id: "1" } }
|
||||
{ type: 'comments', id: '1' },
|
||||
{ type: 'comments', id: '2' }
|
||||
] },
|
||||
writer: { data: { type: 'authors', id: '1' } },
|
||||
site: { data: { type: 'blogs', id: '1' } }
|
||||
}, adapter.serializable_hash[:data][:relationships])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -53,94 +53,94 @@ module ActiveModel
|
||||
expected = {
|
||||
data: [
|
||||
{
|
||||
id: "10",
|
||||
type: "posts",
|
||||
id: '10',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: "Hello!!",
|
||||
body: "Hello, world!!"
|
||||
title: 'Hello!!',
|
||||
body: 'Hello, world!!'
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
|
||||
blog: { data: { type: "blogs", id: "999" } },
|
||||
author: { data: { type: "authors", id: "1" } }
|
||||
comments: { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] },
|
||||
blog: { data: { type: 'blogs', id: '999' } },
|
||||
author: { data: { type: 'authors', id: '1' } }
|
||||
}
|
||||
},
|
||||
{
|
||||
id: "20",
|
||||
type: "posts",
|
||||
id: '20',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: "New Post",
|
||||
body: "Body"
|
||||
title: 'New Post',
|
||||
body: 'Body'
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [] },
|
||||
blog: { data: { type: "blogs", id: "999" } },
|
||||
author: { data: { type: "authors", id: "2" } }
|
||||
blog: { data: { type: 'blogs', id: '999' } },
|
||||
author: { data: { type: 'authors', id: '2' } }
|
||||
}
|
||||
}
|
||||
],
|
||||
included: [
|
||||
{
|
||||
id: "1",
|
||||
type: "comments",
|
||||
id: '1',
|
||||
type: 'comments',
|
||||
attributes: {
|
||||
body: "ZOMG A COMMENT"
|
||||
body: 'ZOMG A COMMENT'
|
||||
},
|
||||
relationships: {
|
||||
post: { data: { type: "posts", id: "10" } },
|
||||
post: { data: { type: 'posts', id: '10' } },
|
||||
author: { data: nil }
|
||||
}
|
||||
}, {
|
||||
id: "2",
|
||||
type: "comments",
|
||||
id: '2',
|
||||
type: 'comments',
|
||||
attributes: {
|
||||
body: "ZOMG ANOTHER COMMENT",
|
||||
body: 'ZOMG ANOTHER COMMENT',
|
||||
},
|
||||
relationships: {
|
||||
post: { data: { type: "posts", id: "10" } },
|
||||
post: { data: { type: 'posts', id: '10' } },
|
||||
author: { data: nil }
|
||||
}
|
||||
}, {
|
||||
id: "1",
|
||||
type: "authors",
|
||||
id: '1',
|
||||
type: 'authors',
|
||||
attributes: {
|
||||
name: "Steve K."
|
||||
name: 'Steve K.'
|
||||
},
|
||||
relationships: {
|
||||
posts: { data: [ { type: "posts", id: "10" }, { type: "posts", id: "30" } ] },
|
||||
posts: { data: [{ type: 'posts', id: '10' }, { type: 'posts', id: '30' }] },
|
||||
roles: { data: [] },
|
||||
bio: { data: { type: "bios", id: "1" } }
|
||||
bio: { data: { type: 'bios', id: '1' } }
|
||||
}
|
||||
}, {
|
||||
id: "1",
|
||||
type: "bios",
|
||||
id: '1',
|
||||
type: 'bios',
|
||||
attributes: {
|
||||
content: "AMS Contributor",
|
||||
content: 'AMS Contributor',
|
||||
rating: nil
|
||||
},
|
||||
relationships: {
|
||||
author: { data: { type: "authors", id: "1" } }
|
||||
author: { data: { type: 'authors', id: '1' } }
|
||||
}
|
||||
}, {
|
||||
id: "2",
|
||||
type: "authors",
|
||||
id: '2',
|
||||
type: 'authors',
|
||||
attributes: {
|
||||
name: "Tenderlove"
|
||||
name: 'Tenderlove'
|
||||
},
|
||||
relationships: {
|
||||
posts: { data: [ { type: "posts", id:"20" } ] },
|
||||
posts: { data: [{ type: 'posts', id: '20' }] },
|
||||
roles: { data: [] },
|
||||
bio: { data: { type: "bios", id: "2" } }
|
||||
bio: { data: { type: 'bios', id: '2' } }
|
||||
}
|
||||
}, {
|
||||
id: "2",
|
||||
type: "bios",
|
||||
id: '2',
|
||||
type: 'bios',
|
||||
attributes: {
|
||||
rating: nil,
|
||||
content: "Rails Contributor",
|
||||
content: 'Rails Contributor',
|
||||
},
|
||||
relationships: {
|
||||
author: { data: { type: "authors", id: "2" } }
|
||||
author: { data: { type: 'authors', id: '2' } }
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -162,39 +162,39 @@ module ActiveModel
|
||||
|
||||
expected = [
|
||||
{
|
||||
id: "1",
|
||||
type: "authors",
|
||||
id: '1',
|
||||
type: 'authors',
|
||||
attributes: {
|
||||
name: "Steve K."
|
||||
name: 'Steve K.'
|
||||
},
|
||||
relationships: {
|
||||
posts: { data: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] },
|
||||
posts: { data: [{ type: 'posts', id: '10' }, { type: 'posts', id: '30' }] },
|
||||
roles: { data: [] },
|
||||
bio: { data: { type: "bios", id: "1" }}
|
||||
bio: { data: { type: 'bios', id: '1' } }
|
||||
}
|
||||
}, {
|
||||
id: "10",
|
||||
type: "posts",
|
||||
id: '10',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: "Hello!!",
|
||||
body: "Hello, world!!"
|
||||
title: 'Hello!!',
|
||||
body: 'Hello, world!!'
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] },
|
||||
blog: { data: { type: "blogs", id: "999" } },
|
||||
author: { data: { type: "authors", id: "1" } }
|
||||
comments: { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] },
|
||||
blog: { data: { type: 'blogs', id: '999' } },
|
||||
author: { data: { type: 'authors', id: '1' } }
|
||||
}
|
||||
}, {
|
||||
id: "30",
|
||||
type: "posts",
|
||||
id: '30',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: "Yet Another Post",
|
||||
body: "Body"
|
||||
title: 'Yet Another Post',
|
||||
body: 'Body'
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [] },
|
||||
blog: { data: { type: "blogs", id: "999" } },
|
||||
author: { data: { type: "authors", id: "1" } }
|
||||
blog: { data: { type: 'blogs', id: '999' } },
|
||||
author: { data: { type: 'authors', id: '1' } }
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -229,21 +229,21 @@ module ActiveModel
|
||||
|
||||
expected = [
|
||||
{
|
||||
id: "10",
|
||||
type: "posts",
|
||||
id: '10',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: "Hello!!",
|
||||
body: "Hello, world!!"
|
||||
title: 'Hello!!',
|
||||
body: 'Hello, world!!'
|
||||
},
|
||||
relationships: {
|
||||
comments: {
|
||||
data: [{type: "comments", id: "1"}, {type: "comments", id: "2"}]
|
||||
data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }]
|
||||
},
|
||||
blog: {
|
||||
data: {type: "blogs", id: "999"}
|
||||
data: { type: 'blogs', id: '999' }
|
||||
},
|
||||
author: {
|
||||
data: {type: "authors", id: "1"}
|
||||
data: { type: 'authors', id: '1' }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -262,14 +262,14 @@ module ActiveModel
|
||||
|
||||
expected = {
|
||||
data: {
|
||||
id: "10",
|
||||
type: "posts",
|
||||
id: '10',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: "Hello!!",
|
||||
body: "Hello, world!!"
|
||||
title: 'Hello!!',
|
||||
body: 'Hello, world!!'
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
|
||||
comments: { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] },
|
||||
author: { data: nil }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,9 +20,9 @@ module ActiveModel
|
||||
]
|
||||
end
|
||||
|
||||
def mock_request(query_parameters={}, original_url=URI)
|
||||
def mock_request(query_parameters = {}, original_url = URI)
|
||||
context = Minitest::Mock.new
|
||||
context.expect(:original_url, original_url )
|
||||
context.expect(:original_url, original_url)
|
||||
context.expect(:query_parameters, query_parameters)
|
||||
@options = {}
|
||||
@options[:context] = context
|
||||
@@ -42,17 +42,17 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def data
|
||||
{ data:[
|
||||
{ id:"1", type:"profiles", attributes:{name:"Name 1", description:"Description 1" } },
|
||||
{ id:"2", type:"profiles", attributes:{name:"Name 2", description:"Description 2" } },
|
||||
{ id:"3", type:"profiles", attributes:{name:"Name 3", description:"Description 3" } }
|
||||
{ data: [
|
||||
{ id: '1', type: 'profiles', attributes: { name: 'Name 1', description: 'Description 1' } },
|
||||
{ id: '2', type: 'profiles', attributes: { name: 'Name 2', description: 'Description 2' } },
|
||||
{ id: '3', type: 'profiles', attributes: { name: 'Name 3', description: 'Description 3' } }
|
||||
]
|
||||
}
|
||||
end
|
||||
|
||||
def links
|
||||
{
|
||||
links:{
|
||||
links: {
|
||||
self: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
||||
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
prev: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
@@ -74,7 +74,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def expected_response_with_pagination_links_and_additional_params
|
||||
new_links = links[:links].each_with_object({}) {|(key, value), hash| hash[key] = "#{value}&test=test" }
|
||||
new_links = links[:links].each_with_object({}) { |(key, value), hash| hash[key] = "#{value}&test=test" }
|
||||
{}.tap do |hash|
|
||||
hash[:data] = [data.values.flatten.second]
|
||||
hash.merge! links: new_links
|
||||
|
||||
@@ -21,7 +21,7 @@ module ActiveModel
|
||||
@comment.author = nil
|
||||
@post.author = @author
|
||||
@anonymous_post.author = nil
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
||||
@blog.writer = @author
|
||||
@blog.articles = [@post, @anonymous_post]
|
||||
@author.posts = []
|
||||
|
||||
@@ -14,7 +14,7 @@ module ActiveModel
|
||||
@first_comment.post = @post
|
||||
@second_comment.post = @post
|
||||
@post.author = @author
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
||||
@post.blog = @blog
|
||||
|
||||
@serializer = PostSerializer.new(@post)
|
||||
@@ -23,8 +23,8 @@ module ActiveModel
|
||||
|
||||
def test_has_many
|
||||
assert_equal([
|
||||
{id: 1, body: 'ZOMG A COMMENT'},
|
||||
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
|
||||
{ id: 1, body: 'ZOMG A COMMENT' },
|
||||
{ id: 2, body: 'ZOMG ANOTHER COMMENT' }
|
||||
], @adapter.serializable_hash[:post][:comments])
|
||||
end
|
||||
|
||||
@@ -34,11 +34,11 @@ module ActiveModel
|
||||
|
||||
assert_equal({
|
||||
id: 1,
|
||||
reviews: [{id: 1, body: "ZOMG A COMMENT"},
|
||||
{id: 2, body: "ZOMG ANOTHER COMMENT"}
|
||||
reviews: [{ id: 1, body: 'ZOMG A COMMENT' },
|
||||
{ id: 2, body: 'ZOMG ANOTHER COMMENT' }
|
||||
],
|
||||
writer: {id: 1, name: "Steve K."},
|
||||
site: {id: 1, name: "My Blog!!"}
|
||||
writer: { id: 1, name: 'Steve K.' },
|
||||
site: { id: 1, name: 'My Blog!!' }
|
||||
}, adapter.serializable_hash[:post])
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user