1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
# frozen_string_literal: true
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 = ActiveModelSerializers::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 = ActiveModelSerializers::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
|