From d248f4e58bf3afa71026a827b3a73ea9988ffec7 Mon Sep 17 00:00:00 2001 From: Aidan Haran Date: Thu, 14 Sep 2023 15:57:56 +0100 Subject: [PATCH] Update coerced_tests.rb --- test/cases/coerced_tests.rb | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/test/cases/coerced_tests.rb b/test/cases/coerced_tests.rb index 2aa2ee05d..1595fb621 100644 --- a/test/cases/coerced_tests.rb +++ b/test/cases/coerced_tests.rb @@ -2104,7 +2104,7 @@ def test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflectio end class PreloaderTest < ActiveRecord::TestCase - # SQL format for `order_id_constraint` different. + # Need to handle query parameters in SQL regex. coerce_tests! :test_preloads_has_many_on_model_with_a_composite_primary_key_through_id_attribute def test_preloads_has_many_on_model_with_a_composite_primary_key_through_id_attribute_coerced order = cpk_orders(:cpk_groceries_order_2) @@ -2130,6 +2130,30 @@ def test_preloads_has_many_on_model_with_a_composite_primary_key_through_id_attr assert_match(expectation, preload_sql) assert_equal order_agreements.sort, loaded_order.order_agreements.sort end + + # Need to handle query parameters in SQL regex. + coerce_tests! :test_preloads_belongs_to_a_composite_primary_key_model_through_id_attribute + def test_preloads_belongs_to_a_composite_primary_key_model_through_id_attribute_coerced + order_agreement = cpk_order_agreements(:order_agreement_three) + order = cpk_orders(:cpk_groceries_order_2) + assert_equal order, order_agreement.order + + loaded_order_agreement = nil + sql = capture_sql do + loaded_order_agreement = Cpk::OrderAgreement.where(id: order_agreement.id).includes(:order).to_a.first + end + + assert_equal 2, sql.size + preload_sql = sql.last + + c = Cpk::Order.connection + order_id = Regexp.escape(c.quote_table_name("cpk_orders.id")) + order_constraint = /#{order_id} = @0.*@0 = \d+$/ + expectation = /SELECT.*WHERE.* #{order_constraint}/ + + assert_match(expectation, preload_sql) + assert_equal order, loaded_order_agreement.order + end end class BasePreventWritesTest < ActiveRecord::TestCase