updating tests to match new adapters structure

This commit is contained in:
João Moura 2015-06-15 13:39:36 -03:00
parent 2e46507971
commit 1ea5608e78
17 changed files with 49 additions and 43 deletions

View File

@ -6,7 +6,7 @@ module ActionController
include ActionController::Renderers
ADAPTER_OPTION_KEYS = [:include, :fields, :root, :adapter]
ADAPTER_OPTION_KEYS = [:include, :fields, :adapter]
included do
class_attribute :_serialization_scope

View File

@ -22,7 +22,8 @@ module ActiveModel
def as_json(options = {})
hash = serializable_hash(options)
include_meta(hash)
include_meta(hash) unless self.class == FlattenJson
hash
end
def self.create(resource, options = {})

View File

@ -7,8 +7,6 @@ module ActiveModel
attr_reader :meta, :meta_key
def initialize(objects, options = {})
options.merge!(root: nil)
@objects = objects.map do |object|
serializer_class = options.fetch(
:serializer,
@ -21,7 +19,7 @@ module ActiveModel
end
def json_key
@objects.first.json_key.pluralize if @objects.first
@objects.first.json_key if @objects.first
end
def root=(root)

View File

@ -12,7 +12,7 @@ module ActiveModel
end
def fields_for(serializer)
key = serializer.json_key || serializer.class.root_name
key = serializer.json_key
fields[key.to_sym]
end

View File

@ -77,12 +77,10 @@ module ActionController
get :render_array_using_explicit_serializer
assert_equal 'application/json', @response.content_type
expected = {
'paginated' => [
{ 'name' => 'Name 1' },
{ 'name' => 'Name 2' }
]
}
expected = [
{ 'name' => 'Name 1' },
{ 'name' => 'Name 2' }
]
assert_equal expected.to_json, @response.body
end

View File

@ -10,13 +10,17 @@ module ActionController
end
def render_using_custom_root
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile, root: "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"
end
end
def render_using_custom_root_and_meta
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile, root: "custom_root", meta: { total: 10 }
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", meta: { total: 10 }
end
end
def render_using_default_adapter_root
@ -34,11 +38,13 @@ module ActionController
end
def render_array_using_custom_root_and_meta
array = [
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 }
with_adapter ActiveModel::Serializer::Adapter::Json do
array = [
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 }
end
end
def render_array_using_implicit_serializer
@ -219,6 +225,7 @@ module ActionController
def test_render_array_using_custom_root_and_meta
get :render_array_using_custom_root_and_meta
assert_equal 'application/json', @response.content_type
expected = { custom_root: [

View File

@ -25,21 +25,21 @@ module ActiveModel
end
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
def test_include_nil_author
serializer = PostSerializer.new(@anonymous_post)
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
def test_include_nil_author_with_specified_serializer
serializer = PostPreviewSerializer.new(@anonymous_post)
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

View File

@ -28,16 +28,16 @@ module ActiveModel
@serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
expected = [{
expected = {custom_blog:[{
id: 1,
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
def test_include_multiple_posts
expected = [{
expected = { post: [{
title: "Hello!!",
body: "Hello, world!!",
id: 1,
@ -63,7 +63,7 @@ module ActiveModel
id: 999,
name: "Custom blog"
}
}]
}]}
assert_equal expected, @adapter.serializable_hash
end
end

View File

@ -26,7 +26,7 @@ module ActiveModel
assert_equal([
{id: 1, body: 'ZOMG A COMMENT'},
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
], @adapter.serializable_hash[:comments])
], @adapter.serializable_hash[:post][:comments])
end
end
end

View File

@ -86,7 +86,6 @@ module ActiveModel
}
}
]
assert_equal(expected, @adapter.serializable_hash[:data])
end

View File

@ -25,7 +25,7 @@ module ActiveModel
assert_equal([
{id: 1, body: 'ZOMG A COMMENT'},
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
], @adapter.serializable_hash[:comments])
], @adapter.serializable_hash[:post][:comments])
end
end
end

View File

@ -31,7 +31,7 @@ module ActiveModel
def test_create_adapter
adapter = ActiveModel::Serializer::Adapter.create(@serializer)
assert_equal ActiveModel::Serializer::Adapter::Json, adapter.class
assert_equal ActiveModel::Serializer::Adapter::FlattenJson, adapter.class
end
def test_create_adapter_with_override

View File

@ -11,7 +11,7 @@ module ActiveModel
def test_returns_default_adapter
adapter = ActiveModel::Serializer.adapter
assert_equal ActiveModel::Serializer::Adapter::Json, adapter
assert_equal ActiveModel::Serializer::Adapter::FlattenJson, adapter
end
def test_overwrite_adapter_with_symbol

View File

@ -15,13 +15,13 @@ module ActiveModel
def test_json_serializable_hash
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
def test_attribute_inheritance_with_key
inherited_klass = Class.new(AlternateBlogSerializer)
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)
end

View File

@ -8,7 +8,7 @@ module ActiveModel
end
def test_default_adapter
assert_equal :json, ActiveModel::Serializer.config.adapter
assert_equal :flatten_json, ActiveModel::Serializer.config.adapter
end
end
end

View File

@ -12,9 +12,10 @@ module ActiveModel
end
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 = {
"blog" => {
blog: {
id: 1,
title: "AMS Hints"
},
@ -35,9 +36,10 @@ module ActiveModel
end
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 = {
"blog" => {
blog: {
id: 1,
title: "AMS Hints"
},
@ -50,7 +52,7 @@ module ActiveModel
def test_meta_is_not_present_on_arrays_without_root
serializer = ArraySerializer.new([@blog], meta: {total: 10})
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer)
expected = [{
id: 1,
name: "AMS Hints",
@ -71,7 +73,7 @@ module ActiveModel
serializer = ArraySerializer.new([@blog], meta: {total: 10}, meta_key: "haha_meta")
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, root: 'blog')
expected = {
'blog' => [{
blog: [{
id: 1,
name: "AMS Hints",
writer: {
@ -98,7 +100,7 @@ module ActiveModel
options.partition { |k, _| ActionController::Serialization::ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
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

View File

@ -20,6 +20,7 @@ class Foo < Rails::Application
config.active_support.test_order = :random
config.logger = Logger.new(nil)
ActionController::Base.cache_store = :memory_store
ActiveModel::Serializer.config.adapter = :flatten_json
end
end
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))