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
|
# frozen_string_literal: true
require "cases/helper"
require "models/author"
require "models/post"
require "models/pet"
require "models/toy"
class DeleteAllTest < ActiveRecord::TestCase
fixtures :authors, :author_addresses, :posts, :pets, :toys
def test_destroy_all
davids = Author.where(name: "David")
# Force load
assert_equal [authors(:david)], davids.to_a
assert_predicate davids, :loaded?
assert_difference("Author.count", -1) do
destroyed = davids.destroy_all
assert_equal [authors(:david)], destroyed
assert_predicate destroyed.first, :frozen?
end
assert_equal [], davids.to_a
assert_predicate davids, :loaded?
end
def test_delete_all
davids = Author.where(name: "David")
assert_difference("Author.count", -1) { davids.delete_all }
assert_not_predicate davids, :loaded?
end
def test_delete_all_loaded
davids = Author.where(name: "David")
# Force load
assert_equal [authors(:david)], davids.to_a
assert_predicate davids, :loaded?
assert_difference("Author.count", -1) { davids.delete_all }
assert_equal [], davids.to_a
assert_predicate davids, :loaded?
end
def test_delete_all_with_unpermitted_relation_raises_error
assert_raises(ActiveRecord::ActiveRecordError) { Author.distinct.delete_all }
assert_raises(ActiveRecord::ActiveRecordError) { Author.group(:name).delete_all }
assert_raises(ActiveRecord::ActiveRecordError) { Author.having("SUM(id) < 3").delete_all }
end
def test_delete_all_with_joins_and_where_part_is_hash
pets = Pet.joins(:toys).where(toys: { name: "Bone" })
assert_equal true, pets.exists?
assert_equal pets.count, pets.delete_all
end
def test_delete_all_with_joins_and_where_part_is_not_hash
pets = Pet.joins(:toys).where("toys.name = ?", "Bone")
assert_equal true, pets.exists?
assert_equal pets.count, pets.delete_all
end
def test_delete_all_with_left_joins
pets = Pet.left_joins(:toys).where(toys: { name: "Bone" })
assert_equal true, pets.exists?
assert_equal pets.count, pets.delete_all
end
def test_delete_all_with_includes
pets = Pet.includes(:toys).where(toys: { name: "Bone" })
assert_equal true, pets.exists?
assert_equal pets.count, pets.delete_all
end
unless current_adapter?(:OracleAdapter)
def test_delete_all_with_order_and_limit_deletes_subset_only
author = authors(:david)
limited_posts = Post.where(author: author).order(:id).limit(1)
assert_equal 1, limited_posts.size
assert_equal 2, limited_posts.limit(2).size
assert_equal 1, limited_posts.delete_all
assert_raise(ActiveRecord::RecordNotFound) { posts(:welcome) }
assert posts(:thinking)
end
def test_delete_all_with_order_and_limit_and_offset_deletes_subset_only
author = authors(:david)
limited_posts = Post.where(author: author).order(:id).limit(1).offset(1)
assert_equal 1, limited_posts.size
assert_equal 2, limited_posts.limit(2).size
assert_equal 1, limited_posts.delete_all
assert_raise(ActiveRecord::RecordNotFound) { posts(:thinking) }
assert posts(:welcome)
end
end
end
|