Skip to content

Commit

Permalink
Update coerced_tests.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanharan committed Sep 14, 2023
1 parent a49ffff commit d248f4e
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion test/cases/coerced_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit d248f4e

Please sign in to comment.