From 5b64e90956d8dd9c0661821b11048be4369b1657 Mon Sep 17 00:00:00 2001 From: Shuhei Kitagawa Date: Mon, 30 Apr 2018 14:56:19 +0900 Subject: [PATCH] Add tests for block relationship --- .../object_serializer_class_methods_spec.rb | 70 ++++++++++++++++ spec/shared/contexts/movie_context.rb | 80 ++++++++++++++++++- 2 files changed, 146 insertions(+), 4 deletions(-) diff --git a/spec/lib/object_serializer_class_methods_spec.rb b/spec/lib/object_serializer_class_methods_spec.rb index 13f7be3..b6b5830 100644 --- a/spec/lib/object_serializer_class_methods_spec.rb +++ b/spec/lib/object_serializer_class_methods_spec.rb @@ -49,6 +49,44 @@ describe FastJsonapi::ObjectSerializer do end end + describe '#has_many with block' do + before do + MovieSerializer.has_many :awards do |movie| + movie.actors.map(&:awards).flatten + end + end + + after do + MovieSerializer.relationships_to_serialize.delete(:awards) + end + + context 'awards is not included' do + subject(:hash) { MovieSerializer.new(movie).serializable_hash } + + it 'returns correct hash' do + expect(hash[:data][:relationships][:awards][:data].length).to eq(6) + expect(hash[:data][:relationships][:awards][:data][0]).to eq({ id: '9', type: :award }) + expect(hash[:data][:relationships][:awards][:data][-1]).to eq({ id: '28', type: :award }) + end + end + + context 'state is included' do + subject(:hash) { MovieSerializer.new(movie, include: [:awards]).serializable_hash } + + it 'returns correct hash' do + expect(hash[:included].length).to eq 6 + expect(hash[:included][0][:id]).to eq '9' + expect(hash[:included][0][:type]).to eq :award + expect(hash[:included][0][:attributes]).to eq({ id: 9, title: 'Test Award 9' }) + expect(hash[:included][0][:relationships]).to eq({ actor: { data: { id: '1', type: :actor } } }) + expect(hash[:included][-1][:id]).to eq '28' + expect(hash[:included][-1][:type]).to eq :award + expect(hash[:included][-1][:attributes]).to eq({ id: 28, title: 'Test Award 28' }) + expect(hash[:included][-1][:relationships]).to eq({ actor: { data: { id: '3', type: :actor } } }) + end + end + end + describe '#belongs_to' do subject(:relationship) { MovieSerializer.relationships_to_serialize[:area] } @@ -73,6 +111,38 @@ describe FastJsonapi::ObjectSerializer do end end + describe '#belongs_to with block' do + before do + ActorSerializer.belongs_to :state do |actor| + actor.agency.state + end + end + + after do + ActorSerializer.relationships_to_serialize.delete(:actorc) + end + + context 'state is not included' do + subject(:hash) { ActorSerializer.new(actor).serializable_hash } + + it 'returns correct hash' do + expect(hash[:data][:relationships][:state][:data]).to eq({ id: '1', type: :state }) + end + end + + context 'state is included' do + subject(:hash) { ActorSerializer.new(actor, include: [:state]).serializable_hash } + + it 'returns correct hash' do + expect(hash[:included].length).to eq 1 + expect(hash[:included][0][:id]).to eq '1' + expect(hash[:included][0][:type]).to eq :state + expect(hash[:included][0][:attributes]).to eq({ id: 1, name: 'Test State 1' }) + expect(hash[:included][0][:relationships]).to eq({ agency: { data: [{ id: '432', type: :agency }] } }) + end + end + end + describe '#has_one' do subject(:relationship) { MovieSerializer.relationships_to_serialize[:area] } diff --git a/spec/shared/contexts/movie_context.rb b/spec/shared/contexts/movie_context.rb index 5daac5b..a71326d 100644 --- a/spec/shared/contexts/movie_context.rb +++ b/spec/shared/contexts/movie_context.rb @@ -13,11 +13,12 @@ RSpec.shared_context 'movie class' do :movie_type_id def actors - actor_ids.map do |id| + actor_ids.map.with_index do |id, i| a = Actor.new a.id = id a.name = "Test #{a.id}" a.email = "test#{a.id}@test.com" + a.agency_id =i a end end @@ -35,7 +36,49 @@ RSpec.shared_context 'movie class' do end class Actor - attr_accessor :id, :name, :email + attr_accessor :id, :name, :email, :agency_id + + def agency + Agency.new.tap do |a| + a.id = agency_id + a.name = "Test Agency #{agency_id}" + a.state_id = 1 + end + end + + def awards + award_ids.map do |i| + Award.new.tap do |a| + a.id = i + a.title = "Test Award #{i}" + a.actor_id = id + end + end + end + + def award_ids + [id * 9, id * 9 + 1] + end + end + + class Agency + attr_accessor :id, :name, :state_id + + def state + State.new.tap do |s| + s.id = state_id + s.name = "Test State #{state_id}" + s.agency_ids = [id] + end + end + end + + class Award + attr_accessor :id, :title, :actor_id + end + + class State + attr_accessor :id, :name, :agency_ids end class MovieType @@ -100,6 +143,26 @@ RSpec.shared_context 'movie class' do include FastJsonapi::ObjectSerializer set_type :actor attributes :name, :email + has_many :awards + belongs_to :agency + end + + class AgencySerializer + include FastJsonapi::ObjectSerializer + attributes :id, :name + belongs_to :city + end + + class AwardSerializer + include FastJsonapi::ObjectSerializer + attributes :id, :title + belongs_to :actor + end + + class StateSerializer + include FastJsonapi::ObjectSerializer + attributes :id, :name + has_many :agency end class MovieTypeSerializer @@ -148,7 +211,7 @@ RSpec.shared_context 'movie class' do :movie_type_id ) - ActorStruct = Struct.new(:id, :name, :email) + ActorStruct = Struct.new(:id, :name, :email, :agency_id, :award_ids) MovieWithoutIdStruct = Struct.new(:name, :release_year) end @@ -177,7 +240,7 @@ RSpec.shared_context 'movie class' do actors = [] 3.times.each do |id| - actors << ActorStruct.new(id, id.to_s, id.to_s) + actors << ActorStruct.new(id, id.to_s, id.to_s, id, [id]) end m = MovieStruct.new @@ -205,6 +268,15 @@ RSpec.shared_context 'movie class' do m end + let(:actor) do + Actor.new.tap do |a| + a.id = 234 + a.name = 'test actor' + a.email = 'test@test.com' + a.agency_id = 432 + end + end + let(:supplier) do s = Supplier.new s.id = 1