mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Increase coverage
This commit is contained in:
parent
25a0c29432
commit
0d4e2d6ba4
@ -128,13 +128,13 @@ module AMS
|
|||||||
# #=> { data: [ { id: 1, type: :users}, { id: 2, type: :users] } }
|
# #=> { data: [ { id: 1, type: :users}, { id: 2, type: :users] } }
|
||||||
def relation(relation_name, type:, to:, key: relation_name, **options)
|
def relation(relation_name, type:, to:, key: relation_name, **options)
|
||||||
_fields << key
|
_fields << key
|
||||||
_relations[relation_name] = { key: key, type: type, to: to }
|
|
||||||
case to
|
case to
|
||||||
when :many then _relation_to_many(relation_name, type: type, key: key, **options)
|
when :many then _relation_to_many(relation_name, type: type, key: key, **options)
|
||||||
when :one then _relation_to_one(relation_name, type: type, key: key, **options)
|
when :one then _relation_to_one(relation_name, type: type, key: key, **options)
|
||||||
else
|
else
|
||||||
fail ArgumentError, "UnknownRelationship to='#{to}'"
|
fail ArgumentError, "UnknownRelationship to='#{to}'"
|
||||||
end
|
end
|
||||||
|
_relations[relation_name] = { key: key, type: type, to: to }
|
||||||
end
|
end
|
||||||
|
|
||||||
# @example
|
# @example
|
||||||
|
|||||||
@ -7,7 +7,6 @@ module AMS
|
|||||||
class ParentModelSerializer < Serializer
|
class ParentModelSerializer < Serializer
|
||||||
# TODO: test to: :many without :ids option
|
# TODO: test to: :many without :ids option
|
||||||
# TODO: test to: :one without :id option
|
# TODO: test to: :one without :id option
|
||||||
# TODO: test to: :unknown_option raises ArgumentError
|
|
||||||
relation :child_models, type: :comments, to: :many, ids: "object.child_models.map(&:id)"
|
relation :child_models, type: :comments, to: :many, ids: "object.child_models.map(&:id)"
|
||||||
relation :child_model, type: :comments, to: :one, id: "object.child_model.id"
|
relation :child_model, type: :comments, to: :one, id: "object.child_model.id"
|
||||||
end
|
end
|
||||||
@ -22,6 +21,27 @@ module AMS
|
|||||||
@serializer_instance = @serializer_class.new(@object)
|
@serializer_instance = @serializer_class.new(@object)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_relation_macro_missing_type
|
||||||
|
exception = assert_raises(ArgumentError) do
|
||||||
|
ParentModelSerializer.relation :missing_type, to: :anything
|
||||||
|
end
|
||||||
|
assert_match(/missing keyword: type/, exception.message)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_relation_macro_bad_to
|
||||||
|
exception = assert_raises(ArgumentError) do
|
||||||
|
ParentModelSerializer.relation :unknown_relation_to, type: :anything, to: :unknown_option
|
||||||
|
end
|
||||||
|
assert_match(/UnknownRelationship to='unknown_option'/, exception.message)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_relation_macro_missing_to
|
||||||
|
exception = assert_raises(ArgumentError) do
|
||||||
|
ParentModelSerializer.relation :missing_relation_to, type: :anything
|
||||||
|
end
|
||||||
|
assert_match(/missing keyword: to/, exception.message)
|
||||||
|
end
|
||||||
|
|
||||||
def test_model_instance_relations
|
def test_model_instance_relations
|
||||||
expected_relations = {
|
expected_relations = {
|
||||||
child_models: {
|
child_models: {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user