From a0097cd70181465564f07b223ed069e6f6d23cd9 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 28 Mar 2021 10:48:56 +0900 Subject: [PATCH] Add test case for class level `strict_loading_mode` --- activerecord/lib/active_record/core.rb | 2 +- .../test/cases/strict_loading_test.rb | 47 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 1731a51691..cf7ffbe1f0 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -140,7 +140,7 @@ module ActiveRecord 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_mode, instance_accessor: true, default: [] + class_attribute :strict_loading_mode, instance_accessor: true, default: :all mattr_accessor :writing_role, instance_accessor: false, default: :writing diff --git a/activerecord/test/cases/strict_loading_test.rb b/activerecord/test/cases/strict_loading_test.rb index 7f5253ce14..3ea04ae1d3 100644 --- a/activerecord/test/cases/strict_loading_test.rb +++ b/activerecord/test/cases/strict_loading_test.rb @@ -38,7 +38,7 @@ class StrictLoadingTest < ActiveRecord::TestCase developer = Developer.first ship = Ship.first ShipPart.create!(name: "Stern", ship: ship) - firm = Firm.create!("name" => "NASA") + firm = Firm.create!(name: "NASA") project = Project.create!(name: "Apollo", firm: firm) ship.update_column(:developer_id, developer.id) @@ -73,6 +73,51 @@ class StrictLoadingTest < ActiveRecord::TestCase 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 Developer.all.each { |d| assert_not d.strict_loading? } Developer.strict_loading.each { |d| assert d.strict_loading? }