Merge remote-tracking branch 'upstream/master' into fix-jsonapi-ri

This commit is contained in:
Lucas Hosseini
2015-09-06 17:21:43 +02:00
65 changed files with 1013 additions and 532 deletions

View File

@@ -33,10 +33,10 @@ module ActionController
expected = {
data: {
id: assigns(:profile).id.to_s,
type: "profiles",
type: 'profiles',
attributes: {
name: "Name 1",
description: "Description 1",
name: 'Name 1',
description: 'Description 1',
}
}
}

View File

@@ -100,11 +100,11 @@ module ActionController
get :render_array_using_explicit_serializer_and_custom_serializers
expected = [
{ "title" => "New Post",
"body" => "Body",
"id" => assigns(:post).id,
"comments" => [{"id" => 1}, {"id" => 2}],
"author" => { "id" => assigns(:author).id }
{ 'title' => 'New Post',
'body' => 'Body',
'id' => assigns(:post).id,
'comments' => [{ 'id' => 1 }, { 'id' => 2 }],
'author' => { 'id' => assigns(:author).id }
}
]
@@ -116,13 +116,13 @@ module ActionController
expected = {
id: 1337,
name: "Amazing Place",
name: 'Amazing Place',
locations: [
{
id: 42,
lat: "-23.550520",
lng: "-46.633309",
place: "Nowhere" # is a virtual attribute on LocationSerializer
lat: '-23.550520',
lng: '-46.633309',
place: 'Nowhere' # is a virtual attribute on LocationSerializer
}
]
}

View File

@@ -28,10 +28,10 @@ module ActionController
@first_comment.author = @author2
@second_comment.post = @post
@second_comment.author = nil
@post2 = Post.new(id: 2, title: "Another Post", body: "Body")
@post2 = Post.new(id: 2, title: 'Another Post', body: 'Body')
@post2.author = @author
@post2.comments = []
@blog = Blog.new(id: 1, name: "My Blog!!")
@blog = Blog.new(id: 1, name: 'My Blog!!')
@post.blog = @blog
@post2.blog = @blog
end
@@ -75,7 +75,7 @@ module ActionController
def render_collection_with_include
setup_post
render json: [@post], include: ['author', 'comments'], adapter: :json_api
render json: [@post], include: %w(author comments), adapter: :json_api
end
end
@@ -100,37 +100,37 @@ module ActionController
response = JSON.parse(@response.body)
expected_linked = [
{
"id" => "1",
"type" => "authors",
"attributes" => {
"name" => "Steve K."
'id' => '1',
'type' => 'authors',
'attributes' => {
'name' => 'Steve K.'
},
"relationships" => {
"posts" => { "data" => [] },
"roles" => { "data" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] },
"bio" => { "data" => nil }
'relationships' => {
'posts' => { 'data' => [] },
'roles' => { 'data' => [{ 'type' => 'roles', 'id' => '1' }, { 'type' => 'roles', 'id' => '2' }] },
'bio' => { 'data' => nil }
}
}, {
"id" => "1",
"type" => "roles",
"attributes" => {
"name" => "admin",
"description" => nil,
"slug" => "admin-1"
'id' => '1',
'type' => 'roles',
'attributes' => {
'name' => 'admin',
'description' => nil,
'slug' => 'admin-1'
},
"relationships" => {
"author" => { "data" => { "type" =>"authors", "id" => "1" } }
'relationships' => {
'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } }
}
}, {
"id" => "2",
"type" => "roles",
"attributes" => {
"name" => "colab",
"description" => nil,
"slug" => "colab-2"
'id' => '2',
'type' => 'roles',
'attributes' => {
'name' => 'colab',
'description' => nil,
'slug' => 'colab-2'
},
"relationships" => {
"author" => { "data" => { "type" =>"authors", "id" => "1" } }
'relationships' => {
'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } }
}
}
]
@@ -172,7 +172,7 @@ module ActionController
end
def has_type?(collection, value)
collection.detect { |i| i['type'] == value}
collection.detect { |i| i['type'] == value }
end
end
end

View File

@@ -47,11 +47,11 @@ module ActionController
tests PaginationTestController
def test_render_pagination_links_with_will_paginate
expected_links = { "self"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
"first"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
"prev"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
"next"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
"last"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"}
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'prev' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
get :render_pagination_using_will_paginate, page: { number: 2, size: 1 }
response = JSON.parse(@response.body)
@@ -59,48 +59,48 @@ module ActionController
end
def test_render_only_last_and_next_pagination_links
expected_links = { "self"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
"next"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
"last"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2"}
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" }
get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
def test_render_pagination_links_with_kaminari
expected_links = { "self"=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
"first"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
"prev"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
"next"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
"last"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"}
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'next' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
get :render_pagination_using_kaminari, page: { number: 2, size: 1 }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
def test_render_only_prev_and_first_pagination_links
expected_links = { "self"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
"first"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
"prev"=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1"}
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1" }
get :render_pagination_using_kaminari, page: { number: 3, size: 1 }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
def test_render_only_last_and_next_pagination_links_with_additional_params
expected_links = { "self"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
"next"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
"last"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional"}
get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }, teste: "additional"
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" }
get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }, teste: 'additional'
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
def test_render_only_prev_and_first_pagination_links_with_additional_params
expected_links = { "self"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
"first"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
"prev"=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional"}
get :render_pagination_using_kaminari, page: { number: 3, size: 1 }, teste: "additional"
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional" }
get :render_pagination_using_kaminari, page: { number: 3, size: 1 }, teste: 'additional'
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end

View File

@@ -1,4 +1,3 @@
require 'test_helper'
module ActionController
@@ -22,20 +21,20 @@ module ActionController
def render_array_using_custom_root
with_adapter ActiveModel::Serializer::Adapter::Json do
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: [@profile], root: "custom_root"
render json: [@profile], root: 'custom_root'
end
end
def render_array_that_is_empty_using_custom_root
with_adapter ActiveModel::Serializer::Adapter::Json do
render json: [], root: "custom_root"
render json: [], root: 'custom_root'
end
end
def render_object_using_custom_root
with_adapter ActiveModel::Serializer::Adapter::Json do
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile, root: "custom_root"
render json: @profile, root: 'custom_root'
end
end
@@ -49,7 +48,6 @@ module ActionController
def render_array_using_implicit_serializer_and_meta
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
@profiles = [
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
]
@@ -70,11 +68,11 @@ module ActionController
end
def render_json_object_without_serializer
render json: {error: 'Result is Invalid'}
render json: { error: 'Result is Invalid' }
end
def render_json_array_object_without_serializer
render json: [{error: 'Result is Invalid'}]
render json: [{ error: 'Result is Invalid' }]
end
def update_and_render_object_with_cache_enabled
@@ -139,6 +137,7 @@ module ActionController
end
private
def generate_cached_serializer(obj)
ActiveModel::SerializableResource.new(obj).to_json
end
@@ -160,8 +159,8 @@ module ActionController
get :render_using_implicit_serializer
expected = {
name: "Name 1",
description: "Description 1"
name: 'Name 1',
description: 'Description 1'
}
assert_equal 'application/json', @response.content_type
@@ -174,10 +173,10 @@ module ActionController
expected = {
data: {
id: assigns(:profile).id.to_s,
type: "profiles",
type: 'profiles',
attributes: {
name: "Name 1",
description: "Description 1"
name: 'Name 1',
description: 'Description 1'
}
}
}
@@ -189,7 +188,7 @@ module ActionController
def test_render_array_using_custom_root
get :render_array_using_custom_root
expected = {custom_roots: [{name: "Name 1", description: "Description 1"}]}
expected = { custom_roots: [{ name: 'Name 1', description: 'Description 1' }] }
assert_equal 'application/json', @response.content_type
assert_equal expected.to_json, @response.body
end
@@ -197,7 +196,7 @@ module ActionController
def test_render_array_that_is_empty_using_custom_root
get :render_array_that_is_empty_using_custom_root
expected = {custom_roots: []}
expected = { custom_roots: [] }
assert_equal 'application/json', @response.content_type
assert_equal expected.to_json, @response.body
end
@@ -205,7 +204,7 @@ module ActionController
def test_render_object_using_custom_root
get :render_object_using_custom_root
expected = {custom_root: {name: "Name 1", description: "Description 1"}}
expected = { custom_root: { name: 'Name 1', description: 'Description 1' } }
assert_equal 'application/json', @response.content_type
assert_equal expected.to_json, @response.body
end
@@ -214,7 +213,7 @@ module ActionController
get :render_json_object_without_serializer
assert_equal 'application/json', @response.content_type
expected_body = {error: 'Result is Invalid'}
expected_body = { error: 'Result is Invalid' }
assert_equal expected_body.to_json, @response.body
end
@@ -222,7 +221,7 @@ module ActionController
get :render_json_array_object_without_serializer
assert_equal 'application/json', @response.content_type
expected_body = [{error: 'Result is Invalid'}]
expected_body = [{ error: 'Result is Invalid' }]
assert_equal expected_body.to_json, @response.body
end
@@ -251,10 +250,10 @@ module ActionController
data: [
{
id: assigns(:profiles).first.id.to_s,
type: "profiles",
type: 'profiles',
attributes: {
name: "Name 1",
description: "Description 1"
name: 'Name 1',
description: 'Description 1'
}
}
],
@@ -336,8 +335,8 @@ module ActionController
response = JSON.parse(@response.body)
assert_equal 'application/json', @response.content_type
assert_equal 'ZOMG A ROLE', response["name"]
assert_equal 'HUEHUEBRBR', response["description"]
assert_equal 'ZOMG A ROLE', response['name']
assert_equal 'HUEHUEBRBR', response['description']
end
def test_render_with_fragment_except_cache_enable
@@ -346,8 +345,8 @@ module ActionController
response = JSON.parse(@response.body)
assert_equal 'application/json', @response.content_type
assert_equal 5, response["rating"]
assert_equal 'lol', response["content"]
assert_equal 5, response['rating']
assert_equal 'lol', response['content']
end
def test_render_fragment_changed_object_with_relationship
@@ -358,11 +357,11 @@ module ActionController
response = JSON.parse(@response.body)
expected_return = {
"id"=>1,
"time"=>Time.now.to_s,
"likeable" => {
"id"=>1,
"body"=>"ZOMG A COMMENT"
'id' => 1,
'time' => Time.now.to_s,
'likeable' => {
'id' => 1,
'body' => 'ZOMG A COMMENT'
}
}
@@ -385,8 +384,8 @@ module ActionController
body: 'ZOMG A COMMENT' }
],
blog: {
id:999,
name: "Custom blog"
id: 999,
name: 'Custom blog'
},
author: {
id: 1,
@@ -417,7 +416,7 @@ module ActionController
true
end
}.new
assert_equal "", (capture(:stderr) {
assert_equal '', (capture(:stderr) {
controller.get_serializer(Profile.new)
})
end

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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 }
}
}

View File

@@ -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

View File

@@ -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' }
]
}

View File

@@ -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])

View File

@@ -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)

View File

@@ -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 }] }
}
}
}

View File

@@ -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

View File

@@ -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 }
}
}

View File

@@ -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

View File

@@ -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 = []

View File

@@ -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

View File

@@ -35,12 +35,12 @@ module ActiveModel
end
def test_create_adapter_with_override
adapter = ActiveModel::Serializer::Adapter.create(@serializer, { adapter: :json_api})
adapter = ActiveModel::Serializer::Adapter.create(@serializer, { adapter: :json_api })
assert_equal ActiveModel::Serializer::Adapter::JsonApi, adapter.class
end
def test_inflected_adapter_class_for_known_adapter
ActiveSupport::Inflector.inflections(:en){|inflect| inflect.acronym 'API' }
ActiveSupport::Inflector.inflections(:en) { |inflect| inflect.acronym 'API' }
klass = ActiveModel::Serializer::Adapter.adapter_class(:json_api)
ActiveSupport::Inflector.inflections.acronyms.clear

View File

@@ -7,14 +7,18 @@ module ActiveModel
@comment = Comment.new
@post = Post.new
@resource = build_named_collection @comment, @post
@serializer = ArraySerializer.new(@resource, {some: :options})
@serializer = ArraySerializer.new(@resource, { some: :options })
end
def build_named_collection(*resource)
resource.define_singleton_method(:name){ 'MeResource' }
resource.define_singleton_method(:name) { 'MeResource' }
resource
end
def test_has_object_reader_serializer_interface
assert_equal @serializer.object, @resource
end
def test_respond_to_each
assert_respond_to @serializer, :each
end
@@ -32,17 +36,17 @@ module ActiveModel
end
def test_serializer_option_not_passed_to_each_serializer
serializers = ArraySerializer.new([@post], {serializer: PostSerializer}).to_a
serializers = ArraySerializer.new([@post], { serializer: PostSerializer }).to_a
refute serializers.first.custom_options.key?(:serializer)
end
def test_meta_and_meta_key_attr_readers
meta_content = {meta: "the meta", meta_key: "the meta key"}
meta_content = { meta: 'the meta', meta_key: 'the meta key' }
@serializer = ArraySerializer.new([@comment, @post], meta_content)
assert_equal @serializer.meta, "the meta"
assert_equal @serializer.meta_key, "the meta key"
assert_equal @serializer.meta, 'the meta'
assert_equal @serializer.meta_key, 'the meta key'
end
def test_root_default
@@ -73,7 +77,7 @@ module ActiveModel
def test_json_key_with_resource_with_nil_name_and_no_serializers
resource = []
resource.define_singleton_method(:name){ nil }
resource.define_singleton_method(:name) { nil }
serializer = ArraySerializer.new(resource)
assert_equal serializer.json_key, nil
end

View File

@@ -1,15 +1,15 @@
# https://raw.githubusercontent.com/metric_fu/metric_fu/master/spec/capture_warnings.rb
require "tempfile"
require "fileutils"
require 'tempfile'
require 'fileutils'
class CaptureWarnings
def initialize(fail_on_warnings = true)
@fail_on_warnings = fail_on_warnings
@stderr_file = Tempfile.new("app.stderr")
@stderr_file = Tempfile.new('app.stderr')
@app_root ||= Dir.pwd
@output_dir = File.join(app_root, "tmp")
@output_dir = File.join(app_root, 'tmp')
FileUtils.mkdir_p(output_dir)
@bundle_dir = File.join(app_root, "bundle")
@bundle_dir = File.join(app_root, 'bundle')
end
def before_tests
@@ -40,9 +40,9 @@ class CaptureWarnings
end
if other_warnings.any?
File.write(File.join(output_dir, "warnings.txt"), other_warnings.join("\n") << "\n")
File.write(File.join(output_dir, 'warnings.txt'), other_warnings.join("\n") << "\n")
puts
puts "Non-app warnings written to tmp/warnings.txt"
puts 'Non-app warnings written to tmp/warnings.txt'
puts
end
@@ -53,5 +53,6 @@ class CaptureWarnings
end
private
attr_reader :stderr_file, :app_root, :output_dir, :bundle_dir, :fail_on_warnings
end

57
test/fixtures/active_record.rb vendored Normal file
View File

@@ -0,0 +1,57 @@
require 'active_record'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :title
t.text :body
t.references :author
t.timestamps null: false
end
create_table :authors, force: true do |t|
t.string :name
t.timestamps null: false
end
create_table :comments, force: true do |t|
t.text :contents
t.references :author
t.references :post
t.timestamp null: false
end
end
module ARModels
class Post < ActiveRecord::Base
has_many :comments
belongs_to :author
end
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :author
end
class Author < ActiveRecord::Base
has_many :posts
end
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_many :comments
belongs_to :author
url :comments
end
class CommentSerializer < ActiveModel::Serializer
attributes :id, :contents
belongs_to :author
end
class AuthorSerializer < ActiveModel::Serializer
attributes :id, :name
has_many :posts
end
end

21
test/fixtures/poro.rb vendored
View File

@@ -7,7 +7,7 @@ class Model
@_model_name ||= ActiveModel::Name.new(self)
end
def initialize(hash={})
def initialize(hash = {})
@attributes = hash
end
@@ -83,7 +83,7 @@ Location = Class.new(Model)
Place = Class.new(Model)
Tag = Class.new(Model)
VirtualValue = Class.new(Model)
Comment = Class.new(Model) do
Comment = Class.new(Model) do
# Uses a custom non-time-based cache key
def cache_key
"#{self.class.name.downcase}/#{self.id}"
@@ -94,7 +94,7 @@ module Spam; end
Spam::UnrelatedLink = Class.new(Model)
PostSerializer = Class.new(ActiveModel::Serializer) do
cache key:'post', expires_in: 0.1, skip_digest: true
cache key: 'post', expires_in: 0.1, skip_digest: true
attributes :id, :title, :body
has_many :comments
@@ -103,7 +103,7 @@ PostSerializer = Class.new(ActiveModel::Serializer) do
url :comments
def blog
Blog.new(id: 999, name: "Custom blog")
Blog.new(id: 999, name: 'Custom blog')
end
def custom_options
@@ -133,11 +133,12 @@ CommentSerializer = Class.new(ActiveModel::Serializer) do
end
AuthorSerializer = Class.new(ActiveModel::Serializer) do
cache key:'writer', skip_digest: true
attributes :id, :name
cache key: 'writer', skip_digest: true
attribute :id
attribute :name
has_many :posts, embed: :ids
has_many :roles, embed: :ids
has_many :posts
has_many :roles
has_one :bio
end
@@ -248,8 +249,8 @@ end
VirtualValueSerializer = Class.new(ActiveModel::Serializer) do
attributes :id
has_many :reviews, virtual_value: [{id: 1}, {id: 2}]
has_one :maker, virtual_value: {id: 1}
has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]
has_one :maker, virtual_value: { id: 1 }
def reviews
end

View File

@@ -1,5 +1,4 @@
require 'test_helper'
# require 'active_model/serializer/railtie'
class ResourceGeneratorTest < Rails::Generators::TestCase
destination File.expand_path('../../../tmp/generators', __FILE__)
@@ -19,6 +18,6 @@ class ResourceGeneratorTest < Rails::Generators::TestCase
def copy_routes
config_dir = File.join(destination_root, 'config')
FileUtils.mkdir_p(config_dir)
File.write(File.join(config_dir, 'routes.rb'), 'Rails.application.routes.draw { }')
File.write(File.join(config_dir, 'routes.rb'), 'Rails.application.routes.draw {}')
end
end

View File

@@ -2,7 +2,7 @@ require 'test_helper'
require 'generators/serializer/serializer_generator'
class SerializerGeneratorTest < Rails::Generators::TestCase
destination File.expand_path("../../../tmp/generators", __FILE__)
destination File.expand_path('../../../tmp/generators', __FILE__)
setup :prepare_destination
tests Rails::Generators::SerializerGenerator
@@ -10,33 +10,33 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
def test_generates_a_serializer
run_generator
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer/
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/
end
def test_generates_a_namespaced_serializer
run_generator ["admin/account"]
assert_file "app/serializers/admin/account_serializer.rb", /class Admin::AccountSerializer < ActiveModel::Serializer/
run_generator ['admin/account']
assert_file 'app/serializers/admin/account_serializer.rb', /class Admin::AccountSerializer < ActiveModel::Serializer/
end
def test_uses_application_serializer_if_one_exists
Object.const_set(:ApplicationSerializer, Class.new)
run_generator
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ApplicationSerializer/
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ApplicationSerializer/
ensure
Object.send :remove_const, :ApplicationSerializer
end
def test_uses_given_parent
Object.const_set(:ApplicationSerializer, Class.new)
run_generator ["Account", "--parent=MySerializer"]
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < MySerializer/
run_generator ['Account', '--parent=MySerializer']
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < MySerializer/
ensure
Object.send :remove_const, :ApplicationSerializer
end
def test_generates_attributes_and_associations
run_generator
assert_file "app/serializers/account_serializer.rb" do |serializer|
assert_file 'app/serializers/account_serializer.rb' do |serializer|
assert_match(/^ attributes :id, :name, :description$/, serializer)
assert_match(/^ has_one :business$/, serializer)
assert_match(/^end\n*\z/, serializer)
@@ -44,8 +44,8 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
end
def test_with_no_attributes_does_not_add_extra_space
run_generator ["account"]
assert_file "app/serializers/account_serializer.rb" do |content|
run_generator ['account']
assert_file 'app/serializers/account_serializer.rb' do |content|
if RUBY_PLATFORM =~ /mingw/
assert_no_match(/\r\n\r\nend/, content)
else

View File

@@ -7,27 +7,21 @@ module ActiveModel
class CompliantResource
def serializable_hash(options = nil)
end
def read_attribute_for_serialization(name)
end
def as_json(options = nil)
end
def to_json(options = nil)
end
def cache_key
end
def id
end
def self.model_name
@@ -38,7 +32,6 @@ module ActiveModel
def setup
@resource = CompliantResource.new
end
end
end
end

View File

@@ -20,7 +20,6 @@ module ActiveModel
adapter = ActiveModel::Serializer.adapter
assert_equal ActiveModel::Serializer::Adapter::Null, adapter
ensure
end
def test_overwrite_adapter_with_class

View File

@@ -6,7 +6,7 @@ module ActiveModel
AuthorSummarySerializer = Class.new
class AssociationsTestSerializer < Serializer
belongs_to :author, serializer: AuthorSummarySerializer
has_many :comments, embed: :ids
has_many :comments
has_one :category
end
@@ -21,7 +21,7 @@ module ActiveModel
end
def test_has_many_defines_reflection
has_many_reflection = HasManyReflection.new(:comments, embed: :ids)
has_many_reflection = HasManyReflection.new(:comments, {})
assert_includes(@reflections, has_many_reflection)
end

View File

@@ -4,7 +4,7 @@ module ActiveModel
class Serializer
class AssociationsTest < Minitest::Test
class Model
def initialize(hash={})
def initialize(hash = {})
@attributes = hash
end
@@ -29,7 +29,7 @@ module ActiveModel
@author.roles = []
@blog = Blog.new({ name: 'AMS Blog' })
@post = Post.new({ title: 'New Post', body: 'Body' })
@tag = Tag.new({name: '#hashtagged'})
@tag = Tag.new({ name: '#hashtagged' })
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
@post.comments = [@comment]
@post.tags = [@tag]
@@ -39,7 +39,7 @@ module ActiveModel
@post.author = @author
@author.posts = [@post]
@post_serializer = PostSerializer.new(@post, {custom_options: true})
@post_serializer = PostSerializer.new(@post, { custom_options: true })
@author_serializer = AuthorSerializer.new(@author)
@comment_serializer = CommentSerializer.new(@comment)
end
@@ -52,13 +52,13 @@ module ActiveModel
case key
when :posts
assert_equal({ embed: :ids }, options)
assert_equal({}, options)
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
when :bio
assert_equal({}, options)
assert_nil serializer
when :roles
assert_equal({ embed: :ids }, options)
assert_equal({}, options)
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
else
flunk "Unknown association: #{key}"
@@ -74,7 +74,7 @@ module ActiveModel
assert_equal key, :tags
assert_equal serializer, nil
assert_equal [{ attributes: { name: "#hashtagged" }}].to_json, options[:virtual_value].to_json
assert_equal [{ attributes: { name: '#hashtagged' } }].to_json, options[:virtual_value].to_json
end
end

View File

@@ -4,25 +4,25 @@ module ActiveModel
class Serializer
class AttributeTest < Minitest::Test
def setup
@blog = Blog.new({ id: 1, name: 'AMS Hints', type: "stuff" })
@blog = Blog.new({ id: 1, name: 'AMS Hints', type: 'stuff' })
@blog_serializer = AlternateBlogSerializer.new(@blog)
end
def test_attributes_definition
assert_equal([:id, :title],
@blog_serializer.class._attributes)
@blog_serializer.class._attributes)
end
def test_json_serializable_hash
adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer)
assert_equal({blog: { id:1, title:"AMS Hints"}}, adapter.serializable_hash)
assert_equal({ blog: { id: 1, title: 'AMS Hints' } }, adapter.serializable_hash)
end
def test_attribute_inheritance_with_key
inherited_klass = Class.new(AlternateBlogSerializer)
blog_serializer = inherited_klass.new(@blog)
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(blog_serializer)
assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash)
assert_equal({ :id => 1, :title => 'AMS Hints' }, adapter.serializable_hash)
end
def test_multiple_calls_with_the_same_attribute
@@ -40,7 +40,7 @@ module ActiveModel
end
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer.new(@blog))
assert_equal({ blog: { id: "AMS Hints" } }, adapter.serializable_hash)
assert_equal({ blog: { id: 'AMS Hints' } }, adapter.serializable_hash)
end
def test_type_attribute
@@ -52,10 +52,10 @@ module ActiveModel
end
adapter = ActiveModel::Serializer::Adapter::Json.new(attribute_serializer.new(@blog))
assert_equal({ blog: { type: 1} }, adapter.serializable_hash)
assert_equal({ blog: { type: 1 } }, adapter.serializable_hash)
adapter = ActiveModel::Serializer::Adapter::Json.new(attributes_serializer.new(@blog))
assert_equal({ blog: { type: "stuff" } }, adapter.serializable_hash)
assert_equal({ blog: { type: 'stuff' } }, adapter.serializable_hash)
end
end
end

View File

@@ -6,7 +6,7 @@ module ActiveModel
def setup
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
@profile_serializer = ProfileSerializer.new(@profile)
@comment = Comment.new(id: 1, body: "ZOMG!!", date: "2015")
@comment = Comment.new(id: 1, body: 'ZOMG!!', date: '2015')
@serializer_klass = Class.new(CommentSerializer)
@serializer_klass_with_new_attributes = Class.new(CommentSerializer) do
attributes :date, :likes
@@ -15,12 +15,12 @@ module ActiveModel
def test_attributes_definition
assert_equal([:name, :description],
@profile_serializer.class._attributes)
@profile_serializer.class._attributes)
end
def test_attributes_with_fields_option
assert_equal({name: 'Name 1'},
@profile_serializer.attributes(fields: [:name]))
assert_equal({ name: 'Name 1' },
@profile_serializer.attributes(fields: [:name]))
end
def test_attributes_inheritance_definition
@@ -29,8 +29,8 @@ module ActiveModel
def test_attributes_inheritance
serializer = @serializer_klass.new(@comment)
assert_equal({id: 1, body: "ZOMG!!"},
serializer.attributes)
assert_equal({ id: 1, body: 'ZOMG!!' },
serializer.attributes)
end
def test_attribute_inheritance_with_new_attribute_definition
@@ -40,8 +40,8 @@ module ActiveModel
def test_attribute_inheritance_with_new_attribute
serializer = @serializer_klass_with_new_attributes.new(@comment)
assert_equal({id: 1, body: "ZOMG!!", date: "2015", likes: nil},
serializer.attributes)
assert_equal({ id: 1, body: 'ZOMG!!', date: '2015', likes: nil },
serializer.attributes)
end
def test_multiple_calls_with_the_same_attribute

View File

@@ -9,7 +9,7 @@ module ActiveModel
@post = Post.new(title: 'New Post', body: 'Body')
@bio = Bio.new(id: 1, content: 'AMS Contributor')
@author = Author.new(name: 'Joao M. D. Moura')
@blog = Blog.new(id: 999, name: "Custom blog", writer: @author, articles: [])
@blog = Blog.new(id: 999, name: 'Custom blog', writer: @author, articles: [])
@role = Role.new(name: 'Great Author')
@location = Location.new(lat: '-23.550520', lng: '-46.633309')
@place = Place.new(name: 'Amazing Place')
@@ -58,9 +58,9 @@ module ActiveModel
end
def test_cache_options_definition
assert_equal({expires_in: 0.1, skip_digest: true}, @post_serializer.class._cache_options)
assert_equal({ expires_in: 0.1, skip_digest: true }, @post_serializer.class._cache_options)
assert_equal(nil, @blog_serializer.class._cache_options)
assert_equal({expires_in: 1.day, skip_digest: true}, @comment_serializer.class._cache_options)
assert_equal({ expires_in: 1.day, skip_digest: true }, @comment_serializer.class._cache_options)
end
def test_fragment_cache_definition
@@ -118,7 +118,7 @@ module ActiveModel
hash = render_object_with_cache(@location)
assert_equal(hash, expected_result)
assert_equal({place: 'Nowhere'}, ActionController::Base.cache_store.fetch(@location.cache_key))
assert_equal({ place: 'Nowhere' }, ActionController::Base.cache_store.fetch(@location.cache_key))
end
def test_uses_file_digest_in_cache_key
@@ -131,20 +131,20 @@ module ActiveModel
end
def test_serializer_file_path_on_nix
path = "/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb"
path = '/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
caller_line = "#{path}:1:in `<top (required)>'"
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
end
def test_serializer_file_path_on_windows
path = "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb"
path = 'c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
caller_line = "#{path}:1:in `<top (required)>'"
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
end
def test_digest_caller_file
contents = "puts 'AMS rocks'!"
file = Tempfile.new("some_ruby.rb")
file = Tempfile.new('some_ruby.rb')
file.write(contents)
path = file.path
caller_line = "#{path}:1:in `<top (required)>'"
@@ -155,6 +155,7 @@ module ActiveModel
end
private
def render_object_with_cache(obj)
ActiveModel::SerializableResource.new(obj).serializable_hash
end

View File

@@ -3,12 +3,11 @@ require 'test_helper'
module ActiveModel
class Serializer
class FieldsetTest < Minitest::Test
def test_fieldset_with_hash
fieldset = ActiveModel::Serializer::Fieldset.new({'post' => ['id', 'title'], 'coment' => ['body']})
fieldset = ActiveModel::Serializer::Fieldset.new({ 'post' => %w(id title), 'coment' => ['body'] })
assert_equal(
{:post=>[:id, :title], :coment=>[:body]},
{ :post => [:id, :title], :coment => [:body] },
fieldset.fields
)
end
@@ -17,7 +16,7 @@ module ActiveModel
fieldset = ActiveModel::Serializer::Fieldset.new(['title'], 'post')
assert_equal(
{:post => [:title]},
{ :post => [:title] },
fieldset.fields
)
end

View File

@@ -7,19 +7,19 @@ module ActiveModel
ActionController::Base.cache_store.clear
@blog = Blog.new(id: 1,
name: 'AMS Hints',
writer: Author.new(id: 2, name: "Steve"),
articles: [Post.new(id: 3, title: "AMS")])
writer: Author.new(id: 2, name: 'Steve'),
articles: [Post.new(id: 3, title: 'AMS')])
end
def test_meta_is_present_with_root
serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10})
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 })
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
expected = {
blog: {
id: 1,
title: "AMS Hints"
title: 'AMS Hints'
},
"meta" => {
'meta' => {
total: 10
}
}
@@ -28,23 +28,23 @@ module ActiveModel
def test_meta_is_not_included_when_root_is_missing
# load_adapter uses FlattenJson Adapter
adapter = load_adapter(meta: {total: 10})
adapter = load_adapter(meta: { total: 10 })
expected = {
id: 1,
title: "AMS Hints"
title: 'AMS Hints'
}
assert_equal expected, adapter.as_json
end
def test_meta_key_is_used
serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}, meta_key: "haha_meta")
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 }, meta_key: 'haha_meta')
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
expected = {
blog: {
id: 1,
title: "AMS Hints"
title: 'AMS Hints'
},
"haha_meta" => {
'haha_meta' => {
total: 10
}
}
@@ -52,33 +52,33 @@ module ActiveModel
end
def test_meta_key_is_used_with_json_api
serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}, meta_key: "haha_meta")
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 }, meta_key: 'haha_meta')
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
expected = {
data: {
id: "1",
type: "blogs",
attributes: { title: "AMS Hints" }
id: '1',
type: 'blogs',
attributes: { title: 'AMS Hints' }
},
"haha_meta" => { total: 10 }
'haha_meta' => { total: 10 }
}
assert_equal expected, adapter.as_json
end
def test_meta_is_not_present_on_arrays_without_root
serializer = ArraySerializer.new([@blog], meta: {total: 10})
serializer = ArraySerializer.new([@blog], meta: { total: 10 })
# FlattenJSON doesn't have support to root
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer)
expected = [{
id: 1,
name: "AMS Hints",
name: 'AMS Hints',
writer: {
id: 2,
name: "Steve"
name: 'Steve'
},
articles: [{
id: 3,
title: "AMS",
title: 'AMS',
body: nil
}]
}]
@@ -86,20 +86,20 @@ module ActiveModel
end
def test_meta_is_present_on_arrays_with_root
serializer = ArraySerializer.new([@blog], meta: {total: 10}, meta_key: "haha_meta")
serializer = ArraySerializer.new([@blog], meta: { total: 10 }, meta_key: 'haha_meta')
# JSON adapter adds root by default
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
expected = {
blogs: [{
id: 1,
name: "AMS Hints",
name: 'AMS Hints',
writer: {
id: 2,
name: "Steve"
name: 'Steve'
},
articles: [{
id: 3,
title: "AMS",
title: 'AMS',
body: nil
}]
}],

View File

@@ -3,13 +3,12 @@ require 'test_helper'
module ActiveModel
class Serializer
class RootTest < Minitest::Test
def setup
@virtual_value = VirtualValue.new(id: 1)
end
def test_overwrite_root
serializer = VirtualValueSerializer.new(@virtual_value, {root: 'smth'})
serializer = VirtualValueSerializer.new(@virtual_value, { root: 'smth' })
assert_equal('smth', serializer.json_key)
end
@@ -17,7 +16,6 @@ module ActiveModel
serializer = VirtualValueSerializer.new(@virtual_value)
assert_equal('virtual_value', serializer.json_key)
end
end
end
end

View File

@@ -26,7 +26,7 @@ module ActiveModel
end
end
class SerializerTest < Minitest::Test
class SerializerTest < Minitest::Test
class MyProfile < Profile
end
class CustomProfile

View File

@@ -3,7 +3,6 @@ require 'test_helper'
module ActiveModel
class Serializer
class UrlsTest < Minitest::Test
def setup
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
@post = Post.new({ title: 'New Post', body: 'Body' })

View File

@@ -1,7 +1,7 @@
# Use cleaner stream testing interface from Rails 5 if available
# see https://github.com/rails/rails/blob/29959eb59d/activesupport/lib/active_support/testing/stream.rb
begin
require "active_support/testing/stream"
require 'active_support/testing/stream'
rescue LoadError
module ActiveSupport
module Testing
@@ -29,7 +29,7 @@ rescue LoadError
def capture(stream)
stream = stream.to_s
captured_stream = Tempfile.new(stream)
stream_io = eval("$#{stream}")
stream_io = eval("$#{stream}") # rubocop:disable Lint/Eval
origin_stream = stream_io.dup
stream_io.reopen(captured_stream)

View File

@@ -13,7 +13,6 @@ require 'minitest/autorun'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
require 'capture_warnings'
@capture_warnings = CaptureWarnings.new(fail_build = true)
@capture_warnings.before_tests
@@ -23,7 +22,7 @@ if Minitest.respond_to?(:after_run)
end
else
at_exit do
STDOUT.puts "Minitest.after_run not available."
STDOUT.puts 'Minitest.after_run not available.'
@capture_warnings.after_tests
end
end
@@ -34,6 +33,8 @@ require 'support/stream_capture'
require 'support/rails_app'
require 'fixtures/poro'
require 'support/test_case'
require 'fixtures/active_record'
require 'fixtures/poro'