Class: CrudTestModelsControllerTest

Inherits:
ActionController::TestCase
  • Object
show all
Includes:
CrudControllerTestHelper, CrudTestHelper
Defined in:
lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb

Overview

Tests all actions of the CrudController based on a dummy model (CrudTestModel). This is useful to test the general behavior of CrudController.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CrudTestHelper

#action_name, #controller_name, #model_class, #params, #path_args, #sortable?

Methods included from CrudControllerTestHelper

#ivar, #perform_combined_request, #perform_request, #remember_request, #restore_request, #scope_params, #test_create_json, #test_destroy_json, #test_index_json, #test_index_sort_asc, #test_index_sort_desc, #test_show, #test_show_json, #test_show_with_non_existing_id_raises_record_not_found, #test_update_json

Instance Attribute Details

#modelsObject

Returns the value of attribute models.



13
14
15
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 13

def models
  @models
end

Instance Method Details

#test_createObject



133
134
135
136
137
138
139
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 133

def test_create
  super
  assert_match(/model got created/, flash[:notice])
  assert flash[:alert].blank?
  assert_equal %i[before_create before_save after_save after_create],
               @controller.called_callbacks
end

#test_create_with_before_callbackObject



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 166

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 :success
  assert entry.new_record?
  assert flash[:alert].present?
  assert_equal "illegal", entry.name
  assert_equal %i[before_render_new before_render_form],
               @controller.called_callbacks
end

#test_create_with_before_callback_redirectObject



179
180
181
182
183
184
185
186
187
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 179

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

#test_create_with_failureObject



195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 195

def test_create_with_failure
  assert_no_difference("CrudTestModel.count") do
    post :create, params: { crud_test_model: { children: 2 } }
  end
  assert_response :success
  assert entry.new_record?
  assert flash[:notice].blank?, flash[:notice].to_s
  assert flash[:alert].blank?, flash[:alert].to_s
  assert entry.name.blank?
  assert_equal %i[before_create before_save
                  before_render_new before_render_form],
               @controller.called_callbacks
end

#test_create_with_failure_jsonObject



209
210
211
212
213
214
215
216
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 209

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 %i[before_create before_save], @controller.called_callbacks
end

#test_destroyObject



157
158
159
160
161
162
163
164
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 157

def test_destroy
  super
  assert_equal %i[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

#test_destroy_failureObject



262
263
264
265
266
267
268
269
270
271
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 262

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

#test_destroy_failure_callbackObject



273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 273

def test_destroy_failure_callback
  e = crud_test_models(:AAAAA)
  # rubocop:disable Rails/SkipsModelValidations
  e.update_attribute(:name, "illegal")
  # rubocop:enable Rails/SkipsModelValidations
  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

#test_destroy_failure_jsonObject



286
287
288
289
290
291
292
293
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 286

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

#test_editObject



141
142
143
144
145
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 141

def test_edit
  super
  assert_equal %i[before_render_edit before_render_form],
               @controller.called_callbacks
end

#test_indexObject



31
32
33
34
35
36
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 31

def test_index
  super
  assert_equal 6, entries.size
  assert_equal entries.sort_by(&:name), entries
  assert_equal({}, session[:list_params])
end

#test_index_jsObject



38
39
40
41
42
43
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 38

def test_index_js
  get :index, params: test_params, xhr: true
  assert_response :success
  assert_equal "index js", @response.body
  assert entries.present?
end

#test_index_returningObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 106

def test_index_returning
  session[:list_params] = {}
  session[:list_params]["/crud_test_models"] = { "q" => "DDD",
                                                 "sort" => "chatty",
                                                 "sort_dir" => "desc" }
  get :index, params: { returning: true }
  assert_response :success
  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

#test_index_searchObject



45
46
47
48
49
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 45

def test_index_search
  super
  assert_equal 1, entries.size
  assert_equal({ "q" => "AAAA" }, session[:list_params]["/crud_test_models"])
end

#test_index_search_with_custom_optionsObject



59
60
61
62
63
64
65
66
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 59

def test_index_search_with_custom_options
  get :index, params: { q: "DDD", filter: true }
  assert_response :success
  assert entries.present?
  assert_equal 1, entries.size
  assert_equal [ CrudTestModel.find_by(name: "BBBBB") ], entries
  assert_equal({ "q" => "DDD" }, session[:list_params]["/crud_test_models"])
end

#test_index_with_custom_optionsObject



51
52
53
54
55
56
57
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 51

def test_index_with_custom_options
  get :index, params: { filter: true }
  assert_response :success
  assert entries.present?
  assert_equal 2, entries.size
  assert_equal entries.sort_by(&:children).reverse, entries
end

#test_models_labelObject



295
296
297
298
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 295

def test_models_label
  assert_equal "Crud Test Models", @controller.models_label
  assert_equal "Crud Test Model", @controller.models_label(plural: false)
end

#test_newObject



121
122
123
124
125
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 121

def test_new
  super
  assert_equal %i[before_render_new before_render_form],
               @controller.called_callbacks
end

#test_new_with_before_render_callback_redirect_does_not_set_companionsObject



189
190
191
192
193
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 189

def test_new_with_before_render_callback_redirect_does_not_set_companions
  @controller.should_redirect = true
  get :new
  assert_redirected_to action: "index"
end

#test_setupObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 19

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

#test_show_with_customObject



127
128
129
130
131
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 127

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

#test_sort_given_columnObject



68
69
70
71
72
73
74
75
76
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 68

def test_sort_given_column
  get :index, params: { sort: "children", sort_dir: "asc" }
  assert_response :success
  assert entries.present?
  assert_equal 6, entries.size
  assert_equal CrudTestModel.all.sort_by(&:children), entries
  assert_equal({ "sort" => "children", "sort_dir" => "asc" },
               session[:list_params]["/crud_test_models"])
end

#test_sort_virtual_columnObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 78

def test_sort_virtual_column
  get :index, params: { sort: "chatty", sort_dir: "desc" }
  assert_response :success
  assert entries.present?
  assert_equal 6, entries.size
  assert_equal({ "sort" => "chatty", "sort_dir" => "desc" },
               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

#test_sort_with_searchObject



96
97
98
99
100
101
102
103
104
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 96

def test_sort_with_search
  get :index, params: { q: "DDD", sort: "chatty", sort_dir: "asc" }
  assert_response :success
  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" },
               session[:list_params]["/crud_test_models"])
end

#test_updateObject



147
148
149
150
151
152
153
154
155
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 147

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 %i[before_update before_save after_save after_update],
               @controller.called_callbacks
end

#test_update_with_failureObject



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 218

def test_update_with_failure
  put :update, params: { id: test_entry.id, crud_test_model: { rating: 20 } }
  assert_response :success
  assert entry.changed?
  assert flash[:notice].blank?
  assert flash[:alert].blank?
  assert_equal 20, entry.rating
  assert_equal %i[before_update before_save
                  before_render_edit before_render_form],
               @controller.called_callbacks
end

#test_update_with_failure_does_not_update_many_relationsObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 230

def test_update_with_failure_does_not_update_many_relations
  put :update,
      params: {
        id: test_entry.id,
        crud_test_model: {
          rating: 20,
          other_ids: [ OtherCrudTestModel.first.id ]
        }
      }
  assert_response :success
  assert entry.changed?
  assert flash[:notice].blank?
  assert flash[:alert].blank?
  assert_equal 20, entry.rating
  assert_equal %i[before_update before_save
                  before_render_edit before_render_form],
               @controller.called_callbacks
  assert_equal [], test_entry.reload.other_ids
end

#test_update_with_failure_jsonObject



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb', line 250

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 %i[before_update before_save], @controller.called_callbacks
end