Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
bfca7d74
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,发现更多精彩内容 >>
提交
bfca7d74
编写于
4月 17, 2010
作者:
M
Mislav Marohnić
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
move counter_cache tests to a separate file and refactor
上级
f493f315
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
58 addition
and
49 deletion
+58
-49
activerecord/test/cases/base_test.rb
activerecord/test/cases/base_test.rb
+0
-49
activerecord/test/cases/counter_cache_test.rb
activerecord/test/cases/counter_cache_test.rb
+58
-0
未找到文件。
activerecord/test/cases/base_test.rb
浏览文件 @
bfca7d74
...
...
@@ -708,55 +708,6 @@ def test_boolean_attributes
assert
Topic
.
find
(
2
).
approved?
end
def
test_increment_counter
Topic
.
increment_counter
(
"replies_count"
,
1
)
assert_equal
2
,
Topic
.
find
(
1
).
replies_count
Topic
.
increment_counter
(
"replies_count"
,
1
)
assert_equal
3
,
Topic
.
find
(
1
).
replies_count
end
def
test_decrement_counter
Topic
.
decrement_counter
(
"replies_count"
,
2
)
assert_equal
(
-
1
,
Topic
.
find
(
2
).
replies_count
)
Topic
.
decrement_counter
(
"replies_count"
,
2
)
assert_equal
(
-
2
,
Topic
.
find
(
2
).
replies_count
)
end
def
test_reset_counters
assert_equal
1
,
Topic
.
find
(
1
).
replies_count
Topic
.
increment_counter
(
"replies_count"
,
1
)
assert_equal
2
,
Topic
.
find
(
1
).
replies_count
Topic
.
reset_counters
(
1
,
:replies
)
assert_equal
1
,
Topic
.
find
(
1
).
replies_count
end
def
test_update_counter
category
=
categories
(
:general
)
assert_nil
category
.
categorizations_count
assert_equal
2
,
category
.
categorizations
.
count
Category
.
update_counters
(
category
.
id
,
"categorizations_count"
=>
category
.
categorizations
.
count
)
category
.
reload
assert_not_nil
category
.
categorizations_count
assert_equal
2
,
category
.
categorizations_count
Category
.
update_counters
(
category
.
id
,
"categorizations_count"
=>
category
.
categorizations
.
count
)
category
.
reload
assert_not_nil
category
.
categorizations_count
assert_equal
4
,
category
.
categorizations_count
category_2
=
categories
(
:technology
)
count_1
,
count_2
=
(
category
.
categorizations_count
||
0
),
(
category_2
.
categorizations_count
||
0
)
Category
.
update_counters
([
category
.
id
,
category_2
.
id
],
"categorizations_count"
=>
2
)
category
.
reload
;
category_2
.
reload
assert_equal
count_1
+
2
,
category
.
categorizations_count
assert_equal
count_2
+
2
,
category_2
.
categorizations_count
end
def
test_update_all
assert_equal
Topic
.
count
,
Topic
.
update_all
(
"content = 'bulk updated!'"
)
assert_equal
"bulk updated!"
,
Topic
.
find
(
1
).
content
...
...
activerecord/test/cases/counter_cache_test.rb
0 → 100755
浏览文件 @
bfca7d74
require
'cases/helper'
require
'models/topic'
require
'models/reply'
require
'models/category'
require
'models/categorization'
class
CounterCacheTest
<
ActiveRecord
::
TestCase
fixtures
:topics
,
:categories
,
:categorizations
setup
do
@topic
=
Topic
.
find
(
1
)
end
test
"increment counter"
do
assert_difference
'@topic.reload.replies_count'
do
Topic
.
increment_counter
(
:replies_count
,
@topic
.
id
)
end
end
test
"decrement counter"
do
assert_difference
'@topic.reload.replies_count'
,
-
1
do
Topic
.
decrement_counter
(
:replies_count
,
@topic
.
id
)
end
end
test
"reset counters"
do
# throw the count off by 1
Topic
.
increment_counter
(
:replies_count
,
@topic
.
id
)
# check that it gets reset
assert_difference
'@topic.reload.replies_count'
,
-
1
do
Topic
.
reset_counters
(
@topic
.
id
,
:replies
)
end
end
test
"update counter with initial null value"
do
category
=
categories
(
:general
)
assert_equal
2
,
category
.
categorizations
.
count
assert_nil
category
.
categorizations_count
Category
.
update_counters
(
category
.
id
,
:categorizations_count
=>
category
.
categorizations
.
count
)
assert_equal
2
,
category
.
reload
.
categorizations_count
end
test
"update counter for decrement"
do
assert_difference
'@topic.reload.replies_count'
,
-
3
do
Topic
.
update_counters
(
@topic
.
id
,
:replies_count
=>
-
3
)
end
end
test
"update counters of multiple records"
do
t1
,
t2
=
topics
(
:first
,
:second
)
assert_difference
[
't1.reload.replies_count'
,
't2.reload.replies_count'
],
2
do
Topic
.
update_counters
([
t1
.
id
,
t2
.
id
],
:replies_count
=>
2
)
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录