Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
89399ed8
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,发现更多精彩内容 >>
未验证
提交
89399ed8
编写于
8月 02, 2019
作者:
R
Ryuta Kamizono
提交者:
GitHub
8月 02, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #36821 from rails/virtual-attribute-type
Add tests for selecting aggregrates
上级
0168ac3d
05300be6
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
84 addition
and
0 deletion
+84
-0
activerecord/test/cases/calculations_test.rb
activerecord/test/cases/calculations_test.rb
+84
-0
未找到文件。
activerecord/test/cases/calculations_test.rb
浏览文件 @
89399ed8
...
@@ -952,6 +952,90 @@ def test_group_by_attribute_with_custom_type
...
@@ -952,6 +952,90 @@ def test_group_by_attribute_with_custom_type
assert_equal
({
"proposed"
=>
2
,
"published"
=>
2
},
Book
.
group
(
:status
).
count
)
assert_equal
({
"proposed"
=>
2
,
"published"
=>
2
},
Book
.
group
(
:status
).
count
)
end
end
def
test_select_avg_with_group_by_as_virtual_attribute_with_sql
rails_core
=
companies
(
:rails_core
)
sql
=
<<~
SQL
SELECT firm_id, AVG(credit_limit) AS avg_credit_limit
FROM accounts
WHERE firm_id = ?
GROUP BY firm_id
LIMIT 1
SQL
account
=
Account
.
find_by_sql
([
sql
,
rails_core
]).
first
# id was not selected, so it should be nil
# (cannot select id because it wasn't used in the GROUP BY clause)
assert_nil
account
.
id
# firm_id was explicitly selected, so it should be present
assert_equal
(
rails_core
,
account
.
firm
)
# avg_credit_limit should be present as a virtual attribute
assert_equal
(
52.5
,
account
.
avg_credit_limit
)
end
def
test_select_avg_with_group_by_as_virtual_attribute_with_ar
rails_core
=
companies
(
:rails_core
)
account
=
Account
.
select
(
:firm_id
,
"AVG(credit_limit) AS avg_credit_limit"
)
.
where
(
firm:
rails_core
)
.
group
(
:firm_id
)
.
take!
# id was not selected, so it should be nil
# (cannot select id because it wasn't used in the GROUP BY clause)
assert_nil
account
.
id
# firm_id was explicitly selected, so it should be present
assert_equal
(
rails_core
,
account
.
firm
)
# avg_credit_limit should be present as a virtual attribute
assert_equal
(
52.5
,
account
.
avg_credit_limit
)
end
def
test_select_avg_with_joins_and_group_by_as_virtual_attribute_with_sql
rails_core
=
companies
(
:rails_core
)
sql
=
<<~
SQL
SELECT companies.*, AVG(accounts.credit_limit) AS avg_credit_limit
FROM companies
INNER JOIN accounts ON companies.id = accounts.firm_id
WHERE companies.id = ?
GROUP BY companies.id
LIMIT 1
SQL
firm
=
DependentFirm
.
find_by_sql
([
sql
,
rails_core
]).
first
# all the DependentFirm attributes should be present
assert_equal
rails_core
,
firm
assert_equal
rails_core
.
name
,
firm
.
name
# avg_credit_limit should be present as a virtual attribute
assert_equal
(
52.5
,
firm
.
avg_credit_limit
)
end
def
test_select_avg_with_joins_and_group_by_as_virtual_attribute_with_ar
rails_core
=
companies
(
:rails_core
)
firm
=
DependentFirm
.
select
(
"companies.*"
,
"AVG(accounts.credit_limit) AS avg_credit_limit"
)
.
where
(
id:
rails_core
)
.
joins
(
:account
)
.
group
(
:id
)
.
take!
# all the DependentFirm attributes should be present
assert_equal
rails_core
,
firm
assert_equal
rails_core
.
name
,
firm
.
name
# avg_credit_limit should be present as a virtual attribute
assert_equal
(
52.5
,
firm
.
avg_credit_limit
)
end
def
test_count_with_block_and_column_name_raises_an_error
def
test_count_with_block_and_column_name_raises_an_error
assert_raises
(
ArgumentError
)
do
assert_raises
(
ArgumentError
)
do
Account
.
count
(
:firm_id
)
{
true
}
Account
.
count
(
:firm_id
)
{
true
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录