mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
updating tests to match new adapters structure
This commit is contained in:
parent
2e46507971
commit
1ea5608e78
@ -6,7 +6,7 @@ module ActionController
|
|||||||
|
|
||||||
include ActionController::Renderers
|
include ActionController::Renderers
|
||||||
|
|
||||||
ADAPTER_OPTION_KEYS = [:include, :fields, :root, :adapter]
|
ADAPTER_OPTION_KEYS = [:include, :fields, :adapter]
|
||||||
|
|
||||||
included do
|
included do
|
||||||
class_attribute :_serialization_scope
|
class_attribute :_serialization_scope
|
||||||
|
|||||||
@ -22,7 +22,8 @@ module ActiveModel
|
|||||||
|
|
||||||
def as_json(options = {})
|
def as_json(options = {})
|
||||||
hash = serializable_hash(options)
|
hash = serializable_hash(options)
|
||||||
include_meta(hash)
|
include_meta(hash) unless self.class == FlattenJson
|
||||||
|
hash
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.create(resource, options = {})
|
def self.create(resource, options = {})
|
||||||
|
|||||||
@ -7,8 +7,6 @@ module ActiveModel
|
|||||||
attr_reader :meta, :meta_key
|
attr_reader :meta, :meta_key
|
||||||
|
|
||||||
def initialize(objects, options = {})
|
def initialize(objects, options = {})
|
||||||
options.merge!(root: nil)
|
|
||||||
|
|
||||||
@objects = objects.map do |object|
|
@objects = objects.map do |object|
|
||||||
serializer_class = options.fetch(
|
serializer_class = options.fetch(
|
||||||
:serializer,
|
:serializer,
|
||||||
@ -21,7 +19,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
@objects.first.json_key.pluralize if @objects.first
|
@objects.first.json_key if @objects.first
|
||||||
end
|
end
|
||||||
|
|
||||||
def root=(root)
|
def root=(root)
|
||||||
|
|||||||
@ -12,7 +12,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def fields_for(serializer)
|
def fields_for(serializer)
|
||||||
key = serializer.json_key || serializer.class.root_name
|
key = serializer.json_key
|
||||||
fields[key.to_sym]
|
fields[key.to_sym]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -77,12 +77,10 @@ module ActionController
|
|||||||
get :render_array_using_explicit_serializer
|
get :render_array_using_explicit_serializer
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
|
|
||||||
expected = {
|
expected = [
|
||||||
'paginated' => [
|
{ 'name' => 'Name 1' },
|
||||||
{ 'name' => 'Name 1' },
|
{ 'name' => 'Name 2' }
|
||||||
{ 'name' => 'Name 2' }
|
]
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_equal expected.to_json, @response.body
|
assert_equal expected.to_json, @response.body
|
||||||
end
|
end
|
||||||
|
|||||||
@ -10,13 +10,17 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_using_custom_root
|
def render_using_custom_root
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
with_adapter ActiveModel::Serializer::Adapter::Json do
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_using_custom_root_and_meta
|
def render_using_custom_root_and_meta
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
with_adapter ActiveModel::Serializer::Adapter::Json do
|
||||||
render json: @profile, root: "custom_root", meta: { total: 10 }
|
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||||
|
render json: @profile, root: "custom_root", meta: { total: 10 }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_using_default_adapter_root
|
def render_using_default_adapter_root
|
||||||
@ -34,11 +38,13 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_array_using_custom_root_and_meta
|
def render_array_using_custom_root_and_meta
|
||||||
array = [
|
with_adapter ActiveModel::Serializer::Adapter::Json do
|
||||||
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
array = [
|
||||||
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, root: "custom_root", meta: { total: 10 }
|
]
|
||||||
|
render json: array, root: "custom_root", meta: { total: 10 }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_array_using_implicit_serializer
|
def render_array_using_implicit_serializer
|
||||||
@ -219,6 +225,7 @@ module ActionController
|
|||||||
|
|
||||||
def test_render_array_using_custom_root_and_meta
|
def test_render_array_using_custom_root_and_meta
|
||||||
get :render_array_using_custom_root_and_meta
|
get :render_array_using_custom_root_and_meta
|
||||||
|
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
|
|
||||||
expected = { custom_root: [
|
expected = { custom_root: [
|
||||||
|
|||||||
@ -25,21 +25,21 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_post
|
def test_includes_post
|
||||||
assert_equal({id: 42, title: 'New Post', body: 'Body'}, @adapter.serializable_hash[:post])
|
assert_equal({id: 42, title: 'New Post', body: 'Body'}, @adapter.serializable_hash[:comment][:post])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_include_nil_author
|
def test_include_nil_author
|
||||||
serializer = PostSerializer.new(@anonymous_post)
|
serializer = PostSerializer.new(@anonymous_post)
|
||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||||
|
|
||||||
assert_equal({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
|
end
|
||||||
|
|
||||||
def test_include_nil_author_with_specified_serializer
|
def test_include_nil_author_with_specified_serializer
|
||||||
serializer = PostPreviewSerializer.new(@anonymous_post)
|
serializer = PostPreviewSerializer.new(@anonymous_post)
|
||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||||
|
|
||||||
assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}, adapter.serializable_hash)
|
assert_equal({posts: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}}, adapter.serializable_hash)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -28,16 +28,16 @@ module ActiveModel
|
|||||||
@serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
|
@serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
|
||||||
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
||||||
|
|
||||||
expected = [{
|
expected = {custom_blog:[{
|
||||||
id: 1,
|
id: 1,
|
||||||
special_attribute: "Special",
|
special_attribute: "Special",
|
||||||
articles: [{id: 1,title: "Hello!!", body: "Hello, world!!"}, {id: 2, title: "New Post", body: "Body"}]
|
articles: [{id: 1,title: "Hello!!", body: "Hello, world!!"}, {id: 2, title: "New Post", body: "Body"}]
|
||||||
}]
|
}]}
|
||||||
assert_equal expected, @adapter.serializable_hash
|
assert_equal expected, @adapter.serializable_hash
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_include_multiple_posts
|
def test_include_multiple_posts
|
||||||
expected = [{
|
expected = { post: [{
|
||||||
title: "Hello!!",
|
title: "Hello!!",
|
||||||
body: "Hello, world!!",
|
body: "Hello, world!!",
|
||||||
id: 1,
|
id: 1,
|
||||||
@ -63,7 +63,7 @@ module ActiveModel
|
|||||||
id: 999,
|
id: 999,
|
||||||
name: "Custom blog"
|
name: "Custom blog"
|
||||||
}
|
}
|
||||||
}]
|
}]}
|
||||||
assert_equal expected, @adapter.serializable_hash
|
assert_equal expected, @adapter.serializable_hash
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -26,7 +26,7 @@ module ActiveModel
|
|||||||
assert_equal([
|
assert_equal([
|
||||||
{id: 1, body: 'ZOMG A COMMENT'},
|
{id: 1, body: 'ZOMG A COMMENT'},
|
||||||
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
|
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
|
||||||
], @adapter.serializable_hash[:comments])
|
], @adapter.serializable_hash[:post][:comments])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -86,7 +86,6 @@ module ActiveModel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
assert_equal(expected, @adapter.serializable_hash[:data])
|
assert_equal(expected, @adapter.serializable_hash[:data])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -25,7 +25,7 @@ module ActiveModel
|
|||||||
assert_equal([
|
assert_equal([
|
||||||
{id: 1, body: 'ZOMG A COMMENT'},
|
{id: 1, body: 'ZOMG A COMMENT'},
|
||||||
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
|
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
|
||||||
], @adapter.serializable_hash[:comments])
|
], @adapter.serializable_hash[:post][:comments])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -31,7 +31,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_create_adapter
|
def test_create_adapter
|
||||||
adapter = ActiveModel::Serializer::Adapter.create(@serializer)
|
adapter = ActiveModel::Serializer::Adapter.create(@serializer)
|
||||||
assert_equal ActiveModel::Serializer::Adapter::Json, adapter.class
|
assert_equal ActiveModel::Serializer::Adapter::FlattenJson, adapter.class
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_create_adapter_with_override
|
def test_create_adapter_with_override
|
||||||
|
|||||||
@ -11,7 +11,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_returns_default_adapter
|
def test_returns_default_adapter
|
||||||
adapter = ActiveModel::Serializer.adapter
|
adapter = ActiveModel::Serializer.adapter
|
||||||
assert_equal ActiveModel::Serializer::Adapter::Json, adapter
|
assert_equal ActiveModel::Serializer::Adapter::FlattenJson, adapter
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_overwrite_adapter_with_symbol
|
def test_overwrite_adapter_with_symbol
|
||||||
|
|||||||
@ -15,13 +15,13 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_json_serializable_hash
|
def test_json_serializable_hash
|
||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer)
|
adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer)
|
||||||
assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash)
|
assert_equal({alternate_blog: { id:1, title:"AMS Hints"}}, adapter.serializable_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_attribute_inheritance_with_key
|
def test_attribute_inheritance_with_key
|
||||||
inherited_klass = Class.new(AlternateBlogSerializer)
|
inherited_klass = Class.new(AlternateBlogSerializer)
|
||||||
blog_serializer = inherited_klass.new(@blog)
|
blog_serializer = inherited_klass.new(@blog)
|
||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(blog_serializer)
|
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
|
end
|
||||||
|
|
||||||
|
|||||||
@ -8,7 +8,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_default_adapter
|
def test_default_adapter
|
||||||
assert_equal :json, ActiveModel::Serializer.config.adapter
|
assert_equal :flatten_json, ActiveModel::Serializer.config.adapter
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -12,9 +12,10 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_meta_is_present_with_root
|
def test_meta_is_present_with_root
|
||||||
adapter = load_adapter(root: "blog", meta: {total: 10})
|
serializer = AlternateBlogSerializer.new(@blog, root: "blog", meta: {total: 10})
|
||||||
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, root: 'blog')
|
||||||
expected = {
|
expected = {
|
||||||
"blog" => {
|
blog: {
|
||||||
id: 1,
|
id: 1,
|
||||||
title: "AMS Hints"
|
title: "AMS Hints"
|
||||||
},
|
},
|
||||||
@ -35,9 +36,10 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_meta_key_is_used
|
def test_meta_key_is_used
|
||||||
adapter = load_adapter(root: "blog", meta: {total: 10}, meta_key: "haha_meta")
|
serializer = AlternateBlogSerializer.new(@blog, root: 'blog', meta: {total: 10}, meta_key: "haha_meta")
|
||||||
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, root: 'blog')
|
||||||
expected = {
|
expected = {
|
||||||
"blog" => {
|
blog: {
|
||||||
id: 1,
|
id: 1,
|
||||||
title: "AMS Hints"
|
title: "AMS Hints"
|
||||||
},
|
},
|
||||||
@ -50,7 +52,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_meta_is_not_present_on_arrays_without_root
|
def test_meta_is_not_present_on_arrays_without_root
|
||||||
serializer = ArraySerializer.new([@blog], meta: {total: 10})
|
serializer = ArraySerializer.new([@blog], meta: {total: 10})
|
||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer)
|
||||||
expected = [{
|
expected = [{
|
||||||
id: 1,
|
id: 1,
|
||||||
name: "AMS Hints",
|
name: "AMS Hints",
|
||||||
@ -71,7 +73,7 @@ module ActiveModel
|
|||||||
serializer = ArraySerializer.new([@blog], meta: {total: 10}, meta_key: "haha_meta")
|
serializer = ArraySerializer.new([@blog], meta: {total: 10}, meta_key: "haha_meta")
|
||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, root: 'blog')
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, root: 'blog')
|
||||||
expected = {
|
expected = {
|
||||||
'blog' => [{
|
blog: [{
|
||||||
id: 1,
|
id: 1,
|
||||||
name: "AMS Hints",
|
name: "AMS Hints",
|
||||||
writer: {
|
writer: {
|
||||||
@ -98,7 +100,7 @@ module ActiveModel
|
|||||||
options.partition { |k, _| ActionController::Serialization::ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
|
options.partition { |k, _| ActionController::Serialization::ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
|
||||||
|
|
||||||
serializer = AlternateBlogSerializer.new(@blog, serializer_opts)
|
serializer = AlternateBlogSerializer.new(@blog, serializer_opts)
|
||||||
ActiveModel::Serializer::Adapter::Json.new(serializer, adapter_opts)
|
ActiveModel::Serializer::Adapter::FlattenJson.new(serializer, adapter_opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -20,6 +20,7 @@ class Foo < Rails::Application
|
|||||||
config.active_support.test_order = :random
|
config.active_support.test_order = :random
|
||||||
config.logger = Logger.new(nil)
|
config.logger = Logger.new(nil)
|
||||||
ActionController::Base.cache_store = :memory_store
|
ActionController::Base.cache_store = :memory_store
|
||||||
|
ActiveModel::Serializer.config.adapter = :flatten_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))
|
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user