mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Grape formatter feature requested in #1258
- adds handling for when the returned resource is not serializable via ams - fix for when resource is an Array - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable. - Add integration test - Refine scope of Grape version dependency - Assert that the response is equal to a manually defined JSON string - Add single module to include in Grape projects - Create a Serializable Resource to test rails-api from Grape - Update docs - Fix discrepency between ActiveRecord 4.0 - 4.1 and 4.2 - Updated Changelog - Remove parens from `render`, use `serializable` in all tests.
This commit is contained in:
committed by
John Hamelink
parent
614e349502
commit
d85a17bb33
89
test/grape_test.rb
Normal file
89
test/grape_test.rb
Normal file
@@ -0,0 +1,89 @@
|
||||
require 'test_helper'
|
||||
require 'grape'
|
||||
require 'grape/active_model_serializers'
|
||||
|
||||
class ActiveModelSerializers::GrapeTest < Minitest::Test
|
||||
include Rack::Test::Methods
|
||||
|
||||
class GrapeTest < Grape::API
|
||||
format :json
|
||||
include Grape::ActiveModelSerializers
|
||||
|
||||
resources :grape do
|
||||
get '/render' do
|
||||
render ARModels::Post.new(title: 'Dummy Title', body: 'Lorem Ipsum')
|
||||
end
|
||||
|
||||
get '/render_with_json_api' do
|
||||
post = ARModels::Post.new(title: 'Dummy Title', body: 'Lorem Ipsum')
|
||||
render post, meta: { page: 1, total_pages: 2 }, adapter: :json_api
|
||||
end
|
||||
|
||||
get '/render_array_with_json_api' do
|
||||
post = ARModels::Post.create(title: 'Dummy Title', body: 'Lorem Ipsum')
|
||||
post.dup.save
|
||||
render ARModels::Post.all, adapter: :json_api
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def app
|
||||
GrapeTest.new
|
||||
end
|
||||
|
||||
def test_formatter_returns_json
|
||||
get '/grape/render'
|
||||
|
||||
post = ARModels::Post.new(title: 'Dummy Title', body: 'Lorem Ipsum')
|
||||
serializable_resource = serializable(post)
|
||||
|
||||
assert last_response.ok?
|
||||
assert_equal serializable_resource.to_json, last_response.body
|
||||
end
|
||||
|
||||
def test_render_helper_passes_through_options_correctly
|
||||
get '/grape/render_with_json_api'
|
||||
|
||||
post = ARModels::Post.new(title: 'Dummy Title', body: 'Lorem Ipsum')
|
||||
serializable_resource = serializable(post, serializer: ARModels::PostSerializer, adapter: :json_api, meta: { page: 1, total_pages: 2 })
|
||||
|
||||
assert last_response.ok?
|
||||
assert_equal serializable_resource.to_json, last_response.body
|
||||
end
|
||||
|
||||
def test_formatter_handles_arrays
|
||||
get '/grape/render_array_with_json_api'
|
||||
|
||||
expected = {
|
||||
'data' => [
|
||||
{
|
||||
id: '1',
|
||||
type: 'ar_models_posts',
|
||||
attributes: {
|
||||
title: 'Dummy Title',
|
||||
body: 'Lorem Ipsum'
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [] },
|
||||
author: { data: nil }
|
||||
}
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
type: 'ar_models_posts',
|
||||
attributes: {
|
||||
title: 'Dummy Title',
|
||||
body: 'Lorem Ipsum'
|
||||
},
|
||||
relationships: {
|
||||
comments: { data: [] },
|
||||
author: { data: nil }
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
assert last_response.ok?
|
||||
assert_equal expected.to_json, last_response.body
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user