Increase coverage

This commit is contained in:
Benjamin Fleischer 2017-08-02 16:35:04 -05:00
parent 25a0c29432
commit 0d4e2d6ba4
2 changed files with 22 additions and 2 deletions

View File

@ -128,13 +128,13 @@ module AMS
# #=> { data: [ { id: 1, type: :users}, { id: 2, type: :users] } }
def relation(relation_name, type:, to:, key: relation_name, **options)
_fields << key
_relations[relation_name] = { key: key, type: type, to: to }
case to
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)
else
fail ArgumentError, "UnknownRelationship to='#{to}'"
end
_relations[relation_name] = { key: key, type: type, to: to }
end
# @example

View File

@ -7,7 +7,6 @@ module AMS
class ParentModelSerializer < Serializer
# TODO: test to: :many without :ids 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_model, type: :comments, to: :one, id: "object.child_model.id"
end
@ -22,6 +21,27 @@ module AMS
@serializer_instance = @serializer_class.new(@object)
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
expected_relations = {
child_models: {