Skip to content

Commit 62d0414

Browse files
authored
Merge pull request #991 from aidanharan/coerce-update-all-delete-all-select-columns
[Rails 7] Coerce update all and delete all tests
2 parents 15b1d2b + 00eb520 commit 62d0414

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

test/cases/coerced_tests.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,40 @@ def test_update_all_doesnt_ignore_order_coerced
11201120
_(david.reload.name).must_equal "David"
11211121
_(mary.reload.name).must_equal "Test"
11221122
end
1123+
1124+
# SELECT columns must be in the GROUP clause.
1125+
coerce_tests! :test_update_all_with_group_by
1126+
def test_update_all_with_group_by_coerced
1127+
minimum_comments_count = 2
1128+
1129+
Post.most_commented(minimum_comments_count).update_all(title: "ig")
1130+
posts = Post.select(:id, :title).group(:title).most_commented(minimum_comments_count).all.to_a
1131+
1132+
assert_operator posts.length, :>, 0
1133+
assert posts.all? { |post| post.comments.length >= minimum_comments_count }
1134+
assert posts.all? { |post| "ig" == post.title }
1135+
1136+
post = Post.select(:id, :title).group(:title).joins(:comments).group("posts.id").having("count(comments.id) < #{minimum_comments_count}").first
1137+
assert_not_equal "ig", post.title
1138+
end
1139+
end
1140+
1141+
class DeleteAllTest < ActiveRecord::TestCase
1142+
# SELECT columns must be in the GROUP clause.
1143+
coerce_tests! :test_delete_all_with_group_by_and_having
1144+
def test_delete_all_with_group_by_and_having_coerced
1145+
minimum_comments_count = 2
1146+
posts_to_be_deleted = Post.select(:id).most_commented(minimum_comments_count).all.to_a
1147+
assert_operator posts_to_be_deleted.length, :>, 0
1148+
1149+
assert_difference("Post.count", -posts_to_be_deleted.length) do
1150+
Post.most_commented(minimum_comments_count).delete_all
1151+
end
1152+
1153+
posts_to_be_deleted.each do |deleted_post|
1154+
assert_raise(ActiveRecord::RecordNotFound) { deleted_post.reload }
1155+
end
1156+
end
11231157
end
11241158

11251159
require "models/topic"

0 commit comments

Comments
 (0)