Merge remote-tracking branch 'upstream/master' into improve-tests

This commit is contained in:
Lucas Hosseini
2015-09-07 09:00:08 +02:00
68 changed files with 1110 additions and 624 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

@@ -6,49 +6,48 @@ module ActionController
include ActiveSupport::Testing::Stream
class ImplicitSerializationTestController < ActionController::Base
def render_using_implicit_serializer
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
render json: @profile
end
def render_using_default_adapter_root
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
render json: @profile
end
def render_array_using_custom_root
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: [@profile], root: "custom_root"
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
render json: [@profile], root: 'custom_root'
end
def render_array_that_is_empty_using_custom_root
render json: [], root: "custom_root"
render json: [], root: 'custom_root'
end
def render_object_using_custom_root
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile, root: "custom_root"
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
render json: @profile, root: 'custom_root'
end
def render_array_using_implicit_serializer
array = [
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
Profile.new(name: 'Name 2', description: 'Description 2', comments: 'Comments 2')
]
render json: array
end
def render_array_using_implicit_serializer_and_meta
@profiles = [
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
]
render json: @profiles, meta: { total: 10 }
end
def render_object_with_cache_enabled
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
@author = Author.new(id: 1, name: 'Joao Moura.')
@post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [@comment], author: @author })
@post = Post.new(id: 1, title: 'New Post', body: 'Body', comments: [@comment], author: @author)
generate_cached_serializer(@post)
@@ -57,11 +56,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
@@ -72,9 +71,9 @@ module ActionController
end
def render_object_expired_with_cache_enabled
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
author = Author.new(id: 1, name: 'Joao Moura.')
post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author })
post = Post.new(id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author)
generate_cached_serializer(post)
@@ -84,16 +83,16 @@ module ActionController
end
def render_changed_object_with_cache_enabled
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
author = Author.new(id: 1, name: 'Joao Moura.')
post = Post.new({ id: 1, title: 'ZOMG a New Post', body: 'Body', comments: [comment], author: author })
post = Post.new(id: 1, title: 'ZOMG a New Post', body: 'Body', comments: [comment], author: author)
render json: post
end
def render_fragment_changed_object_with_only_cache_enabled
author = Author.new(id: 1, name: 'Joao Moura.')
role = Role.new({ id: 42, name: 'ZOMG A ROLE', description: 'DESCRIPTION HERE', author: author })
role = Role.new(id: 42, name: 'ZOMG A ROLE', description: 'DESCRIPTION HERE', author: author)
generate_cached_serializer(role)
role.name = 'lol'
@@ -104,7 +103,7 @@ module ActionController
def render_fragment_changed_object_with_except_cache_enabled
author = Author.new(id: 1, name: 'Joao Moura.')
bio = Bio.new({ id: 42, content: 'ZOMG A ROLE', rating: 5, author: author })
bio = Bio.new(id: 42, content: 'ZOMG A ROLE', rating: 5, author: author)
generate_cached_serializer(bio)
bio.content = 'lol'
@@ -114,18 +113,19 @@ module ActionController
end
def render_fragment_changed_object_with_relationship
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
comment2 = Comment.new({ id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT' })
like = Like.new({ id: 1, likeable: comment, time: 3.days.ago })
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
comment2 = Comment.new(id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT')
like = Like.new(id: 1, likeable: comment, time: 3.days.ago)
generate_cached_serializer(like)
like.likable = comment2
like.time = Time.now.to_s
like.time = Time.zone.now.to_s
render json: like
end
private
def generate_cached_serializer(obj)
ActiveModel::SerializableResource.new(obj).to_json
end
@@ -147,8 +147,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
@@ -162,10 +162,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'
}
}
}
@@ -178,7 +178,7 @@ module ActionController
with_adapter :json do
get :render_array_using_custom_root
end
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
@@ -188,7 +188,7 @@ module ActionController
get :render_array_that_is_empty_using_custom_root
end
expected = {custom_roots: []}
expected = { custom_roots: [] }
assert_equal 'application/json', @response.content_type
assert_equal expected.to_json, @response.body
end
@@ -198,7 +198,7 @@ module ActionController
get :render_object_using_custom_root
end
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
@@ -207,7 +207,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
@@ -215,7 +215,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
@@ -226,11 +226,11 @@ module ActionController
expected = [
{
name: 'Name 1',
description: 'Description 1',
description: 'Description 1'
},
{
name: 'Name 2',
description: 'Description 2',
description: 'Description 2'
}
]
@@ -245,10 +245,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'
}
}
],
@@ -282,7 +282,7 @@ module ActionController
}
ActionController::Base.cache_store.clear
Timecop.freeze(Time.now) do
Timecop.freeze(Time.zone.now) do
get :render_object_with_cache_enabled
assert_equal 'application/json', @response.content_type
@@ -330,8 +330,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
@@ -340,23 +340,23 @@ 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
ActionController::Base.cache_store.clear
Timecop.freeze(Time.now) do
Timecop.freeze(Time.zone.now) do
get :render_fragment_changed_object_with_relationship
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.zone.now.to_s,
'likeable' => {
'id' => 1,
'body' => 'ZOMG A COMMENT'
}
}
@@ -379,8 +379,8 @@ module ActionController
body: 'ZOMG A COMMENT' }
],
blog: {
id:999,
name: "Custom blog"
id: 999,
name: 'Custom blog'
},
author: {
id: 1,
@@ -411,7 +411,7 @@ module ActionController
true
end
}.new
assert_equal "", (capture(:stderr) {
assert_equal '', (capture(:stderr) {
controller.get_serializer(Profile.new)
})
end