forked from rails-api/active_model_serializers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
include_param_test.rb
170 lines (138 loc) · 4.71 KB
/
include_param_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
require 'test_helper'
module ActiveModel
class Serializer
module Adapter
class JsonApi
class IncludeParamTest < ActiveSupport::TestCase
IncludeParamAuthor = Class.new(::Model)
class CustomCommentLoader
def all
[{ foo: 'bar' }]
end
end
class TagSerializer < ActiveModel::Serializer
attributes :id, :name
end
class IncludeParamAuthorSerializer < ActiveModel::Serializer
class_attribute :comment_loader
associations_via_include_param(true)
has_many :tags, serializer: TagSerializer do
link :self, '//example.com/link_author/relationships/tags'
end
has_many :unlinked_tags, serializer: TagSerializer
has_many :posts, serializer: PostWithTagsSerializer
has_many :locations
has_many :comments do
load_data { IncludeParamAuthorSerializer.comment_loader.all }
end
end
def setup
IncludeParamAuthorSerializer.comment_loader = Class.new(CustomCommentLoader).new
@tag = Tag.new(id: 1337, name: 'mytag')
@author = IncludeParamAuthor.new(
id: 1337,
tags: [@tag]
)
end
def test_relationship_not_loaded_when_not_included
expected = {
links: {
self: '//example.com/link_author/relationships/tags'
}
}
@author.define_singleton_method(:read_attribute_for_serialization) do |attr|
fail 'should not be called' if attr == :tags
super(attr)
end
assert_relationship(:tags, expected)
end
def test_relationship_included
expected = {
data: [
{
id: '1337',
type: 'tags'
}
],
links: {
self: '//example.com/link_author/relationships/tags'
}
}
assert_relationship(:tags, expected, include: :tags)
end
def test_sideloads_included
expected = [
{
id: '1337',
type: 'tags',
attributes: { name: 'mytag' }
}
]
hash = result(include: :tags)
assert_equal(expected, hash[:included])
end
def test_nested_relationship
expected = {
data: [
{
id: '1337',
type: 'tags'
}
],
links: {
self: '//example.com/link_author/relationships/tags'
}
}
expected_no_data = {
links: {
self: '//example.com/link_author/relationships/tags'
}
}
assert_relationship(:tags, expected, include: [:tags, { posts: :tags }])
@author.define_singleton_method(:read_attribute_for_serialization) do |attr|
fail 'should not be called' if attr == :tags
super(attr)
end
assert_relationship(:tags, expected_no_data, include: { posts: :tags })
end
def test_include_params_with_no_block
@author.define_singleton_method(:read_attribute_for_serialization) do |attr|
fail 'should not be called' if attr == :locations
super(attr)
end
expected = {}
assert_relationship(:locations, expected)
end
def test_block_relationship
expected = {
data: [
{ 'foo' => 'bar' }
]
}
assert_relationship(:comments, expected, include: [:comments])
end
def test_block_relationship_not_included
expected = {}
IncludeParamAuthorSerializer.comment_loader.define_singleton_method(:all) do
fail 'should not be called'
end
assert_relationship(:comments, expected)
end
def test_node_not_included_when_no_link
expected = nil
assert_relationship(:unlinked_tags, expected)
end
private
def result(opts)
opts = { adapter: :json_api }.merge(opts)
serializable(@author, opts).serializable_hash
end
def assert_relationship(relationship_name, expected, opts = {})
hash = result(opts)
assert_equal(expected, hash[:data][:relationships][relationship_name])
end
end
end
end
end
end