mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Add support for JSON API deserialization (experimental).
This commit is contained in:
139
test/adapter/json_api/parse_test.rb
Normal file
139
test/adapter/json_api/parse_test.rb
Normal file
@@ -0,0 +1,139 @@
|
||||
require 'test_helper'
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
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 = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash)
|
||||
assert_equal(@expected, parsed_hash)
|
||||
end
|
||||
|
||||
def test_actioncontroller_parameters
|
||||
assert_equal(false, @params.permitted?)
|
||||
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@params)
|
||||
assert_equal(@expected, parsed_hash)
|
||||
end
|
||||
|
||||
def test_illformed_payloads_safe
|
||||
@illformed_payloads.each do |p|
|
||||
parsed_hash = ActiveModel::Serializer::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
|
||||
ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(p)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_filter_fields_only
|
||||
parsed_hash = ActiveModel::Serializer::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 = ActiveModel::Serializer::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 = ActiveModel::Serializer::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 = ActiveModel::Serializer::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
|
||||
end
|
||||
Reference in New Issue
Block a user