mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add test case for class level strict_loading_mode
This commit is contained in:
parent
4fe1220e57
commit
a0097cd701
2 changed files with 47 additions and 2 deletions
|
@ -140,7 +140,7 @@ module ActiveRecord
|
||||||
mattr_accessor :action_on_strict_loading_violation, instance_accessor: false, default: :raise
|
mattr_accessor :action_on_strict_loading_violation, instance_accessor: false, default: :raise
|
||||||
|
|
||||||
class_attribute :strict_loading_by_default, instance_accessor: false, default: false
|
class_attribute :strict_loading_by_default, instance_accessor: false, default: false
|
||||||
class_attribute :strict_loading_mode, instance_accessor: true, default: []
|
class_attribute :strict_loading_mode, instance_accessor: true, default: :all
|
||||||
|
|
||||||
mattr_accessor :writing_role, instance_accessor: false, default: :writing
|
mattr_accessor :writing_role, instance_accessor: false, default: :writing
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ class StrictLoadingTest < ActiveRecord::TestCase
|
||||||
developer = Developer.first
|
developer = Developer.first
|
||||||
ship = Ship.first
|
ship = Ship.first
|
||||||
ShipPart.create!(name: "Stern", ship: ship)
|
ShipPart.create!(name: "Stern", ship: ship)
|
||||||
firm = Firm.create!("name" => "NASA")
|
firm = Firm.create!(name: "NASA")
|
||||||
project = Project.create!(name: "Apollo", firm: firm)
|
project = Project.create!(name: "Apollo", firm: firm)
|
||||||
|
|
||||||
ship.update_column(:developer_id, developer.id)
|
ship.update_column(:developer_id, developer.id)
|
||||||
|
@ -73,6 +73,51 @@ class StrictLoadingTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_strict_loading_n_plus_one_only_mode_by_default
|
||||||
|
with_strict_loading_by_default(Developer) do
|
||||||
|
previous_strict_loading_mode = Developer.strict_loading_mode
|
||||||
|
Developer.strict_loading_mode = :n_plus_one_only
|
||||||
|
|
||||||
|
developer = Developer.first
|
||||||
|
ship = Ship.first
|
||||||
|
ShipPart.create!(name: "Stern", ship: ship)
|
||||||
|
firm = Firm.create!(name: "NASA")
|
||||||
|
project = Project.create!(name: "Apollo", firm: firm)
|
||||||
|
|
||||||
|
ship.update_column(:developer_id, developer.id)
|
||||||
|
developer.projects << project
|
||||||
|
developer.reload
|
||||||
|
|
||||||
|
assert_predicate developer, :strict_loading?
|
||||||
|
|
||||||
|
# Does not raise when loading a has_many association (:projects)
|
||||||
|
assert_nothing_raised do
|
||||||
|
developer.projects.to_a
|
||||||
|
end
|
||||||
|
|
||||||
|
# strict_loading is enabled for has_many associations
|
||||||
|
assert developer.projects.all?(&:strict_loading?)
|
||||||
|
assert_raises ActiveRecord::StrictLoadingViolationError do
|
||||||
|
developer.projects.last.firm
|
||||||
|
end
|
||||||
|
|
||||||
|
# Does not raise when a belongs_to association (:ship) loads its
|
||||||
|
# has_many association (:parts)
|
||||||
|
assert_nothing_raised do
|
||||||
|
developer.ship.parts.to_a
|
||||||
|
end
|
||||||
|
|
||||||
|
# strict_loading is enabled for has_many through a belongs_to
|
||||||
|
assert_not developer.ship.strict_loading?
|
||||||
|
assert developer.ship.parts.all?(&:strict_loading?)
|
||||||
|
assert_raises ActiveRecord::StrictLoadingViolationError do
|
||||||
|
developer.ship.parts.first.trinkets.to_a
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
Developer.strict_loading_mode = previous_strict_loading_mode
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_strict_loading
|
def test_strict_loading
|
||||||
Developer.all.each { |d| assert_not d.strict_loading? }
|
Developer.all.each { |d| assert_not d.strict_loading? }
|
||||||
Developer.strict_loading.each { |d| assert d.strict_loading? }
|
Developer.strict_loading.each { |d| assert d.strict_loading? }
|
||||||
|
|
Loading…
Reference in a new issue