mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
merge upstream update fieldset
This commit is contained in:
41
test/action_controller/adapter_selector_test.rb
Normal file
41
test/action_controller/adapter_selector_test.rb
Normal file
@@ -0,0 +1,41 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActionController
|
||||
module Serialization
|
||||
class AdapterSelectorTest < ActionController::TestCase
|
||||
class MyController < ActionController::Base
|
||||
def render_using_default_adapter
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
render json: @profile
|
||||
end
|
||||
|
||||
def render_using_adapter_override
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
render json: @profile, adapter: :json_api
|
||||
end
|
||||
|
||||
def render_skipping_adapter
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
render json: @profile, adapter: false
|
||||
end
|
||||
end
|
||||
|
||||
tests MyController
|
||||
|
||||
def test_render_using_default_adapter
|
||||
get :render_using_default_adapter
|
||||
assert_equal '{"name":"Name 1","description":"Description 1"}', response.body
|
||||
end
|
||||
|
||||
def test_render_using_adapter_override
|
||||
get :render_using_adapter_override
|
||||
assert_equal '{"profiles":{"name":"Name 1","description":"Description 1"}}', response.body
|
||||
end
|
||||
|
||||
def test_render_skipping_adapter
|
||||
get :render_skipping_adapter
|
||||
assert_equal '{"attributes":{"name":"Name 1","description":"Description 1","comments":"Comments 1"}}', response.body
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
78
test/action_controller/explicit_serializer_test.rb
Normal file
78
test/action_controller/explicit_serializer_test.rb
Normal file
@@ -0,0 +1,78 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActionController
|
||||
module Serialization
|
||||
class ExplicitSerializerTest < ActionController::TestCase
|
||||
class MyController < ActionController::Base
|
||||
def render_using_explicit_serializer
|
||||
@profile = Profile.new(name: 'Name 1',
|
||||
description: 'Description 1',
|
||||
comments: 'Comments 1')
|
||||
render json: @profile, serializer: ProfilePreviewSerializer
|
||||
end
|
||||
|
||||
def render_array_using_explicit_serializer
|
||||
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,
|
||||
serializer: PaginatedSerializer,
|
||||
each_serializer: ProfilePreviewSerializer
|
||||
end
|
||||
|
||||
def render_array_using_implicit_serializer
|
||||
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,
|
||||
each_serializer: ProfilePreviewSerializer
|
||||
end
|
||||
end
|
||||
|
||||
tests MyController
|
||||
|
||||
def test_render_using_explicit_serializer
|
||||
get :render_using_explicit_serializer
|
||||
|
||||
assert_equal 'application/json', @response.content_type
|
||||
assert_equal '{"name":"Name 1"}', @response.body
|
||||
end
|
||||
|
||||
def test_render_array_using_explicit_serializer
|
||||
get :render_array_using_explicit_serializer
|
||||
assert_equal 'application/json', @response.content_type
|
||||
|
||||
expected = {
|
||||
'paginated' => [
|
||||
{ 'name' => 'Name 1' },
|
||||
{ 'name' => 'Name 2' }
|
||||
]
|
||||
}
|
||||
|
||||
assert_equal expected.to_json, @response.body
|
||||
end
|
||||
|
||||
def test_render_array_using_explicit_serializer
|
||||
get :render_array_using_implicit_serializer
|
||||
assert_equal 'application/json', @response.content_type
|
||||
|
||||
expected = [
|
||||
{ 'name' => 'Name 1' },
|
||||
{ 'name' => 'Name 2' }
|
||||
]
|
||||
assert_equal expected.to_json, @response.body
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -5,10 +5,18 @@ module ActionController
|
||||
class JsonApiLinkedTest < ActionController::TestCase
|
||||
class MyController < ActionController::Base
|
||||
def setup_post
|
||||
@role1 = Role.new(id: 1, name: 'admin')
|
||||
@role2 = Role.new(id: 2, name: 'colab')
|
||||
@author = Author.new(id: 1, name: 'Steve K.')
|
||||
@author.posts = []
|
||||
@author.bio = nil
|
||||
@author.roles = [@role1, @role2]
|
||||
@role1.author = @author
|
||||
@role2.author = @author
|
||||
@author2 = Author.new(id: 2, name: 'Anonymous')
|
||||
@author2.posts = []
|
||||
@author2.bio = nil
|
||||
@author2.roles = []
|
||||
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
|
||||
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
||||
@@ -20,47 +28,34 @@ module ActionController
|
||||
@second_comment.author = nil
|
||||
end
|
||||
|
||||
def with_json_api_adapter
|
||||
old_adapter = ActiveModel::Serializer.config.adapter
|
||||
ActiveModel::Serializer.config.adapter = :json_api
|
||||
yield
|
||||
ensure
|
||||
ActiveModel::Serializer.config.adapter = old_adapter
|
||||
end
|
||||
|
||||
def render_resource_without_include
|
||||
with_json_api_adapter do
|
||||
setup_post
|
||||
render json: @post
|
||||
end
|
||||
setup_post
|
||||
render json: @post, adapter: :json_api
|
||||
end
|
||||
|
||||
def render_resource_with_include
|
||||
with_json_api_adapter do
|
||||
setup_post
|
||||
render json: @post, include: 'author'
|
||||
end
|
||||
setup_post
|
||||
render json: @post, include: 'author', adapter: :json_api
|
||||
end
|
||||
|
||||
def render_resource_with_nested_include
|
||||
with_json_api_adapter do
|
||||
setup_post
|
||||
render json: @post, include: 'comments.author'
|
||||
end
|
||||
setup_post
|
||||
render json: @post, include: 'comments.author', adapter: :json_api
|
||||
end
|
||||
|
||||
def render_resource_with_nested_has_many_include
|
||||
setup_post
|
||||
render json: @post, include: 'author,author.roles', adapter: :json_api
|
||||
end
|
||||
|
||||
def render_collection_without_include
|
||||
with_json_api_adapter do
|
||||
setup_post
|
||||
render json: [@post]
|
||||
end
|
||||
setup_post
|
||||
render json: [@post], adapter: :json_api
|
||||
end
|
||||
|
||||
def render_collection_with_include
|
||||
with_json_api_adapter do
|
||||
setup_post
|
||||
render json: [@post], include: 'author,comments'
|
||||
end
|
||||
setup_post
|
||||
render json: [@post], include: 'author,comments', adapter: :json_api
|
||||
end
|
||||
end
|
||||
|
||||
@@ -80,6 +75,36 @@ module ActionController
|
||||
assert_equal 'Steve K.', response['linked']['authors'].first['name']
|
||||
end
|
||||
|
||||
def test_render_resource_with_nested_has_many_include
|
||||
get :render_resource_with_nested_has_many_include
|
||||
response = JSON.parse(@response.body)
|
||||
expected_linked = {
|
||||
"authors" => [{
|
||||
"id" => "1",
|
||||
"name" => "Steve K.",
|
||||
"links" => {
|
||||
"posts" => [],
|
||||
"roles" => ["1", "2"],
|
||||
"bio" => nil
|
||||
}
|
||||
}],
|
||||
"roles"=>[{
|
||||
"id" => "1",
|
||||
"name" => "admin",
|
||||
"links" => {
|
||||
"author" => "1"
|
||||
}
|
||||
}, {
|
||||
"id" => "2",
|
||||
"name" => "colab",
|
||||
"links" => {
|
||||
"author" => "1"
|
||||
}
|
||||
}]
|
||||
}
|
||||
assert_equal expected_linked, response['linked']
|
||||
end
|
||||
|
||||
def test_render_resource_with_nested_include
|
||||
get :render_resource_with_nested_include
|
||||
response = JSON.parse(@response.body)
|
||||
|
||||
@@ -25,13 +25,9 @@ module ActionController
|
||||
end
|
||||
|
||||
def render_using_custom_root_in_adapter_with_a_default
|
||||
old_adapter = ActiveModel::Serializer.config.adapter
|
||||
# JSON-API adapter sets root by default
|
||||
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
render json: @profile, root: "profile"
|
||||
ensure
|
||||
ActiveModel::Serializer.config.adapter = old_adapter
|
||||
render json: @profile, root: "profile", adapter: :json_api
|
||||
end
|
||||
|
||||
def render_array_using_implicit_serializer
|
||||
|
||||
@@ -7,6 +7,8 @@ module ActiveModel
|
||||
class BelongsToTest < Minitest::Test
|
||||
def setup
|
||||
@author = Author.new(id: 1, name: 'Steve K.')
|
||||
@author.bio = nil
|
||||
@author.roles = []
|
||||
@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')
|
||||
@@ -31,12 +33,28 @@ module ActiveModel
|
||||
|
||||
def test_includes_linked_post
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post')
|
||||
assert_equal([{id: "42", title: 'New Post', body: 'Body'}], @adapter.serializable_hash[:linked][:posts])
|
||||
expected = [{
|
||||
id: "42",
|
||||
title: 'New Post',
|
||||
body: 'Body',
|
||||
links: {
|
||||
comments: ["1"],
|
||||
author: "1"
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
|
||||
end
|
||||
|
||||
def test_limiting_linked_post_fields
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post', fields: {post: [:title]})
|
||||
assert_equal([{title: 'New Post'}], @adapter.serializable_hash[:linked][:posts])
|
||||
expected = [{
|
||||
title: 'New Post',
|
||||
links: {
|
||||
comments: ["1"],
|
||||
author: "1"
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
|
||||
end
|
||||
|
||||
def test_include_nil_author
|
||||
@@ -49,7 +67,53 @@ module ActiveModel
|
||||
def test_include_type_for_association_when_is_different_than_name
|
||||
serializer = BlogSerializer.new(@blog)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||
assert_equal({type: "author", id: "1"}, adapter.serializable_hash[:blogs][:links][:writer])
|
||||
links = adapter.serializable_hash[:blogs][:links]
|
||||
expected = {
|
||||
writer: {
|
||||
type: "author",
|
||||
id: "1"
|
||||
},
|
||||
articles: {
|
||||
type: "posts",
|
||||
ids: ["42", "43"]
|
||||
}
|
||||
}
|
||||
assert_equal expected, links
|
||||
end
|
||||
|
||||
def test_include_linked_resources_with_type_name
|
||||
serializer = BlogSerializer.new(@blog)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: "writer,articles")
|
||||
linked = adapter.serializable_hash[:linked]
|
||||
expected = {
|
||||
authors: [{
|
||||
id: "1",
|
||||
name: "Steve K.",
|
||||
links: {
|
||||
posts: [],
|
||||
roles: [],
|
||||
bio: nil
|
||||
}
|
||||
}],
|
||||
posts: [{
|
||||
title: "New Post",
|
||||
body: "Body",
|
||||
id: "42",
|
||||
links: {
|
||||
comments: ["1"],
|
||||
author: "1"
|
||||
}
|
||||
}, {
|
||||
title: "Hello!!",
|
||||
body: "Hello, world!!",
|
||||
id: "43",
|
||||
links: {
|
||||
comments: [],
|
||||
author: nil
|
||||
}
|
||||
}]
|
||||
}
|
||||
assert_equal expected, linked
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,6 +7,7 @@ module ActiveModel
|
||||
class CollectionTest < Minitest::Test
|
||||
def setup
|
||||
@author = Author.new(id: 1, name: 'Steve K.')
|
||||
@author.bio = nil
|
||||
@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 = []
|
||||
|
||||
@@ -7,6 +7,8 @@ module ActiveModel
|
||||
class HasManyEmbedIdsTest < Minitest::Test
|
||||
def setup
|
||||
@author = Author.new(name: 'Steve K.')
|
||||
@author.bio = nil
|
||||
@author.roles = nil
|
||||
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
||||
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
||||
@author.posts = [@first_post, @second_post]
|
||||
|
||||
@@ -8,6 +8,7 @@ module ActiveModel
|
||||
def setup
|
||||
@author = Author.new(id: 1, name: 'Steve K.')
|
||||
@author.posts = []
|
||||
@author.bio = nil
|
||||
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
|
||||
@post_without_comments = Post.new(id: 2, title: 'Second Post', body: 'Second')
|
||||
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@@ -31,21 +32,43 @@ module ActiveModel
|
||||
def test_includes_comment_ids
|
||||
assert_equal(["1", "2"], @adapter.serializable_hash[:posts][:links][:comments])
|
||||
end
|
||||
|
||||
|
||||
def test_includes_linked_comments
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments')
|
||||
assert_equal([
|
||||
{id: "1", body: 'ZOMG A COMMENT'},
|
||||
{id: "2", body: 'ZOMG ANOTHER COMMENT'}
|
||||
], @adapter.serializable_hash[:linked][:comments])
|
||||
expected = [{
|
||||
id: "1",
|
||||
body: 'ZOMG A COMMENT',
|
||||
links: {
|
||||
post: "1",
|
||||
author: nil
|
||||
}
|
||||
}, {
|
||||
id: "2",
|
||||
body: 'ZOMG ANOTHER COMMENT',
|
||||
links: {
|
||||
post: "1",
|
||||
author: nil
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:linked][:comments]
|
||||
end
|
||||
|
||||
def test_limit_fields_of_linked_comments
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:body]})
|
||||
assert_equal([
|
||||
{body: 'ZOMG A COMMENT'},
|
||||
{body: 'ZOMG ANOTHER COMMENT'}
|
||||
], @adapter.serializable_hash[:linked][:comments])
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:id]})
|
||||
expected = [{
|
||||
id: "1",
|
||||
links: {
|
||||
post: "1",
|
||||
author: nil
|
||||
}
|
||||
}, {
|
||||
id: "2",
|
||||
links: {
|
||||
post: "1",
|
||||
author: nil
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:linked][:comments]
|
||||
end
|
||||
|
||||
def test_no_include_linked_if_comments_is_empty
|
||||
|
||||
@@ -7,6 +7,7 @@ module ActiveModel
|
||||
class LinkedTest < Minitest::Test
|
||||
def setup
|
||||
@author = Author.new(id: 1, name: 'Steve K.')
|
||||
@bio = Bio.new(id: 1, content: 'AMS Contributor')
|
||||
@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 = []
|
||||
@@ -14,9 +15,12 @@ module ActiveModel
|
||||
@first_post.author = @author
|
||||
@second_post.author = @author
|
||||
@author.posts = [@first_post, @second_post]
|
||||
@author.bio = @bio
|
||||
@author.roles = []
|
||||
@bio.author = @author
|
||||
|
||||
@serializer = ArraySerializer.new([@first_post, @second_post])
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'author,comments')
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'author,author.bio,comments')
|
||||
end
|
||||
|
||||
def test_include_multiple_posts_and_linked
|
||||
@@ -31,7 +35,42 @@ module ActiveModel
|
||||
{ title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: ['1', '2'], author: "1" } },
|
||||
{ title: "New Post", body: "Body", id: "2", links: { comments: [], :author => "1" } }
|
||||
], @adapter.serializable_hash[:posts])
|
||||
assert_equal({ :comments => [{ :id => "1", :body => "ZOMG A COMMENT" }, { :id => "2", :body => "ZOMG ANOTHER COMMENT" }], :authors => [{ :id => "1", :name => "Steve K." }] }, @adapter.serializable_hash[:linked])
|
||||
|
||||
|
||||
expected = {
|
||||
comments: [{
|
||||
id: "1",
|
||||
body: "ZOMG A COMMENT",
|
||||
links: {
|
||||
post: "1",
|
||||
author: nil
|
||||
}
|
||||
}, {
|
||||
id: "2",
|
||||
body: "ZOMG ANOTHER COMMENT",
|
||||
links: {
|
||||
post: "1",
|
||||
author: nil
|
||||
}
|
||||
}],
|
||||
authors: [{
|
||||
id: "1",
|
||||
name: "Steve K.",
|
||||
links: {
|
||||
posts: ["1", "2"],
|
||||
roles: [],
|
||||
bio: "1"
|
||||
}
|
||||
}],
|
||||
bios: [{
|
||||
id: "1",
|
||||
content: "AMS Contributor",
|
||||
links: {
|
||||
author: "1"
|
||||
}
|
||||
}]
|
||||
}
|
||||
assert_equal expected, @adapter.serializable_hash[:linked]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -18,6 +18,26 @@ module ActiveModel
|
||||
def test_serializer
|
||||
assert_equal @serializer, @adapter.serializer
|
||||
end
|
||||
|
||||
def test_adapter_class_for_known_adapter
|
||||
klass = ActiveModel::Serializer::Adapter.adapter_class(:json_api)
|
||||
assert_equal ActiveModel::Serializer::Adapter::JsonApi, klass
|
||||
end
|
||||
|
||||
def test_adapter_class_for_unknown_adapter
|
||||
klass = ActiveModel::Serializer::Adapter.adapter_class(:json_simple)
|
||||
assert_nil klass
|
||||
end
|
||||
|
||||
def test_create_adapter
|
||||
adapter = ActiveModel::Serializer::Adapter.create(@serializer)
|
||||
assert_equal ActiveModel::Serializer::Adapter::Json, adapter.class
|
||||
end
|
||||
|
||||
def test_create_adapter_with_override
|
||||
adapter = ActiveModel::Serializer::Adapter.create(@serializer, { adapter: :json_api})
|
||||
assert_equal ActiveModel::Serializer::Adapter::JsonApi, adapter.class
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
33
test/fixtures/poro.rb
vendored
33
test/fixtures/poro.rb
vendored
@@ -35,10 +35,18 @@ class ProfileSerializer < ActiveModel::Serializer
|
||||
urls :posts, :comments
|
||||
end
|
||||
|
||||
class ProfilePreviewSerializer < ActiveModel::Serializer
|
||||
attributes :name
|
||||
|
||||
urls :posts, :comments
|
||||
end
|
||||
|
||||
Post = Class.new(Model)
|
||||
Comment = Class.new(Model)
|
||||
Author = Class.new(Model)
|
||||
Bio = Class.new(Model)
|
||||
Blog = Class.new(Model)
|
||||
Role = Class.new(Model)
|
||||
|
||||
PostSerializer = Class.new(ActiveModel::Serializer) do
|
||||
attributes :title, :body, :id
|
||||
@@ -59,6 +67,20 @@ AuthorSerializer = Class.new(ActiveModel::Serializer) do
|
||||
attributes :id, :name
|
||||
|
||||
has_many :posts, embed: :ids
|
||||
has_many :roles, embed: :ids
|
||||
belongs_to :bio
|
||||
end
|
||||
|
||||
RoleSerializer = Class.new(ActiveModel::Serializer) do
|
||||
attributes :id, :name
|
||||
|
||||
belongs_to :author
|
||||
end
|
||||
|
||||
BioSerializer = Class.new(ActiveModel::Serializer) do
|
||||
attributes :id, :content
|
||||
|
||||
belongs_to :author
|
||||
end
|
||||
|
||||
BlogSerializer = Class.new(ActiveModel::Serializer) do
|
||||
@@ -67,3 +89,14 @@ BlogSerializer = Class.new(ActiveModel::Serializer) do
|
||||
belongs_to :writer
|
||||
has_many :articles
|
||||
end
|
||||
|
||||
PaginatedSerializer = Class.new(ActiveModel::Serializer::ArraySerializer) do
|
||||
def json_key
|
||||
'paginated'
|
||||
end
|
||||
end
|
||||
|
||||
AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do
|
||||
attribute :id
|
||||
attribute :name, key: :title
|
||||
end
|
||||
|
||||
@@ -26,6 +26,8 @@ module ActiveModel
|
||||
|
||||
def setup
|
||||
@author = Author.new(name: 'Steve K.')
|
||||
@author.bio = nil
|
||||
@author.roles = []
|
||||
@post = Post.new({ title: 'New Post', body: 'Body' })
|
||||
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
||||
@post.comments = [@comment]
|
||||
@@ -39,11 +41,25 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_has_many
|
||||
assert_equal({posts: {type: :has_many, options: {embed: :ids}}}, @author_serializer.class._associations)
|
||||
assert_equal(
|
||||
{ posts: { type: :has_many, options: { embed: :ids } },
|
||||
roles: { type: :has_many, options: { embed: :ids } },
|
||||
bio: { type: :belongs_to, options: {} } },
|
||||
@author_serializer.class._associations
|
||||
)
|
||||
@author_serializer.each_association do |name, serializer, options|
|
||||
assert_equal(:posts, name)
|
||||
assert_equal({embed: :ids}, options)
|
||||
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
||||
if name == :posts
|
||||
assert_equal({embed: :ids}, options)
|
||||
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
||||
elsif name == :bio
|
||||
assert_equal({}, options)
|
||||
assert_nil serializer
|
||||
elsif name == :roles
|
||||
assert_equal({embed: :ids}, options)
|
||||
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
||||
else
|
||||
flunk "Unknown association: #{name}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
23
test/serializers/attribute_test.rb
Normal file
23
test/serializers/attribute_test.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class AttributeTest < Minitest::Test
|
||||
def setup
|
||||
@blog = Blog.new({ id: 1, name: 'AMS Hints' })
|
||||
@blog_serializer = AlternateBlogSerializer.new(@blog)
|
||||
end
|
||||
|
||||
def test_attributes_definition
|
||||
assert_equal([:id, :title],
|
||||
@blog_serializer.class._attributes)
|
||||
end
|
||||
|
||||
def test_json_serializable_hash
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer)
|
||||
assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,7 +7,7 @@ module ActiveModel
|
||||
assert_equal ActiveModel::Serializer::ArraySerializer, ActiveModel::Serializer.config.array_serializer
|
||||
end
|
||||
|
||||
def test_adapter
|
||||
def test_default_adapter
|
||||
assert_equal :json, ActiveModel::Serializer.config.adapter
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user