forked from rails-api/active_model_serializers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polymorphic_test.rb
171 lines (156 loc) · 4.66 KB
/
polymorphic_test.rb
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
require 'test_helper'
module ActiveModel
class Serializer
module Adapter
class PolymorphicTest < ActiveSupport::TestCase
setup do
@employee = Employee.new(id: 42, name: 'Zoop Zoopler', email: '[email protected]')
@picture = @employee.pictures.new(id: 1, title: 'headshot-1.jpg')
@picture.imageable = @employee
end
def serialization(resource, adapter = :attributes)
serializable(resource, adapter: adapter, serializer: PolymorphicBelongsToSerializer).as_json
end
def tag_serialization(adapter = :attributes)
tag = PolyTag.new(id: 1, phrase: 'foo')
tag.object_tags << ObjectTag.new(id: 1, poly_tag_id: 1, taggable: @employee)
tag.object_tags << ObjectTag.new(id: 5, poly_tag_id: 1, taggable: @picture)
serializable(tag, adapter: adapter, serializer: PolymorphicTagSerializer, include: '*.*').as_json
end
def test_attributes_serialization
expected =
{
id: 1,
title: 'headshot-1.jpg',
imageable: {
type: 'employee',
employee: {
id: 42,
name: 'Zoop Zoopler'
}
}
}
assert_equal(expected, serialization(@picture))
end
def test_attributes_serialization_without_polymorphic_association
expected =
{
id: 2,
title: 'headshot-2.jpg',
imageable: nil
}
simple_picture = Picture.new(id: 2, title: 'headshot-2.jpg')
assert_equal(expected, serialization(simple_picture))
end
def test_attributes_serialization_with_polymorphic_has_many
expected =
{
id: 1,
phrase: 'foo',
object_tags: [
{
id: 1,
taggable: {
type: 'employee',
employee: {
id: 42
}
}
},
{
id: 5,
taggable: {
type: 'picture',
picture: {
id: 1
}
}
}
]
}
assert_equal(expected, tag_serialization)
end
def test_json_serialization
expected =
{
picture: {
id: 1,
title: 'headshot-1.jpg',
imageable: {
type: 'employee',
employee: {
id: 42,
name: 'Zoop Zoopler'
}
}
}
}
assert_equal(expected, serialization(@picture, :json))
end
def test_json_serialization_without_polymorphic_association
expected =
{
picture: {
id: 2,
title: 'headshot-2.jpg',
imageable: nil
}
}
simple_picture = Picture.new(id: 2, title: 'headshot-2.jpg')
assert_equal(expected, serialization(simple_picture, :json))
end
def test_json_serialization_with_polymorphic_has_many
expected =
{
poly_tag: {
id: 1,
phrase: 'foo',
object_tags: [
{
id: 1,
taggable: {
type: 'employee',
employee: {
id: 42
}
}
},
{
id: 5,
taggable: {
type: 'picture',
picture: {
id: 1
}
}
}
]
}
}
assert_equal(expected, tag_serialization(:json))
end
def test_json_api_serialization
expected =
{
data: {
id: '1',
type: 'pictures',
attributes: {
title: 'headshot-1.jpg'
},
relationships: {
imageable: {
data: {
id: '42',
type: 'employees'
}
}
}
}
}
assert_equal(expected, serialization(@picture, :json_api))
end
end
end
end
end