1
1
require 'test_helper'
2
2
require 'grape'
3
3
require 'grape/active_model_serializers'
4
+ require 'kaminari'
5
+ require 'kaminari/hooks'
6
+ ::Kaminari ::Hooks . init
4
7
5
8
class ActiveModelSerializers ::GrapeTest < ActiveSupport ::TestCase
6
9
include Rack ::Test ::Methods
@@ -21,6 +24,30 @@ def self.all
21
24
ARModels ::Post . all
22
25
end
23
26
end
27
+
28
+ def self . reset_all
29
+ ARModels ::Post . delete_all
30
+ @all = nil
31
+ end
32
+
33
+ def self . collection_per
34
+ 2
35
+ end
36
+
37
+ def self . collection
38
+ @collection ||=
39
+ begin
40
+ Kaminari . paginate_array (
41
+ [
42
+ Profile . new ( id : 1 , name : 'Name 1' , description : 'Description 1' , comments : 'Comments 1' ) ,
43
+ Profile . new ( id : 2 , name : 'Name 2' , description : 'Description 2' , comments : 'Comments 2' ) ,
44
+ Profile . new ( id : 3 , name : 'Name 3' , description : 'Description 3' , comments : 'Comments 3' ) ,
45
+ Profile . new ( id : 4 , name : 'Name 4' , description : 'Description 4' , comments : 'Comments 4' ) ,
46
+ Profile . new ( id : 5 , name : 'Name 5' , description : 'Description 5' , comments : 'Comments 5' )
47
+ ]
48
+ ) . page ( 1 ) . per ( collection_per )
49
+ end
50
+ end
24
51
end
25
52
26
53
class GrapeTest < Grape ::API
@@ -41,11 +68,28 @@ class GrapeTest < Grape::API
41
68
posts = Models . all
42
69
render posts , adapter : :json_api
43
70
end
71
+
72
+ get '/render_collection_with_json_api' do
73
+ posts = Models . collection
74
+ render posts , adapter : :json_api
75
+ end
76
+
77
+ get '/render_with_implicit_formatter' do
78
+ Models . model1
79
+ end
80
+
81
+ get '/render_array_with_implicit_formatter' do
82
+ Models . all
83
+ end
84
+
85
+ get '/render_collection_with_implicit_formatter' do
86
+ Models . collection
87
+ end
44
88
end
45
89
end
46
90
47
91
def app
48
- GrapeTest . new
92
+ Grape :: Middleware :: Globals . new ( GrapeTest . new )
49
93
end
50
94
51
95
def test_formatter_returns_json
@@ -77,6 +121,53 @@ def test_formatter_handles_arrays
77
121
assert last_response . ok?
78
122
assert_equal serializable_resource . to_json , last_response . body
79
123
ensure
80
- ARModels ::Post . delete_all
124
+ Models . reset_all
125
+ end
126
+
127
+ def test_formatter_handles_collections
128
+ get '/grape/render_collection_with_json_api'
129
+ assert last_response . ok?
130
+
131
+ representation = JSON . parse ( last_response . body )
132
+ assert representation . include? ( 'data' )
133
+ assert representation [ 'data' ] . count == Models . collection_per
134
+ assert representation . include? ( 'links' )
135
+ assert representation [ 'links' ] . count > 0
136
+ end
137
+
138
+ def test_implicit_formatter
139
+ ActiveModel ::Serializer . config . adapter = :json_api
140
+ get '/grape/render_with_implicit_formatter'
141
+
142
+ post = Models . model1
143
+ serializable_resource = serializable ( post , adapter : :json_api )
144
+
145
+ assert last_response . ok?
146
+ assert_equal serializable_resource . to_json , last_response . body
147
+ end
148
+
149
+ def test_implicit_formatter_handles_arrays
150
+ ActiveModel ::Serializer . config . adapter = :json_api
151
+ get '/grape/render_array_with_implicit_formatter'
152
+
153
+ posts = Models . all
154
+ serializable_resource = serializable ( posts , adapter : :json_api )
155
+
156
+ assert last_response . ok?
157
+ assert_equal serializable_resource . to_json , last_response . body
158
+ ensure
159
+ Models . reset_all
160
+ end
161
+
162
+ def test_implicit_formatter_handles_collections
163
+ ActiveModel ::Serializer . config . adapter = :json_api
164
+ get '/grape/render_collection_with_implicit_formatter'
165
+ assert last_response . ok?
166
+
167
+ representation = JSON . parse ( last_response . body )
168
+ assert representation . include? ( 'data' )
169
+ assert representation [ 'data' ] . count == Models . collection_per
170
+ assert representation . include? ( 'links' )
171
+ assert representation [ 'links' ] . count > 0
81
172
end
82
173
end
0 commit comments