From 4bba16bf4e1d695881dc806e72e07b1076d6f5f5 Mon Sep 17 00:00:00 2001 From: Lucas Hosseini Date: Fri, 28 Aug 2015 17:00:24 +0200 Subject: [PATCH 1/2] Factor `with_adapter` + force cache clear before each test. --- test/action_controller/serialization_test.rb | 58 +++++++++---------- .../json_api/resource_type_config_test.rb | 22 +++---- test/support/serialization_testing.rb | 13 +++++ test/test_helper.rb | 2 + 4 files changed, 52 insertions(+), 43 deletions(-) create mode 100644 test/support/serialization_testing.rb diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb index fab43af0..45006597 100644 --- a/test/action_controller/serialization_test.rb +++ b/test/action_controller/serialization_test.rb @@ -1,4 +1,3 @@ - require 'test_helper' module ActionController @@ -12,31 +11,22 @@ module ActionController 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 @@ -48,14 +38,11 @@ module ActionController 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' }) + ] - @profiles = [ - Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - ] - - render json: @profiles, meta: { total: 10 } - end + render json: @profiles, meta: { total: 10 } end def render_object_with_cache_enabled @@ -169,8 +156,9 @@ module ActionController 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, @@ -187,15 +175,18 @@ module ActionController 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 @@ -203,7 +194,9 @@ module ActionController 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 @@ -245,8 +238,9 @@ module ActionController 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: [ { diff --git a/test/adapter/json_api/resource_type_config_test.rb b/test/adapter/json_api/resource_type_config_test.rb index e389b9b4..aaf247e5 100644 --- a/test/adapter/json_api/resource_type_config_test.rb +++ b/test/adapter/json_api/resource_type_config_test.rb @@ -32,24 +32,24 @@ module ActiveModel 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.serialize(@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.serialize(@comment).serializable_hash + assert_equal('comment', hash[:data][:type]) + end end end end diff --git a/test/support/serialization_testing.rb b/test/support/serialization_testing.rb new file mode 100644 index 00000000..38fcdf5e --- /dev/null +++ b/test/support/serialization_testing.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 1327188e..115972d6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -37,3 +37,5 @@ require 'support/rails_app' require 'fixtures/poro' require 'support/test_case' + +require 'support/serialization_testing' From 890003b305b80fd34396773c6a34e15e331a8349 Mon Sep 17 00:00:00 2001 From: Lucas Hosseini Date: Mon, 7 Sep 2015 09:06:17 +0200 Subject: [PATCH 2/2] Minor style improvements. --- test/adapter/json_api/resource_type_config_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/adapter/json_api/resource_type_config_test.rb b/test/adapter/json_api/resource_type_config_test.rb index c5802a03..1f2f534d 100644 --- a/test/adapter/json_api/resource_type_config_test.rb +++ b/test/adapter/json_api/resource_type_config_test.rb @@ -28,8 +28,8 @@ module ActiveModel 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 @@ -38,7 +38,7 @@ module ActiveModel def test_config_plural with_adapter :json_api do with_jsonapi_resource_type :plural do - hash = ActiveModel::SerializableResource.serialize(@comment).serializable_hash + hash = ActiveModel::SerializableResource.new(@comment).serializable_hash assert_equal('comments', hash[:data][:type]) end end @@ -47,7 +47,7 @@ module ActiveModel def test_config_singular with_adapter :json_api do with_jsonapi_resource_type :singular do - hash = ActiveModel::SerializableResource.serialize(@comment).serializable_hash + hash = ActiveModel::SerializableResource.new(@comment).serializable_hash assert_equal('comment', hash[:data][:type]) end end