active_model_serializers/test/adapter/json_api/errors_test.rb
Benjamin Fleischer 3d986377b6 Collapse JSON API success/failure documents in one adapter
Idea per remear (Ben Mills) in the slack:
https://amserializers.slack.com/archives/general/p1455140474000171

remear:

    just so i understand, the adapter in `render json: resource, status: 422, adapter: 'json_api/error',
    serializer: ActiveModel::Serializer::ErrorSerializer` is a different one than, say what i’ve
    specified in a base serializer with `ActiveModel::Serializer.config.adapter = :json_api`. correct?

    and a followup question of, why not same adapter but different serializer?

me:

   With the way the code is written now, it might be possible to not require a special jsonapi adapter.
   However, the behavior is pretty different from the jsonapi adapter.

   this first draft of the PR had it automatically set the adapter if there were errors.  since that
   requires more discussion, I took a step back and made it explicit for this PR

   If I were to re-use the json api adapter and remove the error one, it think the serializable hash
   method would look like

   ```
   def serializable_hash(options = nil)
     return { errors: JsonApi::Error.collection_errors } if serializer.is_a?(ErrorsSerializer)
     return { errors: JsonApi::Error.resource_errors(serializer) } if serializer.is_a?(ErrorSerializer)
     options ||= {}
   ```

   I suppose it could be something more duckish like

   ```
   def serializable_hash(options = nil)
     if serializer.errors? # object.errors.any? || object.any? {|o| o.errors.any? }
       JsonApi::Error.new(serializer).serializable_hash
     else
       # etc
   ```
2016-03-06 12:03:17 -06:00

79 lines
2.7 KiB
Ruby

require 'test_helper'
module ActiveModelSerializers
module Adapter
class JsonApi < Base
class ErrorsTest < Minitest::Test
include ActiveModel::Serializer::Lint::Tests
def setup
@resource = ModelWithErrors.new
end
def test_active_model_with_error
options = {
serializer: ActiveModel::Serializer::ErrorSerializer,
adapter: :json_api
}
@resource.errors.add(:name, 'cannot be nil')
serializable_resource = ActiveModel::SerializableResource.new(@resource, options)
assert_equal serializable_resource.serializer_instance.attributes, {}
assert_equal serializable_resource.serializer_instance.object, @resource
expected_errors_object =
{ :errors =>
[
{
source: { pointer: '/data/attributes/name' },
detail: 'cannot be nil'
}
]
}
assert_equal serializable_resource.as_json, expected_errors_object
end
def test_active_model_with_multiple_errors
options = {
serializer: ActiveModel::Serializer::ErrorSerializer,
adapter: :json_api
}
@resource.errors.add(:name, 'cannot be nil')
@resource.errors.add(:name, 'must be longer')
@resource.errors.add(:id, 'must be a uuid')
serializable_resource = ActiveModel::SerializableResource.new(@resource, options)
assert_equal serializable_resource.serializer_instance.attributes, {}
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' }
]
}
assert_equal serializable_resource.as_json, expected_errors_object
end
# see http://jsonapi.org/examples/
def test_parameter_source_type_error
parameter = 'auther'
error_source = ActiveModelSerializers::Adapter::JsonApi::Error.error_source(:parameter, parameter)
assert_equal({ parameter: parameter }, error_source)
end
def test_unknown_source_type_error
value = 'auther'
assert_raises(ActiveModelSerializers::Adapter::JsonApi::Error::UnknownSourceTypeError) do
ActiveModelSerializers::Adapter::JsonApi::Error.error_source(:hyper, value)
end
end
end
end
end
end