active_model_serializers/test/adapter/json_api/collection_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

97 lines
3.0 KiB
Ruby

require 'test_helper'
module ActiveModelSerializers
module Adapter
class JsonApi
class CollectionTest < ActiveSupport::TestCase
def setup
@author = Author.new(id: 1, name: 'Steve K.')
@author.bio = nil
@blog = Blog.new(id: 23, name: 'AMS Blog')
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
@first_post.comments = []
@second_post.comments = []
@first_post.blog = @blog
@second_post.blog = nil
@first_post.author = @author
@second_post.author = @author
@author.posts = [@first_post, @second_post]
@serializer = ActiveModel::Serializer::CollectionSerializer.new([@first_post, @second_post])
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer)
ActionController::Base.cache_store.clear
end
def test_include_multiple_posts
expected = [
{
id: '1',
type: 'posts',
attributes: {
title: 'Hello!!',
body: 'Hello, world!!'
},
relationships: {
comments: { data: [] },
blog: { data: { type: 'blogs', id: '999' } },
author: { data: { type: 'authors', id: '1' } }
}
},
{
id: '2',
type: 'posts',
attributes: {
title: 'New Post',
body: 'Body'
},
relationships: {
comments: { data: [] },
blog: { data: { type: 'blogs', id: '999' } },
author: { data: { type: 'authors', id: '1' } }
}
}
]
assert_equal(expected, @adapter.serializable_hash[:data])
end
def test_limiting_fields
actual = ActiveModelSerializers::SerializableResource.new(
[@first_post, @second_post],
adapter: :json_api,
fields: { posts: %w(title comments blog author) }
).serializable_hash
expected = [
{
id: '1',
type: 'posts',
attributes: {
title: 'Hello!!'
},
relationships: {
comments: { data: [] },
blog: { data: { type: 'blogs', id: '999' } },
author: { data: { type: 'authors', id: '1' } }
}
},
{
id: '2',
type: 'posts',
attributes: {
title: 'New Post'
},
relationships: {
comments: { data: [] },
blog: { data: { type: 'blogs', id: '999' } },
author: { data: { type: 'authors', id: '1' } }
}
}
]
assert_equal(expected, actual[:data])
end
end
end
end
end