Skip to content

Commit 05eb098

Browse files
authored
Merge pull request rails#51142 from Shopify/refactor-bind-parameter-tests
Refactor BindParameterTest
2 parents d75e552 + da9cf15 commit 05eb098

File tree

3 files changed

+68
-47
lines changed

3 files changed

+68
-47
lines changed

activerecord/test/cases/adapters/abstract_mysql_adapter/bind_parameter_test.rb

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,46 @@ def test_create_null_bytes
4949
end
5050

5151
def test_where_with_string_for_string_column_using_bind_parameters
52-
count = Post.where("title = ?", "Welcome to the weblog").count
53-
assert_equal 1, count
52+
assert_quoted_as "'Welcome to the weblog'", "Welcome to the weblog"
5453
end
5554

5655
def test_where_with_integer_for_string_column_using_bind_parameters
57-
count = Post.where("title = ?", 0).count
58-
assert_equal 0, count
56+
assert_quoted_as "'0'", 0
5957
end
6058

6159
def test_where_with_float_for_string_column_using_bind_parameters
62-
count = Post.where("title = ?", 0.0).count
63-
assert_equal 0, count
60+
assert_quoted_as "'0.0'", 0.0
6461
end
6562

6663
def test_where_with_boolean_for_string_column_using_bind_parameters
67-
count = Post.where("title = ?", false).count
68-
assert_equal 0, count
64+
assert_quoted_as "'0'", false
6965
end
7066

7167
def test_where_with_decimal_for_string_column_using_bind_parameters
72-
count = Post.where("title = ?", BigDecimal(0)).count
73-
assert_equal 0, count
68+
assert_quoted_as "'0.0'", BigDecimal(0)
7469
end
7570

7671
def test_where_with_rational_for_string_column_using_bind_parameters
77-
count = Post.where("title = ?", Rational(0)).count
78-
assert_equal 0, count
72+
assert_quoted_as "'0.0'", Rational(0)
7973
end
8074

8175
def test_where_with_duration_for_string_column_using_bind_parameters
82-
count = assert_deprecated(ActiveRecord.deprecator) { Post.where("title = ?", 0.seconds).count }
83-
assert_equal 0, count
76+
assert_deprecated(ActiveRecord.deprecator) do
77+
assert_quoted_as "'0'", 0.seconds
78+
end
8479
end
80+
81+
private
82+
def assert_quoted_as(expected, value)
83+
relation = Post.where("title = ?", value)
84+
assert_equal(
85+
%{SELECT `posts`.* FROM `posts` WHERE (title = #{expected})},
86+
relation.to_sql,
87+
)
88+
assert_nothing_raised do # Make sure SQL is valid
89+
relation.to_a
90+
end
91+
end
8592
end
8693
end
8794
end

activerecord/test/cases/adapters/postgresql/bind_parameter_test.rb

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,52 @@ class BindParameterTest < ActiveRecord::PostgreSQLTestCase
1010
fixtures :posts
1111

1212
def test_where_with_string_for_string_column_using_bind_parameters
13-
count = Post.where("title = ?", "Welcome to the weblog").count
14-
assert_equal 1, count
13+
assert_quoted_as "'Welcome to the weblog'", "Welcome to the weblog"
1514
end
1615

1716
def test_where_with_integer_for_string_column_using_bind_parameters
18-
assert_raises ActiveRecord::StatementInvalid do
19-
Post.where("title = ?", 0).count
20-
end
17+
assert_quoted_as "0", 0, valid: false
2118
end
2219

2320
def test_where_with_float_for_string_column_using_bind_parameters
24-
assert_raises ActiveRecord::StatementInvalid do
25-
Post.where("title = ?", 0.0).count
26-
end
21+
assert_quoted_as "0.0", 0.0, valid: false
2722
end
2823

2924
def test_where_with_boolean_for_string_column_using_bind_parameters
30-
assert_raises ActiveRecord::StatementInvalid do
31-
Post.where("title = ?", false).count
32-
end
25+
assert_quoted_as "FALSE", false, valid: false
3326
end
3427

3528
def test_where_with_decimal_for_string_column_using_bind_parameters
36-
assert_raises ActiveRecord::StatementInvalid do
37-
Post.where("title = ?", BigDecimal(0)).count
38-
end
29+
assert_quoted_as "0.0", BigDecimal(0), valid: false
3930
end
4031

4132
def test_where_with_rational_for_string_column_using_bind_parameters
42-
assert_raises ActiveRecord::StatementInvalid do
43-
Post.where("title = ?", Rational(0)).count
44-
end
33+
assert_quoted_as "0/1", Rational(0), valid: false
4534
end
4635

4736
def test_where_with_duration_for_string_column_using_bind_parameters
48-
assert_raises ActiveRecord::StatementInvalid do
49-
assert_deprecated(ActiveRecord.deprecator) { Post.where("title = ?", 0.seconds).count }
37+
assert_deprecated(ActiveRecord.deprecator) do
38+
assert_quoted_as "0", 0.seconds, valid: false
5039
end
5140
end
41+
42+
private
43+
def assert_quoted_as(expected, value, valid: true)
44+
relation = Post.where("title = ?", value)
45+
assert_equal(
46+
%{SELECT "posts".* FROM "posts" WHERE (title = #{expected})},
47+
relation.to_sql,
48+
)
49+
if valid
50+
assert_nothing_raised do # Make sure SQL is valid
51+
relation.to_a
52+
end
53+
else
54+
assert_raises ActiveRecord::StatementInvalid do
55+
relation.to_a
56+
end
57+
end
58+
end
5259
end
5360
end
5461
end

activerecord/test/cases/adapters/sqlite3/bind_parameter_test.rb

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,46 @@ class BindParameterTest < ActiveRecord::SQLite3TestCase
1010
fixtures :posts
1111

1212
def test_where_with_string_for_string_column_using_bind_parameters
13-
count = Post.where("title = ?", "Welcome to the weblog").count
14-
assert_equal 1, count
13+
assert_quoted_as "'Welcome to the weblog'", "Welcome to the weblog"
1514
end
1615

1716
def test_where_with_integer_for_string_column_using_bind_parameters
18-
count = Post.where("title = ?", 0).count
19-
assert_equal 0, count
17+
assert_quoted_as "0", 0
2018
end
2119

2220
def test_where_with_float_for_string_column_using_bind_parameters
23-
count = Post.where("title = ?", 0.0).count
24-
assert_equal 0, count
21+
assert_quoted_as "0.0", 0.0
2522
end
2623

2724
def test_where_with_boolean_for_string_column_using_bind_parameters
28-
count = Post.where("title = ?", false).count
29-
assert_equal 0, count
25+
assert_quoted_as "0", false
3026
end
3127

3228
def test_where_with_decimal_for_string_column_using_bind_parameters
33-
count = Post.where("title = ?", BigDecimal(0)).count
34-
assert_equal 0, count
29+
assert_quoted_as "0.0", BigDecimal(0)
3530
end
3631

3732
def test_where_with_rational_for_string_column_using_bind_parameters
38-
count = Post.where("title = ?", Rational(0)).count
39-
assert_equal 0, count
33+
assert_quoted_as "0/1", Rational(0)
4034
end
4135

4236
def test_where_with_duration_for_string_column_using_bind_parameters
43-
count = assert_deprecated(ActiveRecord.deprecator) { Post.where("title = ?", 0.seconds).count }
44-
assert_equal 0, count
37+
assert_deprecated(ActiveRecord.deprecator) do
38+
assert_quoted_as "0", 0.seconds
39+
end
4540
end
41+
42+
private
43+
def assert_quoted_as(expected, value)
44+
relation = Post.where("title = ?", value)
45+
assert_equal(
46+
%{SELECT "posts".* FROM "posts" WHERE (title = #{expected})},
47+
relation.to_sql,
48+
)
49+
assert_nothing_raised do # Make sure SQL is valid
50+
relation.to_a
51+
end
52+
end
4653
end
4754
end
4855
end

0 commit comments

Comments
 (0)