Skip to content

Commit 527483b

Browse files
authored
Merge pull request rails#53923 from byroot/fix-nested-update-all-test
Fix nested tests in `insert_all_test.rb`
2 parents d3a689d + acf3f2b commit 527483b

File tree

9 files changed

+45
-34
lines changed

9 files changed

+45
-34
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ Layout/TrailingWhitespace:
215215
Style/RedundantPercentQ:
216216
Enabled: true
217217

218+
Lint/NestedMethodDefinition:
219+
Enabled: true
220+
218221
Lint/AmbiguousOperator:
219222
Enabled: true
220223

actionpack/test/controller/params_wrapper_test.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ def test_handles_empty_content_type
257257
end
258258

259259
def test_derived_wrapped_keys_from_nested_attributes
260-
def User.nested_attributes_options
260+
assert_not_respond_to User, :nested_attributes_options
261+
User.define_singleton_method(:nested_attributes_options) do
261262
{ person: {} }
262263
end
263264

@@ -268,6 +269,8 @@ def User.nested_attributes_options
268269
assert_parameters("username" => "sikachu", "person_attributes" => { "title" => "Developer" }, "user" => { "username" => "sikachu", "person_attributes" => { "title" => "Developer" } })
269270
end
270271
end
272+
ensure
273+
User.singleton_class.undef_method(:nested_attributes_options)
271274
end
272275
end
273276

actionview/test/template/log_subscriber_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def setup
2222

2323
unless Rails.respond_to?(:root)
2424
@defined_root = true
25-
def Rails.root; :defined_root; end # Minitest `stub` expects the method to be defined.
25+
Rails.define_singleton_method(:root) { :defined_root } # Minitest `stub` expects the method to be defined.
2626
end
2727
end
2828

activemodel/lib/active_model/lint.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Tests
3030
# of the model, and is used to a generate unique DOM id for the object.
3131
def test_to_key
3232
assert_respond_to model, :to_key
33-
def model.persisted?() false end
33+
def_method(model, :persisted?) { false }
3434
assert model.to_key.nil?, "to_key should return nil when `persisted?` returns false"
3535
end
3636

@@ -45,8 +45,8 @@ def model.persisted?() false end
4545
# any of the possible implementation strategies on the implementer.
4646
def test_to_param
4747
assert_respond_to model, :to_param
48-
def model.to_key() [1] end
49-
def model.persisted?() false end
48+
def_method(model, :to_key) { [1] }
49+
def_method(model, :persisted?) { false }
5050
assert model.to_param.nil?, "to_param should return nil when `persisted?` returns false"
5151
end
5252

@@ -105,6 +105,10 @@ def test_errors_aref
105105
end
106106

107107
private
108+
def def_method(receiver, name, &block)
109+
::Object.instance_method(:define_singleton_method).bind_call(receiver, name, &block)
110+
end
111+
108112
def model
109113
assert_respond_to @model, :to_model
110114
@model.to_model

activerecord/test/cases/associations/eager_test.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,11 +1026,6 @@ def test_polymorphic_type_condition
10261026
end
10271027

10281028
def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
1029-
# Eager includes of has many and habtm associations aren't necessarily sorted in the same way
1030-
def assert_equal_after_sort(item1, item2, item3 = nil)
1031-
assert_equal(item1.sort { |a, b| a.id <=> b.id }, item2.sort { |a, b| a.id <=> b.id })
1032-
assert_equal(item3.sort { |a, b| a.id <=> b.id }, item2.sort { |a, b| a.id <=> b.id }) if item3
1033-
end
10341029
# Test regular association, association with conditions, association with
10351030
# STI, and association with conditions assured not to be true
10361031
post_types = [:posts, :other_posts, :special_posts]
@@ -1754,4 +1749,10 @@ def test_preloading_has_many_through_with_custom_scope
17541749
def find_all_ordered(klass, include = nil)
17551750
klass.order("#{klass.table_name}.#{klass.primary_key}").includes(include).to_a
17561751
end
1752+
1753+
# Eager includes of has many and habtm associations aren't necessarily sorted in the same way
1754+
def assert_equal_after_sort(item1, item2, item3 = nil)
1755+
assert_equal(item1.sort { |a, b| a.id <=> b.id }, item2.sort { |a, b| a.id <=> b.id })
1756+
assert_equal(item3.sort { |a, b| a.id <=> b.id }, item2.sort { |a, b| a.id <=> b.id }) if item3
1757+
end
17571758
end

activerecord/test/cases/relation/update_all_test.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -345,25 +345,25 @@ def test_update_all_doesnt_ignore_order
345345
test_update_with_order_succeeds.call("id DESC")
346346
end
347347
end
348+
end
348349

349-
def test_update_all_with_order_and_limit_updates_subset_only
350-
author = authors(:david)
351-
limited_posts = author.posts_sorted_by_id_limited
352-
assert_equal 1, limited_posts.size
353-
assert_equal 2, limited_posts.limit(2).size
354-
assert_equal 1, limited_posts.update_all([ "body = ?", "bulk update!" ])
355-
assert_equal "bulk update!", posts(:welcome).body
356-
assert_not_equal "bulk update!", posts(:thinking).body
357-
end
350+
def test_update_all_with_order_and_limit_updates_subset_only
351+
author = authors(:david)
352+
limited_posts = author.posts_sorted_by_id_limited
353+
assert_equal 1, limited_posts.size
354+
assert_equal 2, limited_posts.limit(2).size
355+
assert_equal 1, limited_posts.update_all([ "body = ?", "bulk update!" ])
356+
assert_equal "bulk update!", posts(:welcome).body
357+
assert_not_equal "bulk update!", posts(:thinking).body
358+
end
358359

359-
def test_update_all_with_order_and_limit_and_offset_updates_subset_only
360-
author = authors(:david)
361-
limited_posts = author.posts_sorted_by_id_limited.offset(1)
362-
assert_equal 1, limited_posts.size
363-
assert_equal 2, limited_posts.limit(2).size
364-
assert_equal 1, limited_posts.update_all([ "body = ?", "bulk update!" ])
365-
assert_equal "bulk update!", posts(:thinking).body
366-
assert_not_equal "bulk update!", posts(:welcome).body
367-
end
360+
def test_update_all_with_order_and_limit_and_offset_updates_subset_only
361+
author = authors(:david)
362+
limited_posts = author.posts_sorted_by_id_limited.offset(1)
363+
assert_equal 1, limited_posts.size
364+
assert_equal 2, limited_posts.limit(2).size
365+
assert_equal 1, limited_posts.update_all([ "body = ?", "bulk update!" ])
366+
assert_equal "bulk update!", posts(:thinking).body
367+
assert_not_equal "bulk update!", posts(:welcome).body
368368
end
369369
end

activerecord/test/cases/relations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,7 @@ def test_only
17301730

17311731
def test_anonymous_extension
17321732
relation = Post.where(author_id: 1).order("id ASC").extending do
1733-
def author
1733+
def author # rubocop:disable Lint/NestedMethodDefinition
17341734
"lifo"
17351735
end
17361736
end

activesupport/test/concern_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ def test_prepended_and_included_class_methods
201201
prepended = Module.new.extend(ActiveSupport::Concern)
202202

203203
@klass.class_eval { @foo = [] }
204-
included.class_methods { def foo; @foo << :included; end }
204+
included.class_methods { def foo; @foo << :included; end } # rubocop:disable Lint/NestedMethodDefinition
205205
@klass.class_eval { def self.foo; super; @foo << :class; end }
206-
prepended.class_methods { def foo; super; @foo << :prepended; end }
206+
prepended.class_methods { def foo; super; @foo << :prepended; end } # rubocop:disable Lint/NestedMethodDefinition
207207

208208
@klass.include included
209209
@klass.prepend prepended

activesupport/test/lazy_load_hooks_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def test_hook_with_yield_true_afterward
128128

129129
def test_hook_uses_class_eval_when_base_is_a_class
130130
ActiveSupport.on_load(:uses_class_eval) do
131-
def first_wrestler
131+
def first_wrestler # rubocop:disable Lint/NestedMethodDefinition
132132
"John Cena"
133133
end
134134
end
@@ -142,7 +142,7 @@ def first_wrestler
142142
def test_hook_uses_class_eval_when_base_is_a_module
143143
mod = Module.new
144144
ActiveSupport.on_load(:uses_class_eval2) do
145-
def last_wrestler
145+
def last_wrestler # rubocop:disable Lint/NestedMethodDefinition
146146
"Dwayne Johnson"
147147
end
148148
end
@@ -157,7 +157,7 @@ def last_wrestler
157157

158158
def test_hook_uses_instance_eval_when_base_is_an_instance
159159
ActiveSupport.on_load(:uses_instance_eval) do
160-
def second_wrestler
160+
def second_wrestler # rubocop:disable Lint/NestedMethodDefinition
161161
"Hulk Hogan"
162162
end
163163
end

0 commit comments

Comments
 (0)