Skip to content

Commit bb18fc6

Browse files
committed
Add tests for has_one polymorphic
1 parent 63589f7 commit bb18fc6

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed

test/fixtures/poro.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ class Comment < Model
3838
class WebLog < Model
3939
end
4040

41+
class Interview < Model
42+
def attachment
43+
@attachment ||= Image.new(url: 'U1')
44+
end
45+
end
46+
4147
class Mail < Model
4248
def attachments
4349
@attachments ||= [Image.new(url: 'U1'),
@@ -87,6 +93,12 @@ class WebLogLowerCamelSerializer < WebLogSerializer
8793
format_keys :lower_camel
8894
end
8995

96+
class InterviewSerializer < ActiveModel::Serializer
97+
attributes :text
98+
99+
has_one :attachment, polymorphic: true
100+
end
101+
90102
class MailSerializer < ActiveModel::Serializer
91103
attributes :body
92104

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
require 'test_helper'
2+
3+
module ActiveModel
4+
class Serializer
5+
class HasOnePolymorphicTest < ActiveModel::TestCase
6+
def setup
7+
@association = InterviewSerializer._associations[:attachment]
8+
@old_association = @association.dup
9+
10+
@interview = Interview.new({ text: 'Text 1' })
11+
@interview_serializer = InterviewSerializer.new(@interview)
12+
end
13+
14+
def teardown
15+
InterviewSerializer._associations[:attachment] = @old_association
16+
end
17+
18+
def model_name(object)
19+
object.class.to_s.demodulize.underscore.to_sym
20+
end
21+
22+
def test_associations_definition
23+
assert_equal 1, InterviewSerializer._associations.length
24+
assert_kind_of Association::HasOne, @association
25+
assert_equal true, @association.polymorphic
26+
assert_equal 'attachment', @association.name
27+
end
28+
29+
def test_associations_embedding_ids_serialization_using_serializable_hash
30+
@association.embed = :ids
31+
32+
assert_equal({
33+
text: 'Text 1',
34+
'attachment_id' => {
35+
type: model_name(@interview.attachment),
36+
id: @interview.attachment.object_id
37+
}
38+
}, @interview_serializer.serializable_hash)
39+
end
40+
41+
def test_associations_embedding_ids_serialization_using_as_json
42+
@association.embed = :ids
43+
44+
assert_equal({
45+
'interview' => {
46+
text: 'Text 1',
47+
'attachment_id' => {
48+
type: model_name(@interview.attachment),
49+
id: @interview.attachment.object_id
50+
}
51+
}
52+
}, @interview_serializer.as_json)
53+
end
54+
55+
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
56+
@association.embed = :ids
57+
@association.key = 'key'
58+
59+
assert_equal({
60+
text: 'Text 1',
61+
'key' => {
62+
type: model_name(@interview.attachment),
63+
id: @interview.attachment.object_id
64+
}
65+
}, @interview_serializer.serializable_hash)
66+
end
67+
68+
def test_associations_embedding_objects_serialization_using_serializable_hash
69+
@association.embed = :objects
70+
71+
assert_equal({
72+
text: 'Text 1',
73+
attachment: {
74+
type: model_name(@interview.attachment),
75+
model_name(@interview.attachment) => { url: 'U1'}
76+
}
77+
}, @interview_serializer.serializable_hash)
78+
end
79+
80+
def test_associations_embedding_objects_serialization_using_as_json
81+
@association.embed = :objects
82+
83+
assert_equal({
84+
'interview' => {
85+
text: 'Text 1',
86+
attachment: {
87+
type: model_name(@interview.attachment),
88+
model_name(@interview.attachment) => { url: 'U1'}
89+
}
90+
}
91+
}, @interview_serializer.as_json)
92+
end
93+
94+
def test_associations_embedding_nil_ids_serialization_using_as_json
95+
@association.embed = :ids
96+
@interview.instance_eval do
97+
def attachment
98+
nil
99+
end
100+
end
101+
102+
assert_equal({
103+
'interview' => { text: 'Text 1', 'attachment_id' => nil }
104+
}, @interview_serializer.as_json)
105+
end
106+
107+
def test_associations_embedding_nil_objects_serialization_using_as_json
108+
@association.embed = :objects
109+
@interview.instance_eval do
110+
def attachment
111+
nil
112+
end
113+
end
114+
115+
assert_equal({
116+
'interview' => { text: 'Text 1', attachment: nil }
117+
}, @interview_serializer.as_json)
118+
end
119+
120+
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
121+
@association.embed = :objects
122+
@association.embedded_key = 'root'
123+
124+
assert_equal({
125+
text: 'Text 1',
126+
'root' => {
127+
type: model_name(@interview.attachment),
128+
model_name(@interview.attachment) => { url: 'U1'}
129+
}
130+
}, @interview_serializer.serializable_hash)
131+
end
132+
133+
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
134+
@association.embed = :ids
135+
@association.embed_in_root = true
136+
137+
assert_equal({
138+
text: 'Text 1',
139+
'attachment_id' => {
140+
type: model_name(@interview.attachment),
141+
id: @interview.attachment.object_id
142+
}
143+
}, @interview_serializer.serializable_hash)
144+
end
145+
146+
def test_associations_embedding_ids_including_objects_serialization_using_as_json
147+
@association.embed = :ids
148+
@association.embed_in_root = true
149+
150+
assert_equal({
151+
'interview' => {
152+
text: 'Text 1',
153+
'attachment_id' => {
154+
type: model_name(@interview.attachment),
155+
id: @interview.attachment.object_id
156+
}
157+
},
158+
"attachments" => [{
159+
type: model_name(@interview.attachment),
160+
model_name(@interview.attachment) => {
161+
url: 'U1'
162+
}
163+
}]
164+
}, @interview_serializer.as_json)
165+
end
166+
167+
def test_associations_using_a_given_serializer
168+
@association.embed = :ids
169+
@association.embed_in_root = true
170+
@association.serializer_from_options = Class.new(ActiveModel::Serializer) do
171+
def name
172+
'fake'
173+
end
174+
175+
attributes :name
176+
end
177+
178+
assert_equal({
179+
'interview' => {
180+
text: 'Text 1',
181+
'attachment_id' => {
182+
type: model_name(@interview.attachment),
183+
id: @interview.attachment.object_id
184+
}
185+
},
186+
"attachments" => [{
187+
type: model_name(@interview.attachment),
188+
model_name(@interview.attachment) => {
189+
name: 'fake'
190+
}
191+
}]
192+
}, @interview_serializer.as_json)
193+
end
194+
end
195+
end
196+
end

0 commit comments

Comments
 (0)