@@ -4,6 +4,10 @@ module ActionController
4
4
module Serialization
5
5
class Json
6
6
class IncludeTest < ActionController ::TestCase
7
+ INCLUDE_STRING = 'posts.comments' . freeze
8
+ INCLUDE_HASH = { posts : :comments } . freeze
9
+ DEEP_INCLUDE = 'posts.comments.author' . freeze
10
+
7
11
class IncludeTestController < ActionController ::Base
8
12
def setup_data
9
13
ActionController ::Base . cache_store . clear
@@ -38,17 +42,28 @@ def render_without_include
38
42
39
43
def render_resource_with_include_hash
40
44
setup_data
41
- render json : @author , include : { posts : :comments } , adapter : :json
45
+ render json : @author , include : INCLUDE_HASH , adapter : :json
42
46
end
43
47
44
48
def render_resource_with_include_string
45
49
setup_data
46
- render json : @author , include : 'posts.comments' , adapter : :json
50
+ render json : @author , include : INCLUDE_STRING , adapter : :json
47
51
end
48
52
49
53
def render_resource_with_deep_include
50
54
setup_data
51
- render json : @author , include : 'posts.comments.author' , adapter : :json
55
+ render json : @author , include : DEEP_INCLUDE , adapter : :json
56
+ end
57
+
58
+ def render_without_recursive_relationships
59
+ # testing recursive includes ('**') can't have any cycles in the
60
+ # relationships, or we enter an infinite loop.
61
+ author = Author . new ( id : 11 , name : 'Jane Doe' )
62
+ post = Post . new ( id : 12 , title : 'Hello World' , body : 'My first post' )
63
+ comment = Comment . new ( id : 13 , body : 'Commentary' )
64
+ author . posts = [ post ]
65
+ post . comments = [ comment ]
66
+ render json : author
52
67
end
53
68
end
54
69
@@ -77,34 +92,90 @@ def test_render_without_include
77
92
def test_render_resource_with_include_hash
78
93
get :render_resource_with_include_hash
79
94
response = JSON . parse ( @response . body )
80
- expected = {
81
- 'author' => {
82
- 'id' => 1 ,
83
- 'name' => 'Steve K.' ,
95
+
96
+ assert_equal ( expected_include_response , response )
97
+ end
98
+
99
+ def test_render_resource_with_include_string
100
+ get :render_resource_with_include_string
101
+
102
+ response = JSON . parse ( @response . body )
103
+
104
+ assert_equal ( expected_include_response , response )
105
+ end
106
+
107
+ def test_render_resource_with_deep_include
108
+ get :render_resource_with_deep_include
109
+
110
+ response = JSON . parse ( @response . body )
111
+
112
+ assert_equal ( expected_deep_include_response , response )
113
+ end
114
+
115
+ def test_render_with_empty_default_includes
116
+ with_default_includes '' do
117
+ get :render_without_include
118
+ response = JSON . parse ( @response . body )
119
+ expected = {
120
+ 'author' => {
121
+ 'id' => 1 ,
122
+ 'name' => 'Steve K.'
123
+ }
124
+ }
125
+ assert_equal ( expected , response )
126
+ end
127
+ end
128
+
129
+ def test_render_with_recursive_default_includes
130
+ with_default_includes '**' do
131
+ get :render_without_recursive_relationships
132
+ response = JSON . parse ( @response . body )
133
+
134
+ expected = {
135
+ 'id' => 11 ,
136
+ 'name' => 'Jane Doe' ,
137
+ 'roles' => nil ,
138
+ 'bio' => nil ,
84
139
'posts' => [
85
140
{
86
- 'id' => 42 , 'title' => 'New Post' , 'body' => 'Body' ,
141
+ 'id' => 12 ,
142
+ 'title' => 'Hello World' ,
143
+ 'body' => 'My first post' ,
87
144
'comments' => [
88
145
{
89
- 'id' => 1 , 'body' => 'ZOMG A COMMENT'
90
- } ,
91
- {
92
- 'id ' => 2 , 'body' => 'ZOMG ANOTHER COMMENT'
146
+ 'id' => 13 ,
147
+ 'body' => 'Commentary' ,
148
+ 'post' => nil , # not set to avoid infinite recursion
149
+ 'author ' => nil , # not set to avoid infinite recursion
93
150
}
94
- ]
151
+ ] ,
152
+ 'blog' => {
153
+ 'id' => 999 ,
154
+ 'name' => 'Custom blog' ,
155
+ 'writer' => nil ,
156
+ 'articles' => nil
157
+ } ,
158
+ 'author' => nil # not set to avoid infinite recursion
95
159
}
96
160
]
97
161
}
98
- }
162
+ assert_equal ( expected , response )
163
+ end
164
+ end
99
165
100
- assert_equal ( expected , response )
166
+ def test_render_with_includes_overrides_default_includes
167
+ with_default_includes '' do
168
+ get :render_resource_with_include_hash
169
+ response = JSON . parse ( @response . body )
170
+
171
+ assert_equal ( expected_include_response , response )
172
+ end
101
173
end
102
174
103
- def test_render_resource_with_include_string
104
- get :render_resource_with_include_string
175
+ private
105
176
106
- response = JSON . parse ( @response . body )
107
- expected = {
177
+ def expected_include_response
178
+ {
108
179
'author' => {
109
180
'id' => 1 ,
110
181
'name' => 'Steve K.' ,
@@ -123,15 +194,10 @@ def test_render_resource_with_include_string
123
194
]
124
195
}
125
196
}
126
-
127
- assert_equal ( expected , response )
128
197
end
129
198
130
- def test_render_resource_with_deep_include
131
- get :render_resource_with_deep_include
132
-
133
- response = JSON . parse ( @response . body )
134
- expected = {
199
+ def expected_deep_include_response
200
+ {
135
201
'author' => {
136
202
'id' => 1 ,
137
203
'name' => 'Steve K.' ,
@@ -158,8 +224,21 @@ def test_render_resource_with_deep_include
158
224
]
159
225
}
160
226
}
227
+ end
161
228
162
- assert_equal ( expected , response )
229
+ def with_default_includes ( include_tree )
230
+ original = ActiveModelSerializers . config . default_includes
231
+ ActiveModelSerializers . config . default_includes = include_tree
232
+ clear_include_tree_cache
233
+ yield
234
+ ensure
235
+ ActiveModelSerializers . config . default_includes = original
236
+ clear_include_tree_cache
237
+ end
238
+
239
+ def clear_include_tree_cache
240
+ ActiveModelSerializers
241
+ . instance_variable_set ( :@default_include_tree , nil )
163
242
end
164
243
end
165
244
end
0 commit comments