mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Follow up to #1454
PR #1454 was merged with some missing fixes. All these fixes are addressed by this commit: - Rename ActiveModel::Serializer::Adapter::JsonApi::Association to ActiveModel::Serializer::Adapter::JsonApi::Relationship - Move ActiveModel::Serializer::Adapter:: JsonApi::Relationship and ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier to ActiveModel::Serializer::Adapter::JsonApi::ApiObjects module - Add unit test for ActiveModel::Serializer::Adapter::JsonApi::Relationship - Add unit test for ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier
This commit is contained in:
168
test/adapter/json_api/api_objects/relationship_test.rb
Normal file
168
test/adapter/json_api/api_objects/relationship_test.rb
Normal 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::ArraySerializer.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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user