Skip to content

Commit 5b64e90

Browse files
Shuhei Kitagawashishirmk
authored andcommitted
Add tests for block relationship
1 parent e39de8c commit 5b64e90

2 files changed

Lines changed: 146 additions & 4 deletions

File tree

spec/lib/object_serializer_class_methods_spec.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,44 @@
4949
end
5050
end
5151

52+
describe '#has_many with block' do
53+
before do
54+
MovieSerializer.has_many :awards do |movie|
55+
movie.actors.map(&:awards).flatten
56+
end
57+
end
58+
59+
after do
60+
MovieSerializer.relationships_to_serialize.delete(:awards)
61+
end
62+
63+
context 'awards is not included' do
64+
subject(:hash) { MovieSerializer.new(movie).serializable_hash }
65+
66+
it 'returns correct hash' do
67+
expect(hash[:data][:relationships][:awards][:data].length).to eq(6)
68+
expect(hash[:data][:relationships][:awards][:data][0]).to eq({ id: '9', type: :award })
69+
expect(hash[:data][:relationships][:awards][:data][-1]).to eq({ id: '28', type: :award })
70+
end
71+
end
72+
73+
context 'state is included' do
74+
subject(:hash) { MovieSerializer.new(movie, include: [:awards]).serializable_hash }
75+
76+
it 'returns correct hash' do
77+
expect(hash[:included].length).to eq 6
78+
expect(hash[:included][0][:id]).to eq '9'
79+
expect(hash[:included][0][:type]).to eq :award
80+
expect(hash[:included][0][:attributes]).to eq({ id: 9, title: 'Test Award 9' })
81+
expect(hash[:included][0][:relationships]).to eq({ actor: { data: { id: '1', type: :actor } } })
82+
expect(hash[:included][-1][:id]).to eq '28'
83+
expect(hash[:included][-1][:type]).to eq :award
84+
expect(hash[:included][-1][:attributes]).to eq({ id: 28, title: 'Test Award 28' })
85+
expect(hash[:included][-1][:relationships]).to eq({ actor: { data: { id: '3', type: :actor } } })
86+
end
87+
end
88+
end
89+
5290
describe '#belongs_to' do
5391
subject(:relationship) { MovieSerializer.relationships_to_serialize[:area] }
5492

@@ -73,6 +111,38 @@
73111
end
74112
end
75113

114+
describe '#belongs_to with block' do
115+
before do
116+
ActorSerializer.belongs_to :state do |actor|
117+
actor.agency.state
118+
end
119+
end
120+
121+
after do
122+
ActorSerializer.relationships_to_serialize.delete(:actorc)
123+
end
124+
125+
context 'state is not included' do
126+
subject(:hash) { ActorSerializer.new(actor).serializable_hash }
127+
128+
it 'returns correct hash' do
129+
expect(hash[:data][:relationships][:state][:data]).to eq({ id: '1', type: :state })
130+
end
131+
end
132+
133+
context 'state is included' do
134+
subject(:hash) { ActorSerializer.new(actor, include: [:state]).serializable_hash }
135+
136+
it 'returns correct hash' do
137+
expect(hash[:included].length).to eq 1
138+
expect(hash[:included][0][:id]).to eq '1'
139+
expect(hash[:included][0][:type]).to eq :state
140+
expect(hash[:included][0][:attributes]).to eq({ id: 1, name: 'Test State 1' })
141+
expect(hash[:included][0][:relationships]).to eq({ agency: { data: [{ id: '432', type: :agency }] } })
142+
end
143+
end
144+
end
145+
76146
describe '#has_one' do
77147
subject(:relationship) { MovieSerializer.relationships_to_serialize[:area] }
78148

spec/shared/contexts/movie_context.rb

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ class Movie
1313
:movie_type_id
1414

1515
def actors
16-
actor_ids.map do |id|
16+
actor_ids.map.with_index do |id, i|
1717
a = Actor.new
1818
a.id = id
1919
a.name = "Test #{a.id}"
2020
a.email = "test#{a.id}@test.com"
21+
a.agency_id =i
2122
a
2223
end
2324
end
@@ -35,7 +36,49 @@ def cache_key
3536
end
3637

3738
class Actor
38-
attr_accessor :id, :name, :email
39+
attr_accessor :id, :name, :email, :agency_id
40+
41+
def agency
42+
Agency.new.tap do |a|
43+
a.id = agency_id
44+
a.name = "Test Agency #{agency_id}"
45+
a.state_id = 1
46+
end
47+
end
48+
49+
def awards
50+
award_ids.map do |i|
51+
Award.new.tap do |a|
52+
a.id = i
53+
a.title = "Test Award #{i}"
54+
a.actor_id = id
55+
end
56+
end
57+
end
58+
59+
def award_ids
60+
[id * 9, id * 9 + 1]
61+
end
62+
end
63+
64+
class Agency
65+
attr_accessor :id, :name, :state_id
66+
67+
def state
68+
State.new.tap do |s|
69+
s.id = state_id
70+
s.name = "Test State #{state_id}"
71+
s.agency_ids = [id]
72+
end
73+
end
74+
end
75+
76+
class Award
77+
attr_accessor :id, :title, :actor_id
78+
end
79+
80+
class State
81+
attr_accessor :id, :name, :agency_ids
3982
end
4083

4184
class MovieType
@@ -100,6 +143,26 @@ class ActorSerializer
100143
include FastJsonapi::ObjectSerializer
101144
set_type :actor
102145
attributes :name, :email
146+
has_many :awards
147+
belongs_to :agency
148+
end
149+
150+
class AgencySerializer
151+
include FastJsonapi::ObjectSerializer
152+
attributes :id, :name
153+
belongs_to :city
154+
end
155+
156+
class AwardSerializer
157+
include FastJsonapi::ObjectSerializer
158+
attributes :id, :title
159+
belongs_to :actor
160+
end
161+
162+
class StateSerializer
163+
include FastJsonapi::ObjectSerializer
164+
attributes :id, :name
165+
has_many :agency
103166
end
104167

105168
class MovieTypeSerializer
@@ -148,7 +211,7 @@ class MovieSerializer
148211
:movie_type_id
149212
)
150213

151-
ActorStruct = Struct.new(:id, :name, :email)
214+
ActorStruct = Struct.new(:id, :name, :email, :agency_id, :award_ids)
152215
MovieWithoutIdStruct = Struct.new(:name, :release_year)
153216
end
154217

@@ -177,7 +240,7 @@ class MovieSerializer
177240
actors = []
178241

179242
3.times.each do |id|
180-
actors << ActorStruct.new(id, id.to_s, id.to_s)
243+
actors << ActorStruct.new(id, id.to_s, id.to_s, id, [id])
181244
end
182245

183246
m = MovieStruct.new
@@ -205,6 +268,15 @@ class MovieSerializer
205268
m
206269
end
207270

271+
let(:actor) do
272+
Actor.new.tap do |a|
273+
a.id = 234
274+
a.name = 'test actor'
275+
a.email = 'test@test.com'
276+
a.agency_id = 432
277+
end
278+
end
279+
208280
let(:supplier) do
209281
s = Supplier.new
210282
s.id = 1

0 commit comments

Comments
 (0)