Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
cabad0e8
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,体验更适合开发者的 AI 搜索 >>
提交
cabad0e8
编写于
7月 17, 2017
作者:
S
Sean Griffin
提交者:
GitHub
7月 17, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #29801 from kamipo/extract_null_relation_test
Extract `NullRelationTest` from `RelationTest`
上级
e8ad1ed8
91565aea
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
82 addition
and
118 deletion
+82
-118
activerecord/test/cases/null_relation_test.rb
activerecord/test/cases/null_relation_test.rb
+82
-0
activerecord/test/cases/relations_test.rb
activerecord/test/cases/relations_test.rb
+0
-118
未找到文件。
activerecord/test/cases/null_relation_test.rb
0 → 100644
浏览文件 @
cabad0e8
require
"cases/helper"
require
"models/developer"
require
"models/comment"
require
"models/post"
require
"models/topic"
class
NullRelationTest
<
ActiveRecord
::
TestCase
fixtures
:posts
,
:comments
def
test_none
assert_no_queries
(
ignore_none:
false
)
do
assert_equal
[],
Developer
.
none
assert_equal
[],
Developer
.
all
.
none
end
end
def
test_none_chainable
assert_no_queries
(
ignore_none:
false
)
do
assert_equal
[],
Developer
.
none
.
where
(
name:
"David"
)
end
end
def
test_none_chainable_to_existing_scope_extension_method
assert_no_queries
(
ignore_none:
false
)
do
assert_equal
1
,
Topic
.
anonymous_extension
.
none
.
one
end
end
def
test_none_chained_to_methods_firing_queries_straight_to_db
assert_no_queries
(
ignore_none:
false
)
do
assert_equal
[],
Developer
.
none
.
pluck
(
:id
,
:name
)
assert_equal
0
,
Developer
.
none
.
delete_all
assert_equal
0
,
Developer
.
none
.
update_all
(
name:
"David"
)
assert_equal
0
,
Developer
.
none
.
delete
(
1
)
assert_equal
false
,
Developer
.
none
.
exists?
(
1
)
end
end
def
test_null_relation_content_size_methods
assert_no_queries
(
ignore_none:
false
)
do
assert_equal
0
,
Developer
.
none
.
size
assert_equal
0
,
Developer
.
none
.
count
assert_equal
true
,
Developer
.
none
.
empty?
assert_equal
true
,
Developer
.
none
.
none?
assert_equal
false
,
Developer
.
none
.
any?
assert_equal
false
,
Developer
.
none
.
one?
assert_equal
false
,
Developer
.
none
.
many?
end
end
def
test_null_relation_metadata_methods
assert_equal
""
,
Developer
.
none
.
to_sql
assert_equal
({},
Developer
.
none
.
where_values_hash
)
end
def
test_null_relation_where_values_hash
assert_equal
({
"salary"
=>
100_000
},
Developer
.
none
.
where
(
salary:
100_000
).
where_values_hash
)
end
[
:count
,
:sum
].
each
do
|
method
|
define_method
"test_null_relation_
#{
method
}
"
do
assert_no_queries
(
ignore_none:
false
)
do
assert_equal
0
,
Comment
.
none
.
public_send
(
method
,
:id
)
assert_equal
Hash
.
new
,
Comment
.
none
.
group
(
:post_id
).
public_send
(
method
,
:id
)
end
end
end
[
:average
,
:minimum
,
:maximum
].
each
do
|
method
|
define_method
"test_null_relation_
#{
method
}
"
do
assert_no_queries
(
ignore_none:
false
)
do
assert_nil
Comment
.
none
.
public_send
(
method
,
:id
)
assert_equal
Hash
.
new
,
Comment
.
none
.
group
(
:post_id
).
public_send
(
method
,
:id
)
end
end
end
def
test_null_relation_in_where_condition
assert_operator
Comment
.
count
,
:
>
,
0
# precondition, make sure there are comments.
assert_equal
0
,
Comment
.
where
(
post_id:
Post
.
none
).
count
end
end
activerecord/test/cases/relations_test.rb
浏览文件 @
cabad0e8
...
...
@@ -15,7 +15,6 @@
require
"models/engine"
require
"models/tyre"
require
"models/minivan"
require
"models/aircraft"
require
"models/possession"
require
"models/reader"
require
"models/categorization"
...
...
@@ -420,123 +419,6 @@ def test_select_with_block
assert_equal
[
2
,
4
,
6
,
8
,
10
],
even_ids
.
sort
end
def
test_none
assert_no_queries
(
ignore_none:
false
)
do
assert_equal
[],
Developer
.
none
assert_equal
[],
Developer
.
all
.
none
end
end
def
test_none_chainable
assert_no_queries
(
ignore_none:
false
)
do
assert_equal
[],
Developer
.
none
.
where
(
name:
"David"
)
end
end
def
test_none_chainable_to_existing_scope_extension_method
assert_no_queries
(
ignore_none:
false
)
do
assert_equal
1
,
Topic
.
anonymous_extension
.
none
.
one
end
end
def
test_none_chained_to_methods_firing_queries_straight_to_db
assert_no_queries
(
ignore_none:
false
)
do
assert_equal
[],
Developer
.
none
.
pluck
(
:id
,
:name
)
assert_equal
0
,
Developer
.
none
.
delete_all
assert_equal
0
,
Developer
.
none
.
update_all
(
name:
"David"
)
assert_equal
0
,
Developer
.
none
.
delete
(
1
)
assert_equal
false
,
Developer
.
none
.
exists?
(
1
)
end
end
def
test_null_relation_content_size_methods
assert_no_queries
(
ignore_none:
false
)
do
assert_equal
0
,
Developer
.
none
.
size
assert_equal
0
,
Developer
.
none
.
count
assert_equal
true
,
Developer
.
none
.
empty?
assert_equal
true
,
Developer
.
none
.
none?
assert_equal
false
,
Developer
.
none
.
any?
assert_equal
false
,
Developer
.
none
.
one?
assert_equal
false
,
Developer
.
none
.
many?
end
end
def
test_null_relation_calculations_methods
assert_no_queries
(
ignore_none:
false
)
do
assert_equal
0
,
Developer
.
none
.
count
assert_equal
0
,
Developer
.
none
.
calculate
(
:count
,
nil
)
assert_nil
Developer
.
none
.
calculate
(
:average
,
"salary"
)
end
end
def
test_null_relation_metadata_methods
assert_equal
""
,
Developer
.
none
.
to_sql
assert_equal
({},
Developer
.
none
.
where_values_hash
)
end
def
test_null_relation_where_values_hash
assert_equal
({
"salary"
=>
100_000
},
Developer
.
none
.
where
(
salary:
100_000
).
where_values_hash
)
end
def
test_null_relation_sum
ac
=
Aircraft
.
new
assert_equal
Hash
.
new
,
ac
.
engines
.
group
(
:id
).
sum
(
:id
)
assert_equal
0
,
ac
.
engines
.
count
ac
.
save
assert_equal
Hash
.
new
,
ac
.
engines
.
group
(
:id
).
sum
(
:id
)
assert_equal
0
,
ac
.
engines
.
count
end
def
test_null_relation_count
ac
=
Aircraft
.
new
assert_equal
Hash
.
new
,
ac
.
engines
.
group
(
:id
).
count
assert_equal
0
,
ac
.
engines
.
count
ac
.
save
assert_equal
Hash
.
new
,
ac
.
engines
.
group
(
:id
).
count
assert_equal
0
,
ac
.
engines
.
count
end
def
test_null_relation_size
ac
=
Aircraft
.
new
assert_equal
Hash
.
new
,
ac
.
engines
.
group
(
:id
).
size
assert_equal
0
,
ac
.
engines
.
size
ac
.
save
assert_equal
Hash
.
new
,
ac
.
engines
.
group
(
:id
).
size
assert_equal
0
,
ac
.
engines
.
size
end
def
test_null_relation_average
ac
=
Aircraft
.
new
assert_equal
Hash
.
new
,
ac
.
engines
.
group
(
:car_id
).
average
(
:id
)
assert_nil
ac
.
engines
.
average
(
:id
)
ac
.
save
assert_equal
Hash
.
new
,
ac
.
engines
.
group
(
:car_id
).
average
(
:id
)
assert_nil
ac
.
engines
.
average
(
:id
)
end
def
test_null_relation_minimum
ac
=
Aircraft
.
new
assert_equal
Hash
.
new
,
ac
.
engines
.
group
(
:car_id
).
minimum
(
:id
)
assert_nil
ac
.
engines
.
minimum
(
:id
)
ac
.
save
assert_equal
Hash
.
new
,
ac
.
engines
.
group
(
:car_id
).
minimum
(
:id
)
assert_nil
ac
.
engines
.
minimum
(
:id
)
end
def
test_null_relation_maximum
ac
=
Aircraft
.
new
assert_equal
Hash
.
new
,
ac
.
engines
.
group
(
:car_id
).
maximum
(
:id
)
assert_nil
ac
.
engines
.
maximum
(
:id
)
ac
.
save
assert_equal
Hash
.
new
,
ac
.
engines
.
group
(
:car_id
).
maximum
(
:id
)
assert_nil
ac
.
engines
.
maximum
(
:id
)
end
def
test_null_relation_in_where_condition
assert_operator
Comment
.
count
,
:
>
,
0
# precondition, make sure there are comments.
assert_equal
0
,
Comment
.
where
(
post_id:
Post
.
none
).
to_a
.
size
end
def
test_joins_with_nil_argument
assert_nothing_raised
{
DependentFirm
.
joins
(
nil
).
first
}
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录