-
Notifications
You must be signed in to change notification settings - Fork 422
Expand file tree
/
Copy pathobject_serializer_inheritance_spec.rb
More file actions
181 lines (147 loc) · 4.57 KB
/
object_serializer_inheritance_spec.rb
File metadata and controls
181 lines (147 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
require 'spec_helper'
describe FastJsonapi::ObjectSerializer do
after(:all) do
classes_to_remove = %i[
User
UserSerializer
Country
CountrySerializer
Employee
EmployeeSerializer
Photo
PhotoSerializer
EmployeeAccount
]
classes_to_remove.each do |klass_name|
Object.send(:remove_const, klass_name) if Object.constants.include?(klass_name)
end
end
class User
attr_accessor :id, :first_name, :last_name
attr_accessor :address_ids, :country_id
def photo
p = Photo.new
p.id = 1
p.user_id = id
p
end
def photo_id
1
end
end
class UserSerializer
include FastJsonapi::ObjectSerializer
set_type :user
attributes :first_name, :last_name
attribute :full_name do |user, params|
"#{user.first_name} #{user.last_name}"
end
has_many :addresses, cached: true
belongs_to :country
has_one :photo
end
class Photo
attr_accessor :id, :user_id
end
class PhotoSerializer
include FastJsonapi::ObjectSerializer
attributes :id, :name
end
class Country
attr_accessor :id, :name
end
class CountrySerializer
include FastJsonapi::ObjectSerializer
attributes :name
end
class EmployeeAccount
attr_accessor :id, :employee_id
end
class Employee < User
attr_accessor :id, :location, :compensation
def account
a = EmployeeAccount.new
a.id = 1
a.employee_id = id
a
end
def account_id
1
end
end
class EmployeeSerializer < UserSerializer
include FastJsonapi::ObjectSerializer
attributes :location
attributes :compensation
has_one :account
end
class LegacyUserSerializer < UserSerializer
pluralize_type true
end
class LegacyEmployeeSerializer < LegacyUserSerializer
attributes :location
attributes :compensation
has_one :account
end
it 'sets the correct record type' do
expect(EmployeeSerializer.reflected_record_type).to eq :employee
expect(EmployeeSerializer.record_type).to eq :employee
end
context 'when testing inheritance of attributes' do
it 'includes parent attributes' do
subclass_attributes = EmployeeSerializer.attributes_to_serialize
superclass_attributes = UserSerializer.attributes_to_serialize
expect(subclass_attributes).to include(superclass_attributes)
end
it 'returns inherited attribute with a block correctly' do
e = Employee.new
e.id = 1
e.first_name = 'S'
e.last_name = 'K'
attributes_hash = EmployeeSerializer.new(e).serializable_hash[:data][:attributes]
expect(attributes_hash).to include(full_name: 'S K')
end
it 'includes child attributes' do
expect(EmployeeSerializer.attributes_to_serialize[:location].method).to eq(:location)
end
it 'doesnt change parent class attributes' do
EmployeeSerializer
expect(UserSerializer.attributes_to_serialize).not_to have_key(:location)
end
end
context 'when testing inheritance of relationship' do
it 'includes parent relationships' do
subclass_relationships = EmployeeSerializer.relationships_to_serialize
superclass_relationships = UserSerializer.relationships_to_serialize
expect(subclass_relationships).to include(superclass_relationships)
end
it 'returns inherited relationship correctly' do
e = Employee.new
e.country_id = 1
relationships_hash = EmployeeSerializer.new(e).serializable_hash[:data][:relationships][:country]
expect(relationships_hash).to include(data: { id: "1", type: :country })
end
it 'includes child relationships' do
expect(EmployeeSerializer.relationships_to_serialize.keys).to include(:account)
end
it 'doesnt change parent class attributes' do
EmployeeSerializer
expect(UserSerializer.relationships_to_serialize.keys).not_to include(:account)
end
it 'includes parent cached relationships' do
subclass_relationships = EmployeeSerializer.cachable_relationships_to_serialize
superclass_relationships = UserSerializer.cachable_relationships_to_serialize
expect(subclass_relationships).to include(superclass_relationships)
end
end
context 'when testing inheritence of other attributes' do
it 'inherits the transform method' do
EmployeeSerializer
expect(UserSerializer.transform_method).to eq EmployeeSerializer.transform_method
end
it 'inherits pluralized_type' do
LegacyEmployeeSerializer
expect(LegacyUserSerializer.pluralized_type).to eq LegacyEmployeeSerializer.pluralized_type
end
end
end