-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
bind_parameter_test.rb
97 lines (83 loc) · 2.73 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# frozen_string_literal: true
require "cases/helper"
require "models/topic"
require "models/post"
module ActiveRecord
module ConnectionAdapters
class AbstractMysqlAdapter
class BindParameterTest < ActiveRecord::AbstractMysqlTestCase
fixtures :topics, :posts
def test_update_question_marks
str = "foo?bar"
x = Topic.first
x.title = str
x.content = str
x.save!
x.reload
assert_equal str, x.title
assert_equal str, x.content
end
def test_create_question_marks
str = "foo?bar"
x = Topic.create!(title: str, content: str)
x.reload
assert_equal str, x.title
assert_equal str, x.content
end
def test_update_null_bytes
str = "foo\0bar"
x = Topic.first
x.title = str
x.content = str
x.save!
x.reload
assert_equal str, x.title
assert_equal str, x.content
end
def test_create_null_bytes
str = "foo\0bar"
x = Topic.create!(title: str, content: str)
x.reload
assert_equal str, x.title
assert_equal str, x.content
end
def test_where_with_string_for_string_column_using_bind_parameters
assert_quoted_as "'Welcome to the weblog'", "Welcome to the weblog", match: 1
end
def test_where_with_integer_for_string_column_using_bind_parameters
assert_quoted_as "'0'", 0
end
def test_where_with_float_for_string_column_using_bind_parameters
assert_quoted_as "'0.0'", 0.0
end
def test_where_with_boolean_for_string_column_using_bind_parameters
assert_quoted_as "'0'", false
end
def test_where_with_decimal_for_string_column_using_bind_parameters
assert_quoted_as "'0.0'", BigDecimal(0)
end
def test_where_with_rational_for_string_column_using_bind_parameters
assert_quoted_as "'0.0'", Rational(0)
end
def test_where_with_duration_for_string_column_using_bind_parameters
assert_deprecated(ActiveRecord.deprecator) do
assert_quoted_as "'0'", 0.seconds
end
end
private
def assert_quoted_as(expected, value, match: 0)
relation = Post.where("title = ?", value)
assert_equal(
%{SELECT `posts`.* FROM `posts` WHERE (title = #{expected})},
relation.to_sql,
)
if match == 0
assert_empty relation.to_a
else
assert_equal match, relation.count
end
end
end
end
end
end