Move serializer caching from adapter

This commit is contained in:
Benjamin Fleischer
2016-01-27 15:03:34 -05:00
parent 093d198bc4
commit eda8ff1737
19 changed files with 247 additions and 297 deletions

View File

@@ -0,0 +1,34 @@
require 'test_helper'
module ActiveModelSerializers
class FragmentCacheTest < ActiveSupport::TestCase
def setup
super
@spam = Spam::UnrelatedLink.new(id: 'spam-id-1')
@author = Author.new(name: 'Joao M. D. Moura')
@role = Role.new(name: 'Great Author', description: nil)
@role.author = [@author]
@role_serializer = RoleSerializer.new(@role)
@spam_serializer = Spam::UnrelatedLinkSerializer.new(@spam)
adapter = ActiveModelSerializers::Adapter.configured_adapter
@role_hash = FragmentCache.new(adapter.new(@role_serializer), @role_serializer, {})
@spam_hash = FragmentCache.new(adapter.new(@spam_serializer), @spam_serializer, {})
end
def test_fragment_fetch_with_virtual_attributes
expected_result = {
id: @role.id,
description: @role.description,
slug: "#{@role.name}-#{@role.id}",
name: @role.name
}
assert_equal(@role_hash.fetch, expected_result)
end
def test_fragment_fetch_with_namespaced_object
expected_result = {
id: @spam.id
}
assert_equal(@spam_hash.fetch, expected_result)
end
end
end

View File

@@ -1,48 +0,0 @@
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')
@author = Author.new(name: 'Joao M. D. Moura')
@role = Role.new(name: 'Great Author', description: nil)
@role.author = [@author]
@role_serializer = RoleSerializer.new(@role)
@spam_serializer = Spam::UnrelatedLinkSerializer.new(@spam)
@role_hash = FragmentCache.new(::ActiveModelSerializers::Adapter.configured_adapter.new(@role_serializer), @role_serializer, {})
@spam_hash = FragmentCache.new(::ActiveModelSerializers::Adapter.configured_adapter.new(@spam_serializer), @spam_serializer, {})
end
def test_fragment_fetch_with_virtual_attributes
expected_result = {
id: @role.id,
description: @role.description,
slug: "#{@role.name}-#{@role.id}",
name: @role.name
}
assert_equal(@role_hash.fetch, expected_result)
end
def test_fragment_fetch_with_namespaced_object
expected_result = {
id: @spam.id
}
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

View File

@@ -89,7 +89,7 @@ module ActiveModelSerializers
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
Timecop.freeze(Time.current) do
render_object_with_cache(@post)
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
@@ -101,7 +101,7 @@ module ActiveModelSerializers
# Clean the Cache
ActionController::Base.cache_store.clear
Timecop.freeze(Time.now) do
Timecop.freeze(Time.current) do
# Generate a new Cache of Post object and each objects related to it.
render_object_with_cache(@post)
@@ -150,7 +150,7 @@ module ActiveModelSerializers
serializer = ActiveModel::Serializer::CollectionSerializer.new([@comment, @comment])
include_tree = ActiveModel::Serializer::IncludeTree.from_include_args('*')
actual = Adapter::CachedSerializer.object_cache_keys(serializer, include_tree)
actual = CachedSerializer.object_cache_keys(serializer, include_tree)
assert_equal actual.size, 3
assert actual.any? { |key| key == 'comment/1' }
@@ -161,7 +161,7 @@ module ActiveModelSerializers
def test_cached_attributes
serializer = ActiveModel::Serializer::CollectionSerializer.new([@comment, @comment])
Timecop.freeze(Time.now) do
Timecop.freeze(Time.current) do
render_object_with_cache(@comment)
attributes = Adapter::Attributes.new(serializer)