mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #1093 from beauby/improve-tests
Factor `with_adapter` + force cache clear before each test.
This commit is contained in:
commit
f149e5084b
@ -6,60 +6,48 @@ module ActionController
|
|||||||
include ActiveSupport::Testing::Stream
|
include ActiveSupport::Testing::Stream
|
||||||
class ImplicitSerializationTestController < ActionController::Base
|
class ImplicitSerializationTestController < ActionController::Base
|
||||||
def render_using_implicit_serializer
|
def render_using_implicit_serializer
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||||
render json: @profile
|
render json: @profile
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_using_default_adapter_root
|
def render_using_default_adapter_root
|
||||||
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
|
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||||
# JSON-API adapter sets root by default
|
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
|
||||||
render json: @profile
|
render json: @profile
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def render_array_using_custom_root
|
def render_array_using_custom_root
|
||||||
with_adapter ActiveModel::Serializer::Adapter::Json do
|
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
|
||||||
render json: [@profile], root: 'custom_root'
|
render json: [@profile], root: 'custom_root'
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def render_array_that_is_empty_using_custom_root
|
def render_array_that_is_empty_using_custom_root
|
||||||
with_adapter ActiveModel::Serializer::Adapter::Json do
|
|
||||||
render json: [], root: 'custom_root'
|
render json: [], root: 'custom_root'
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def render_object_using_custom_root
|
def render_object_using_custom_root
|
||||||
with_adapter ActiveModel::Serializer::Adapter::Json do
|
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
|
||||||
render json: @profile, root: 'custom_root'
|
render json: @profile, root: 'custom_root'
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def render_array_using_implicit_serializer
|
def render_array_using_implicit_serializer
|
||||||
array = [
|
array = [
|
||||||
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
||||||
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
Profile.new(name: 'Name 2', description: 'Description 2', comments: 'Comments 2')
|
||||||
]
|
]
|
||||||
render json: array
|
render json: array
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_array_using_implicit_serializer_and_meta
|
def render_array_using_implicit_serializer_and_meta
|
||||||
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
|
|
||||||
@profiles = [
|
@profiles = [
|
||||||
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||||
]
|
]
|
||||||
|
|
||||||
render json: @profiles, meta: { total: 10 }
|
render json: @profiles, meta: { total: 10 }
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def render_object_with_cache_enabled
|
def render_object_with_cache_enabled
|
||||||
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||||
@author = Author.new(id: 1, name: 'Joao Moura.')
|
@author = Author.new(id: 1, name: 'Joao Moura.')
|
||||||
@post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [@comment], author: @author })
|
@post = Post.new(id: 1, title: 'New Post', body: 'Body', comments: [@comment], author: @author)
|
||||||
|
|
||||||
generate_cached_serializer(@post)
|
generate_cached_serializer(@post)
|
||||||
|
|
||||||
@ -83,9 +71,9 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_object_expired_with_cache_enabled
|
def render_object_expired_with_cache_enabled
|
||||||
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||||
author = Author.new(id: 1, name: 'Joao Moura.')
|
author = Author.new(id: 1, name: 'Joao Moura.')
|
||||||
post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author })
|
post = Post.new(id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author)
|
||||||
|
|
||||||
generate_cached_serializer(post)
|
generate_cached_serializer(post)
|
||||||
|
|
||||||
@ -95,16 +83,16 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_changed_object_with_cache_enabled
|
def render_changed_object_with_cache_enabled
|
||||||
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||||
author = Author.new(id: 1, name: 'Joao Moura.')
|
author = Author.new(id: 1, name: 'Joao Moura.')
|
||||||
post = Post.new({ id: 1, title: 'ZOMG a New Post', body: 'Body', comments: [comment], author: author })
|
post = Post.new(id: 1, title: 'ZOMG a New Post', body: 'Body', comments: [comment], author: author)
|
||||||
|
|
||||||
render json: post
|
render json: post
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_fragment_changed_object_with_only_cache_enabled
|
def render_fragment_changed_object_with_only_cache_enabled
|
||||||
author = Author.new(id: 1, name: 'Joao Moura.')
|
author = Author.new(id: 1, name: 'Joao Moura.')
|
||||||
role = Role.new({ id: 42, name: 'ZOMG A ROLE', description: 'DESCRIPTION HERE', author: author })
|
role = Role.new(id: 42, name: 'ZOMG A ROLE', description: 'DESCRIPTION HERE', author: author)
|
||||||
|
|
||||||
generate_cached_serializer(role)
|
generate_cached_serializer(role)
|
||||||
role.name = 'lol'
|
role.name = 'lol'
|
||||||
@ -115,7 +103,7 @@ module ActionController
|
|||||||
|
|
||||||
def render_fragment_changed_object_with_except_cache_enabled
|
def render_fragment_changed_object_with_except_cache_enabled
|
||||||
author = Author.new(id: 1, name: 'Joao Moura.')
|
author = Author.new(id: 1, name: 'Joao Moura.')
|
||||||
bio = Bio.new({ id: 42, content: 'ZOMG A ROLE', rating: 5, author: author })
|
bio = Bio.new(id: 42, content: 'ZOMG A ROLE', rating: 5, author: author)
|
||||||
|
|
||||||
generate_cached_serializer(bio)
|
generate_cached_serializer(bio)
|
||||||
bio.content = 'lol'
|
bio.content = 'lol'
|
||||||
@ -125,13 +113,13 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_fragment_changed_object_with_relationship
|
def render_fragment_changed_object_with_relationship
|
||||||
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||||
comment2 = Comment.new({ id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT' })
|
comment2 = Comment.new(id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT')
|
||||||
like = Like.new({ id: 1, likeable: comment, time: 3.days.ago })
|
like = Like.new(id: 1, likeable: comment, time: 3.days.ago)
|
||||||
|
|
||||||
generate_cached_serializer(like)
|
generate_cached_serializer(like)
|
||||||
like.likable = comment2
|
like.likable = comment2
|
||||||
like.time = Time.now.to_s
|
like.time = Time.zone.now.to_s
|
||||||
|
|
||||||
render json: like
|
render json: like
|
||||||
end
|
end
|
||||||
@ -168,8 +156,9 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_render_using_default_root
|
def test_render_using_default_root
|
||||||
|
with_adapter :json_api do
|
||||||
get :render_using_default_adapter_root
|
get :render_using_default_adapter_root
|
||||||
|
end
|
||||||
expected = {
|
expected = {
|
||||||
data: {
|
data: {
|
||||||
id: assigns(:profile).id.to_s,
|
id: assigns(:profile).id.to_s,
|
||||||
@ -186,15 +175,18 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_render_array_using_custom_root
|
def test_render_array_using_custom_root
|
||||||
|
with_adapter :json do
|
||||||
get :render_array_using_custom_root
|
get :render_array_using_custom_root
|
||||||
|
end
|
||||||
expected = { custom_roots: [{ name: 'Name 1', description: 'Description 1' }] }
|
expected = { custom_roots: [{ name: 'Name 1', description: 'Description 1' }] }
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal expected.to_json, @response.body
|
assert_equal expected.to_json, @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_array_that_is_empty_using_custom_root
|
def test_render_array_that_is_empty_using_custom_root
|
||||||
|
with_adapter :json do
|
||||||
get :render_array_that_is_empty_using_custom_root
|
get :render_array_that_is_empty_using_custom_root
|
||||||
|
end
|
||||||
|
|
||||||
expected = { custom_roots: [] }
|
expected = { custom_roots: [] }
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
@ -202,7 +194,9 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_render_object_using_custom_root
|
def test_render_object_using_custom_root
|
||||||
|
with_adapter :json do
|
||||||
get :render_object_using_custom_root
|
get :render_object_using_custom_root
|
||||||
|
end
|
||||||
|
|
||||||
expected = { custom_root: { name: 'Name 1', description: 'Description 1' } }
|
expected = { custom_root: { name: 'Name 1', description: 'Description 1' } }
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
@ -232,11 +226,11 @@ module ActionController
|
|||||||
expected = [
|
expected = [
|
||||||
{
|
{
|
||||||
name: 'Name 1',
|
name: 'Name 1',
|
||||||
description: 'Description 1',
|
description: 'Description 1'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Name 2',
|
name: 'Name 2',
|
||||||
description: 'Description 2',
|
description: 'Description 2'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -244,8 +238,9 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_render_array_using_implicit_serializer_and_meta
|
def test_render_array_using_implicit_serializer_and_meta
|
||||||
|
with_adapter :json_api do
|
||||||
get :render_array_using_implicit_serializer_and_meta
|
get :render_array_using_implicit_serializer_and_meta
|
||||||
|
end
|
||||||
expected = {
|
expected = {
|
||||||
data: [
|
data: [
|
||||||
{
|
{
|
||||||
@ -287,7 +282,7 @@ module ActionController
|
|||||||
}
|
}
|
||||||
|
|
||||||
ActionController::Base.cache_store.clear
|
ActionController::Base.cache_store.clear
|
||||||
Timecop.freeze(Time.now) do
|
Timecop.freeze(Time.zone.now) do
|
||||||
get :render_object_with_cache_enabled
|
get :render_object_with_cache_enabled
|
||||||
|
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
@ -352,13 +347,13 @@ module ActionController
|
|||||||
def test_render_fragment_changed_object_with_relationship
|
def test_render_fragment_changed_object_with_relationship
|
||||||
ActionController::Base.cache_store.clear
|
ActionController::Base.cache_store.clear
|
||||||
|
|
||||||
Timecop.freeze(Time.now) do
|
Timecop.freeze(Time.zone.now) do
|
||||||
get :render_fragment_changed_object_with_relationship
|
get :render_fragment_changed_object_with_relationship
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
|
|
||||||
expected_return = {
|
expected_return = {
|
||||||
'id' => 1,
|
'id' => 1,
|
||||||
'time' => Time.now.to_s,
|
'time' => Time.zone.now.to_s,
|
||||||
'likeable' => {
|
'likeable' => {
|
||||||
'id' => 1,
|
'id' => 1,
|
||||||
'body' => 'ZOMG A COMMENT'
|
'body' => 'ZOMG A COMMENT'
|
||||||
|
|||||||
@ -28,28 +28,28 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def with_jsonapi_resource_type type
|
def with_jsonapi_resource_type type
|
||||||
old_type = ActiveModel::Serializer.config[:jsonapi_resource_type]
|
old_type = ActiveModel::Serializer.config.jsonapi_resource_type
|
||||||
ActiveModel::Serializer.config[:jsonapi_resource_type] = type
|
ActiveModel::Serializer.config.jsonapi_resource_type = type
|
||||||
yield
|
yield
|
||||||
ensure
|
ensure
|
||||||
ActiveModel::Serializer.config[:jsonapi_resource_type] = old_type
|
ActiveModel::Serializer.config.jsonapi_resource_type = old_type
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_config_plural
|
def test_config_plural
|
||||||
|
with_adapter :json_api do
|
||||||
with_jsonapi_resource_type :plural do
|
with_jsonapi_resource_type :plural do
|
||||||
serializer = CommentSerializer.new(@comment)
|
hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
|
||||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
assert_equal('comments', hash[:data][:type])
|
||||||
ActionController::Base.cache_store.clear
|
end
|
||||||
assert_equal('comments', adapter.serializable_hash[:data][:type])
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_config_singular
|
def test_config_singular
|
||||||
|
with_adapter :json_api do
|
||||||
with_jsonapi_resource_type :singular do
|
with_jsonapi_resource_type :singular do
|
||||||
serializer = CommentSerializer.new(@comment)
|
hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
|
||||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
assert_equal('comment', hash[:data][:type])
|
||||||
ActionController::Base.cache_store.clear
|
end
|
||||||
assert_equal('comment', adapter.serializable_hash[:data][:type])
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
13
test/support/serialization_testing.rb
Normal file
13
test/support/serialization_testing.rb
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
class Minitest::Test
|
||||||
|
def before_setup
|
||||||
|
ActionController::Base.cache_store.clear
|
||||||
|
end
|
||||||
|
|
||||||
|
def with_adapter(adapter)
|
||||||
|
old_adapter = ActiveModel::Serializer.config.adapter
|
||||||
|
ActiveModel::Serializer.config.adapter = adapter
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
ActiveModel::Serializer.config.adapter = old_adapter
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -46,6 +46,8 @@ require 'support/rails_app'
|
|||||||
|
|
||||||
require 'support/test_case'
|
require 'support/test_case'
|
||||||
|
|
||||||
|
require 'support/serialization_testing'
|
||||||
|
|
||||||
require 'fixtures/active_record'
|
require 'fixtures/active_record'
|
||||||
|
|
||||||
require 'fixtures/poro'
|
require 'fixtures/poro'
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user