mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Merge pull request #1815 from dubadub/making-rubocop-happy
Fix code-styling issues from .rubocop_todo.yml * re: RuboCop: Bulk minor style corrections * re: RuboCop - hash indention corrections * re: RuboCop - replace rocket style hashes * re: RuboCop - get rid of redundant curly braces around a hash parameter * re: RuboCop - Align the elements of a hash literal if they span more than one line. * re: RuboCop - Use nested module/class definition instead of compact style. * re: RuboCop - Suppress of handling LoadError for optional dependencies * re: RuboCop - use include_ prefix instead of has_ * re: RuboCop - Disable Style/PredicateName rule for public API methods * re: RuboCop - Remove empty .rubocop_todo.yml * re: RuboCop - replace rocket style hashes
This commit is contained in:
@@ -5,17 +5,17 @@ module ActionController
|
||||
class AdapterSelectorTest < ActionController::TestCase
|
||||
class AdapterSelectorTestController < ActionController::Base
|
||||
def render_using_default_adapter
|
||||
@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_adapter_override
|
||||
@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, adapter: :json_api
|
||||
end
|
||||
|
||||
def render_skipping_adapter
|
||||
@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, adapter: false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,12 +8,11 @@ module ActionController
|
||||
get :render_resource_with_errors
|
||||
|
||||
expected_errors_object = {
|
||||
:errors =>
|
||||
[
|
||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' },
|
||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' },
|
||||
{ :source => { :pointer => '/data/attributes/id' }, :detail => 'must be a uuid' }
|
||||
]
|
||||
errors: [
|
||||
{ source: { pointer: '/data/attributes/name' }, detail: 'cannot be nil' },
|
||||
{ source: { pointer: '/data/attributes/name' }, detail: 'must be longer' },
|
||||
{ source: { pointer: '/data/attributes/id' }, detail: 'must be a uuid' }
|
||||
]
|
||||
}.to_json
|
||||
assert_equal json_reponse_body.to_json, expected_errors_object
|
||||
end
|
||||
|
||||
@@ -87,7 +87,7 @@ module ActionController
|
||||
setup do
|
||||
@routes = Rails.application.routes.draw do
|
||||
ActiveSupport::Deprecation.silence do
|
||||
match ':action', :to => LinkedTestController, via: [:get, :post]
|
||||
match ':action', to: LinkedTestController, via: [:get, :post]
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -183,17 +183,17 @@ module ActionController
|
||||
get '/render_resource_with_missing_nested_has_many_include'
|
||||
response = JSON.parse(@response.body)
|
||||
assert response.key? 'included'
|
||||
refute has_type?(response['included'], 'roles')
|
||||
refute include_type?(response['included'], 'roles')
|
||||
end
|
||||
|
||||
def test_render_collection_with_missing_nested_has_many_include
|
||||
get '/render_collection_with_missing_nested_has_many_include'
|
||||
response = JSON.parse(@response.body)
|
||||
assert response.key? 'included'
|
||||
assert has_type?(response['included'], 'roles')
|
||||
assert include_type?(response['included'], 'roles')
|
||||
end
|
||||
|
||||
def has_type?(collection, value)
|
||||
def include_type?(collection, value)
|
||||
collection.detect { |i| i['type'] == value }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,9 +14,9 @@ module ActionController
|
||||
class PaginationTestController < ActionController::Base
|
||||
def setup
|
||||
@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 3', description: 'Description 3', comments: 'Comments 3' })
|
||||
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 3', description: 'Description 3', comments: 'Comments 3')
|
||||
]
|
||||
end
|
||||
|
||||
@@ -48,10 +48,10 @@ module ActionController
|
||||
|
||||
def test_render_pagination_links_with_will_paginate
|
||||
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
||||
'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
'prev' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
||||
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
|
||||
'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
'prev' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
||||
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
|
||||
|
||||
get :render_pagination_using_will_paginate, params: { page: { number: 2, size: 1 } }
|
||||
response = JSON.parse(@response.body)
|
||||
@@ -60,8 +60,8 @@ module ActionController
|
||||
|
||||
def test_render_only_last_and_next_pagination_links
|
||||
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
||||
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
|
||||
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" }
|
||||
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
|
||||
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" }
|
||||
get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 } }
|
||||
response = JSON.parse(@response.body)
|
||||
assert_equal expected_links, response['links']
|
||||
@@ -69,10 +69,10 @@ module ActionController
|
||||
|
||||
def test_render_pagination_links_with_kaminari
|
||||
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
||||
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
'next' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
||||
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
|
||||
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
'next' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
||||
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
|
||||
get :render_pagination_using_kaminari, params: { page: { number: 2, size: 1 } }
|
||||
response = JSON.parse(@response.body)
|
||||
assert_equal expected_links, response['links']
|
||||
@@ -80,8 +80,8 @@ module ActionController
|
||||
|
||||
def test_render_only_prev_and_first_pagination_links
|
||||
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
||||
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1" }
|
||||
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
||||
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1" }
|
||||
get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 } }
|
||||
response = JSON.parse(@response.body)
|
||||
assert_equal expected_links, response['links']
|
||||
@@ -89,8 +89,8 @@ module ActionController
|
||||
|
||||
def test_render_only_last_and_next_pagination_links_with_additional_params
|
||||
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
|
||||
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
|
||||
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" }
|
||||
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
|
||||
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" }
|
||||
get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 }, teste: 'additional' }
|
||||
response = JSON.parse(@response.body)
|
||||
assert_equal expected_links, response['links']
|
||||
@@ -98,8 +98,8 @@ module ActionController
|
||||
|
||||
def test_render_only_prev_and_first_pagination_links_with_additional_params
|
||||
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
|
||||
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
|
||||
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional" }
|
||||
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
|
||||
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional" }
|
||||
get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 }, teste: 'additional' }
|
||||
response = JSON.parse(@response.body)
|
||||
assert_equal expected_links, response['links']
|
||||
|
||||
@@ -74,7 +74,7 @@ module ActionController
|
||||
end
|
||||
|
||||
def update_and_render_object_with_cache_enabled
|
||||
@post.updated_at = Time.now
|
||||
@post.updated_at = Time.zone.now
|
||||
|
||||
generate_cached_serializer(@post)
|
||||
render json: @post
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ActiveModelSerializers::JsonPointerTest < ActiveSupport::TestCase
|
||||
def test_attribute_pointer
|
||||
attribute_name = 'title'
|
||||
pointer = ActiveModelSerializers::JsonPointer.new(:attribute, attribute_name)
|
||||
assert_equal '/data/attributes/title', pointer
|
||||
end
|
||||
module ActiveModelSerializers
|
||||
class JsonPointerTest < ActiveSupport::TestCase
|
||||
def test_attribute_pointer
|
||||
attribute_name = 'title'
|
||||
pointer = ActiveModelSerializers::JsonPointer.new(:attribute, attribute_name)
|
||||
assert_equal '/data/attributes/title', pointer
|
||||
end
|
||||
|
||||
def test_primary_data_pointer
|
||||
pointer = ActiveModelSerializers::JsonPointer.new(:primary_data)
|
||||
assert_equal '/data', pointer
|
||||
end
|
||||
def test_primary_data_pointer
|
||||
pointer = ActiveModelSerializers::JsonPointer.new(:primary_data)
|
||||
assert_equal '/data', pointer
|
||||
end
|
||||
|
||||
def test_unkown_data_pointer
|
||||
assert_raises(TypeError) do
|
||||
ActiveModelSerializers::JsonPointer.new(:unknown)
|
||||
def test_unkown_data_pointer
|
||||
assert_raises(TypeError) do
|
||||
ActiveModelSerializers::JsonPointer.new(:unknown)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,263 +1,265 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ActiveModelSerializers::KeyTransformTest < ActiveSupport::TestCase
|
||||
def test_camel
|
||||
obj = Object.new
|
||||
scenarios = [
|
||||
{
|
||||
value: { :"some-key" => 'value' },
|
||||
expected: { :SomeKey => 'value' }
|
||||
},
|
||||
{
|
||||
value: { :someKey => 'value' },
|
||||
expected: { :SomeKey => 'value' }
|
||||
},
|
||||
{
|
||||
value: { :some_key => 'value' },
|
||||
expected: { :SomeKey => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'some-key' => 'value' },
|
||||
expected: { 'SomeKey' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'someKey' => 'value' },
|
||||
expected: { 'SomeKey' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'some_key' => 'value' },
|
||||
expected: { 'SomeKey' => 'value' }
|
||||
},
|
||||
{
|
||||
value: :"some-value",
|
||||
expected: :SomeValue
|
||||
},
|
||||
{
|
||||
value: :some_value,
|
||||
expected: :SomeValue
|
||||
},
|
||||
{
|
||||
value: :someValue,
|
||||
expected: :SomeValue
|
||||
},
|
||||
{
|
||||
value: 'some-value',
|
||||
expected: 'SomeValue'
|
||||
},
|
||||
{
|
||||
value: 'someValue',
|
||||
expected: 'SomeValue'
|
||||
},
|
||||
{
|
||||
value: 'some_value',
|
||||
expected: 'SomeValue'
|
||||
},
|
||||
{
|
||||
value: obj,
|
||||
expected: obj
|
||||
},
|
||||
{
|
||||
value: nil,
|
||||
expected: nil
|
||||
}
|
||||
]
|
||||
scenarios.each do |s|
|
||||
result = ActiveModelSerializers::KeyTransform.camel(s[:value])
|
||||
assert_equal s[:expected], result
|
||||
module ActiveModelSerializers
|
||||
class KeyTransformTest < ActiveSupport::TestCase
|
||||
def test_camel
|
||||
obj = Object.new
|
||||
scenarios = [
|
||||
{
|
||||
value: { :"some-key" => 'value' },
|
||||
expected: { SomeKey: 'value' }
|
||||
},
|
||||
{
|
||||
value: { someKey: 'value' },
|
||||
expected: { SomeKey: 'value' }
|
||||
},
|
||||
{
|
||||
value: { some_key: 'value' },
|
||||
expected: { SomeKey: 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'some-key' => 'value' },
|
||||
expected: { 'SomeKey' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'someKey' => 'value' },
|
||||
expected: { 'SomeKey' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'some_key' => 'value' },
|
||||
expected: { 'SomeKey' => 'value' }
|
||||
},
|
||||
{
|
||||
value: :"some-value",
|
||||
expected: :SomeValue
|
||||
},
|
||||
{
|
||||
value: :some_value,
|
||||
expected: :SomeValue
|
||||
},
|
||||
{
|
||||
value: :someValue,
|
||||
expected: :SomeValue
|
||||
},
|
||||
{
|
||||
value: 'some-value',
|
||||
expected: 'SomeValue'
|
||||
},
|
||||
{
|
||||
value: 'someValue',
|
||||
expected: 'SomeValue'
|
||||
},
|
||||
{
|
||||
value: 'some_value',
|
||||
expected: 'SomeValue'
|
||||
},
|
||||
{
|
||||
value: obj,
|
||||
expected: obj
|
||||
},
|
||||
{
|
||||
value: nil,
|
||||
expected: nil
|
||||
}
|
||||
]
|
||||
scenarios.each do |s|
|
||||
result = ActiveModelSerializers::KeyTransform.camel(s[:value])
|
||||
assert_equal s[:expected], result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_camel_lower
|
||||
obj = Object.new
|
||||
scenarios = [
|
||||
{
|
||||
value: { :"some-key" => 'value' },
|
||||
expected: { :someKey => 'value' }
|
||||
},
|
||||
{
|
||||
value: { :SomeKey => 'value' },
|
||||
expected: { :someKey => 'value' }
|
||||
},
|
||||
{
|
||||
value: { :some_key => 'value' },
|
||||
expected: { :someKey => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'some-key' => 'value' },
|
||||
expected: { 'someKey' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'SomeKey' => 'value' },
|
||||
expected: { 'someKey' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'some_key' => 'value' },
|
||||
expected: { 'someKey' => 'value' }
|
||||
},
|
||||
{
|
||||
value: :"some-value",
|
||||
expected: :someValue
|
||||
},
|
||||
{
|
||||
value: :SomeValue,
|
||||
expected: :someValue
|
||||
},
|
||||
{
|
||||
value: :some_value,
|
||||
expected: :someValue
|
||||
},
|
||||
{
|
||||
value: 'some-value',
|
||||
expected: 'someValue'
|
||||
},
|
||||
{
|
||||
value: 'SomeValue',
|
||||
expected: 'someValue'
|
||||
},
|
||||
{
|
||||
value: 'some_value',
|
||||
expected: 'someValue'
|
||||
},
|
||||
{
|
||||
value: obj,
|
||||
expected: obj
|
||||
},
|
||||
{
|
||||
value: nil,
|
||||
expected: nil
|
||||
}
|
||||
]
|
||||
scenarios.each do |s|
|
||||
result = ActiveModelSerializers::KeyTransform.camel_lower(s[:value])
|
||||
assert_equal s[:expected], result
|
||||
def test_camel_lower
|
||||
obj = Object.new
|
||||
scenarios = [
|
||||
{
|
||||
value: { :"some-key" => 'value' },
|
||||
expected: { someKey: 'value' }
|
||||
},
|
||||
{
|
||||
value: { SomeKey: 'value' },
|
||||
expected: { someKey: 'value' }
|
||||
},
|
||||
{
|
||||
value: { some_key: 'value' },
|
||||
expected: { someKey: 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'some-key' => 'value' },
|
||||
expected: { 'someKey' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'SomeKey' => 'value' },
|
||||
expected: { 'someKey' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'some_key' => 'value' },
|
||||
expected: { 'someKey' => 'value' }
|
||||
},
|
||||
{
|
||||
value: :"some-value",
|
||||
expected: :someValue
|
||||
},
|
||||
{
|
||||
value: :SomeValue,
|
||||
expected: :someValue
|
||||
},
|
||||
{
|
||||
value: :some_value,
|
||||
expected: :someValue
|
||||
},
|
||||
{
|
||||
value: 'some-value',
|
||||
expected: 'someValue'
|
||||
},
|
||||
{
|
||||
value: 'SomeValue',
|
||||
expected: 'someValue'
|
||||
},
|
||||
{
|
||||
value: 'some_value',
|
||||
expected: 'someValue'
|
||||
},
|
||||
{
|
||||
value: obj,
|
||||
expected: obj
|
||||
},
|
||||
{
|
||||
value: nil,
|
||||
expected: nil
|
||||
}
|
||||
]
|
||||
scenarios.each do |s|
|
||||
result = ActiveModelSerializers::KeyTransform.camel_lower(s[:value])
|
||||
assert_equal s[:expected], result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_dash
|
||||
obj = Object.new
|
||||
scenarios = [
|
||||
{
|
||||
value: { :some_key => 'value' },
|
||||
expected: { :"some-key" => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'some_key' => 'value' },
|
||||
expected: { 'some-key' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { :SomeKey => 'value' },
|
||||
expected: { :"some-key" => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'SomeKey' => 'value' },
|
||||
expected: { 'some-key' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { :someKey => 'value' },
|
||||
expected: { :"some-key" => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'someKey' => 'value' },
|
||||
expected: { 'some-key' => 'value' }
|
||||
},
|
||||
{
|
||||
value: :some_value,
|
||||
expected: :"some-value"
|
||||
},
|
||||
{
|
||||
value: :SomeValue,
|
||||
expected: :"some-value"
|
||||
},
|
||||
{
|
||||
value: 'SomeValue',
|
||||
expected: 'some-value'
|
||||
},
|
||||
{
|
||||
value: :someValue,
|
||||
expected: :"some-value"
|
||||
},
|
||||
{
|
||||
value: 'someValue',
|
||||
expected: 'some-value'
|
||||
},
|
||||
{
|
||||
value: obj,
|
||||
expected: obj
|
||||
},
|
||||
{
|
||||
value: nil,
|
||||
expected: nil
|
||||
}
|
||||
]
|
||||
scenarios.each do |s|
|
||||
result = ActiveModelSerializers::KeyTransform.dash(s[:value])
|
||||
assert_equal s[:expected], result
|
||||
def test_dash
|
||||
obj = Object.new
|
||||
scenarios = [
|
||||
{
|
||||
value: { some_key: 'value' },
|
||||
expected: { :"some-key" => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'some_key' => 'value' },
|
||||
expected: { 'some-key' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { SomeKey: 'value' },
|
||||
expected: { :"some-key" => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'SomeKey' => 'value' },
|
||||
expected: { 'some-key' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { someKey: 'value' },
|
||||
expected: { :"some-key" => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'someKey' => 'value' },
|
||||
expected: { 'some-key' => 'value' }
|
||||
},
|
||||
{
|
||||
value: :some_value,
|
||||
expected: :"some-value"
|
||||
},
|
||||
{
|
||||
value: :SomeValue,
|
||||
expected: :"some-value"
|
||||
},
|
||||
{
|
||||
value: 'SomeValue',
|
||||
expected: 'some-value'
|
||||
},
|
||||
{
|
||||
value: :someValue,
|
||||
expected: :"some-value"
|
||||
},
|
||||
{
|
||||
value: 'someValue',
|
||||
expected: 'some-value'
|
||||
},
|
||||
{
|
||||
value: obj,
|
||||
expected: obj
|
||||
},
|
||||
{
|
||||
value: nil,
|
||||
expected: nil
|
||||
}
|
||||
]
|
||||
scenarios.each do |s|
|
||||
result = ActiveModelSerializers::KeyTransform.dash(s[:value])
|
||||
assert_equal s[:expected], result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_underscore
|
||||
obj = Object.new
|
||||
scenarios = [
|
||||
{
|
||||
value: { :"some-key" => 'value' },
|
||||
expected: { :some_key => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'some-key' => 'value' },
|
||||
expected: { 'some_key' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { :SomeKey => 'value' },
|
||||
expected: { :some_key => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'SomeKey' => 'value' },
|
||||
expected: { 'some_key' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { :someKey => 'value' },
|
||||
expected: { :some_key => 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'someKey' => 'value' },
|
||||
expected: { 'some_key' => 'value' }
|
||||
},
|
||||
{
|
||||
value: :"some-value",
|
||||
expected: :some_value
|
||||
},
|
||||
{
|
||||
value: :SomeValue,
|
||||
expected: :some_value
|
||||
},
|
||||
{
|
||||
value: :someValue,
|
||||
expected: :some_value
|
||||
},
|
||||
{
|
||||
value: 'some-value',
|
||||
expected: 'some_value'
|
||||
},
|
||||
{
|
||||
value: 'SomeValue',
|
||||
expected: 'some_value'
|
||||
},
|
||||
{
|
||||
value: 'someValue',
|
||||
expected: 'some_value'
|
||||
},
|
||||
{
|
||||
value: obj,
|
||||
expected: obj
|
||||
},
|
||||
{
|
||||
value: nil,
|
||||
expected: nil
|
||||
}
|
||||
]
|
||||
scenarios.each do |s|
|
||||
result = ActiveModelSerializers::KeyTransform.underscore(s[:value])
|
||||
assert_equal s[:expected], result
|
||||
def test_underscore
|
||||
obj = Object.new
|
||||
scenarios = [
|
||||
{
|
||||
value: { :"some-key" => 'value' },
|
||||
expected: { some_key: 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'some-key' => 'value' },
|
||||
expected: { 'some_key' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { SomeKey: 'value' },
|
||||
expected: { some_key: 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'SomeKey' => 'value' },
|
||||
expected: { 'some_key' => 'value' }
|
||||
},
|
||||
{
|
||||
value: { someKey: 'value' },
|
||||
expected: { some_key: 'value' }
|
||||
},
|
||||
{
|
||||
value: { 'someKey' => 'value' },
|
||||
expected: { 'some_key' => 'value' }
|
||||
},
|
||||
{
|
||||
value: :"some-value",
|
||||
expected: :some_value
|
||||
},
|
||||
{
|
||||
value: :SomeValue,
|
||||
expected: :some_value
|
||||
},
|
||||
{
|
||||
value: :someValue,
|
||||
expected: :some_value
|
||||
},
|
||||
{
|
||||
value: 'some-value',
|
||||
expected: 'some_value'
|
||||
},
|
||||
{
|
||||
value: 'SomeValue',
|
||||
expected: 'some_value'
|
||||
},
|
||||
{
|
||||
value: 'someValue',
|
||||
expected: 'some_value'
|
||||
},
|
||||
{
|
||||
value: obj,
|
||||
expected: obj
|
||||
},
|
||||
{
|
||||
value: nil,
|
||||
expected: nil
|
||||
}
|
||||
]
|
||||
scenarios.each do |s|
|
||||
result = ActiveModelSerializers::KeyTransform.underscore(s[:value])
|
||||
assert_equal s[:expected], result
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ActiveModelSerializers::ModelTest < ActiveSupport::TestCase
|
||||
include ActiveModel::Serializer::Lint::Tests
|
||||
module ActiveModelSerializers
|
||||
class ModelTest < ActiveSupport::TestCase
|
||||
include ActiveModel::Serializer::Lint::Tests
|
||||
|
||||
def setup
|
||||
@resource = ActiveModelSerializers::Model.new
|
||||
def setup
|
||||
@resource = ActiveModelSerializers::Model.new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -44,7 +44,7 @@ class JsonApiRendererTest < ActionDispatch::IntegrationTest
|
||||
|
||||
Rails.application.routes.draw do
|
||||
ActiveSupport::Deprecation.silence do
|
||||
match ':action', :to => TestController, via: [:get, :post]
|
||||
match ':action', to: TestController, via: [:get, :post]
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -95,7 +95,7 @@ class JsonApiRendererTest < ActionDispatch::IntegrationTest
|
||||
|
||||
Rails.application.routes.draw do
|
||||
ActiveSupport::Deprecation.silence do
|
||||
match ':action', :to => TestController, via: [:get, :post]
|
||||
match ':action', to: TestController, via: [:get, :post]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -28,8 +28,8 @@ module ActiveModelSerializers
|
||||
def test_transform_default
|
||||
mock_request
|
||||
assert_equal({
|
||||
blog: { id: 1, special_attribute: 'neat', articles: nil }
|
||||
}, @adapter.serializable_hash)
|
||||
blog: { id: 1, special_attribute: 'neat', articles: nil }
|
||||
}, @adapter.serializable_hash)
|
||||
end
|
||||
|
||||
def test_transform_global_config
|
||||
@@ -38,8 +38,8 @@ module ActiveModelSerializers
|
||||
@adapter.serializable_hash
|
||||
end
|
||||
assert_equal({
|
||||
blog: { id: 1, specialAttribute: 'neat', articles: nil }
|
||||
}, result)
|
||||
blog: { id: 1, specialAttribute: 'neat', articles: nil }
|
||||
}, result)
|
||||
end
|
||||
|
||||
def test_transform_serialization_ctx_overrides_global_config
|
||||
@@ -48,8 +48,8 @@ module ActiveModelSerializers
|
||||
@adapter.serializable_hash
|
||||
end
|
||||
assert_equal({
|
||||
Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
|
||||
}, result)
|
||||
Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
|
||||
}, result)
|
||||
end
|
||||
|
||||
def test_transform_undefined
|
||||
@@ -63,29 +63,29 @@ module ActiveModelSerializers
|
||||
def test_transform_dash
|
||||
mock_request(:dash)
|
||||
assert_equal({
|
||||
blog: { id: 1, :"special-attribute" => 'neat', articles: nil }
|
||||
}, @adapter.serializable_hash)
|
||||
blog: { id: 1, :"special-attribute" => 'neat', articles: nil }
|
||||
}, @adapter.serializable_hash)
|
||||
end
|
||||
|
||||
def test_transform_unaltered
|
||||
mock_request(:unaltered)
|
||||
assert_equal({
|
||||
blog: { id: 1, special_attribute: 'neat', articles: nil }
|
||||
}, @adapter.serializable_hash)
|
||||
blog: { id: 1, special_attribute: 'neat', articles: nil }
|
||||
}, @adapter.serializable_hash)
|
||||
end
|
||||
|
||||
def test_transform_camel
|
||||
mock_request(:camel)
|
||||
assert_equal({
|
||||
Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
|
||||
}, @adapter.serializable_hash)
|
||||
Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
|
||||
}, @adapter.serializable_hash)
|
||||
end
|
||||
|
||||
def test_transform_camel_lower
|
||||
mock_request(:camel_lower)
|
||||
assert_equal({
|
||||
blog: { id: 1, specialAttribute: 'neat', articles: nil }
|
||||
}, @adapter.serializable_hash)
|
||||
blog: { id: 1, specialAttribute: 'neat', articles: nil }
|
||||
}, @adapter.serializable_hash)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -12,8 +12,8 @@ module ActiveModelSerializers
|
||||
|
||||
def test_active_model_with_error
|
||||
options = {
|
||||
serializer: ActiveModel::Serializer::ErrorSerializer,
|
||||
adapter: :json_api
|
||||
serializer: ActiveModel::Serializer::ErrorSerializer,
|
||||
adapter: :json_api
|
||||
}
|
||||
|
||||
@resource.errors.add(:name, 'cannot be nil')
|
||||
@@ -23,7 +23,7 @@ module ActiveModelSerializers
|
||||
assert_equal serializable_resource.serializer_instance.object, @resource
|
||||
|
||||
expected_errors_object = {
|
||||
:errors => [
|
||||
errors: [
|
||||
{
|
||||
source: { pointer: '/data/attributes/name' },
|
||||
detail: 'cannot be nil'
|
||||
@@ -35,8 +35,8 @@ module ActiveModelSerializers
|
||||
|
||||
def test_active_model_with_multiple_errors
|
||||
options = {
|
||||
serializer: ActiveModel::Serializer::ErrorSerializer,
|
||||
adapter: :json_api
|
||||
serializer: ActiveModel::Serializer::ErrorSerializer,
|
||||
adapter: :json_api
|
||||
}
|
||||
|
||||
@resource.errors.add(:name, 'cannot be nil')
|
||||
@@ -48,10 +48,10 @@ module ActiveModelSerializers
|
||||
assert_equal serializable_resource.serializer_instance.object, @resource
|
||||
|
||||
expected_errors_object = {
|
||||
:errors => [
|
||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' },
|
||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' },
|
||||
{ :source => { :pointer => '/data/attributes/id' }, :detail => 'must be a uuid' }
|
||||
errors: [
|
||||
{ source: { pointer: '/data/attributes/name' }, detail: 'cannot be nil' },
|
||||
{ source: { pointer: '/data/attributes/name' }, detail: 'must be longer' },
|
||||
{ source: { pointer: '/data/attributes/id' }, detail: 'must be a uuid' }
|
||||
]
|
||||
}
|
||||
assert_equal serializable_resource.as_json, expected_errors_object
|
||||
|
||||
@@ -112,14 +112,14 @@ module ActiveModelSerializers
|
||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
||||
|
||||
assert_equal({
|
||||
data: {
|
||||
id: '1',
|
||||
type: 'posts',
|
||||
relationships: {
|
||||
tags: { data: [@tag.as_json] }
|
||||
}
|
||||
}
|
||||
}, adapter.serializable_hash)
|
||||
data: {
|
||||
id: '1',
|
||||
type: 'posts',
|
||||
relationships: {
|
||||
tags: { data: [@tag.as_json] }
|
||||
}
|
||||
}
|
||||
}, adapter.serializable_hash)
|
||||
end
|
||||
|
||||
def test_has_many_with_virtual_value
|
||||
@@ -127,16 +127,16 @@ module ActiveModelSerializers
|
||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
||||
|
||||
assert_equal({
|
||||
data: {
|
||||
id: '1',
|
||||
type: 'virtual-values',
|
||||
relationships: {
|
||||
maker: { data: { type: 'makers', id: '1' } },
|
||||
reviews: { data: [{ type: 'reviews', id: '1' },
|
||||
{ type: 'reviews', id: '2' }] }
|
||||
}
|
||||
}
|
||||
}, adapter.serializable_hash)
|
||||
data: {
|
||||
id: '1',
|
||||
type: 'virtual-values',
|
||||
relationships: {
|
||||
maker: { data: { type: 'makers', id: '1' } },
|
||||
reviews: { data: [{ type: 'reviews', id: '1' },
|
||||
{ type: 'reviews', id: '2' }] }
|
||||
}
|
||||
}
|
||||
}, adapter.serializable_hash)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -22,13 +22,11 @@ module ActiveModelSerializers
|
||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
||||
|
||||
assert_equal({
|
||||
reviews: { data: [
|
||||
{ type: 'comments', id: '1' },
|
||||
{ type: 'comments', id: '2' }
|
||||
] },
|
||||
writer: { data: { type: 'authors', id: '1' } },
|
||||
site: { data: { type: 'blogs', id: '1' } }
|
||||
}, adapter.serializable_hash[:data][:relationships])
|
||||
reviews: { data: [{ type: 'comments', id: '1' },
|
||||
{ type: 'comments', id: '2' }] },
|
||||
writer: { data: { type: 'authors', id: '1' } },
|
||||
site: { data: { type: 'blogs', id: '1' } }
|
||||
}, adapter.serializable_hash[:data][:relationships])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -17,7 +17,7 @@ module ActiveModelSerializers
|
||||
@first_post = Post.new(id: 10, title: 'Hello!!', body: 'Hello, world!!')
|
||||
@second_post = Post.new(id: 20, title: 'New Post', body: 'Body')
|
||||
@third_post = Post.new(id: 30, title: 'Yet Another Post', body: 'Body')
|
||||
@blog = Blog.new({ name: 'AMS Blog' })
|
||||
@blog = Blog.new(name: 'AMS Blog')
|
||||
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
||||
@first_post.blog = @blog
|
||||
|
||||
@@ -13,11 +13,11 @@ module ActiveModelSerializers
|
||||
def setup
|
||||
ActionController::Base.cache_store.clear
|
||||
@array = [
|
||||
Profile.new({ id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
||||
Profile.new({ id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
|
||||
Profile.new({ id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3' }),
|
||||
Profile.new({ id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4' }),
|
||||
Profile.new({ id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5' })
|
||||
Profile.new(id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
||||
Profile.new(id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2'),
|
||||
Profile.new(id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3'),
|
||||
Profile.new(id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4'),
|
||||
Profile.new(id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5')
|
||||
]
|
||||
end
|
||||
|
||||
@@ -122,7 +122,7 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def test_pagination_links_with_additional_params
|
||||
adapter = load_adapter(using_will_paginate, mock_request({ test: 'test' }))
|
||||
adapter = load_adapter(using_will_paginate, mock_request(test: 'test'))
|
||||
|
||||
assert_equal expected_response_with_pagination_links_and_additional_params,
|
||||
adapter.serializable_hash
|
||||
|
||||
@@ -68,9 +68,9 @@ module ActiveModel
|
||||
adapter: :json_api
|
||||
).serializable_hash
|
||||
expected = {
|
||||
:data => [
|
||||
{ :id => '1337', :type => 'posts', :meta => { :"comments-count" => 0 } },
|
||||
{ :id => '1339', :type => 'posts', :meta => { :"comments-count" => 1 } }
|
||||
data: [
|
||||
{ id: '1337', type: 'posts', meta: { :"comments-count" => 0 } },
|
||||
{ id: '1339', type: 'posts', meta: { :"comments-count" => 1 } }
|
||||
]
|
||||
}
|
||||
assert_equal(expected, hash)
|
||||
|
||||
@@ -70,33 +70,33 @@ module ActiveModelSerializers
|
||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
|
||||
result = adapter.serializable_hash
|
||||
assert_equal({
|
||||
data: {
|
||||
id: '1337',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: 'Title 1',
|
||||
body: 'Body 1',
|
||||
:"publish-at" => @publish_at
|
||||
},
|
||||
relationships: {
|
||||
author: {
|
||||
data: { id: '1', type: 'authors' }
|
||||
},
|
||||
comments: {
|
||||
data: [
|
||||
{ id: '7', type: 'comments' },
|
||||
{ id: '12', type: 'comments' }
|
||||
]
|
||||
}
|
||||
},
|
||||
links: {
|
||||
self: 'http://example.com/posts/1337',
|
||||
:"post-authors" => 'http://example.com/posts/1337/authors',
|
||||
:"subscriber-comments" => 'http://example.com/posts/1337/comments'
|
||||
},
|
||||
meta: { rating: 5, :"favorite-count" => 10 }
|
||||
}
|
||||
}, result)
|
||||
data: {
|
||||
id: '1337',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: 'Title 1',
|
||||
body: 'Body 1',
|
||||
:"publish-at" => @publish_at
|
||||
},
|
||||
relationships: {
|
||||
author: {
|
||||
data: { id: '1', type: 'authors' }
|
||||
},
|
||||
comments: {
|
||||
data: [
|
||||
{ id: '7', type: 'comments' },
|
||||
{ id: '12', type: 'comments' }
|
||||
]
|
||||
}
|
||||
},
|
||||
links: {
|
||||
self: 'http://example.com/posts/1337',
|
||||
:"post-authors" => 'http://example.com/posts/1337/authors',
|
||||
:"subscriber-comments" => 'http://example.com/posts/1337/comments'
|
||||
},
|
||||
meta: { rating: 5, :"favorite-count" => 10 }
|
||||
}
|
||||
}, result)
|
||||
end
|
||||
|
||||
def test_success_document_transform_global_config
|
||||
@@ -107,33 +107,33 @@ module ActiveModelSerializers
|
||||
adapter.serializable_hash
|
||||
end
|
||||
assert_equal({
|
||||
data: {
|
||||
id: '1337',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: 'Title 1',
|
||||
body: 'Body 1',
|
||||
publishAt: @publish_at
|
||||
},
|
||||
relationships: {
|
||||
author: {
|
||||
data: { id: '1', type: 'authors' }
|
||||
},
|
||||
comments: {
|
||||
data: [
|
||||
{ id: '7', type: 'comments' },
|
||||
{ id: '12', type: 'comments' }
|
||||
]
|
||||
}
|
||||
},
|
||||
links: {
|
||||
self: 'http://example.com/posts/1337',
|
||||
postAuthors: 'http://example.com/posts/1337/authors',
|
||||
subscriberComments: 'http://example.com/posts/1337/comments'
|
||||
},
|
||||
meta: { rating: 5, favoriteCount: 10 }
|
||||
}
|
||||
}, result)
|
||||
data: {
|
||||
id: '1337',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: 'Title 1',
|
||||
body: 'Body 1',
|
||||
publishAt: @publish_at
|
||||
},
|
||||
relationships: {
|
||||
author: {
|
||||
data: { id: '1', type: 'authors' }
|
||||
},
|
||||
comments: {
|
||||
data: [
|
||||
{ id: '7', type: 'comments' },
|
||||
{ id: '12', type: 'comments' }
|
||||
]
|
||||
}
|
||||
},
|
||||
links: {
|
||||
self: 'http://example.com/posts/1337',
|
||||
postAuthors: 'http://example.com/posts/1337/authors',
|
||||
subscriberComments: 'http://example.com/posts/1337/comments'
|
||||
},
|
||||
meta: { rating: 5, favoriteCount: 10 }
|
||||
}
|
||||
}, result)
|
||||
end
|
||||
|
||||
def test_success_doc_transform_serialization_ctx_overrides_global
|
||||
@@ -144,33 +144,33 @@ module ActiveModelSerializers
|
||||
adapter.serializable_hash
|
||||
end
|
||||
assert_equal({
|
||||
Data: {
|
||||
Id: '1337',
|
||||
Type: 'Posts',
|
||||
Attributes: {
|
||||
Title: 'Title 1',
|
||||
Body: 'Body 1',
|
||||
PublishAt: @publish_at
|
||||
},
|
||||
Relationships: {
|
||||
Author: {
|
||||
Data: { Id: '1', Type: 'Authors' }
|
||||
},
|
||||
Comments: {
|
||||
Data: [
|
||||
{ Id: '7', Type: 'Comments' },
|
||||
{ Id: '12', Type: 'Comments' }
|
||||
]
|
||||
}
|
||||
},
|
||||
Links: {
|
||||
Self: 'http://example.com/posts/1337',
|
||||
PostAuthors: 'http://example.com/posts/1337/authors',
|
||||
SubscriberComments: 'http://example.com/posts/1337/comments'
|
||||
},
|
||||
Meta: { Rating: 5, FavoriteCount: 10 }
|
||||
}
|
||||
}, result)
|
||||
Data: {
|
||||
Id: '1337',
|
||||
Type: 'Posts',
|
||||
Attributes: {
|
||||
Title: 'Title 1',
|
||||
Body: 'Body 1',
|
||||
PublishAt: @publish_at
|
||||
},
|
||||
Relationships: {
|
||||
Author: {
|
||||
Data: { Id: '1', Type: 'Authors' }
|
||||
},
|
||||
Comments: {
|
||||
Data: [
|
||||
{ Id: '7', Type: 'Comments' },
|
||||
{ Id: '12', Type: 'Comments' }
|
||||
]
|
||||
}
|
||||
},
|
||||
Links: {
|
||||
Self: 'http://example.com/posts/1337',
|
||||
PostAuthors: 'http://example.com/posts/1337/authors',
|
||||
SubscriberComments: 'http://example.com/posts/1337/comments'
|
||||
},
|
||||
Meta: { Rating: 5, FavoriteCount: 10 }
|
||||
}
|
||||
}, result)
|
||||
end
|
||||
|
||||
def test_success_document_transform_dash
|
||||
@@ -179,33 +179,33 @@ module ActiveModelSerializers
|
||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
|
||||
result = adapter.serializable_hash
|
||||
assert_equal({
|
||||
data: {
|
||||
id: '1337',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: 'Title 1',
|
||||
body: 'Body 1',
|
||||
:"publish-at" => @publish_at
|
||||
},
|
||||
relationships: {
|
||||
author: {
|
||||
data: { id: '1', type: 'authors' }
|
||||
},
|
||||
comments: {
|
||||
data: [
|
||||
{ id: '7', type: 'comments' },
|
||||
{ id: '12', type: 'comments' }
|
||||
]
|
||||
}
|
||||
},
|
||||
links: {
|
||||
self: 'http://example.com/posts/1337',
|
||||
:"post-authors" => 'http://example.com/posts/1337/authors',
|
||||
:"subscriber-comments" => 'http://example.com/posts/1337/comments'
|
||||
},
|
||||
meta: { rating: 5, :"favorite-count" => 10 }
|
||||
}
|
||||
}, result)
|
||||
data: {
|
||||
id: '1337',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: 'Title 1',
|
||||
body: 'Body 1',
|
||||
:"publish-at" => @publish_at
|
||||
},
|
||||
relationships: {
|
||||
author: {
|
||||
data: { id: '1', type: 'authors' }
|
||||
},
|
||||
comments: {
|
||||
data: [
|
||||
{ id: '7', type: 'comments' },
|
||||
{ id: '12', type: 'comments' }
|
||||
]
|
||||
}
|
||||
},
|
||||
links: {
|
||||
self: 'http://example.com/posts/1337',
|
||||
:"post-authors" => 'http://example.com/posts/1337/authors',
|
||||
:"subscriber-comments" => 'http://example.com/posts/1337/comments'
|
||||
},
|
||||
meta: { rating: 5, :"favorite-count" => 10 }
|
||||
}
|
||||
}, result)
|
||||
end
|
||||
|
||||
def test_success_document_transform_unaltered
|
||||
@@ -214,33 +214,33 @@ module ActiveModelSerializers
|
||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
|
||||
result = adapter.serializable_hash
|
||||
assert_equal({
|
||||
data: {
|
||||
id: '1337',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: 'Title 1',
|
||||
body: 'Body 1',
|
||||
publish_at: @publish_at
|
||||
},
|
||||
relationships: {
|
||||
author: {
|
||||
data: { id: '1', type: 'authors' }
|
||||
},
|
||||
comments: {
|
||||
data: [
|
||||
{ id: '7', type: 'comments' },
|
||||
{ id: '12', type: 'comments' }
|
||||
]
|
||||
}
|
||||
},
|
||||
links: {
|
||||
self: 'http://example.com/posts/1337',
|
||||
post_authors: 'http://example.com/posts/1337/authors',
|
||||
subscriber_comments: 'http://example.com/posts/1337/comments'
|
||||
},
|
||||
meta: { rating: 5, favorite_count: 10 }
|
||||
}
|
||||
}, result)
|
||||
data: {
|
||||
id: '1337',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: 'Title 1',
|
||||
body: 'Body 1',
|
||||
publish_at: @publish_at
|
||||
},
|
||||
relationships: {
|
||||
author: {
|
||||
data: { id: '1', type: 'authors' }
|
||||
},
|
||||
comments: {
|
||||
data: [
|
||||
{ id: '7', type: 'comments' },
|
||||
{ id: '12', type: 'comments' }
|
||||
]
|
||||
}
|
||||
},
|
||||
links: {
|
||||
self: 'http://example.com/posts/1337',
|
||||
post_authors: 'http://example.com/posts/1337/authors',
|
||||
subscriber_comments: 'http://example.com/posts/1337/comments'
|
||||
},
|
||||
meta: { rating: 5, favorite_count: 10 }
|
||||
}
|
||||
}, result)
|
||||
end
|
||||
|
||||
def test_success_document_transform_undefined
|
||||
@@ -259,33 +259,33 @@ module ActiveModelSerializers
|
||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
|
||||
result = adapter.serializable_hash
|
||||
assert_equal({
|
||||
Data: {
|
||||
Id: '1337',
|
||||
Type: 'Posts',
|
||||
Attributes: {
|
||||
Title: 'Title 1',
|
||||
Body: 'Body 1',
|
||||
PublishAt: @publish_at
|
||||
},
|
||||
Relationships: {
|
||||
Author: {
|
||||
Data: { Id: '1', Type: 'Authors' }
|
||||
},
|
||||
Comments: {
|
||||
Data: [
|
||||
{ Id: '7', Type: 'Comments' },
|
||||
{ Id: '12', Type: 'Comments' }
|
||||
]
|
||||
}
|
||||
},
|
||||
Links: {
|
||||
Self: 'http://example.com/posts/1337',
|
||||
PostAuthors: 'http://example.com/posts/1337/authors',
|
||||
SubscriberComments: 'http://example.com/posts/1337/comments'
|
||||
},
|
||||
Meta: { Rating: 5, FavoriteCount: 10 }
|
||||
}
|
||||
}, result)
|
||||
Data: {
|
||||
Id: '1337',
|
||||
Type: 'Posts',
|
||||
Attributes: {
|
||||
Title: 'Title 1',
|
||||
Body: 'Body 1',
|
||||
PublishAt: @publish_at
|
||||
},
|
||||
Relationships: {
|
||||
Author: {
|
||||
Data: { Id: '1', Type: 'Authors' }
|
||||
},
|
||||
Comments: {
|
||||
Data: [
|
||||
{ Id: '7', Type: 'Comments' },
|
||||
{ Id: '12', Type: 'Comments' }
|
||||
]
|
||||
}
|
||||
},
|
||||
Links: {
|
||||
Self: 'http://example.com/posts/1337',
|
||||
PostAuthors: 'http://example.com/posts/1337/authors',
|
||||
SubscriberComments: 'http://example.com/posts/1337/comments'
|
||||
},
|
||||
Meta: { Rating: 5, FavoriteCount: 10 }
|
||||
}
|
||||
}, result)
|
||||
end
|
||||
|
||||
def test_success_document_transform_camel_lower
|
||||
@@ -294,33 +294,33 @@ module ActiveModelSerializers
|
||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
|
||||
result = adapter.serializable_hash
|
||||
assert_equal({
|
||||
data: {
|
||||
id: '1337',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: 'Title 1',
|
||||
body: 'Body 1',
|
||||
publishAt: @publish_at
|
||||
},
|
||||
relationships: {
|
||||
author: {
|
||||
data: { id: '1', type: 'authors' }
|
||||
},
|
||||
comments: {
|
||||
data: [
|
||||
{ id: '7', type: 'comments' },
|
||||
{ id: '12', type: 'comments' }
|
||||
]
|
||||
}
|
||||
},
|
||||
links: {
|
||||
self: 'http://example.com/posts/1337',
|
||||
postAuthors: 'http://example.com/posts/1337/authors',
|
||||
subscriberComments: 'http://example.com/posts/1337/comments'
|
||||
},
|
||||
meta: { rating: 5, favoriteCount: 10 }
|
||||
}
|
||||
}, result)
|
||||
data: {
|
||||
id: '1337',
|
||||
type: 'posts',
|
||||
attributes: {
|
||||
title: 'Title 1',
|
||||
body: 'Body 1',
|
||||
publishAt: @publish_at
|
||||
},
|
||||
relationships: {
|
||||
author: {
|
||||
data: { id: '1', type: 'authors' }
|
||||
},
|
||||
comments: {
|
||||
data: [
|
||||
{ id: '7', type: 'comments' },
|
||||
{ id: '12', type: 'comments' }
|
||||
]
|
||||
}
|
||||
},
|
||||
links: {
|
||||
self: 'http://example.com/posts/1337',
|
||||
postAuthors: 'http://example.com/posts/1337/authors',
|
||||
subscriberComments: 'http://example.com/posts/1337/comments'
|
||||
},
|
||||
meta: { rating: 5, favoriteCount: 10 }
|
||||
}
|
||||
}, result)
|
||||
end
|
||||
|
||||
def test_error_document_transform_default
|
||||
@@ -332,14 +332,14 @@ module ActiveModelSerializers
|
||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
|
||||
result = adapter.serializable_hash
|
||||
expected_errors_object = {
|
||||
:errors => [
|
||||
errors: [
|
||||
{
|
||||
:source => { :pointer => '/data/attributes/published-at' },
|
||||
:detail => 'must be in the future'
|
||||
source: { pointer: '/data/attributes/published-at' },
|
||||
detail: 'must be in the future'
|
||||
},
|
||||
{
|
||||
:source => { :pointer => '/data/attributes/title' },
|
||||
:detail => 'must be longer'
|
||||
source: { pointer: '/data/attributes/title' },
|
||||
detail: 'must be longer'
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -357,14 +357,14 @@ module ActiveModelSerializers
|
||||
adapter.serializable_hash
|
||||
end
|
||||
expected_errors_object = {
|
||||
:Errors => [
|
||||
Errors: [
|
||||
{
|
||||
:Source => { :Pointer => '/data/attributes/PublishedAt' },
|
||||
:Detail => 'must be in the future'
|
||||
Source: { Pointer: '/data/attributes/PublishedAt' },
|
||||
Detail: 'must be in the future'
|
||||
},
|
||||
{
|
||||
:Source => { :Pointer => '/data/attributes/Title' },
|
||||
:Detail => 'must be longer'
|
||||
Source: { Pointer: '/data/attributes/Title' },
|
||||
Detail: 'must be longer'
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -382,14 +382,14 @@ module ActiveModelSerializers
|
||||
adapter.serializable_hash
|
||||
end
|
||||
expected_errors_object = {
|
||||
:Errors => [
|
||||
Errors: [
|
||||
{
|
||||
:Source => { :Pointer => '/data/attributes/PublishedAt' },
|
||||
:Detail => 'must be in the future'
|
||||
Source: { Pointer: '/data/attributes/PublishedAt' },
|
||||
Detail: 'must be in the future'
|
||||
},
|
||||
{
|
||||
:Source => { :Pointer => '/data/attributes/Title' },
|
||||
:Detail => 'must be longer'
|
||||
Source: { Pointer: '/data/attributes/Title' },
|
||||
Detail: 'must be longer'
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -408,14 +408,14 @@ module ActiveModelSerializers
|
||||
result = adapter.serializable_hash
|
||||
|
||||
expected_errors_object = {
|
||||
:errors => [
|
||||
errors: [
|
||||
{
|
||||
:source => { :pointer => '/data/attributes/published-at' },
|
||||
:detail => 'must be in the future'
|
||||
source: { pointer: '/data/attributes/published-at' },
|
||||
detail: 'must be in the future'
|
||||
},
|
||||
{
|
||||
:source => { :pointer => '/data/attributes/title' },
|
||||
:detail => 'must be longer'
|
||||
source: { pointer: '/data/attributes/title' },
|
||||
detail: 'must be longer'
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -434,9 +434,9 @@ module ActiveModelSerializers
|
||||
result = adapter.serializable_hash
|
||||
|
||||
expected_errors_object = {
|
||||
:errors => [
|
||||
{ :source => { :pointer => '/data/attributes/published_at' }, :detail => 'must be in the future' },
|
||||
{ :source => { :pointer => '/data/attributes/title' }, :detail => 'must be longer' }
|
||||
errors: [
|
||||
{ source: { pointer: '/data/attributes/published_at' }, detail: 'must be in the future' },
|
||||
{ source: { pointer: '/data/attributes/title' }, detail: 'must be longer' }
|
||||
]
|
||||
}
|
||||
assert_equal expected_errors_object, result
|
||||
@@ -470,9 +470,9 @@ module ActiveModelSerializers
|
||||
result = adapter.serializable_hash
|
||||
|
||||
expected_errors_object = {
|
||||
:Errors => [
|
||||
{ :Source => { :Pointer => '/data/attributes/PublishedAt' }, :Detail => 'must be in the future' },
|
||||
{ :Source => { :Pointer => '/data/attributes/Title' }, :Detail => 'must be longer' }
|
||||
Errors: [
|
||||
{ Source: { Pointer: '/data/attributes/PublishedAt' }, Detail: 'must be in the future' },
|
||||
{ Source: { Pointer: '/data/attributes/Title' }, Detail: 'must be longer' }
|
||||
]
|
||||
}
|
||||
assert_equal expected_errors_object, result
|
||||
@@ -490,9 +490,9 @@ module ActiveModelSerializers
|
||||
result = adapter.serializable_hash
|
||||
|
||||
expected_errors_object = {
|
||||
:errors => [
|
||||
{ :source => { :pointer => '/data/attributes/publishedAt' }, :detail => 'must be in the future' },
|
||||
{ :source => { :pointer => '/data/attributes/title' }, :detail => 'must be longer' }
|
||||
errors: [
|
||||
{ source: { pointer: '/data/attributes/publishedAt' }, detail: 'must be in the future' },
|
||||
{ source: { pointer: '/data/attributes/title' }, detail: 'must be longer' }
|
||||
]
|
||||
}
|
||||
assert_equal expected_errors_object, result
|
||||
|
||||
@@ -47,7 +47,7 @@ module ActiveModel
|
||||
assert_equal(expected_type, hash.fetch(:data).fetch(:type))
|
||||
end
|
||||
|
||||
def with_jsonapi_resource_type inflection
|
||||
def with_jsonapi_resource_type(inflection)
|
||||
old_inflection = ActiveModelSerializers.config.jsonapi_resource_type
|
||||
ActiveModelSerializers.config.jsonapi_resource_type = inflection
|
||||
yield
|
||||
|
||||
@@ -32,14 +32,14 @@ module ActiveModelSerializers
|
||||
adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
|
||||
|
||||
assert_equal({
|
||||
id: 1,
|
||||
reviews: [
|
||||
{ id: 1, body: 'ZOMG A COMMENT' },
|
||||
{ id: 2, body: 'ZOMG ANOTHER COMMENT' }
|
||||
],
|
||||
writer: { id: 1, name: 'Steve K.' },
|
||||
site: { id: 1, name: 'My Blog!!' }
|
||||
}, adapter.serializable_hash[:post])
|
||||
id: 1,
|
||||
reviews: [
|
||||
{ id: 1, body: 'ZOMG A COMMENT' },
|
||||
{ id: 2, body: 'ZOMG ANOTHER COMMENT' }
|
||||
],
|
||||
writer: { id: 1, name: 'Steve K.' },
|
||||
site: { id: 1, name: 'My Blog!!' }
|
||||
}, adapter.serializable_hash[:post])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ module ActiveModelSerializers
|
||||
module Adapter
|
||||
class NullTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||
serializer = ProfileSerializer.new(profile)
|
||||
|
||||
@adapter = Null.new(serializer)
|
||||
@@ -20,4 +20,3 @@ module ActiveModelSerializers
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -27,11 +27,11 @@ module ActiveModel
|
||||
id: 1,
|
||||
title: 'headshot-1.jpg',
|
||||
imageable: {
|
||||
type: 'employee',
|
||||
employee: {
|
||||
id: 42,
|
||||
name: 'Zoop Zoopler'
|
||||
}
|
||||
type: 'employee',
|
||||
employee: {
|
||||
id: 42,
|
||||
name: 'Zoop Zoopler'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def test_create_adapter_with_override
|
||||
adapter = ActiveModelSerializers::Adapter.create(@serializer, { adapter: :json_api })
|
||||
adapter = ActiveModelSerializers::Adapter.create(@serializer, adapter: :json_api)
|
||||
assert_equal ActiveModelSerializers::Adapter::JsonApi, adapter.class
|
||||
end
|
||||
|
||||
|
||||
@@ -86,7 +86,7 @@ class ApiAssertion
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def assert_equal(expected, actual, message)
|
||||
|
||||
@@ -101,7 +101,7 @@ module ActiveModelSerializers
|
||||
uncached_author_serializer = AuthorSerializer.new(uncached_author)
|
||||
|
||||
render_object_with_cache(uncached_author)
|
||||
key = "#{uncached_author_serializer.class._cache_key}/#{uncached_author_serializer.object.id}-#{uncached_author_serializer.object.updated_at.strftime("%Y%m%d%H%M%S%9N")}"
|
||||
key = "#{uncached_author_serializer.class._cache_key}/#{uncached_author_serializer.object.id}-#{uncached_author_serializer.object.updated_at.strftime('%Y%m%d%H%M%S%9N')}"
|
||||
key = "#{key}/#{adapter.cache_key}"
|
||||
assert_equal(uncached_author_serializer.attributes.to_json, cache_store.fetch(key).to_json)
|
||||
end
|
||||
|
||||
@@ -11,7 +11,7 @@ module ActiveModel
|
||||
@comment = Comment.new
|
||||
@post = Post.new
|
||||
@resource = build_named_collection @comment, @post
|
||||
@serializer = collection_serializer.new(@resource, { some: :options })
|
||||
@serializer = collection_serializer.new(@resource, some: :options)
|
||||
end
|
||||
|
||||
def collection_serializer
|
||||
@@ -44,7 +44,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_serializer_option_not_passed_to_each_serializer
|
||||
serializers = collection_serializer.new([@post], { serializer: PostSerializer }).to_a
|
||||
serializers = collection_serializer.new([@post], serializer: PostSerializer).to_a
|
||||
|
||||
refute serializers.first.custom_options.key?(:serializer)
|
||||
end
|
||||
|
||||
4
test/fixtures/poro.rb
vendored
4
test/fixtures/poro.rb
vendored
@@ -8,7 +8,7 @@ class Model < ActiveModelSerializers::Model
|
||||
# Convenience when not adding @attributes readers and writers
|
||||
def method_missing(meth, *args)
|
||||
if meth.to_s =~ /^(.*)=$/
|
||||
attributes[$1.to_sym] = args[0]
|
||||
attributes[Regexp.last_match(1).to_sym] = args[0]
|
||||
elsif attributes.key?(meth)
|
||||
attributes[meth]
|
||||
else
|
||||
@@ -64,7 +64,7 @@ VirtualValue = Class.new(Model)
|
||||
Comment = Class.new(Model) do
|
||||
# Uses a custom non-time-based cache key
|
||||
def cache_key
|
||||
"#{self.class.name.downcase}/#{self.id}"
|
||||
"#{self.class.name.downcase}/#{id}"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -5,172 +5,174 @@ require 'kaminari'
|
||||
require 'kaminari/hooks'
|
||||
::Kaminari::Hooks.init
|
||||
|
||||
class ActiveModelSerializers::GrapeTest < ActiveSupport::TestCase
|
||||
include Rack::Test::Methods
|
||||
module Models
|
||||
def self.model1
|
||||
ARModels::Post.new(id: 1, title: 'Dummy Title', body: 'Lorem Ipsum')
|
||||
end
|
||||
module ActiveModelSerializers
|
||||
class GrapeTest < ActiveSupport::TestCase
|
||||
include Rack::Test::Methods
|
||||
module Models
|
||||
def self.model1
|
||||
ARModels::Post.new(id: 1, title: 'Dummy Title', body: 'Lorem Ipsum')
|
||||
end
|
||||
|
||||
def self.model2
|
||||
ARModels::Post.new(id: 2, title: 'Second Dummy Title', body: 'Second Lorem Ipsum')
|
||||
end
|
||||
def self.model2
|
||||
ARModels::Post.new(id: 2, title: 'Second Dummy Title', body: 'Second Lorem Ipsum')
|
||||
end
|
||||
|
||||
def self.all
|
||||
@all ||=
|
||||
begin
|
||||
model1.save!
|
||||
model2.save!
|
||||
ARModels::Post.all
|
||||
end
|
||||
end
|
||||
def self.all
|
||||
@all ||=
|
||||
begin
|
||||
model1.save!
|
||||
model2.save!
|
||||
ARModels::Post.all
|
||||
end
|
||||
end
|
||||
|
||||
def self.reset_all
|
||||
ARModels::Post.delete_all
|
||||
@all = nil
|
||||
end
|
||||
def self.reset_all
|
||||
ARModels::Post.delete_all
|
||||
@all = nil
|
||||
end
|
||||
|
||||
def self.collection_per
|
||||
2
|
||||
end
|
||||
def self.collection_per
|
||||
2
|
||||
end
|
||||
|
||||
def self.collection
|
||||
@collection ||=
|
||||
begin
|
||||
Kaminari.paginate_array(
|
||||
[
|
||||
Profile.new(id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
||||
Profile.new(id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2'),
|
||||
Profile.new(id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3'),
|
||||
Profile.new(id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4'),
|
||||
Profile.new(id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5')
|
||||
def self.collection
|
||||
@collection ||=
|
||||
begin
|
||||
Kaminari.paginate_array(
|
||||
[
|
||||
Profile.new(id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
||||
Profile.new(id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2'),
|
||||
Profile.new(id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3'),
|
||||
Profile.new(id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4'),
|
||||
Profile.new(id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5')
|
||||
]
|
||||
).page(1).per(collection_per)
|
||||
).page(1).per(collection_per)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class GrapeTest < Grape::API
|
||||
format :json
|
||||
include Grape::ActiveModelSerializers
|
||||
|
||||
resources :grape do
|
||||
get '/render' do
|
||||
render Models.model1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class GrapeTest < Grape::API
|
||||
format :json
|
||||
include Grape::ActiveModelSerializers
|
||||
get '/render_with_json_api' do
|
||||
post = Models.model1
|
||||
render post, meta: { page: 1, total_pages: 2 }, adapter: :json_api
|
||||
end
|
||||
|
||||
resources :grape do
|
||||
get '/render' do
|
||||
render Models.model1
|
||||
end
|
||||
get '/render_array_with_json_api' do
|
||||
posts = Models.all
|
||||
render posts, adapter: :json_api
|
||||
end
|
||||
|
||||
get '/render_with_json_api' do
|
||||
post = Models.model1
|
||||
render post, meta: { page: 1, total_pages: 2 }, adapter: :json_api
|
||||
end
|
||||
get '/render_collection_with_json_api' do
|
||||
posts = Models.collection
|
||||
render posts, adapter: :json_api
|
||||
end
|
||||
|
||||
get '/render_array_with_json_api' do
|
||||
posts = Models.all
|
||||
render posts, adapter: :json_api
|
||||
end
|
||||
get '/render_with_implicit_formatter' do
|
||||
Models.model1
|
||||
end
|
||||
|
||||
get '/render_collection_with_json_api' do
|
||||
posts = Models.collection
|
||||
render posts, adapter: :json_api
|
||||
end
|
||||
get '/render_array_with_implicit_formatter' do
|
||||
Models.all
|
||||
end
|
||||
|
||||
get '/render_with_implicit_formatter' do
|
||||
Models.model1
|
||||
end
|
||||
|
||||
get '/render_array_with_implicit_formatter' do
|
||||
Models.all
|
||||
end
|
||||
|
||||
get '/render_collection_with_implicit_formatter' do
|
||||
Models.collection
|
||||
get '/render_collection_with_implicit_formatter' do
|
||||
Models.collection
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def app
|
||||
Grape::Middleware::Globals.new(GrapeTest.new)
|
||||
end
|
||||
|
||||
def test_formatter_returns_json
|
||||
get '/grape/render'
|
||||
|
||||
post = Models.model1
|
||||
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 = Models.model1
|
||||
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'
|
||||
|
||||
posts = Models.all
|
||||
serializable_resource = serializable(posts, adapter: :json_api)
|
||||
|
||||
assert last_response.ok?
|
||||
assert_equal serializable_resource.to_json, last_response.body
|
||||
ensure
|
||||
Models.reset_all
|
||||
end
|
||||
|
||||
def test_formatter_handles_collections
|
||||
get '/grape/render_collection_with_json_api'
|
||||
assert last_response.ok?
|
||||
|
||||
representation = JSON.parse(last_response.body)
|
||||
assert representation.include?('data')
|
||||
assert representation['data'].count == Models.collection_per
|
||||
assert representation.include?('links')
|
||||
assert representation['links'].count > 0
|
||||
end
|
||||
|
||||
def test_implicit_formatter
|
||||
post = Models.model1
|
||||
serializable_resource = serializable(post, adapter: :json_api)
|
||||
|
||||
with_adapter :json_api do
|
||||
get '/grape/render_with_implicit_formatter'
|
||||
def app
|
||||
Grape::Middleware::Globals.new(GrapeTest.new)
|
||||
end
|
||||
|
||||
assert last_response.ok?
|
||||
assert_equal serializable_resource.to_json, last_response.body
|
||||
end
|
||||
def test_formatter_returns_json
|
||||
get '/grape/render'
|
||||
|
||||
def test_implicit_formatter_handles_arrays
|
||||
posts = Models.all
|
||||
serializable_resource = serializable(posts, adapter: :json_api)
|
||||
post = Models.model1
|
||||
serializable_resource = serializable(post)
|
||||
|
||||
with_adapter :json_api do
|
||||
get '/grape/render_array_with_implicit_formatter'
|
||||
assert last_response.ok?
|
||||
assert_equal serializable_resource.to_json, last_response.body
|
||||
end
|
||||
|
||||
assert last_response.ok?
|
||||
assert_equal serializable_resource.to_json, last_response.body
|
||||
ensure
|
||||
Models.reset_all
|
||||
end
|
||||
def test_render_helper_passes_through_options_correctly
|
||||
get '/grape/render_with_json_api'
|
||||
|
||||
def test_implicit_formatter_handles_collections
|
||||
with_adapter :json_api do
|
||||
get '/grape/render_collection_with_implicit_formatter'
|
||||
post = Models.model1
|
||||
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
|
||||
|
||||
representation = JSON.parse(last_response.body)
|
||||
assert last_response.ok?
|
||||
assert representation.include?('data')
|
||||
assert representation['data'].count == Models.collection_per
|
||||
assert representation.include?('links')
|
||||
assert representation['links'].count > 0
|
||||
def test_formatter_handles_arrays
|
||||
get '/grape/render_array_with_json_api'
|
||||
|
||||
posts = Models.all
|
||||
serializable_resource = serializable(posts, adapter: :json_api)
|
||||
|
||||
assert last_response.ok?
|
||||
assert_equal serializable_resource.to_json, last_response.body
|
||||
ensure
|
||||
Models.reset_all
|
||||
end
|
||||
|
||||
def test_formatter_handles_collections
|
||||
get '/grape/render_collection_with_json_api'
|
||||
assert last_response.ok?
|
||||
|
||||
representation = JSON.parse(last_response.body)
|
||||
assert representation.include?('data')
|
||||
assert representation['data'].count == Models.collection_per
|
||||
assert representation.include?('links')
|
||||
assert representation['links'].count > 0
|
||||
end
|
||||
|
||||
def test_implicit_formatter
|
||||
post = Models.model1
|
||||
serializable_resource = serializable(post, adapter: :json_api)
|
||||
|
||||
with_adapter :json_api do
|
||||
get '/grape/render_with_implicit_formatter'
|
||||
end
|
||||
|
||||
assert last_response.ok?
|
||||
assert_equal serializable_resource.to_json, last_response.body
|
||||
end
|
||||
|
||||
def test_implicit_formatter_handles_arrays
|
||||
posts = Models.all
|
||||
serializable_resource = serializable(posts, adapter: :json_api)
|
||||
|
||||
with_adapter :json_api do
|
||||
get '/grape/render_array_with_implicit_formatter'
|
||||
end
|
||||
|
||||
assert last_response.ok?
|
||||
assert_equal serializable_resource.to_json, last_response.body
|
||||
ensure
|
||||
Models.reset_all
|
||||
end
|
||||
|
||||
def test_implicit_formatter_handles_collections
|
||||
with_adapter :json_api do
|
||||
get '/grape/render_collection_with_implicit_formatter'
|
||||
end
|
||||
|
||||
representation = JSON.parse(last_response.body)
|
||||
assert last_response.ok?
|
||||
assert representation.include?('data')
|
||||
assert representation['data'].count == Models.collection_per
|
||||
assert representation.include?('links')
|
||||
assert representation['links'].count > 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -30,7 +30,7 @@ module ActiveModel
|
||||
def errors
|
||||
end
|
||||
|
||||
def self.human_attribute_name(attr, options = {})
|
||||
def self.human_attribute_name(_, _ = {})
|
||||
end
|
||||
|
||||
def self.lookup_ancestors
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ActiveModelSerializers::LoggerTest < ActiveSupport::TestCase
|
||||
def test_logger_is_set_to_action_controller_logger_when_initializer_runs
|
||||
assert_equal $action_controller_logger, ActionController::Base.logger # rubocop:disable Style/GlobalVars
|
||||
end
|
||||
module ActiveModelSerializers
|
||||
class LoggerTest < ActiveSupport::TestCase
|
||||
def test_logger_is_set_to_action_controller_logger_when_initializer_runs
|
||||
assert_equal $action_controller_logger, ActionController::Base.logger # rubocop:disable Style/GlobalVars
|
||||
end
|
||||
|
||||
def test_logger_can_be_set
|
||||
original_logger = ActiveModelSerializers.logger
|
||||
logger = Logger.new(STDOUT)
|
||||
def test_logger_can_be_set
|
||||
original_logger = ActiveModelSerializers.logger
|
||||
logger = Logger.new(STDOUT)
|
||||
|
||||
ActiveModelSerializers.logger = logger
|
||||
ActiveModelSerializers.logger = logger
|
||||
|
||||
assert_equal ActiveModelSerializers.logger, logger
|
||||
ensure
|
||||
ActiveModelSerializers.logger = original_logger
|
||||
assert_equal ActiveModelSerializers.logger, logger
|
||||
ensure
|
||||
ActiveModelSerializers.logger = original_logger
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,7 +3,7 @@ require 'test_helper'
|
||||
module ActiveModelSerializers
|
||||
class SerializableResourceTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
@resource = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@resource = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||
@serializer = ProfileSerializer.new(@resource)
|
||||
@adapter = ActiveModelSerializers::Adapter.create(@serializer)
|
||||
@serializable_resource = SerializableResource.new(@resource)
|
||||
@@ -32,11 +32,11 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def test_use_adapter_with_adapter_option
|
||||
assert SerializableResource.new(@resource, { adapter: 'json' }).use_adapter?
|
||||
assert SerializableResource.new(@resource, adapter: 'json').use_adapter?
|
||||
end
|
||||
|
||||
def test_use_adapter_with_adapter_option_as_false
|
||||
refute SerializableResource.new(@resource, { adapter: false }).use_adapter?
|
||||
refute SerializableResource.new(@resource, adapter: false).use_adapter?
|
||||
end
|
||||
|
||||
class SerializableResourceErrorsTest < Minitest::Test
|
||||
@@ -45,14 +45,12 @@ module ActiveModelSerializers
|
||||
resource = ModelWithErrors.new
|
||||
resource.errors.add(:name, 'must be awesome')
|
||||
serializable_resource = ActiveModelSerializers::SerializableResource.new(
|
||||
resource, {
|
||||
serializer: ActiveModel::Serializer::ErrorSerializer,
|
||||
adapter: :json_api
|
||||
}
|
||||
resource, serializer: ActiveModel::Serializer::ErrorSerializer,
|
||||
adapter: :json_api
|
||||
)
|
||||
expected_response_document = {
|
||||
:errors => [
|
||||
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'must be awesome' }
|
||||
errors: [
|
||||
{ source: { pointer: '/data/attributes/name' }, detail: 'must be awesome' }
|
||||
]
|
||||
}
|
||||
assert_equal serializable_resource.as_json(options), expected_response_document
|
||||
@@ -65,15 +63,13 @@ module ActiveModelSerializers
|
||||
resource.errors.add(:title, 'must be amazing')
|
||||
resources << ModelWithErrors.new
|
||||
serializable_resource = SerializableResource.new(
|
||||
resources, {
|
||||
serializer: ActiveModel::Serializer::ErrorsSerializer,
|
||||
each_serializer: ActiveModel::Serializer::ErrorSerializer,
|
||||
adapter: :json_api
|
||||
}
|
||||
resources, serializer: ActiveModel::Serializer::ErrorsSerializer,
|
||||
each_serializer: ActiveModel::Serializer::ErrorSerializer,
|
||||
adapter: :json_api
|
||||
)
|
||||
expected_response_document = {
|
||||
:errors => [
|
||||
{ :source => { :pointer => '/data/attributes/title' }, :detail => 'must be amazing' }
|
||||
errors: [
|
||||
{ source: { pointer: '/data/attributes/title' }, detail: 'must be amazing' }
|
||||
]
|
||||
}
|
||||
assert_equal serializable_resource.as_json(options), expected_response_document
|
||||
|
||||
@@ -7,10 +7,10 @@ module ActiveModel
|
||||
@author = Author.new(name: 'Steve K.')
|
||||
@author.bio = nil
|
||||
@author.roles = []
|
||||
@blog = Blog.new({ name: 'AMS Blog' })
|
||||
@post = Post.new({ title: 'New Post', body: 'Body' })
|
||||
@tag = Tag.new({ name: '#hashtagged' })
|
||||
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
||||
@blog = Blog.new(name: 'AMS Blog')
|
||||
@post = Post.new(title: 'New Post', body: 'Body')
|
||||
@tag = Tag.new(name: '#hashtagged')
|
||||
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@post.comments = [@comment]
|
||||
@post.tags = [@tag]
|
||||
@post.blog = @blog
|
||||
@@ -19,7 +19,7 @@ module ActiveModel
|
||||
@post.author = @author
|
||||
@author.posts = [@post]
|
||||
|
||||
@post_serializer = PostSerializer.new(@post, { custom_options: true })
|
||||
@post_serializer = PostSerializer.new(@post, custom_options: true)
|
||||
@author_serializer = AuthorSerializer.new(@author)
|
||||
@comment_serializer = CommentSerializer.new(@comment)
|
||||
end
|
||||
@@ -143,12 +143,12 @@ module ActiveModel
|
||||
)
|
||||
actual = serializable(post, adapter: :attributes, serializer: InlineAssociationTestPostSerializer).as_json
|
||||
expected = {
|
||||
:comments => [
|
||||
{ :id => 1, :contents => 'first comment' },
|
||||
{ :id => 2, :contents => 'last comment' }
|
||||
comments: [
|
||||
{ id: 1, contents: 'first comment' },
|
||||
{ id: 2, contents: 'last comment' }
|
||||
],
|
||||
:last_comments => [
|
||||
{ :id => 2, :contents => 'last comment' }
|
||||
last_comments: [
|
||||
{ id: 2, contents: 'last comment' }
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ module ActiveModel
|
||||
inherited_klass = Class.new(AlternateBlogSerializer)
|
||||
blog_serializer = inherited_klass.new(@blog)
|
||||
adapter = ActiveModelSerializers::Adapter::Attributes.new(blog_serializer)
|
||||
assert_equal({ :id => 1, :title => 'AMS Hints' }, adapter.serializable_hash)
|
||||
assert_equal({ id: 1, title: 'AMS Hints' }, adapter.serializable_hash)
|
||||
end
|
||||
|
||||
def test_multiple_calls_with_the_same_attribute
|
||||
|
||||
@@ -4,7 +4,7 @@ module ActiveModel
|
||||
class Serializer
|
||||
class AttributesTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||
@profile_serializer = ProfileSerializer.new(@profile)
|
||||
@comment = Comment.new(id: 1, body: 'ZOMG!!', date: '2015')
|
||||
@serializer_klass = Class.new(CommentSerializer)
|
||||
|
||||
@@ -5,7 +5,7 @@ module ActiveModel
|
||||
class FieldsetTest < ActiveSupport::TestCase
|
||||
def test_fieldset_with_hash
|
||||
fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body'])
|
||||
expected = { :post => [:id, :title], :comment => [:body] }
|
||||
expected = { post: [:id, :title], comment: [:body] }
|
||||
|
||||
assert_equal(expected, fieldset.fields)
|
||||
end
|
||||
|
||||
@@ -8,7 +8,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_overwrite_root
|
||||
serializer = VirtualValueSerializer.new(@virtual_value, { root: 'smth' })
|
||||
serializer = VirtualValueSerializer.new(@virtual_value, root: 'smth')
|
||||
assert_equal('smth', serializer.json_key)
|
||||
end
|
||||
|
||||
|
||||
@@ -41,7 +41,9 @@ module ActiveModel
|
||||
end
|
||||
|
||||
class CustomProfile
|
||||
def serializer_class; ProfileSerializer; end
|
||||
def serializer_class
|
||||
ProfileSerializer
|
||||
end
|
||||
end
|
||||
|
||||
Tweet = Class.new(::Model)
|
||||
|
||||
@@ -75,6 +75,8 @@ module TestHelpers
|
||||
end
|
||||
end
|
||||
|
||||
class ActiveSupport::TestCase
|
||||
include TestHelpers::Generation
|
||||
module ActiveSupport
|
||||
class TestCase
|
||||
include TestHelpers::Generation
|
||||
end
|
||||
end
|
||||
|
||||
@@ -44,10 +44,12 @@ module SerializationTesting
|
||||
end
|
||||
end
|
||||
|
||||
class Minitest::Test
|
||||
def before_setup
|
||||
ActionController::Base.cache_store.clear
|
||||
end
|
||||
module Minitest
|
||||
class Test
|
||||
def before_setup
|
||||
ActionController::Base.cache_store.clear
|
||||
end
|
||||
|
||||
include SerializationTesting
|
||||
include SerializationTesting
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user