-
Notifications
You must be signed in to change notification settings - Fork 18
/
crud_test_models_controller_test.rb
331 lines (293 loc) · 10.7 KB
/
crud_test_models_controller_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# Copyright (c) 2006-2017, Puzzle ITC GmbH. This file is part of
# PuzzleTime and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/puzzle/puzzletime.
require 'test_helper'
require 'support/crud_test_helper'
require 'support/crud_test_model'
require 'support/crud_controller_test_helper'
# Tests all actions of the CrudController based on a dummy model
# (CrudTestModel). This is useful to test the general behavior
# of CrudController.
class CrudTestModelsControllerTest < ActionController::TestCase
include CrudControllerTestHelper
include CrudTestHelper
attr_accessor :models
setup :reset_db, :setup_db, :create_test_data, :special_routing, :login
teardown :reset_db
def test_setup
assert_equal 6, CrudTestModel.count
assert_equal CrudTestModelsController, @controller.class
assert_recognizes({ controller: 'crud_test_models',
action: 'index' },
'/crud_test_models')
assert_recognizes({ controller: 'crud_test_models',
action: 'show',
id: '1' },
'/crud_test_models/1')
end
def test_index
super
assert_equal 6, entries.size
assert_equal entries.sort_by(&:name), entries
assert_equal({}, session[:list_params])
assert_equal entries, assigns(:crud_test_models)
assert_respond_to assigns(:crud_test_models), :klass
end
def test_index_js
get :index, xhr: true, params: test_params
assert_response :success
assert_equal 'index js', @response.body
assert entries.present?
end
def test_index_search
super
assert_equal 1, entries.size
assert_equal({ q: 'AAAA' }.with_indifferent_access, session[:list_params]['/crud_test_models'])
end
def test_index_with_custom_options
get :index, params: { filter: true }
assert_response :success
assert_template 'index'
assert entries.present?
assert_equal 2, entries.size
assert_equal entries.sort_by(&:children).reverse, entries
end
def test_index_search_with_custom_options
get :index, params: { q: 'DDD', filter: true }
assert_response :success
assert_template 'index'
assert entries.present?
assert_equal 1, entries.size
assert_equal [CrudTestModel.find_by_name('BBBBB')], entries
assert_equal({ q: 'DDD' }.with_indifferent_access, session[:list_params]['/crud_test_models'])
end
def test_sort_given_column
get :index, params: { sort: 'children', sort_dir: 'asc' }
assert_response :success
assert_template 'index'
assert entries.present?
assert_equal 6, entries.size
assert_equal CrudTestModel.all.sort_by(&:children), entries
assert_equal({ sort: 'children', sort_dir: 'asc' }.with_indifferent_access,
session[:list_params]['/crud_test_models'])
end
def test_sort_virtual_column
get :index, params: { sort: 'chatty', sort_dir: 'desc' }
assert_response :success
assert_template 'index'
assert entries.present?
assert_equal 6, entries.size
assert_equal({ sort: 'chatty', sort_dir: 'desc' }.with_indifferent_access,
session[:list_params]['/crud_test_models'])
# sort order is ambiguous, use index
names = entries.map(&:name)
assert names.index('BBBBB') < names.index('AAAAA')
assert names.index('BBBBB') < names.index('DDDDD')
assert names.index('EEEEE') < names.index('AAAAA')
assert names.index('EEEEE') < names.index('DDDDD')
assert names.index('AAAAA') < names.index('CCCCC')
assert names.index('DDDDD') < names.index('CCCCC')
end
def test_sort_with_search
get :index, params: { q: 'DDD', sort: 'chatty', sort_dir: 'asc' }
assert_response :success
assert_template 'index'
assert entries.present?
assert_equal 3, entries.size
assert_equal %w(CCCCC DDDDD BBBBB), entries.map(&:name)
assert_equal({ sort: 'chatty', sort_dir: 'asc', q: 'DDD' }.with_indifferent_access,
session[:list_params]['/crud_test_models'])
end
def test_index_returning
session[:list_params] = {}
session[:list_params]['/crud_test_models'] = { q: 'DDD',
sort: 'chatty',
sort_dir: 'desc' }.with_indifferent_access
get :index, params: { returning: true }
assert_response :success
assert_template 'index'
assert entries.present?
assert_equal 3, entries.size
assert_equal %w(BBBBB DDDDD CCCCC), entries.map(&:name)
assert_equal 'DDD', @controller.params[:q]
assert_equal 'chatty', @controller.params[:sort]
assert_equal 'desc', @controller.params[:sort_dir]
end
def test_new
super
assert assigns(:companions)
assert_equal @controller.send(:entry), assigns(:crud_test_model)
assert_equal [:before_render_new, :before_render_form],
@controller.called_callbacks
end
def test_show
super
assert_equal @controller.send(:entry), assigns(:crud_test_model)
end
def test_show_with_custom
get :show, params: test_params(id: crud_test_models(:BBBBB).id)
assert_response :success
assert_equal 'custom html', @response.body
end
def test_create
super
assert_match /model got created/, flash[:notice]
assert flash[:alert].blank?
assert_equal [:before_create, :before_save, :after_save, :after_create],
@controller.called_callbacks
end
def test_edit
super
assert_equal @controller.send(:entry), assigns(:crud_test_model)
assert_equal [:before_render_edit, :before_render_form],
@controller.called_callbacks
end
def test_update
super
assert_equal I18n.t('crud.update.flash.success',
model: 'Crud Test Model <i>foo</i>'),
flash[:notice]
assert flash[:alert].blank?
assert_equal @controller.send(:entry), assigns(:crud_test_model)
assert_equal [:before_update, :before_save, :after_save, :after_update],
@controller.called_callbacks
end
def test_destroy
super
assert_equal [:before_destroy, :after_destroy],
@controller.called_callbacks
assert_equal I18n.t('crud.destroy.flash.success',
model: 'Crud Test Model <i>AAAAA</i>'),
flash[:notice]
end
def test_create_with_before_callback
assert_no_difference('CrudTestModel.count') do
post :create, params: { crud_test_model: { name: 'illegal', children: 2 } }
end
assert_response :unprocessable_entity
assert_template 'new'
assert entry.new_record?
assert assigns(:companions).present?
assert flash[:alert].present?
assert_equal 'illegal', entry.name
assert_equal [:before_render_new, :before_render_form],
@controller.called_callbacks
end
def test_create_with_before_callback_redirect
@controller.should_redirect = true
assert_no_difference('CrudTestModel.count') do
post :create, params: { crud_test_model: { name: 'illegal', children: 2 } }
end
assert_redirected_to action: 'index'
assert_nil @controller.called_callbacks
end
def test_new_with_before_render_callback_redirect_does_not_set_companions
@controller.should_redirect = true
get :new
assert_redirected_to action: 'index'
assert_nil assigns(:companions)
end
def test_create_with_failure
assert_no_difference('CrudTestModel.count') do
post :create, params: { crud_test_model: { children: 2 } }
end
assert_response :unprocessable_entity
assert_template 'new'
assert entry.new_record?
assert assigns(:companions)
assert flash[:notice].blank?, flash[:notice].to_s
assert flash[:alert].blank?, flash[:alert].to_s
assert entry.name.blank?
assert_equal [:before_create, :before_save,
:before_render_new, :before_render_form],
@controller.called_callbacks
end
def test_create_with_failure_json
assert_no_difference('CrudTestModel.count') do
post :create, params: { crud_test_model: { children: 2 } }, format: 'json'
end
assert_response :unprocessable_entity
assert entry.new_record?
assert_equal [:before_create, :before_save], @controller.called_callbacks
end
def test_update_with_failure
put :update, params: { id: test_entry.id, crud_test_model: { rating: 20 } }
assert_response :unprocessable_entity
assert_template 'edit'
assert entry.changed?
assert flash[:notice].blank?
assert flash[:alert].blank?
assert_equal 20, entry.rating
assert_equal [:before_update, :before_save,
:before_render_edit, :before_render_form],
@controller.called_callbacks
end
def test_update_with_failure_json
put :update, params: {
id: test_entry.id,
crud_test_model: { rating: 20 }
},
format: 'json'
assert_response :unprocessable_entity
assert entry.changed?
assert flash[:notice].blank?
assert_equal 20, entry.rating
assert_equal [:before_update, :before_save], @controller.called_callbacks
end
def test_destroy_failure
assert_no_difference("#{model_class.name}.count") do
@request.env['HTTP_REFERER'] =
crud_test_model_url(crud_test_models(:BBBBB))
delete :destroy, params: test_params(id: crud_test_models(:BBBBB).id)
end
assert_redirected_to_show(entry)
assert_match /companion/, flash[:alert]
assert flash[:notice].blank?
end
def test_destroy_failure_callback
e = crud_test_models(:AAAAA)
e.update_attribute :name, 'illegal'
assert_no_difference("#{model_class.name}.count") do
delete :destroy, params: test_params(id: e.id)
end
assert_redirected_to_index
assert_match /illegal name/, flash[:alert]
assert flash[:notice].blank?
end
def test_destroy_failure_json
assert_no_difference("#{model_class.name}.count") do
delete :destroy, params: test_params(id: crud_test_models(:BBBBB).id,
format: 'json')
end
assert_response :unprocessable_entity
assert flash[:notice].blank?
end
def test_models_label
assert_equal 'Crud Test Models', @controller.models_label
assert_equal 'Crud Test Model', @controller.models_label(false)
end
private
def test_entry
crud_test_models(:AAAAA)
end
def new_entry_attrs
{ name: 'foo',
children: 42,
companion_id: 3,
rating: 8.5,
income: 2.42,
birthdate: '31-12-1999'.to_date,
human: true,
remarks: "some custom\n\tremarks" }
end
def edit_entry_attrs
{ name: 'foo',
children: 42,
rating: 8.5,
income: 2.42,
birthdate: '31-12-1999'.to_date,
human: true,
remarks: "some custom\n\tremarks" }
end
end