forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 7
/
bind_parameter_test.rb
62 lines (52 loc) · 1.9 KB
/
bind_parameter_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
require "cases/helper"
require "models/post"
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter
class BindParameterTest < ActiveRecord::PostgreSQLTestCase
fixtures :posts
def test_where_with_string_for_string_column_using_bind_parameters
assert_quoted_as "'Welcome to the weblog'", "Welcome to the weblog"
end
def test_where_with_integer_for_string_column_using_bind_parameters
assert_quoted_as "0", 0, valid: false
end
def test_where_with_float_for_string_column_using_bind_parameters
assert_quoted_as "0.0", 0.0, valid: false
end
def test_where_with_boolean_for_string_column_using_bind_parameters
assert_quoted_as "0", false, valid: false
end
def test_where_with_decimal_for_string_column_using_bind_parameters
assert_quoted_as "0.0", BigDecimal(0), valid: false
end
def test_where_with_rational_for_string_column_using_bind_parameters
assert_quoted_as "0/1", Rational(0), valid: false
end
def test_where_with_duration_for_string_column_using_bind_parameters
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
end