Merge branch 'master' into domitian-move-namespace-of-adapter-to-active-model-serializers

Conflicts:
	CHANGELOG.md
	lib/active_model/serializer/adapter/attributes.rb
	lib/active_model/serializer/adapter/cached_serializer.rb
	lib/active_model/serializer/adapter/fragment_cache.rb
	lib/active_model/serializer/adapter/json_api.rb
	lib/active_model/serializer/adapter/json_api/link.rb
	test/adapter/fragment_cache_test.rb
	test/adapter/json_api/links_test.rb
	test/adapter/json_api/resource_type_config_test.rb
This commit is contained in:
Benjamin Fleischer
2016-02-23 23:21:49 -06:00
39 changed files with 1357 additions and 341 deletions

View File

@@ -0,0 +1,168 @@
require 'test_helper'
module ActiveModel
class Serializer
module Adapter
class JsonApi
module ApiObjects
class RelationshipTest < ActiveSupport::TestCase
def setup
@blog = Blog.new(id: 1)
@author = Author.new(id: 1, name: 'Steve K.', blog: @blog)
@serializer = BlogSerializer.new(@blog)
ActionController::Base.cache_store.clear
end
def test_relationship_with_data
expected = {
data: {
id: '1',
type: 'blogs'
}
}
test_relationship(expected, options: { include_data: true })
end
def test_relationship_with_nil_model
@serializer = BlogSerializer.new(nil)
expected = { data: nil }
test_relationship(expected, options: { include_data: true })
end
def test_relationship_with_nil_serializer
@serializer = nil
expected = { data: nil }
test_relationship(expected, options: { include_data: true })
end
def test_relationship_with_data_array
posts = [Post.new(id: 1), Post.new(id: 2)]
@serializer = ActiveModel::Serializer::CollectionSerializer.new(posts)
@author.posts = posts
@author.blog = nil
expected = {
data: [
{
id: '1',
type: 'posts'
},
{
id: '2',
type: 'posts'
}
]
}
test_relationship(expected, options: { include_data: true })
end
def test_relationship_data_not_included
test_relationship({}, options: { include_data: false })
end
def test_relationship_simple_link
links = { self: 'a link' }
test_relationship({ links: { self: 'a link' } }, links: links)
end
def test_relationship_many_links
links = {
self: 'a link',
related: 'another link'
}
expected = {
links: {
self: 'a link',
related: 'another link'
}
}
test_relationship(expected, links: links)
end
def test_relationship_block_link
links = { self: proc { "#{object.id}" } }
expected = { links: { self: "#{@blog.id}" } }
test_relationship(expected, links: links)
end
def test_relationship_block_link_with_meta
links = {
self: proc do
href "#{object.id}"
meta(id: object.id)
end
}
expected = {
links: {
self: {
href: "#{@blog.id}",
meta: { id: @blog.id }
}
}
}
test_relationship(expected, links: links)
end
def test_relationship_simple_meta
meta = { id: '1' }
expected = { meta: meta }
test_relationship(expected, meta: meta)
end
def test_relationship_block_meta
meta = proc do
{ id: object.id }
end
expected = {
meta: {
id: @blog.id
}
}
test_relationship(expected, meta: meta)
end
def test_relationship_with_everything
links = {
self: 'a link',
related: proc do
href "#{object.id}"
meta object.id
end
}
meta = proc do
{ id: object.id }
end
expected = {
data: {
id: '1',
type: 'blogs'
},
links: {
self: 'a link',
related: {
href: '1', meta: 1
}
},
meta: {
id: @blog.id
}
}
test_relationship(expected, meta: meta, options: { include_data: true }, links: links)
end
private
def test_relationship(expected, params = {})
options = params.fetch(:options, {})
links = params.fetch(:links, {})
meta = params[:meta]
parent_serializer = AuthorSerializer.new(@author)
relationship = Relationship.new(parent_serializer, @serializer, options, links, meta)
assert_equal(expected, relationship.as_json)
end
end
end
end
end
end
end

View File

@@ -0,0 +1,88 @@
require 'test_helper'
module ActiveModel
class Serializer
module Adapter
class JsonApi
module ApiObjects
class ResourceIdentifierTest < ActiveSupport::TestCase
class WithDefinedTypeSerializer < Serializer
type 'with_defined_type'
end
class WithDefinedIdSerializer < Serializer
def id
'special_id'
end
end
class FragmentedSerializer < Serializer; end
def setup
@model = Author.new(id: 1, name: 'Steve K.')
ActionController::Base.cache_store.clear
end
def test_defined_type
test_type(WithDefinedTypeSerializer, 'with_defined_type')
end
def test_singular_type
test_type_inflection(AuthorSerializer, 'author', :singular)
end
def test_plural_type
test_type_inflection(AuthorSerializer, 'authors', :plural)
end
def test_id_defined_on_object
test_id(AuthorSerializer, @model.id.to_s)
end
def test_id_defined_on_serializer
test_id(WithDefinedIdSerializer, 'special_id')
end
def test_id_defined_on_fragmented
FragmentedSerializer.fragmented(WithDefinedIdSerializer.new(@author))
test_id(FragmentedSerializer, 'special_id')
end
private
def test_type_inflection(serializer_class, expected_type, inflection)
original_inflection = ActiveModelSerializers.config.jsonapi_resource_type
ActiveModelSerializers.config.jsonapi_resource_type = inflection
test_type(serializer_class, expected_type)
ActiveModelSerializers.config.jsonapi_resource_type = original_inflection
end
def test_type(serializer_class, expected_type)
serializer = serializer_class.new(@model)
resource_identifier = ResourceIdentifier.new(serializer)
expected = {
id: @model.id.to_s,
type: expected_type
}
assert_equal(expected, resource_identifier.as_json)
end
def test_id(serializer_class, id)
serializer = serializer_class.new(@model)
resource_identifier = ResourceIdentifier.new(serializer)
inflection = ActiveModelSerializers.config.jsonapi_resource_type
type = @model.class.model_name.send(inflection)
expected = {
id: id,
type: type
}
assert_equal(expected, resource_identifier.as_json)
end
end
end
end
end
end
end

View File

@@ -20,7 +20,7 @@ module ActiveModelSerializers
def setup
@post = Post.new(id: 1337, comments: [], author: nil)
@author = LinkAuthor.new(id: 1337)
@author = LinkAuthor.new(id: 1337, posts: [@post])
end
def test_toplevel_links
@@ -46,6 +46,24 @@ module ActiveModelSerializers
assert_equal(expected, hash[:links])
end
def test_nil_toplevel_links
hash = ActiveModel::SerializableResource.new(
@post,
adapter: :json_api,
links: nil
).serializable_hash
refute hash.key?(:links), 'No links key to be output'
end
def test_nil_toplevel_links_json_adapter
hash = ActiveModel::SerializableResource.new(
@post,
adapter: :json,
links: nil
).serializable_hash
refute hash.key?(:links), 'No links key to be output'
end
def test_resource_links
hash = serializable(@author, adapter: :json_api).serializable_hash
expected = {

View File

@@ -0,0 +1,192 @@
require 'test_helper'
module ActiveModel
class Serializer
module Adapter
class JsonApi
class RelationshipTest < ActiveSupport::TestCase
RelationshipAuthor = Class.new(::Model)
class RelationshipAuthorSerializer < ActiveModel::Serializer
has_one :bio do
link :self, '//example.com/link_author/relationships/bio'
end
has_one :profile do
link :related do
"//example.com/profiles/#{object.profile.id}"
end
end
has_many :locations do
link :related do
ids = object.locations.map(&:id).join(',')
href "//example.com/locations/#{ids}"
end
end
has_many :posts do
link :related do
ids = object.posts.map(&:id).join(',')
href "//example.com/posts/#{ids}"
meta ids: ids
end
end
has_many :comments do
link :self do
meta ids: [1]
end
end
has_many :roles do
meta count: object.posts.count
end
has_one :blog do
link :self, '//example.com/link_author/relationships/blog'
include_data false
end
belongs_to :reviewer do
meta name: 'Dan Brown'
include_data true
end
has_many :likes do
link :related do
ids = object.likes.map(&:id).join(',')
href "//example.com/likes/#{ids}"
meta ids: ids
end
meta liked: object.likes.any?
end
end
def setup
@post = Post.new(id: 1337, comments: [], author: nil)
@blog = Blog.new(id: 1337, name: 'extra')
@bio = Bio.new(id: 1337)
@like = Like.new(id: 1337)
@role = Role.new(id: 1337)
@profile = Profile.new(id: 1337)
@location = Location.new(id: 1337)
@reviewer = Author.new(id: 1337)
@comment = Comment.new(id: 1337)
@author = RelationshipAuthor.new(
id: 1337,
posts: [@post],
blog: @blog,
reviewer: @reviewer,
bio: @bio,
likes: [@like],
roles: [@role],
locations: [@location],
profile: @profile,
comments: [@comment]
)
end
def test_relationship_simple_link
expected = {
data: {
id: '1337',
type: 'bios'
},
links: {
self: '//example.com/link_author/relationships/bio'
}
}
assert_relationship(:bio, expected)
end
def test_relationship_block_link
expected = {
data: { id: '1337', type: 'profiles' },
links: { related: '//example.com/profiles/1337' }
}
assert_relationship(:profile, expected)
end
def test_relationship_block_link_href
expected = {
data: [{ id: '1337', type: 'locations' }],
links: {
related: { href: '//example.com/locations/1337' }
}
}
assert_relationship(:locations, expected)
end
def test_relationship_block_link_href_and_meta
expected = {
data: [{ id: '1337', type: 'posts' }],
links: {
related: {
href: '//example.com/posts/1337',
meta: { ids: '1337' }
}
}
}
assert_relationship(:posts, expected)
end
def test_relationship_block_link_meta
expected = {
data: [{ id: '1337', type: 'comments' }],
links: {
self: {
meta: { ids: [1] }
}
}
}
assert_relationship(:comments, expected)
end
def test_relationship_meta
expected = {
data: [{ id: '1337', type: 'roles' }],
meta: { count: 1 }
}
assert_relationship(:roles, expected)
end
def test_relationship_not_including_data
expected = {
links: { self: '//example.com/link_author/relationships/blog' }
}
assert_relationship(:blog, expected)
end
def test_relationship_including_data_explicit
expected = {
data: { id: '1337', type: 'authors' },
meta: { name: 'Dan Brown' }
}
assert_relationship(:reviewer, expected)
end
def test_relationship_with_everything
expected = {
data: [{ id: '1337', type: 'likes' }],
links: {
related: {
href: '//example.com/likes/1337',
meta: { ids: '1337' }
}
},
meta: { liked: true }
}
assert_relationship(:likes, expected)
end
private
def assert_relationship(relationship_name, expected)
hash = serializable(@author, adapter: :json_api).serializable_hash
assert_equal(expected, hash[:data][:relationships][relationship_name])
end
end
end
end
end
end

View File

@@ -0,0 +1,68 @@
require 'test_helper'
module ActiveModel
class Serializer
module Adapter
class JsonApi
class ResourceMetaTest < Minitest::Test
class MetaHashPostSerializer < ActiveModel::Serializer
attributes :id
meta stuff: 'value'
end
class MetaBlockPostSerializer < ActiveModel::Serializer
attributes :id
meta do
{ comments_count: object.comments.count }
end
end
def setup
@post = Post.new(id: 1337, comments: [], author: nil)
end
def test_meta_hash_object_resource
hash = ActiveModel::SerializableResource.new(
@post,
serializer: MetaHashPostSerializer,
adapter: :json_api
).serializable_hash
expected = {
stuff: 'value'
}
assert_equal(expected, hash[:data][:meta])
end
def test_meta_block_object_resource
hash = ActiveModel::SerializableResource.new(
@post,
serializer: MetaBlockPostSerializer,
adapter: :json_api
).serializable_hash
expected = {
comments_count: @post.comments.count
}
assert_equal(expected, hash[:data][:meta])
end
def test_meta_object_resource_in_array
post2 = Post.new(id: 1339, comments: [Comment.new])
posts = [@post, post2]
hash = ActiveModel::SerializableResource.new(
posts,
each_serializer: MetaBlockPostSerializer,
adapter: :json_api
).serializable_hash
expected = {
:data => [
{ :id => '1337', :type => 'posts', :meta => { :comments_count => 0 } },
{ :id => '1339', :type => 'posts', :meta => { :comments_count => 1 } }
]
}
assert_equal(expected, hash)
end
end
end
end
end
end

View File

@@ -1,69 +0,0 @@
require 'test_helper'
module ActiveModelSerializers
module Adapter
class JsonApi
class ResourceTypeConfigTest < ActiveSupport::TestCase
class ProfileTypeSerializer < ActiveModel::Serializer
attributes :name
type 'profile'
end
def setup
@author = Author.new(id: 1, name: 'Steve K.')
@author.bio = nil
@author.roles = []
@blog = Blog.new(id: 23, name: 'AMS Blog')
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
@post.comments = [@comment]
@post.blog = @blog
@anonymous_post.comments = []
@anonymous_post.blog = nil
@comment.post = @post
@comment.author = nil
@post.author = @author
@anonymous_post.author = nil
@blog = Blog.new(id: 1, name: 'My Blog!!')
@blog.writer = @author
@blog.articles = [@post, @anonymous_post]
@author.posts = []
end
def with_jsonapi_resource_type type
old_type = ActiveModelSerializers.config.jsonapi_resource_type
ActiveModelSerializers.config.jsonapi_resource_type = type
yield
ensure
ActiveModelSerializers.config.jsonapi_resource_type = old_type
end
def test_config_plural
with_jsonapi_resource_type :plural do
hash = serializable(@comment, adapter: :json_api).serializable_hash
assert_equal('comments', hash[:data][:type])
end
end
def test_config_singular
with_jsonapi_resource_type :singular do
hash = serializable(@comment, adapter: :json_api).serializable_hash
assert_equal('comment', hash[:data][:type])
end
end
def test_explicit_type_value
hash = serializable(@author, serializer: ProfileTypeSerializer, adapter: :json_api).serializable_hash
assert_equal('profile', hash.fetch(:data).fetch(:type))
end
private
def serializable(resource, options = {})
ActiveModel::SerializableResource.new(resource, options)
end
end
end
end
end

View File

@@ -0,0 +1,61 @@
require 'test_helper'
module ActiveModel
class Serializer
module Adapter
class JsonApi
class TypeTest < ActiveSupport::TestCase
class StringTypeSerializer < ActiveModel::Serializer
attribute :name
type 'profile'
end
class SymbolTypeSerializer < ActiveModel::Serializer
attribute :name
type :profile
end
setup do
@author = Author.new(id: 1, name: 'Steve K.')
end
def test_config_plural
with_jsonapi_resource_type :plural do
assert_type(@author, 'authors')
end
end
def test_config_singular
with_jsonapi_resource_type :singular do
assert_type(@author, 'author')
end
end
def test_explicit_string_type_value
assert_type(@author, 'profile', serializer: StringTypeSerializer)
end
def test_explicit_symbol_type_value
assert_type(@author, 'profile', serializer: SymbolTypeSerializer)
end
private
def assert_type(resource, expected_type, opts = {})
opts = opts.reverse_merge(adapter: :json_api)
hash = serializable(resource, opts).serializable_hash
assert_equal(expected_type, hash.fetch(:data).fetch(:type))
end
def with_jsonapi_resource_type inflection
old_inflection = ActiveModelSerializers.config.jsonapi_resource_type
ActiveModelSerializers.config.jsonapi_resource_type = inflection
yield
ensure
ActiveModelSerializers.config.jsonapi_resource_type = old_inflection
end
end
end
end
end
end