active_model_serializers/test/serializers/meta_test.rb
Noah Silas 94db09b3f6 Fix RuboCop 0.40 linter errors (#1722)
These errors are breaking the build, which seems to use RuboCop 0.40 [1]
despite the Gemfile.lock pinning rubocop to 0.38.

New lints that I am updating the code style to reflect:

- Style/EmptyCaseCondition: Do not use empty case condition, instead use
  an if expression.

- Style/MultilineArrayBraceLayout: Closing array brace must be on the
  same line as the last array element when opening brace is on the same
  line as the first array element.

- Style/MultilineHashBraceLayout: Closing hash brace must be on the same
  line as the last hash element when opening brace is on the same line
  as the first hash element.

- Style/MultilineMethodCallBraceLayout: Closing method call brace must
  be on the line after the last argument when opening brace is on a
  separate line from the first argument.

[1] https://github.com/bbatsov/rubocop/releases/tag/v0.40.0
2016-05-26 12:58:05 -04:00

203 lines
5.1 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 = ActiveModelSerializers::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_blank
actual = ActiveModelSerializers::SerializableResource.new(
@blog,
adapter: :json,
serializer: AlternateBlogSerializer,
meta: {}
).as_json
expected = {
blog: {
id: 1,
title: 'AMS Hints'
}
}
assert_equal(expected, actual)
end
def test_meta_is_not_included_when_empty_string
actual = ActiveModelSerializers::SerializableResource.new(
@blog,
adapter: :json,
serializer: AlternateBlogSerializer,
meta: ''
).as_json
expected = {
blog: {
id: 1,
title: 'AMS Hints'
}
}
assert_equal(expected, actual)
end
def test_meta_is_not_included_when_root_is_missing
actual = ActiveModelSerializers::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 = ActiveModelSerializers::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_not_used_with_json_api
actual = ActiveModelSerializers::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' }
},
meta: { total: 10 }
}
assert_equal(expected, actual)
end
def test_meta_key_is_not_present_when_empty_hash_with_json_api
actual = ActiveModelSerializers::SerializableResource.new(
@blog,
adapter: :json_api,
serializer: AlternateBlogSerializer,
meta: {}
).as_json
expected = {
data: {
id: '1',
type: 'blogs',
attributes: { title: 'AMS Hints' }
}
}
assert_equal(expected, actual)
end
def test_meta_key_is_not_present_when_empty_string_with_json_api
actual = ActiveModelSerializers::SerializableResource.new(
@blog,
adapter: :json_api,
serializer: AlternateBlogSerializer,
meta: ''
).as_json
expected = {
data: {
id: '1',
type: 'blogs',
attributes: { title: 'AMS Hints' }
}
}
assert_equal(expected, actual)
end
def test_meta_is_not_present_on_arrays_without_root
actual = ActiveModelSerializers::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 = ActiveModelSerializers::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