mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Refactor jsonapi type/id tests to be more explicit
This commit is contained in:
parent
e3480345e3
commit
b439fe69c6
@ -28,15 +28,25 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_defined_type
|
def test_defined_type
|
||||||
test_type(WithDefinedTypeSerializer, 'with-defined-type')
|
actual = actual_resource_identifier_object(WithDefinedTypeSerializer)
|
||||||
|
expected = { id: expected_model_id, type: 'with-defined-type' }
|
||||||
|
assert_equal actual, expected
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_singular_type
|
def test_singular_type
|
||||||
test_type_inflection(AuthorSerializer, 'author', :singular)
|
actual = with_jsonapi_inflection :singular do
|
||||||
|
actual_resource_identifier_object(AuthorSerializer)
|
||||||
|
end
|
||||||
|
expected = { id: expected_model_id, type: 'author' }
|
||||||
|
assert_equal actual, expected
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_plural_type
|
def test_plural_type
|
||||||
test_type_inflection(AuthorSerializer, 'authors', :plural)
|
actual = with_jsonapi_inflection :plural do
|
||||||
|
actual_resource_identifier_object(AuthorSerializer)
|
||||||
|
end
|
||||||
|
expected = { id: expected_model_id, type: 'authors' }
|
||||||
|
assert_equal actual, expected
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_type_with_namespace
|
def test_type_with_namespace
|
||||||
@ -60,49 +70,38 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_id_defined_on_object
|
def test_id_defined_on_object
|
||||||
test_id(AuthorSerializer, @model.id.to_s)
|
actual = actual_resource_identifier_object(AuthorSerializer)
|
||||||
|
expected = { id: @model.id.to_s, type: expected_model_type }
|
||||||
|
assert_equal actual, expected
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_id_defined_on_serializer
|
def test_id_defined_on_serializer
|
||||||
test_id(WithDefinedIdSerializer, 'special_id')
|
actual = actual_resource_identifier_object(WithDefinedIdSerializer)
|
||||||
|
expected = { id: 'special_id', type: expected_model_type }
|
||||||
|
assert_equal actual, expected
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_id_defined_on_fragmented
|
def test_id_defined_on_fragmented
|
||||||
test_id(FragmentedSerializer, 'special_id')
|
actual = actual_resource_identifier_object(FragmentedSerializer)
|
||||||
|
expected = { id: 'special_id', type: expected_model_type }
|
||||||
|
assert_equal actual, expected
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def test_type_inflection(serializer_class, expected_type, inflection)
|
def actual_resource_identifier_object(serializer_class)
|
||||||
original_inflection = ActiveModelSerializers.config.jsonapi_resource_type
|
|
||||||
ActiveModelSerializers.config.jsonapi_resource_type = inflection
|
|
||||||
test_type(serializer_class, expected_type)
|
|
||||||
ensure
|
|
||||||
ActiveModelSerializers.config.jsonapi_resource_type = original_inflection
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_type(serializer_class, expected_type)
|
|
||||||
serializer = serializer_class.new(@model)
|
serializer = serializer_class.new(@model)
|
||||||
resource_identifier = ResourceIdentifier.new(serializer, nil)
|
resource_identifier = ResourceIdentifier.new(serializer, nil)
|
||||||
expected = {
|
resource_identifier.as_json
|
||||||
id: @model.id.to_s,
|
|
||||||
type: expected_type
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_equal(expected, resource_identifier.as_json)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_id(serializer_class, id)
|
def expected_model_type
|
||||||
serializer = serializer_class.new(@model)
|
|
||||||
resource_identifier = ResourceIdentifier.new(serializer, nil)
|
|
||||||
inflection = ActiveModelSerializers.config.jsonapi_resource_type
|
inflection = ActiveModelSerializers.config.jsonapi_resource_type
|
||||||
type = @model.class.model_name.send(inflection)
|
@model.class.model_name.send(inflection)
|
||||||
expected = {
|
end
|
||||||
id: id,
|
|
||||||
type: type
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_equal(expected, resource_identifier.as_json)
|
def expected_model_id
|
||||||
|
@model.id.to_s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -20,13 +20,13 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_config_plural
|
def test_config_plural
|
||||||
with_jsonapi_resource_type :plural do
|
with_jsonapi_inflection :plural do
|
||||||
assert_type(@author, 'authors')
|
assert_type(@author, 'authors')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_config_singular
|
def test_config_singular
|
||||||
with_jsonapi_resource_type :singular do
|
with_jsonapi_inflection :singular do
|
||||||
assert_type(@author, 'author')
|
assert_type(@author, 'author')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -46,14 +46,6 @@ module ActiveModel
|
|||||||
hash = serializable(resource, opts).serializable_hash
|
hash = serializable(resource, opts).serializable_hash
|
||||||
assert_equal(expected_type, hash.fetch(:data).fetch(:type))
|
assert_equal(expected_type, hash.fetch(:data).fetch(:type))
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_jsonapi_resource_type(inflection)
|
|
||||||
old_inflection = ActiveModelSerializers.config.jsonapi_resource_type
|
|
||||||
ActiveModelSerializers.config.jsonapi_resource_type = inflection
|
|
||||||
yield
|
|
||||||
ensure
|
|
||||||
ActiveModelSerializers.config.jsonapi_resource_type = old_inflection
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -47,6 +47,14 @@ module SerializationTesting
|
|||||||
ActiveModelSerializers.config.replace(old_config)
|
ActiveModelSerializers.config.replace(old_config)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def with_jsonapi_inflection(inflection)
|
||||||
|
original_inflection = ActiveModelSerializers.config.jsonapi_resource_type
|
||||||
|
ActiveModelSerializers.config.jsonapi_resource_type = inflection
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
ActiveModelSerializers.config.jsonapi_resource_type = original_inflection
|
||||||
|
end
|
||||||
|
|
||||||
def with_serializer_lookup_disabled
|
def with_serializer_lookup_disabled
|
||||||
original_serializer_lookup = ActiveModelSerializers.config.serializer_lookup_enabled
|
original_serializer_lookup = ActiveModelSerializers.config.serializer_lookup_enabled
|
||||||
ActiveModelSerializers.config.serializer_lookup_enabled = false
|
ActiveModelSerializers.config.serializer_lookup_enabled = false
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user