mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
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:
@@ -45,6 +45,16 @@ module ActionController
|
||||
render json: @profiles, meta: { total: 10 }
|
||||
end
|
||||
|
||||
def render_array_using_implicit_serializer_and_links
|
||||
with_adapter ActiveModelSerializers::Adapter::JsonApi do
|
||||
@profiles = [
|
||||
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||
]
|
||||
|
||||
render json: @profiles, links: { self: 'http://example.com/api/profiles/1' }
|
||||
end
|
||||
end
|
||||
|
||||
def render_object_with_cache_enabled
|
||||
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@author = Author.new(id: 1, name: 'Joao Moura.')
|
||||
@@ -254,6 +264,29 @@ module ActionController
|
||||
assert_equal expected.to_json, @response.body
|
||||
end
|
||||
|
||||
def test_render_array_using_implicit_serializer_and_links
|
||||
get :render_array_using_implicit_serializer_and_links
|
||||
|
||||
expected = {
|
||||
data: [
|
||||
{
|
||||
id: assigns(:profiles).first.id.to_s,
|
||||
type: 'profiles',
|
||||
attributes: {
|
||||
name: 'Name 1',
|
||||
description: 'Description 1'
|
||||
}
|
||||
}
|
||||
],
|
||||
links: {
|
||||
self: 'http://example.com/api/profiles/1'
|
||||
}
|
||||
}
|
||||
|
||||
assert_equal 'application/json', @response.content_type
|
||||
assert_equal expected.to_json, @response.body
|
||||
end
|
||||
|
||||
def test_render_with_cache_enable
|
||||
expected = {
|
||||
id: 1,
|
||||
|
||||
@@ -2,6 +2,14 @@ require 'test_helper'
|
||||
module ActiveModelSerializers
|
||||
module Adapter
|
||||
class FragmentCacheTest < ActiveSupport::TestCase
|
||||
TypedRoleSerializer = Class.new(ActiveModel::Serializer) do
|
||||
type 'my-roles'
|
||||
cache only: [:name], skip_digest: true
|
||||
attributes :id, :name, :description
|
||||
|
||||
belongs_to :author
|
||||
end
|
||||
|
||||
def setup
|
||||
super
|
||||
@spam = Spam::UnrelatedLink.new(id: 'spam-id-1')
|
||||
@@ -30,7 +38,11 @@ module ActiveModelSerializers
|
||||
}
|
||||
assert_equal(@spam_hash.fetch, expected_result)
|
||||
end
|
||||
|
||||
def test_fragment_fetch_with_type_override
|
||||
serialization = serializable(Role.new(name: 'Another Author'), serializer: TypedRoleSerializer, adapter: :json_api).serializable_hash
|
||||
assert_equal(TypedRoleSerializer._type, serialization.fetch(:data).fetch(:type))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
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::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
|
||||
@@ -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
|
||||
@@ -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 = {
|
||||
|
||||
192
test/adapter/json_api/relationship_test.rb
Normal file
192
test/adapter/json_api/relationship_test.rb
Normal 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
|
||||
68
test/adapter/json_api/resource_meta_test.rb
Normal file
68
test/adapter/json_api/resource_meta_test.rb
Normal 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
|
||||
@@ -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
|
||||
61
test/adapter/json_api/type_test.rb
Normal file
61
test/adapter/json_api/type_test.rb
Normal 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
|
||||
@@ -23,5 +23,13 @@ module ActiveModel
|
||||
options = nil
|
||||
assert_equal @adapter.as_json(options), @serializable_resource.as_json(options)
|
||||
end
|
||||
|
||||
def test_use_adapter_with_adapter_option
|
||||
assert ActiveModel::SerializableResource.new(@resource, { adapter: 'json' }).use_adapter?
|
||||
end
|
||||
|
||||
def test_use_adapter_with_adapter_option_as_false
|
||||
refute ActiveModel::SerializableResource.new(@resource, { adapter: false }).use_adapter?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -32,13 +32,13 @@ module ActiveModel
|
||||
|
||||
case key
|
||||
when :posts
|
||||
assert_equal({}, options)
|
||||
assert_equal({ include_data: true }, options)
|
||||
assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
|
||||
when :bio
|
||||
assert_equal({}, options)
|
||||
assert_equal({ include_data: true }, options)
|
||||
assert_nil serializer
|
||||
when :roles
|
||||
assert_equal({}, options)
|
||||
assert_equal({ include_data: true }, options)
|
||||
assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
|
||||
else
|
||||
flunk "Unknown association: #{key}"
|
||||
@@ -80,7 +80,7 @@ module ActiveModel
|
||||
flunk "Unknown association: #{key}"
|
||||
end
|
||||
|
||||
assert_equal({}, association.options)
|
||||
assert_equal({ include_data: true }, association.options)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,209 +1,238 @@
|
||||
require 'test_helper'
|
||||
require 'tmpdir'
|
||||
require 'tempfile'
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class CacheTest < ActiveSupport::TestCase
|
||||
include ActiveSupport::Testing::Stream
|
||||
module ActiveModelSerializers
|
||||
class CacheTest < ActiveSupport::TestCase
|
||||
include ActiveSupport::Testing::Stream
|
||||
|
||||
def setup
|
||||
ActionController::Base.cache_store.clear
|
||||
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@post = Post.new(title: 'New Post', body: 'Body')
|
||||
@bio = Bio.new(id: 1, content: 'AMS Contributor')
|
||||
@author = Author.new(name: 'Joao M. D. Moura')
|
||||
@blog = Blog.new(id: 999, name: 'Custom blog', writer: @author, articles: [])
|
||||
@role = Role.new(name: 'Great Author')
|
||||
@location = Location.new(lat: '-23.550520', lng: '-46.633309')
|
||||
@place = Place.new(name: 'Amazing Place')
|
||||
@author.posts = [@post]
|
||||
@author.roles = [@role]
|
||||
@role.author = @author
|
||||
@author.bio = @bio
|
||||
@bio.author = @author
|
||||
@post.comments = [@comment]
|
||||
@post.author = @author
|
||||
@comment.post = @post
|
||||
@comment.author = @author
|
||||
@post.blog = @blog
|
||||
@location.place = @place
|
||||
def setup
|
||||
ActionController::Base.cache_store.clear
|
||||
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@post = Post.new(title: 'New Post', body: 'Body')
|
||||
@bio = Bio.new(id: 1, content: 'AMS Contributor')
|
||||
@author = Author.new(name: 'Joao M. D. Moura')
|
||||
@blog = Blog.new(id: 999, name: 'Custom blog', writer: @author, articles: [])
|
||||
@role = Role.new(name: 'Great Author')
|
||||
@location = Location.new(lat: '-23.550520', lng: '-46.633309')
|
||||
@place = Place.new(name: 'Amazing Place')
|
||||
@author.posts = [@post]
|
||||
@author.roles = [@role]
|
||||
@role.author = @author
|
||||
@author.bio = @bio
|
||||
@bio.author = @author
|
||||
@post.comments = [@comment]
|
||||
@post.author = @author
|
||||
@comment.post = @post
|
||||
@comment.author = @author
|
||||
@post.blog = @blog
|
||||
@location.place = @place
|
||||
|
||||
@location_serializer = LocationSerializer.new(@location)
|
||||
@bio_serializer = BioSerializer.new(@bio)
|
||||
@role_serializer = RoleSerializer.new(@role)
|
||||
@post_serializer = PostSerializer.new(@post)
|
||||
@author_serializer = AuthorSerializer.new(@author)
|
||||
@comment_serializer = CommentSerializer.new(@comment)
|
||||
@blog_serializer = BlogSerializer.new(@blog)
|
||||
@location_serializer = LocationSerializer.new(@location)
|
||||
@bio_serializer = BioSerializer.new(@bio)
|
||||
@role_serializer = RoleSerializer.new(@role)
|
||||
@post_serializer = PostSerializer.new(@post)
|
||||
@author_serializer = AuthorSerializer.new(@author)
|
||||
@comment_serializer = CommentSerializer.new(@comment)
|
||||
@blog_serializer = BlogSerializer.new(@blog)
|
||||
end
|
||||
|
||||
def test_inherited_cache_configuration
|
||||
inherited_serializer = Class.new(PostSerializer)
|
||||
|
||||
assert_equal PostSerializer._cache_key, inherited_serializer._cache_key
|
||||
assert_equal PostSerializer._cache_options, inherited_serializer._cache_options
|
||||
end
|
||||
|
||||
def test_override_cache_configuration
|
||||
inherited_serializer = Class.new(PostSerializer) do
|
||||
cache key: 'new-key'
|
||||
end
|
||||
|
||||
def test_inherited_cache_configuration
|
||||
inherited_serializer = Class.new(PostSerializer)
|
||||
assert_equal PostSerializer._cache_key, 'post'
|
||||
assert_equal inherited_serializer._cache_key, 'new-key'
|
||||
end
|
||||
|
||||
assert_equal PostSerializer._cache_key, inherited_serializer._cache_key
|
||||
assert_equal PostSerializer._cache_options, inherited_serializer._cache_options
|
||||
def test_cache_definition
|
||||
assert_equal(ActionController::Base.cache_store, @post_serializer.class._cache)
|
||||
assert_equal(ActionController::Base.cache_store, @author_serializer.class._cache)
|
||||
assert_equal(ActionController::Base.cache_store, @comment_serializer.class._cache)
|
||||
end
|
||||
|
||||
def test_cache_key_definition
|
||||
assert_equal('post', @post_serializer.class._cache_key)
|
||||
assert_equal('writer', @author_serializer.class._cache_key)
|
||||
assert_equal(nil, @comment_serializer.class._cache_key)
|
||||
end
|
||||
|
||||
def test_cache_key_interpolation_with_updated_at
|
||||
render_object_with_cache(@author)
|
||||
assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
|
||||
assert_equal(@author_serializer.attributes.to_json, ActionController::Base.cache_store.fetch("#{@author_serializer.class._cache_key}/#{@author_serializer.object.id}-#{@author_serializer.object.updated_at.strftime("%Y%m%d%H%M%S%9N")}").to_json)
|
||||
end
|
||||
|
||||
def test_default_cache_key_fallback
|
||||
render_object_with_cache(@comment)
|
||||
assert_equal(@comment_serializer.attributes.to_json, ActionController::Base.cache_store.fetch(@comment.cache_key).to_json)
|
||||
end
|
||||
|
||||
def test_cache_options_definition
|
||||
assert_equal({ expires_in: 0.1, skip_digest: true }, @post_serializer.class._cache_options)
|
||||
assert_equal(nil, @blog_serializer.class._cache_options)
|
||||
assert_equal({ expires_in: 1.day, skip_digest: true }, @comment_serializer.class._cache_options)
|
||||
end
|
||||
|
||||
def test_fragment_cache_definition
|
||||
assert_equal([:name], @role_serializer.class._cache_only)
|
||||
assert_equal([:content], @bio_serializer.class._cache_except)
|
||||
end
|
||||
|
||||
def test_associations_separately_cache
|
||||
ActionController::Base.cache_store.clear
|
||||
assert_equal(nil, ActionController::Base.cache_store.fetch(@post.cache_key))
|
||||
assert_equal(nil, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
||||
|
||||
Timecop.freeze(Time.now) do
|
||||
render_object_with_cache(@post)
|
||||
|
||||
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
||||
assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
||||
end
|
||||
end
|
||||
|
||||
def test_override_cache_configuration
|
||||
inherited_serializer = Class.new(PostSerializer) do
|
||||
cache key: 'new-key'
|
||||
end
|
||||
def test_associations_cache_when_updated
|
||||
# Clean the Cache
|
||||
ActionController::Base.cache_store.clear
|
||||
|
||||
assert_equal PostSerializer._cache_key, 'post'
|
||||
assert_equal inherited_serializer._cache_key, 'new-key'
|
||||
Timecop.freeze(Time.now) do
|
||||
# Generate a new Cache of Post object and each objects related to it.
|
||||
render_object_with_cache(@post)
|
||||
|
||||
# Check if it cached the objects separately
|
||||
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
||||
assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
||||
|
||||
# Simulating update on comments relationship with Post
|
||||
new_comment = Comment.new(id: 2, body: 'ZOMG A NEW COMMENT')
|
||||
new_comment_serializer = CommentSerializer.new(new_comment)
|
||||
@post.comments = [new_comment]
|
||||
|
||||
# Ask for the serialized object
|
||||
render_object_with_cache(@post)
|
||||
|
||||
# Check if the the new comment was cached
|
||||
assert_equal(new_comment_serializer.attributes, ActionController::Base.cache_store.fetch(new_comment.cache_key))
|
||||
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
||||
end
|
||||
end
|
||||
|
||||
def test_cache_definition
|
||||
assert_equal(ActionController::Base.cache_store, @post_serializer.class._cache)
|
||||
assert_equal(ActionController::Base.cache_store, @author_serializer.class._cache)
|
||||
assert_equal(ActionController::Base.cache_store, @comment_serializer.class._cache)
|
||||
end
|
||||
def test_fragment_fetch_with_virtual_associations
|
||||
expected_result = {
|
||||
id: @location.id,
|
||||
lat: @location.lat,
|
||||
lng: @location.lng,
|
||||
place: 'Nowhere'
|
||||
}
|
||||
|
||||
def test_cache_key_definition
|
||||
assert_equal('post', @post_serializer.class._cache_key)
|
||||
assert_equal('writer', @author_serializer.class._cache_key)
|
||||
assert_equal(nil, @comment_serializer.class._cache_key)
|
||||
end
|
||||
hash = render_object_with_cache(@location)
|
||||
|
||||
def test_cache_key_interpolation_with_updated_at
|
||||
render_object_with_cache(@author)
|
||||
assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
|
||||
assert_equal(@author_serializer.attributes.to_json, ActionController::Base.cache_store.fetch("#{@author_serializer.class._cache_key}/#{@author_serializer.object.id}-#{@author_serializer.object.updated_at.strftime("%Y%m%d%H%M%S%9N")}").to_json)
|
||||
end
|
||||
assert_equal(hash, expected_result)
|
||||
assert_equal({ place: 'Nowhere' }, ActionController::Base.cache_store.fetch(@location.cache_key))
|
||||
end
|
||||
|
||||
def test_default_cache_key_fallback
|
||||
def test_uses_file_digest_in_cache_key
|
||||
render_object_with_cache(@blog)
|
||||
assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
|
||||
end
|
||||
|
||||
def test_cache_digest_definition
|
||||
assert_equal(::Model::FILE_DIGEST, @post_serializer.class._cache_digest)
|
||||
end
|
||||
|
||||
def test_object_cache_keys
|
||||
serializer = ActiveModel::Serializer::CollectionSerializer.new([@comment, @comment])
|
||||
include_tree = ActiveModel::Serializer::IncludeTree.from_include_args('*')
|
||||
|
||||
actual = Adapter::CachedSerializer.object_cache_keys(serializer, include_tree)
|
||||
|
||||
assert_equal actual.size, 3
|
||||
assert actual.any? { |key| key == 'comment/1' }
|
||||
assert actual.any? { |key| key =~ %r{post/post-\d+} }
|
||||
assert actual.any? { |key| key =~ %r{writer/author-\d+} }
|
||||
end
|
||||
|
||||
def test_cached_attributes
|
||||
serializer = ActiveModel::Serializer::CollectionSerializer.new([@comment, @comment])
|
||||
|
||||
Timecop.freeze(Time.now) do
|
||||
render_object_with_cache(@comment)
|
||||
assert_equal(@comment_serializer.attributes.to_json, ActionController::Base.cache_store.fetch(@comment.cache_key).to_json)
|
||||
|
||||
attributes = Adapter::Attributes.new(serializer)
|
||||
attributes.send(:cache_attributes)
|
||||
cached_attributes = attributes.instance_variable_get(:@cached_attributes)
|
||||
|
||||
assert_equal cached_attributes[@comment.cache_key], Comment.new(id: 1, body: 'ZOMG A COMMENT').attributes
|
||||
assert_equal cached_attributes[@comment.post.cache_key], Post.new(id: 'post', title: 'New Post', body: 'Body').attributes
|
||||
|
||||
writer = @comment.post.blog.writer
|
||||
writer_cache_key = "writer/#{writer.id}-#{writer.updated_at.strftime("%Y%m%d%H%M%S%9N")}"
|
||||
|
||||
assert_equal cached_attributes[writer_cache_key], Author.new(id: 'author', name: 'Joao M. D. Moura').attributes
|
||||
end
|
||||
end
|
||||
|
||||
def test_cache_options_definition
|
||||
assert_equal({ expires_in: 0.1, skip_digest: true }, @post_serializer.class._cache_options)
|
||||
assert_equal(nil, @blog_serializer.class._cache_options)
|
||||
assert_equal({ expires_in: 1.day, skip_digest: true }, @comment_serializer.class._cache_options)
|
||||
end
|
||||
def test_serializer_file_path_on_nix
|
||||
path = '/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
|
||||
caller_line = "#{path}:1:in `<top (required)>'"
|
||||
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
|
||||
end
|
||||
|
||||
def test_fragment_cache_definition
|
||||
assert_equal([:name], @role_serializer.class._cache_only)
|
||||
assert_equal([:content], @bio_serializer.class._cache_except)
|
||||
end
|
||||
def test_serializer_file_path_on_windows
|
||||
path = 'c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
|
||||
caller_line = "#{path}:1:in `<top (required)>'"
|
||||
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
|
||||
end
|
||||
|
||||
def test_associations_separately_cache
|
||||
ActionController::Base.cache_store.clear
|
||||
assert_equal(nil, ActionController::Base.cache_store.fetch(@post.cache_key))
|
||||
assert_equal(nil, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
||||
def test_serializer_file_path_with_space
|
||||
path = '/Users/git/ember js/ember-crm-backend/app/serializers/lead_serializer.rb'
|
||||
caller_line = "#{path}:1:in `<top (required)>'"
|
||||
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
|
||||
end
|
||||
|
||||
Timecop.freeze(Time.now) do
|
||||
render_object_with_cache(@post)
|
||||
def test_serializer_file_path_with_submatch
|
||||
# The submatch in the path ensures we're using a correctly greedy regexp.
|
||||
path = '/Users/git/ember js/ember:123:in x/app/serializers/lead_serializer.rb'
|
||||
caller_line = "#{path}:1:in `<top (required)>'"
|
||||
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
|
||||
end
|
||||
|
||||
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
||||
assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
||||
end
|
||||
end
|
||||
def test_digest_caller_file
|
||||
contents = "puts 'AMS rocks'!"
|
||||
dir = Dir.mktmpdir('space char')
|
||||
file = Tempfile.new('some_ruby.rb', dir)
|
||||
file.write(contents)
|
||||
path = file.path
|
||||
caller_line = "#{path}:1:in `<top (required)>'"
|
||||
file.close
|
||||
assert_equal ActiveModel::Serializer.digest_caller_file(caller_line), Digest::MD5.hexdigest(contents)
|
||||
ensure
|
||||
file.unlink
|
||||
FileUtils.remove_entry dir
|
||||
end
|
||||
|
||||
def test_associations_cache_when_updated
|
||||
# Clean the Cache
|
||||
ActionController::Base.cache_store.clear
|
||||
def test_warn_on_serializer_not_defined_in_file
|
||||
called = false
|
||||
serializer = Class.new(ActiveModel::Serializer)
|
||||
assert_match(/_cache_digest/, (capture(:stderr) do
|
||||
serializer.digest_caller_file('')
|
||||
called = true
|
||||
end))
|
||||
assert called
|
||||
end
|
||||
|
||||
Timecop.freeze(Time.now) do
|
||||
# Generate a new Cache of Post object and each objects related to it.
|
||||
render_object_with_cache(@post)
|
||||
private
|
||||
|
||||
# Check if it cached the objects separately
|
||||
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
||||
assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
||||
|
||||
# Simulating update on comments relationship with Post
|
||||
new_comment = Comment.new(id: 2, body: 'ZOMG A NEW COMMENT')
|
||||
new_comment_serializer = CommentSerializer.new(new_comment)
|
||||
@post.comments = [new_comment]
|
||||
|
||||
# Ask for the serialized object
|
||||
render_object_with_cache(@post)
|
||||
|
||||
# Check if the the new comment was cached
|
||||
assert_equal(new_comment_serializer.attributes, ActionController::Base.cache_store.fetch(new_comment.cache_key))
|
||||
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
||||
end
|
||||
end
|
||||
|
||||
def test_fragment_fetch_with_virtual_associations
|
||||
expected_result = {
|
||||
id: @location.id,
|
||||
lat: @location.lat,
|
||||
lng: @location.lng,
|
||||
place: 'Nowhere'
|
||||
}
|
||||
|
||||
hash = render_object_with_cache(@location)
|
||||
|
||||
assert_equal(hash, expected_result)
|
||||
assert_equal({ place: 'Nowhere' }, ActionController::Base.cache_store.fetch(@location.cache_key))
|
||||
end
|
||||
|
||||
def test_uses_file_digest_in_cache_key
|
||||
render_object_with_cache(@blog)
|
||||
assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
|
||||
end
|
||||
|
||||
def test_cache_digest_definition
|
||||
assert_equal(::Model::FILE_DIGEST, @post_serializer.class._cache_digest)
|
||||
end
|
||||
|
||||
def test_serializer_file_path_on_nix
|
||||
path = '/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
|
||||
caller_line = "#{path}:1:in `<top (required)>'"
|
||||
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
|
||||
end
|
||||
|
||||
def test_serializer_file_path_on_windows
|
||||
path = 'c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
|
||||
caller_line = "#{path}:1:in `<top (required)>'"
|
||||
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
|
||||
end
|
||||
|
||||
def test_serializer_file_path_with_space
|
||||
path = '/Users/git/ember js/ember-crm-backend/app/serializers/lead_serializer.rb'
|
||||
caller_line = "#{path}:1:in `<top (required)>'"
|
||||
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
|
||||
end
|
||||
|
||||
def test_serializer_file_path_with_submatch
|
||||
# The submatch in the path ensures we're using a correctly greedy regexp.
|
||||
path = '/Users/git/ember js/ember:123:in x/app/serializers/lead_serializer.rb'
|
||||
caller_line = "#{path}:1:in `<top (required)>'"
|
||||
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
|
||||
end
|
||||
|
||||
def test_digest_caller_file
|
||||
contents = "puts 'AMS rocks'!"
|
||||
dir = Dir.mktmpdir('space char')
|
||||
file = Tempfile.new('some_ruby.rb', dir)
|
||||
file.write(contents)
|
||||
path = file.path
|
||||
caller_line = "#{path}:1:in `<top (required)>'"
|
||||
file.close
|
||||
assert_equal ActiveModel::Serializer.digest_caller_file(caller_line), Digest::MD5.hexdigest(contents)
|
||||
ensure
|
||||
file.unlink
|
||||
FileUtils.remove_entry dir
|
||||
end
|
||||
|
||||
def test_warn_on_serializer_not_defined_in_file
|
||||
called = false
|
||||
serializer = Class.new(ActiveModel::Serializer)
|
||||
assert_match(/_cache_digest/, (capture(:stderr) do
|
||||
serializer.digest_caller_file('')
|
||||
called = true
|
||||
end))
|
||||
assert called
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def render_object_with_cache(obj)
|
||||
ActiveModel::SerializableResource.new(obj).serializable_hash
|
||||
end
|
||||
def render_object_with_cache(obj)
|
||||
ActiveModel::SerializableResource.new(obj).serializable_hash
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
80
test/serializers/cached_serializer_test.rb
Normal file
80
test/serializers/cached_serializer_test.rb
Normal file
@@ -0,0 +1,80 @@
|
||||
require 'test_helper'
|
||||
module ActiveModelSerializers
|
||||
module Adapter
|
||||
class CachedSerializerTest < ActiveSupport::TestCase
|
||||
def test_cached_false_without_cache_store
|
||||
cached_serializer = build do |serializer|
|
||||
serializer._cache = nil
|
||||
end
|
||||
refute cached_serializer.cached?
|
||||
end
|
||||
|
||||
def test_cached_true_with_cache_store_and_without_cache_only_and_cache_except
|
||||
cached_serializer = build do |serializer|
|
||||
serializer._cache = Object
|
||||
end
|
||||
assert cached_serializer.cached?
|
||||
end
|
||||
|
||||
def test_cached_false_with_cache_store_and_with_cache_only
|
||||
cached_serializer = build do |serializer|
|
||||
serializer._cache = Object
|
||||
serializer._cache_only = [:name]
|
||||
end
|
||||
refute cached_serializer.cached?
|
||||
end
|
||||
|
||||
def test_cached_false_with_cache_store_and_with_cache_except
|
||||
cached_serializer = build do |serializer|
|
||||
serializer._cache = Object
|
||||
serializer._cache_except = [:content]
|
||||
end
|
||||
refute cached_serializer.cached?
|
||||
end
|
||||
|
||||
def test_fragment_cached_false_without_cache_store
|
||||
cached_serializer = build do |serializer|
|
||||
serializer._cache = nil
|
||||
serializer._cache_only = [:name]
|
||||
end
|
||||
refute cached_serializer.fragment_cached?
|
||||
end
|
||||
|
||||
def test_fragment_cached_true_with_cache_store_and_cache_only
|
||||
cached_serializer = build do |serializer|
|
||||
serializer._cache = Object
|
||||
serializer._cache_only = [:name]
|
||||
end
|
||||
assert cached_serializer.fragment_cached?
|
||||
end
|
||||
|
||||
def test_fragment_cached_true_with_cache_store_and_cache_except
|
||||
cached_serializer = build do |serializer|
|
||||
serializer._cache = Object
|
||||
serializer._cache_except = [:content]
|
||||
end
|
||||
assert cached_serializer.fragment_cached?
|
||||
end
|
||||
|
||||
def test_fragment_cached_false_with_cache_store_and_cache_except_and_cache_only
|
||||
cached_serializer = build do |serializer|
|
||||
serializer._cache = Object
|
||||
serializer._cache_except = [:content]
|
||||
serializer._cache_only = [:name]
|
||||
end
|
||||
refute cached_serializer.fragment_cached?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build
|
||||
serializer = Class.new(ActiveModel::Serializer)
|
||||
serializer._cache_key = nil
|
||||
serializer._cache_options = nil
|
||||
yield serializer if block_given?
|
||||
serializer_instance = serializer.new(Object)
|
||||
CachedSerializer.new(serializer_instance)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,6 +0,0 @@
|
||||
# https://github.com/colszowka/simplecov/pull/400
|
||||
# https://github.com/ruby/ruby/blob/trunk/lib/English.rb
|
||||
unless defined?(English)
|
||||
# The exception object passed to +raise+.
|
||||
alias $ERROR_INFO $! # rubocop:disable Style/SpecialGlobalVars
|
||||
end
|
||||
Reference in New Issue
Block a user