Skip to content

Commit

Permalink
Refactor BindParameterTest
Browse files Browse the repository at this point in the history
Ref: rails#51139

Makes it more clear that the expected behavior really is.
  • Loading branch information
byroot committed Feb 20, 2024
1 parent 554e71a commit 8f0b83a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 47 deletions.
Expand Up @@ -49,39 +49,46 @@ def test_create_null_bytes
end

def test_where_with_string_for_string_column_using_bind_parameters
count = Post.where("title = ?", "Welcome to the weblog").count
assert_equal 1, count
assert_quoted_as "'Welcome to the weblog'", "Welcome to the weblog"
end

def test_where_with_integer_for_string_column_using_bind_parameters
count = Post.where("title = ?", 0).count
assert_equal 0, count
assert_quoted_as "'0'", 0
end

def test_where_with_float_for_string_column_using_bind_parameters
count = Post.where("title = ?", 0.0).count
assert_equal 0, count
assert_quoted_as "'0.0'", 0.0
end

def test_where_with_boolean_for_string_column_using_bind_parameters
count = Post.where("title = ?", false).count
assert_equal 0, count
assert_quoted_as "'0'", false
end

def test_where_with_decimal_for_string_column_using_bind_parameters
count = Post.where("title = ?", BigDecimal(0)).count
assert_equal 0, count
assert_quoted_as "'0.0'", BigDecimal(0)
end

def test_where_with_rational_for_string_column_using_bind_parameters
count = Post.where("title = ?", Rational(0)).count
assert_equal 0, count
assert_quoted_as "'0.0'", Rational(0)
end

def test_where_with_duration_for_string_column_using_bind_parameters
count = assert_deprecated(ActiveRecord.deprecator) { Post.where("title = ?", 0.seconds).count }
assert_equal 0, count
assert_deprecated(ActiveRecord.deprecator) do
assert_quoted_as "'0'", 0.seconds
end
end

private
def assert_quoted_as(expected, value)
relation = Post.where("title = ?", value)
assert_equal(
%{SELECT `posts`.* FROM `posts` WHERE (title = #{expected})},
relation.to_sql,
)
assert_nothing_raised do # Make sure SQL is valid
relation.to_a
end
end
end
end
end
Expand Down
45 changes: 26 additions & 19 deletions activerecord/test/cases/adapters/postgresql/bind_parameter_test.rb
Expand Up @@ -10,45 +10,52 @@ class BindParameterTest < ActiveRecord::PostgreSQLTestCase
fixtures :posts

def test_where_with_string_for_string_column_using_bind_parameters
count = Post.where("title = ?", "Welcome to the weblog").count
assert_equal 1, count
assert_quoted_as "'Welcome to the weblog'", "Welcome to the weblog"
end

def test_where_with_integer_for_string_column_using_bind_parameters
assert_raises ActiveRecord::StatementInvalid do
Post.where("title = ?", 0).count
end
assert_quoted_as "0", 0, valid: false
end

def test_where_with_float_for_string_column_using_bind_parameters
assert_raises ActiveRecord::StatementInvalid do
Post.where("title = ?", 0.0).count
end
assert_quoted_as "0.0", 0.0, valid: false
end

def test_where_with_boolean_for_string_column_using_bind_parameters
assert_raises ActiveRecord::StatementInvalid do
Post.where("title = ?", false).count
end
assert_quoted_as "0", false, valid: false
end

def test_where_with_decimal_for_string_column_using_bind_parameters
assert_raises ActiveRecord::StatementInvalid do
Post.where("title = ?", BigDecimal(0)).count
end
assert_quoted_as "0.0", BigDecimal(0), valid: false
end

def test_where_with_rational_for_string_column_using_bind_parameters
assert_raises ActiveRecord::StatementInvalid do
Post.where("title = ?", Rational(0)).count
end
assert_quoted_as "0/1", Rational(0), valid: false
end

def test_where_with_duration_for_string_column_using_bind_parameters
assert_raises ActiveRecord::StatementInvalid do
assert_deprecated(ActiveRecord.deprecator) { Post.where("title = ?", 0.seconds).count }
assert_deprecated(ActiveRecord.deprecator) do
assert_quoted_as "0", 0.seconds, valid: false
end
end

private
def assert_quoted_as(expected, value, valid: true)
relation = Post.where("title = ?", value)
assert_equal(
%{SELECT "posts".* FROM "posts" WHERE (title = #{expected})},
relation.to_sql,
)
if valid
assert_nothing_raised do # Make sure SQL is valid
relation.to_a
end
else
assert_raises ActiveRecord::StatementInvalid do
relation.to_a
end
end
end
end
end
end
Expand Down
35 changes: 21 additions & 14 deletions activerecord/test/cases/adapters/sqlite3/bind_parameter_test.rb
Expand Up @@ -10,39 +10,46 @@ class BindParameterTest < ActiveRecord::SQLite3TestCase
fixtures :posts

def test_where_with_string_for_string_column_using_bind_parameters
count = Post.where("title = ?", "Welcome to the weblog").count
assert_equal 1, count
assert_quoted_as "'Welcome to the weblog'", "Welcome to the weblog"
end

def test_where_with_integer_for_string_column_using_bind_parameters
count = Post.where("title = ?", 0).count
assert_equal 0, count
assert_quoted_as "0", 0
end

def test_where_with_float_for_string_column_using_bind_parameters
count = Post.where("title = ?", 0.0).count
assert_equal 0, count
assert_quoted_as "0.0", 0.0
end

def test_where_with_boolean_for_string_column_using_bind_parameters
count = Post.where("title = ?", false).count
assert_equal 0, count
assert_quoted_as "0", false
end

def test_where_with_decimal_for_string_column_using_bind_parameters
count = Post.where("title = ?", BigDecimal(0)).count
assert_equal 0, count
assert_quoted_as "0.0", BigDecimal(0)
end

def test_where_with_rational_for_string_column_using_bind_parameters
count = Post.where("title = ?", Rational(0)).count
assert_equal 0, count
assert_quoted_as "0/1", Rational(0)
end

def test_where_with_duration_for_string_column_using_bind_parameters
count = assert_deprecated(ActiveRecord.deprecator) { Post.where("title = ?", 0.seconds).count }
assert_equal 0, count
assert_deprecated(ActiveRecord.deprecator) do
assert_quoted_as "0", 0.seconds
end
end

private
def assert_quoted_as(expected, value)
relation = Post.where("title = ?", value)
assert_equal(
%{SELECT "posts".* FROM "posts" WHERE (title = #{expected})},
relation.to_sql,
)
assert_nothing_raised do # Make sure SQL is valid
relation.to_a
end
end
end
end
end
Expand Down

0 comments on commit 8f0b83a

Please sign in to comment.