Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
e57daaae
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e57daaae
编写于
11月 30, 2015
作者:
Y
Yves Senn
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #22363 from yui-knk/mv_ar_test_case
Move some bind related test cases from finder_test.rb to sanitize_t…
上级
7e6d1342
6aec925e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
94 addition
and
91 deletion
+94
-91
activerecord/test/cases/finder_test.rb
activerecord/test/cases/finder_test.rb
+0
-91
activerecord/test/cases/sanitize_test.rb
activerecord/test/cases/sanitize_test.rb
+94
-0
未找到文件。
activerecord/test/cases/finder_test.rb
浏览文件 @
e57daaae
...
...
@@ -706,96 +706,13 @@ def test_named_bind_variables_with_quotes
assert
Company
.
where
([
"name = :name"
,
{
name:
"37signals' go'es agains"
}]).
first
end
def
test_bind_arity
assert_nothing_raised
{
bind
''
}
assert_raise
(
ActiveRecord
::
PreparedStatementInvalid
)
{
bind
''
,
1
}
assert_raise
(
ActiveRecord
::
PreparedStatementInvalid
)
{
bind
'?'
}
assert_nothing_raised
{
bind
'?'
,
1
}
assert_raise
(
ActiveRecord
::
PreparedStatementInvalid
)
{
bind
'?'
,
1
,
1
}
end
def
test_named_bind_variables
assert_equal
'1'
,
bind
(
':a'
,
:a
=>
1
)
# ' ruby-mode
assert_equal
'1 1'
,
bind
(
':a :a'
,
:a
=>
1
)
# ' ruby-mode
assert_nothing_raised
{
bind
(
"'+00:00'"
,
:foo
=>
"bar"
)
}
assert_kind_of
Firm
,
Company
.
where
([
"name = :name"
,
{
name:
"37signals"
}]).
first
assert_nil
Company
.
where
([
"name = :name"
,
{
name:
"37signals!"
}]).
first
assert_nil
Company
.
where
([
"name = :name"
,
{
name:
"37signals!' OR 1=1"
}]).
first
assert_kind_of
Time
,
Topic
.
where
([
"id = :id"
,
{
id:
1
}]).
first
.
written_on
end
def
test_named_bind_arity
assert_nothing_raised
{
bind
"name = :name"
,
{
name:
"37signals"
}
}
assert_nothing_raised
{
bind
"name = :name"
,
{
name:
"37signals"
,
id:
1
}
}
assert_raise
(
ActiveRecord
::
PreparedStatementInvalid
)
{
bind
"name = :name"
,
{
id:
1
}
}
end
class
SimpleEnumerable
include
Enumerable
def
initialize
(
ary
)
@ary
=
ary
end
def
each
(
&
b
)
@ary
.
each
(
&
b
)
end
end
def
test_bind_enumerable
quoted_abc
=
%(#{ActiveRecord::Base.connection.quote('a')},#{ActiveRecord::Base.connection.quote('b')},#{ActiveRecord::Base.connection.quote('c')})
assert_equal
'1,2,3'
,
bind
(
'?'
,
[
1
,
2
,
3
])
assert_equal
quoted_abc
,
bind
(
'?'
,
%w(a b c)
)
assert_equal
'1,2,3'
,
bind
(
':a'
,
:a
=>
[
1
,
2
,
3
])
assert_equal
quoted_abc
,
bind
(
':a'
,
:a
=>
%w(a b c)
)
# '
assert_equal
'1,2,3'
,
bind
(
'?'
,
SimpleEnumerable
.
new
([
1
,
2
,
3
]))
assert_equal
quoted_abc
,
bind
(
'?'
,
SimpleEnumerable
.
new
(
%w(a b c)
))
assert_equal
'1,2,3'
,
bind
(
':a'
,
:a
=>
SimpleEnumerable
.
new
([
1
,
2
,
3
]))
assert_equal
quoted_abc
,
bind
(
':a'
,
:a
=>
SimpleEnumerable
.
new
(
%w(a b c)
))
# '
end
def
test_bind_empty_enumerable
quoted_nil
=
ActiveRecord
::
Base
.
connection
.
quote
(
nil
)
assert_equal
quoted_nil
,
bind
(
'?'
,
[])
assert_equal
" in (
#{
quoted_nil
}
)"
,
bind
(
' in (?)'
,
[])
assert_equal
"foo in (
#{
quoted_nil
}
)"
,
bind
(
'foo in (?)'
,
[])
end
def
test_bind_empty_string
quoted_empty
=
ActiveRecord
::
Base
.
connection
.
quote
(
''
)
assert_equal
quoted_empty
,
bind
(
'?'
,
''
)
end
def
test_bind_chars
quoted_bambi
=
ActiveRecord
::
Base
.
connection
.
quote
(
"Bambi"
)
quoted_bambi_and_thumper
=
ActiveRecord
::
Base
.
connection
.
quote
(
"Bambi
\n
and
\n
Thumper"
)
assert_equal
"name=
#{
quoted_bambi
}
"
,
bind
(
'name=?'
,
"Bambi"
)
assert_equal
"name=
#{
quoted_bambi_and_thumper
}
"
,
bind
(
'name=?'
,
"Bambi
\n
and
\n
Thumper"
)
assert_equal
"name=
#{
quoted_bambi
}
"
,
bind
(
'name=?'
,
"Bambi"
.
mb_chars
)
assert_equal
"name=
#{
quoted_bambi_and_thumper
}
"
,
bind
(
'name=?'
,
"Bambi
\n
and
\n
Thumper"
.
mb_chars
)
end
def
test_bind_record
o
=
Struct
.
new
(
:quoted_id
).
new
(
1
)
assert_equal
'1'
,
bind
(
'?'
,
o
)
os
=
[
o
]
*
3
assert_equal
'1,1,1'
,
bind
(
'?'
,
os
)
end
def
test_named_bind_with_postgresql_type_casts
l
=
Proc
.
new
{
bind
(
":a::integer '2009-01-01'::date"
,
:a
=>
'10'
)
}
assert_nothing_raised
(
&
l
)
assert_equal
"
#{
ActiveRecord
::
Base
.
connection
.
quote
(
'10'
)
}
::integer '2009-01-01'::date"
,
l
.
call
end
def
test_string_sanitation
assert_not_equal
"'something ' 1=1'"
,
ActiveRecord
::
Base
.
sanitize
(
"something ' 1=1"
)
assert_equal
"'something; select table'"
,
ActiveRecord
::
Base
.
sanitize
(
"something; select table"
)
...
...
@@ -1136,14 +1053,6 @@ def test_finder_with_offset_string
end
protected
def
bind
(
statement
,
*
vars
)
if
vars
.
first
.
is_a?
(
Hash
)
ActiveRecord
::
Base
.
send
(
:replace_named_bind_variables
,
statement
,
vars
.
first
)
else
ActiveRecord
::
Base
.
send
(
:replace_bind_variables
,
statement
,
vars
)
end
end
def
table_with_custom_primary_key
yield
(
Class
.
new
(
Toy
)
do
def
self
.
name
...
...
activerecord/test/cases/sanitize_test.rb
浏览文件 @
e57daaae
...
...
@@ -69,4 +69,98 @@ def self.search(term)
searchable_post
.
search
(
"20% _reduction_!"
).
to_a
end
end
def
test_bind_arity
assert_nothing_raised
{
bind
''
}
assert_raise
(
ActiveRecord
::
PreparedStatementInvalid
)
{
bind
''
,
1
}
assert_raise
(
ActiveRecord
::
PreparedStatementInvalid
)
{
bind
'?'
}
assert_nothing_raised
{
bind
'?'
,
1
}
assert_raise
(
ActiveRecord
::
PreparedStatementInvalid
)
{
bind
'?'
,
1
,
1
}
end
def
test_named_bind_variables
assert_equal
'1'
,
bind
(
':a'
,
:a
=>
1
)
# ' ruby-mode
assert_equal
'1 1'
,
bind
(
':a :a'
,
:a
=>
1
)
# ' ruby-mode
assert_nothing_raised
{
bind
(
"'+00:00'"
,
:foo
=>
"bar"
)
}
end
def
test_named_bind_arity
assert_nothing_raised
{
bind
"name = :name"
,
{
name:
"37signals"
}
}
assert_nothing_raised
{
bind
"name = :name"
,
{
name:
"37signals"
,
id:
1
}
}
assert_raise
(
ActiveRecord
::
PreparedStatementInvalid
)
{
bind
"name = :name"
,
{
id:
1
}
}
end
class
SimpleEnumerable
include
Enumerable
def
initialize
(
ary
)
@ary
=
ary
end
def
each
(
&
b
)
@ary
.
each
(
&
b
)
end
end
def
test_bind_enumerable
quoted_abc
=
%(#{ActiveRecord::Base.connection.quote('a')},#{ActiveRecord::Base.connection.quote('b')},#{ActiveRecord::Base.connection.quote('c')})
assert_equal
'1,2,3'
,
bind
(
'?'
,
[
1
,
2
,
3
])
assert_equal
quoted_abc
,
bind
(
'?'
,
%w(a b c)
)
assert_equal
'1,2,3'
,
bind
(
':a'
,
:a
=>
[
1
,
2
,
3
])
assert_equal
quoted_abc
,
bind
(
':a'
,
:a
=>
%w(a b c)
)
# '
assert_equal
'1,2,3'
,
bind
(
'?'
,
SimpleEnumerable
.
new
([
1
,
2
,
3
]))
assert_equal
quoted_abc
,
bind
(
'?'
,
SimpleEnumerable
.
new
(
%w(a b c)
))
assert_equal
'1,2,3'
,
bind
(
':a'
,
:a
=>
SimpleEnumerable
.
new
([
1
,
2
,
3
]))
assert_equal
quoted_abc
,
bind
(
':a'
,
:a
=>
SimpleEnumerable
.
new
(
%w(a b c)
))
# '
end
def
test_bind_empty_enumerable
quoted_nil
=
ActiveRecord
::
Base
.
connection
.
quote
(
nil
)
assert_equal
quoted_nil
,
bind
(
'?'
,
[])
assert_equal
" in (
#{
quoted_nil
}
)"
,
bind
(
' in (?)'
,
[])
assert_equal
"foo in (
#{
quoted_nil
}
)"
,
bind
(
'foo in (?)'
,
[])
end
def
test_bind_empty_string
quoted_empty
=
ActiveRecord
::
Base
.
connection
.
quote
(
''
)
assert_equal
quoted_empty
,
bind
(
'?'
,
''
)
end
def
test_bind_chars
quoted_bambi
=
ActiveRecord
::
Base
.
connection
.
quote
(
"Bambi"
)
quoted_bambi_and_thumper
=
ActiveRecord
::
Base
.
connection
.
quote
(
"Bambi
\n
and
\n
Thumper"
)
assert_equal
"name=
#{
quoted_bambi
}
"
,
bind
(
'name=?'
,
"Bambi"
)
assert_equal
"name=
#{
quoted_bambi_and_thumper
}
"
,
bind
(
'name=?'
,
"Bambi
\n
and
\n
Thumper"
)
assert_equal
"name=
#{
quoted_bambi
}
"
,
bind
(
'name=?'
,
"Bambi"
.
mb_chars
)
assert_equal
"name=
#{
quoted_bambi_and_thumper
}
"
,
bind
(
'name=?'
,
"Bambi
\n
and
\n
Thumper"
.
mb_chars
)
end
def
test_bind_record
o
=
Struct
.
new
(
:quoted_id
).
new
(
1
)
assert_equal
'1'
,
bind
(
'?'
,
o
)
os
=
[
o
]
*
3
assert_equal
'1,1,1'
,
bind
(
'?'
,
os
)
end
def
test_named_bind_with_postgresql_type_casts
l
=
Proc
.
new
{
bind
(
":a::integer '2009-01-01'::date"
,
:a
=>
'10'
)
}
assert_nothing_raised
(
&
l
)
assert_equal
"
#{
ActiveRecord
::
Base
.
connection
.
quote
(
'10'
)
}
::integer '2009-01-01'::date"
,
l
.
call
end
private
def
bind
(
statement
,
*
vars
)
if
vars
.
first
.
is_a?
(
Hash
)
ActiveRecord
::
Base
.
send
(
:replace_named_bind_variables
,
statement
,
vars
.
first
)
else
ActiveRecord
::
Base
.
send
(
:replace_bind_variables
,
statement
,
vars
)
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录