Skip to content

Commit c566ed7

Browse files
committed
Add tests.
1 parent f232089 commit c566ed7

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

test/test/controllers/api/test/find_by_controller_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
class Api::Test::FindByControllerTest < ActionController::TestCase
44
# Test showing a record by find_by (non-primary-key) field.
5-
# Covers controller.rb lines 799-803, 812, 819.
65
def test_show_by_login
76
user = User.create!(login: "find_by_test_user", state: "default", status: "")
87
get(:show, as: :json, params: { id: user.login, find_by: "login" })

test/test/controllers/api/test/users_controller_test.rb

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
class Api::Test::UsersControllerTest < ActionController::TestCase
44
# Test bulk update with a nonexistent record ID.
5-
# Covers controller/bulk.rb lines 190, 196-197.
65
def test_bulk_update_missing_record
76
user = User.create!(login: "bulk_update_test", state: "default", status: "")
87
fake_id = User.maximum(:id) + 9999
@@ -17,7 +16,6 @@ def test_bulk_update_missing_record
1716
end
1817

1918
# Test bulk destroy with a nonexistent record ID (transactional mode).
20-
# Covers controller/bulk.rb lines 252-254.
2119
def test_bulk_destroy_missing_record
2220
user = User.create!(login: "bulk_destroy_test", state: "default", status: "")
2321
fake_id = User.maximum(:id) + 9999
@@ -28,4 +26,48 @@ def test_bulk_destroy_missing_record
2826
# Transactional: user should still exist since destroy rolled back.
2927
assert(User.find_by(id: user.id))
3028
end
29+
30+
# Test bulk update with no _json body (triggers "Expected an array of objects").
31+
def test_bulk_update_no_json
32+
patch(:update_all, as: :json, params: { login: "bad" })
33+
assert_response(400)
34+
assert_match(/Expected an array of objects/, @response.parsed_body["message"])
35+
end
36+
37+
# Test bulk destroy with no _json body (triggers "Expected an array of primary keys").
38+
def test_bulk_destroy_no_json
39+
delete(:destroy_all, as: :json, params: { login: "bad" })
40+
assert_response(400)
41+
assert_match(/Expected an array of primary keys/, @response.parsed_body["message"])
42+
end
43+
44+
# Test bulk update with objects missing the primary key.
45+
def test_bulk_update_nil_pk
46+
patch(
47+
:update_all,
48+
as: :json,
49+
params: { _json: [ { login: "no_pk_1" }, { login: "no_pk_2" } ] },
50+
)
51+
assert_response(400)
52+
assert_match(/primary key/, @response.parsed_body["message"])
53+
end
54+
55+
# Test create with association data submitted by association name (not _id).
56+
def test_create_with_association_by_name
57+
manager = User.create!(login: "mgr_dispatch_test", state: "default", status: "")
58+
post(:create, as: :json, params: { login: "assoc_dispatch_test", manager: manager.id })
59+
assert_response(:success)
60+
end
61+
62+
def test_create_with_nested_association
63+
post(
64+
:create,
65+
as: :json,
66+
params: {
67+
login: "nested_dispatch_test",
68+
manager: { login: "nested_mgr", state: "default", status: "" },
69+
},
70+
)
71+
assert_response(:success)
72+
end
3173
end

0 commit comments

Comments
 (0)