active_model_serializers/test/adapter/fragment_cache_test.rb
Benjamin Fleischer 25c9df0b97 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
2016-02-23 23:21:49 -06:00

49 lines
1.8 KiB
Ruby

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