Skip to content

Commit 63589f7

Browse files
committed
Add tests for has_many polymorphic
1 parent 09a166b commit 63589f7

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed

test/fixtures/poro.rb

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

41+
class Mail < Model
42+
def attachments
43+
@attachments ||= [Image.new(url: 'U1'),
44+
Video.new(html: 'H1')]
45+
end
46+
end
47+
48+
class Image < Model
49+
end
50+
51+
class Video < Model
52+
end
53+
4154
###
4255
## Serializers
4356
###
@@ -73,3 +86,17 @@ class WebLogSerializer < ActiveModel::Serializer
7386
class WebLogLowerCamelSerializer < WebLogSerializer
7487
format_keys :lower_camel
7588
end
89+
90+
class MailSerializer < ActiveModel::Serializer
91+
attributes :body
92+
93+
has_many :attachments, polymorphic: true
94+
end
95+
96+
class ImageSerializer < ActiveModel::Serializer
97+
attributes :url
98+
end
99+
100+
class VideoSerializer < ActiveModel::Serializer
101+
attributes :html
102+
end
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
require 'test_helper'
2+
3+
module ActiveModel
4+
class Serializer
5+
class HasManyPolymorphicTest < ActiveModel::TestCase
6+
def setup
7+
@association = MailSerializer._associations[:attachments]
8+
@old_association = @association.dup
9+
10+
@mail = Mail.new({ body: 'Body 1' })
11+
@mail_serializer = MailSerializer.new(@mail)
12+
end
13+
14+
def teardown
15+
MailSerializer._associations[:attachments] = @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, MailSerializer._associations.length
24+
assert_kind_of Association::HasMany, @association
25+
assert_equal true, @association.polymorphic
26+
assert_equal 'attachments', @association.name
27+
end
28+
29+
def test_associations_embedding_ids_serialization_using_serializable_hash
30+
@association.embed = :ids
31+
32+
assert_equal({
33+
body: 'Body 1',
34+
'attachment_ids' => @mail.attachments.map do |c|
35+
{ id: c.object_id, type: model_name(c) }
36+
end
37+
}, @mail_serializer.serializable_hash)
38+
end
39+
40+
def test_associations_embedding_ids_serialization_using_as_json
41+
@association.embed = :ids
42+
43+
assert_equal({
44+
'mail' => {
45+
:body => 'Body 1',
46+
'attachment_ids' => @mail.attachments.map do |c|
47+
{ id: c.object_id, type: model_name(c) }
48+
end
49+
}
50+
}, @mail_serializer.as_json)
51+
end
52+
53+
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
54+
@association.embed = :ids
55+
@association.key = 'key'
56+
57+
assert_equal({
58+
body: 'Body 1',
59+
'key' => @mail.attachments.map do |c|
60+
{ id: c.object_id, type: model_name(c) }
61+
end
62+
}, @mail_serializer.serializable_hash)
63+
end
64+
65+
def test_associations_embedding_objects_serialization_using_serializable_hash
66+
@association.embed = :objects
67+
68+
assert_equal({
69+
body: 'Body 1',
70+
:attachments => [
71+
{ type: :image, image: { url: 'U1' }},
72+
{ type: :video, video: { html: 'H1' }}
73+
]
74+
}, @mail_serializer.serializable_hash)
75+
end
76+
77+
def test_associations_embedding_objects_serialization_using_as_json
78+
@association.embed = :objects
79+
80+
assert_equal({
81+
'mail' => {
82+
body: 'Body 1',
83+
attachments: [
84+
{ type: :image, image: { url: 'U1' }},
85+
{ type: :video, video: { html: 'H1' }}
86+
]
87+
}
88+
}, @mail_serializer.as_json)
89+
end
90+
91+
def test_associations_embedding_nil_objects_serialization_using_as_json
92+
@association.embed = :objects
93+
@mail.instance_eval do
94+
def attachments
95+
[nil]
96+
end
97+
end
98+
99+
assert_equal({
100+
'mail' => {
101+
:body => 'Body 1',
102+
:attachments => [nil]
103+
}
104+
}, @mail_serializer.as_json)
105+
end
106+
107+
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
108+
@association.embed = :objects
109+
@association.embedded_key = 'root'
110+
111+
assert_equal({
112+
body: 'Body 1',
113+
'root' => [
114+
{ type: :image, image: { url: 'U1' }},
115+
{ type: :video, video: { html: 'H1' }}
116+
]
117+
}, @mail_serializer.serializable_hash)
118+
end
119+
120+
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
121+
@association.embed = :ids
122+
@association.embed_in_root = true
123+
124+
assert_equal({
125+
body: 'Body 1',
126+
'attachment_ids' => @mail.attachments.map do |c|
127+
{ id: c.object_id, type: model_name(c) }
128+
end
129+
}, @mail_serializer.serializable_hash)
130+
end
131+
132+
def test_associations_embedding_ids_including_objects_serialization_using_as_json
133+
@association.embed = :ids
134+
@association.embed_in_root = true
135+
136+
assert_equal({
137+
'mail' => {
138+
body: 'Body 1',
139+
'attachment_ids' => @mail.attachments.map do |c|
140+
{ id: c.object_id, type: model_name(c) }
141+
end,
142+
},
143+
attachments: [
144+
{ type: :image, image: { url: 'U1' }},
145+
{ type: :video, video: { html: 'H1' }}
146+
]
147+
}, @mail_serializer.as_json)
148+
end
149+
150+
def test_associations_embedding_nothing_including_objects_serialization_using_as_json
151+
@association.embed = nil
152+
@association.embed_in_root = true
153+
154+
assert_equal({
155+
'mail' => { body: 'Body 1' },
156+
attachments: [
157+
{ type: :image, image: { url: 'U1' }},
158+
{ type: :video, video: { html: 'H1' }}
159+
]
160+
}, @mail_serializer.as_json)
161+
end
162+
163+
def test_associations_using_a_given_serializer
164+
@association.embed = :ids
165+
@association.embed_in_root = true
166+
@association.serializer_from_options = Class.new(ActiveModel::Serializer) do
167+
def fake
168+
'fake'
169+
end
170+
171+
attributes :fake
172+
end
173+
174+
assert_equal({
175+
'mail' => {
176+
body: 'Body 1',
177+
'attachment_ids' => @mail.attachments.map do |c|
178+
{ id: c.object_id, type: model_name(c) }
179+
end
180+
},
181+
attachments: [
182+
{ type: :image, image: { fake: 'fake' }},
183+
{ type: :video, video: { fake: 'fake' }}
184+
]
185+
}, @mail_serializer.as_json)
186+
end
187+
end
188+
end
189+
end

0 commit comments

Comments
 (0)