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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user