active_model_serializers/test/serializers/meta_test.rb
Benjamin Fleischer 1cc2e04cf6 Address issues in 50950d9533 #1340
- Add changelog entry
- Remove superseded and incorrect tests
- Fix array serialization test
2016-02-08 18:14:25 -06:00

131 lines
3.3 KiB
Ruby

require 'test_helper'
module ActiveModel
class Serializer
class MetaTest < ActiveSupport::TestCase
def setup
@blog = Blog.new(id: 1,
name: 'AMS Hints',
writer: Author.new(id: 2, name: 'Steve'),
articles: [Post.new(id: 3, title: 'AMS')])
end
def test_meta_is_present_with_root
actual = ActiveModel::SerializableResource.new(
@blog,
adapter: :json,
serializer: AlternateBlogSerializer,
meta: { total: 10 }).as_json
expected = {
blog: {
id: 1,
title: 'AMS Hints'
},
'meta' => {
total: 10
}
}
assert_equal(expected, actual)
end
def test_meta_is_not_included_when_root_is_missing
actual = ActiveModel::SerializableResource.new(
@blog,
adapter: :attributes,
serializer: AlternateBlogSerializer,
meta: { total: 10 }).as_json
expected = {
id: 1,
title: 'AMS Hints'
}
assert_equal(expected, actual)
end
def test_meta_key_is_used
actual = ActiveModel::SerializableResource.new(
@blog,
adapter: :json,
serializer: AlternateBlogSerializer,
meta: { total: 10 },
meta_key: 'haha_meta').as_json
expected = {
blog: {
id: 1,
title: 'AMS Hints'
},
'haha_meta' => {
total: 10
}
}
assert_equal(expected, actual)
end
def test_meta_key_is_used_with_json_api
actual = ActiveModel::SerializableResource.new(
@blog,
adapter: :json_api,
serializer: AlternateBlogSerializer,
meta: { total: 10 },
meta_key: 'haha_meta').as_json
expected = {
data: {
id: '1',
type: 'blogs',
attributes: { title: 'AMS Hints' }
},
'haha_meta' => { total: 10 }
}
assert_equal(expected, actual)
end
def test_meta_is_not_present_on_arrays_without_root
actual = ActiveModel::SerializableResource.new(
[@blog],
adapter: :attributes,
meta: { total: 10 }).as_json
expected = [{
id: 1,
name: 'AMS Hints',
writer: {
id: 2,
name: 'Steve'
},
articles: [{
id: 3,
title: 'AMS',
body: nil
}]
}]
assert_equal(expected, actual)
end
def test_meta_is_present_on_arrays_with_root
actual = ActiveModel::SerializableResource.new(
[@blog],
adapter: :json,
meta: { total: 10 },
meta_key: 'haha_meta').as_json
expected = {
blogs: [{
id: 1,
name: 'AMS Hints',
writer: {
id: 2,
name: 'Steve'
},
articles: [{
id: 3,
title: 'AMS',
body: nil
}]
}],
'haha_meta' => {
total: 10
}
}
assert_equal(expected, actual)
end
end
end
end