Skip to content

Commit f232089

Browse files
committed
Improve tests.
1 parent 07e530b commit f232089

5 files changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Api::Test::FindByController < Api::TestController
2+
self.model = User
3+
self.fields = %w[id login age balance]
4+
self.find_by_fields = %w[login]
5+
self.filter_recordset_before_find = false
6+
self.excluded_actions = [:create, :update, :destroy, :update_all, :destroy_all]
7+
end

test/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
rest_resources :no_rescue_unknown_format
5858
rest_resources :read_only
5959
rest_resources :users_with_hidden
60+
rest_resources :find_by
6061
rest_resources :users_with_sub_fields
6162

6263
rest_route :network

test/config/simplecov_setup.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
add_filter "docs/"
2828
add_filter "test/"
2929

30+
# Filter out deprecated mixins.
31+
add_filter "lib/rest_framework/mixins/"
32+
3033
# Setup formatter for submission to `coveralls.io` if configured, otherwise use an HTML formatter.
3134
if IS_COVERALLS
3235
require "simplecov-lcov"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "test_helper"
2+
3+
class Api::Test::FindByControllerTest < ActionController::TestCase
4+
# Test showing a record by find_by (non-primary-key) field.
5+
# Covers controller.rb lines 799-803, 812, 819.
6+
def test_show_by_login
7+
user = User.create!(login: "find_by_test_user", state: "default", status: "")
8+
get(:show, as: :json, params: { id: user.login, find_by: "login" })
9+
assert_response(:success)
10+
assert_equal(user.login, @response.parsed_body["login"])
11+
end
12+
13+
def test_show_by_login_not_found
14+
get(:show, as: :json, params: { id: "nonexistent_login", find_by: "login" })
15+
assert_response(404)
16+
end
17+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require "test_helper"
2+
3+
class Api::Test::UsersControllerTest < ActionController::TestCase
4+
# Test bulk update with a nonexistent record ID.
5+
# Covers controller/bulk.rb lines 190, 196-197.
6+
def test_bulk_update_missing_record
7+
user = User.create!(login: "bulk_update_test", state: "default", status: "")
8+
fake_id = User.maximum(:id) + 9999
9+
10+
patch(
11+
:update_all,
12+
as: :json,
13+
params: { _json: [ { id: user.id, login: "updated" }, { id: fake_id, login: "ghost" } ] },
14+
)
15+
assert_response(400)
16+
assert_match(/not found/, @response.parsed_body["message"])
17+
end
18+
19+
# Test bulk destroy with a nonexistent record ID (transactional mode).
20+
# Covers controller/bulk.rb lines 252-254.
21+
def test_bulk_destroy_missing_record
22+
user = User.create!(login: "bulk_destroy_test", state: "default", status: "")
23+
fake_id = User.maximum(:id) + 9999
24+
25+
delete(:destroy_all, as: :json, params: { _json: [ user.id, fake_id ] })
26+
assert_response(400)
27+
assert_match(/Missing/, @response.parsed_body["message"])
28+
# Transactional: user should still exist since destroy rolled back.
29+
assert(User.find_by(id: user.id))
30+
end
31+
end

0 commit comments

Comments
 (0)