Add tests for block relationship
This commit is contained in:
parent
e39de8c8c4
commit
5b64e90956
@ -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] }
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user