mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 22:36:50 +00:00
Changed the namespace in adapters and folder to active_model_serializers from active_model::serializer Changed namespace of adapters in serializers and other folders Moved adapter_for_test file to active_model_serializers folder and changed namespace of adapter inside the test file Require ActiveSupport's string/inflections We depend on string/inflections to define String#underscore. Refactor JsonApi adapter to avoid redundant computations. Update readme.md to link to v0.10.0.rc4 changed namespace of adapter folder testcases Changed all namespaces of adapter under active_moder_serializers Namespaced IncludeTree which is from serializer module, so needed to namespace it properly Fixed wrong namsepacing of fieldset namespace change in deserializer json_api Fixed the namespace for collection serializer when used inside adapter, changed namespace for adapter to new namespace which I had forgotten previously Modified logging test and adapter test cases to make the testcases pass Changed the yardoc links,as old links are not taking to documentation pages,proper links for 0.10,0.9 and 0.8 in rubydoc Rubocop errors are fixed by underscore naming unused variables Moved the require of adapter to serializable resource Remoeved adapter dependency inside serializer and added warning to Serializer::adapter method Fixed frament cache test which is calling Serializer.adapter Changed the name of lookup_adapter_from_config to configured_adapter Changed the docs which will show the new namespace of adapters Rubocop fix
138 lines
4.9 KiB
Ruby
138 lines
4.9 KiB
Ruby
require 'test_helper'
|
|
module ActiveModelSerializers
|
|
module Adapter
|
|
class JsonApi
|
|
module Deserialization
|
|
class ParseTest < Minitest::Test
|
|
def setup
|
|
@hash = {
|
|
'data' => {
|
|
'type' => 'photos',
|
|
'id' => 'zorglub',
|
|
'attributes' => {
|
|
'title' => 'Ember Hamster',
|
|
'src' => 'http://example.com/images/productivity.png'
|
|
},
|
|
'relationships' => {
|
|
'author' => {
|
|
'data' => nil
|
|
},
|
|
'photographer' => {
|
|
'data' => { 'type' => 'people', 'id' => '9' }
|
|
},
|
|
'comments' => {
|
|
'data' => [
|
|
{ 'type' => 'comments', 'id' => '1' },
|
|
{ 'type' => 'comments', 'id' => '2' }
|
|
]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@params = ActionController::Parameters.new(@hash)
|
|
@expected = {
|
|
id: 'zorglub',
|
|
title: 'Ember Hamster',
|
|
src: 'http://example.com/images/productivity.png',
|
|
author_id: nil,
|
|
photographer_id: '9',
|
|
comment_ids: %w(1 2)
|
|
}
|
|
|
|
@illformed_payloads = [nil,
|
|
{},
|
|
{
|
|
'data' => nil
|
|
}, {
|
|
'data' => { 'attributes' => [] }
|
|
}, {
|
|
'data' => { 'relationships' => [] }
|
|
}, {
|
|
'data' => {
|
|
'relationships' => { 'rel' => nil }
|
|
}
|
|
}, {
|
|
'data' => {
|
|
'relationships' => { 'rel' => {} }
|
|
}
|
|
}]
|
|
end
|
|
|
|
def test_hash
|
|
parsed_hash = ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse!(@hash)
|
|
assert_equal(@expected, parsed_hash)
|
|
end
|
|
|
|
def test_actioncontroller_parameters
|
|
assert_equal(false, @params.permitted?)
|
|
parsed_hash = ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse!(@params)
|
|
assert_equal(@expected, parsed_hash)
|
|
end
|
|
|
|
def test_illformed_payloads_safe
|
|
@illformed_payloads.each do |p|
|
|
parsed_hash = ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse(p)
|
|
assert_equal({}, parsed_hash)
|
|
end
|
|
end
|
|
|
|
def test_illformed_payloads_unsafe
|
|
@illformed_payloads.each do |p|
|
|
assert_raises(InvalidDocument) do
|
|
ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse!(p)
|
|
end
|
|
end
|
|
end
|
|
|
|
def test_filter_fields_only
|
|
parsed_hash = ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse!(@hash, only: [:id, :title, :author])
|
|
expected = {
|
|
id: 'zorglub',
|
|
title: 'Ember Hamster',
|
|
author_id: nil
|
|
}
|
|
assert_equal(expected, parsed_hash)
|
|
end
|
|
|
|
def test_filter_fields_except
|
|
parsed_hash = ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse!(@hash, except: [:id, :title, :author])
|
|
expected = {
|
|
src: 'http://example.com/images/productivity.png',
|
|
photographer_id: '9',
|
|
comment_ids: %w(1 2)
|
|
}
|
|
assert_equal(expected, parsed_hash)
|
|
end
|
|
|
|
def test_keys
|
|
parsed_hash = ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse!(@hash, keys: { author: :user, title: :post_title })
|
|
expected = {
|
|
id: 'zorglub',
|
|
post_title: 'Ember Hamster',
|
|
src: 'http://example.com/images/productivity.png',
|
|
user_id: nil,
|
|
photographer_id: '9',
|
|
comment_ids: %w(1 2)
|
|
}
|
|
assert_equal(expected, parsed_hash)
|
|
end
|
|
|
|
def test_polymorphic
|
|
parsed_hash = ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse!(@hash, polymorphic: [:photographer])
|
|
expected = {
|
|
id: 'zorglub',
|
|
title: 'Ember Hamster',
|
|
src: 'http://example.com/images/productivity.png',
|
|
author_id: nil,
|
|
photographer_id: '9',
|
|
photographer_type: 'people',
|
|
comment_ids: %w(1 2)
|
|
}
|
|
assert_equal(expected, parsed_hash)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|