Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor with_adapter + force cache clear before each test. #1093

Merged
merged 4 commits into from
Sep 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 46 additions & 51 deletions test/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,48 @@ class ImplicitSerializerTest < ActionController::TestCase
include ActiveSupport::Testing::Stream
class ImplicitSerializationTestController < ActionController::Base
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
end

def render_using_default_adapter_root
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
# JSON-API adapter sets root by default
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile
end
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
render json: @profile
end

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' })
render json: [@profile], root: 'custom_root'
end
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
render json: [@profile], root: 'custom_root'
end

def render_array_that_is_empty_using_custom_root
with_adapter ActiveModel::Serializer::Adapter::Json do
render json: [], root: 'custom_root'
end
render json: [], root: 'custom_root'
end

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' })
render json: @profile, root: 'custom_root'
end
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
render json: @profile, root: 'custom_root'
end

def render_array_using_implicit_serializer
array = [
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 1', description: 'Description 1', comments: 'Comments 1'),
Profile.new(name: 'Name 2', description: 'Description 2', comments: 'Comments 2')
]
render json: array
end

def render_array_using_implicit_serializer_and_meta
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
@profiles = [
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
]

render json: @profiles, meta: { total: 10 }
end
@profiles = [
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
]
render json: @profiles, meta: { total: 10 }
end

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.')
@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)

Expand All @@ -83,9 +71,9 @@ def update_and_render_object_with_cache_enabled
end

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.')
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)

Expand All @@ -95,16 +83,16 @@ def render_object_expired_with_cache_enabled
end

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.')
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
end

def render_fragment_changed_object_with_only_cache_enabled
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)
role.name = 'lol'
Expand All @@ -115,7 +103,7 @@ def render_fragment_changed_object_with_only_cache_enabled

def render_fragment_changed_object_with_except_cache_enabled
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)
bio.content = 'lol'
Expand All @@ -125,13 +113,13 @@ def render_fragment_changed_object_with_except_cache_enabled
end

def render_fragment_changed_object_with_relationship
comment = Comment.new({ id: 1, body: 'ZOMG A 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 })
comment = Comment.new(id: 1, body: 'ZOMG A 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)

generate_cached_serializer(like)
like.likable = comment2
like.time = Time.now.to_s
like.time = Time.zone.now.to_s

render json: like
end
Expand Down Expand Up @@ -168,8 +156,9 @@ def test_render_using_implicit_serializer
end

def test_render_using_default_root
get :render_using_default_adapter_root

with_adapter :json_api do
get :render_using_default_adapter_root
end
expected = {
data: {
id: assigns(:profile).id.to_s,
Expand All @@ -186,23 +175,28 @@ def test_render_using_default_root
end

def test_render_array_using_custom_root
get :render_array_using_custom_root

with_adapter :json do
get :render_array_using_custom_root
end
expected = { custom_roots: [{ name: 'Name 1', description: 'Description 1' }] }
assert_equal 'application/json', @response.content_type
assert_equal expected.to_json, @response.body
end

def test_render_array_that_is_empty_using_custom_root
get :render_array_that_is_empty_using_custom_root
with_adapter :json do
get :render_array_that_is_empty_using_custom_root
end

expected = { custom_roots: [] }
assert_equal 'application/json', @response.content_type
assert_equal expected.to_json, @response.body
end

def test_render_object_using_custom_root
get :render_object_using_custom_root
with_adapter :json do
get :render_object_using_custom_root
end

expected = { custom_root: { name: 'Name 1', description: 'Description 1' } }
assert_equal 'application/json', @response.content_type
Expand Down Expand Up @@ -232,20 +226,21 @@ def test_render_array_using_implicit_serializer
expected = [
{
name: 'Name 1',
description: 'Description 1',
description: 'Description 1'
},
{
name: 'Name 2',
description: 'Description 2',
description: 'Description 2'
}
]

assert_equal expected.to_json, @response.body
end

def test_render_array_using_implicit_serializer_and_meta
get :render_array_using_implicit_serializer_and_meta

with_adapter :json_api do
get :render_array_using_implicit_serializer_and_meta
end
expected = {
data: [
{
Expand Down Expand Up @@ -287,7 +282,7 @@ def test_render_with_cache_enable
}

ActionController::Base.cache_store.clear
Timecop.freeze(Time.now) do
Timecop.freeze(Time.zone.now) do
get :render_object_with_cache_enabled

assert_equal 'application/json', @response.content_type
Expand Down Expand Up @@ -352,13 +347,13 @@ def test_render_with_fragment_except_cache_enable
def test_render_fragment_changed_object_with_relationship
ActionController::Base.cache_store.clear

Timecop.freeze(Time.now) do
Timecop.freeze(Time.zone.now) do
get :render_fragment_changed_object_with_relationship
response = JSON.parse(@response.body)

expected_return = {
'id' => 1,
'time' => Time.now.to_s,
'time' => Time.zone.now.to_s,
'likeable' => {
'id' => 1,
'body' => 'ZOMG A COMMENT'
Expand Down
26 changes: 13 additions & 13 deletions test/adapter/json_api/resource_type_config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ def setup
end

def with_jsonapi_resource_type type
old_type = ActiveModel::Serializer.config[:jsonapi_resource_type]
ActiveModel::Serializer.config[:jsonapi_resource_type] = type
old_type = ActiveModel::Serializer.config.jsonapi_resource_type
ActiveModel::Serializer.config.jsonapi_resource_type = type
yield
ensure
ActiveModel::Serializer.config[:jsonapi_resource_type] = old_type
ActiveModel::Serializer.config.jsonapi_resource_type = old_type
end

def test_config_plural
with_jsonapi_resource_type :plural do
serializer = CommentSerializer.new(@comment)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
ActionController::Base.cache_store.clear
assert_equal('comments', adapter.serializable_hash[:data][:type])
with_adapter :json_api do
with_jsonapi_resource_type :plural do
hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
assert_equal('comments', hash[:data][:type])
end
end
end

def test_config_singular
with_jsonapi_resource_type :singular do
serializer = CommentSerializer.new(@comment)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
ActionController::Base.cache_store.clear
assert_equal('comment', adapter.serializable_hash[:data][:type])
with_adapter :json_api do
with_jsonapi_resource_type :singular do
hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
assert_equal('comment', hash[:data][:type])
end
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions test/support/serialization_testing.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

require 'support/test_case'

require 'support/serialization_testing'

require 'fixtures/active_record'

require 'fixtures/poro'