From 0d4e2d6ba428cbb6e38400c6c7bdf0d523025533 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Wed, 2 Aug 2017 16:35:04 -0500 Subject: [PATCH] Increase coverage --- lib/ams/serializer.rb | 2 +- test/unit/serializer/relationships_test.rb | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/ams/serializer.rb b/lib/ams/serializer.rb index f6671b20..ef13641d 100644 --- a/lib/ams/serializer.rb +++ b/lib/ams/serializer.rb @@ -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 diff --git a/test/unit/serializer/relationships_test.rb b/test/unit/serializer/relationships_test.rb index c0873124..4f156107 100644 --- a/test/unit/serializer/relationships_test.rb +++ b/test/unit/serializer/relationships_test.rb @@ -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: {