From fb7d01368a4e17821891b021feea12b31c896be0 Mon Sep 17 00:00:00 2001 From: Shuhei Kitagawa Date: Thu, 29 Mar 2018 20:22:05 +0900 Subject: [PATCH] Integrate tests for #set_id and #attribute to object_serializer_class_methods_spec --- .../object_serializer_class_methods_spec.rb | 67 +++++++++++++++++++ spec/lib/object_serializer_set_id_spec.rb | 30 --------- ...ct_serializer_with_attribute_block_spec.rb | 13 ---- spec/shared/contexts/movie_context.rb | 10 --- 4 files changed, 67 insertions(+), 53 deletions(-) delete mode 100644 spec/lib/object_serializer_set_id_spec.rb delete mode 100644 spec/lib/object_serializer_with_attribute_block_spec.rb diff --git a/spec/lib/object_serializer_class_methods_spec.rb b/spec/lib/object_serializer_class_methods_spec.rb index b8f5133..7c6def2 100644 --- a/spec/lib/object_serializer_class_methods_spec.rb +++ b/spec/lib/object_serializer_class_methods_spec.rb @@ -11,6 +11,10 @@ describe FastJsonapi::ObjectSerializer do serializer.has_many *children end + after do + serializer.relationships_to_serialize = {} + end + context 'with namespace' do let(:serializer) { AppName::V1::MovieSerializer } let(:children) { [:roles] } @@ -52,6 +56,10 @@ describe FastJsonapi::ObjectSerializer do MovieSerializer.belongs_to *parent end + after do + MovieSerializer.relationships_to_serialize = {} + end + context 'with overrides' do let(:parent) { [:area, id_method_name: :blah_id, record_type: :awesome_area, serializer: :my_area] } @@ -72,6 +80,10 @@ describe FastJsonapi::ObjectSerializer do MovieSerializer.has_one *partner end + after do + MovieSerializer.relationships_to_serialize = {} + end + context 'with overrides' do let(:partner) { [:area, id_method_name: :blah_id, record_type: :awesome_area, serializer: :my_area] } @@ -85,13 +97,68 @@ describe FastJsonapi::ObjectSerializer do end end + describe '#set_id' do + subject(:serializable_hash) { MovieSerializer.new(resource).serializable_hash } + + before do + MovieSerializer.set_id :owner_id + end + + after do + MovieSerializer.set_id nil + end + + context 'when one record is given' do + let(:resource) { movie } + + it 'returns correct hash which id equals owner_id' do + expect(serializable_hash[:data][:id].to_i).to eq movie.owner_id + end + end + + context 'when an array of records is given' do + let(:resource) { [movie, movie] } + + it 'returns correct hash which id equals owner_id' do + expect(serializable_hash[:data][0][:id].to_i).to eq movie.owner_id + expect(serializable_hash[:data][1][:id].to_i).to eq movie.owner_id + end + end + end + describe '#use_hyphen' do subject { MovieSerializer.use_hyphen } + after do + MovieSerializer.transform_method = nil + end + it 'sets the correct transform_method when use_hyphen is used' do warning_message = "DEPRECATION WARNING: use_hyphen is deprecated and will be removed from fast_jsonapi 2.0 use (set_key_transform :dash) instead\n" expect { subject }.to output(warning_message).to_stderr expect(MovieSerializer.instance_variable_get(:@transform_method)).to eq :dasherize end end + + describe '#attribute' do + subject(:serializable_hash) { MovieSerializer.new(movie).serializable_hash } + + after do + MovieSerializer.attributes_to_serialize = {} + end + + context 'with block' do + before do + movie.release_year = 2008 + MovieSerializer.attribute :title_with_year do |record| + "#{record.name} (#{record.release_year})" + end + end + + it 'returns correct hash when serializable_hash is called' do + expect(serializable_hash[:data][:attributes][:name]).to eq movie.name + expect(serializable_hash[:data][:attributes][:title_with_year]).to eq "#{movie.name} (#{movie.release_year})" + end + end + end end diff --git a/spec/lib/object_serializer_set_id_spec.rb b/spec/lib/object_serializer_set_id_spec.rb deleted file mode 100644 index 2f731fd..0000000 --- a/spec/lib/object_serializer_set_id_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'spec_helper' - -describe FastJsonapi::ObjectSerializer do - include_context 'movie class' - - context 'when setting id' do - subject(:serializable_hash) { MovieSerializer.new(resource).serializable_hash } - - before(:all) do - MovieSerializer.set_id :owner_id - end - - context 'when one record is given' do - let(:resource) { movie } - - it 'returns correct hash which id equals owner_id' do - expect(serializable_hash[:data][:id].to_i).to eq movie.owner_id - end - end - - context 'when an array of records is given' do - let(:resource) { [movie, movie] } - - it 'returns correct hash which id equals owner_id' do - expect(serializable_hash[:data][0][:id].to_i).to eq movie.owner_id - expect(serializable_hash[:data][1][:id].to_i).to eq movie.owner_id - end - end - end -end diff --git a/spec/lib/object_serializer_with_attribute_block_spec.rb b/spec/lib/object_serializer_with_attribute_block_spec.rb deleted file mode 100644 index 885cae0..0000000 --- a/spec/lib/object_serializer_with_attribute_block_spec.rb +++ /dev/null @@ -1,13 +0,0 @@ -require 'spec_helper' - -describe FastJsonapi::ObjectSerializer do - include_context 'movie class' - - context 'when including attribute blocks' do - it 'returns correct hash when serializable_hash is called' do - serializable_hash = MovieSerializerWithAttributeBlock.new([movie]).serializable_hash - expect(serializable_hash[:data][0][:attributes][:name]).to eq movie.name - expect(serializable_hash[:data][0][:attributes][:title_with_year]).to eq "#{movie.name} (#{movie.release_year})" - end - end -end diff --git a/spec/shared/contexts/movie_context.rb b/spec/shared/contexts/movie_context.rb index 89b197e..5daac5b 100644 --- a/spec/shared/contexts/movie_context.rb +++ b/spec/shared/contexts/movie_context.rb @@ -108,15 +108,6 @@ RSpec.shared_context 'movie class' do attributes :name end - class MovieSerializerWithAttributeBlock - include FastJsonapi::ObjectSerializer - set_type :movie - attributes :name, :release_year - attribute :title_with_year do |record| - "#{record.name} (#{record.release_year})" - end - end - class SupplierSerializer include FastJsonapi::ObjectSerializer set_type :supplier @@ -169,7 +160,6 @@ RSpec.shared_context 'movie class' do ActorSerializer MovieType MovieTypeSerializer - MovieSerializerWithAttributeBlock AppName::V1::MovieSerializer MovieStruct ActorStruct