diff options
author | Mislav Marohnić <mislav.marohnic@gmail.com> | 2010-04-17 00:32:06 +0200 |
---|---|---|
committer | Mislav Marohnić <mislav.marohnic@gmail.com> | 2010-05-24 11:42:03 +0200 |
commit | bfca7d744d6172fc6c0bc05beaff2abe260a4f60 (patch) | |
tree | 47cfff1627da81a75d9ac5f38b6ffb035bcf159d /activerecord/test/cases/counter_cache_test.rb | |
parent | f493f31533cf8db7756be8ed62a749ed57b7c7cf (diff) | |
download | rails-bfca7d744d6172fc6c0bc05beaff2abe260a4f60.tar.gz rails-bfca7d744d6172fc6c0bc05beaff2abe260a4f60.tar.bz2 rails-bfca7d744d6172fc6c0bc05beaff2abe260a4f60.zip |
move counter_cache tests to a separate file and refactor
Diffstat (limited to 'activerecord/test/cases/counter_cache_test.rb')
-rwxr-xr-x | activerecord/test/cases/counter_cache_test.rb | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/activerecord/test/cases/counter_cache_test.rb b/activerecord/test/cases/counter_cache_test.rb new file mode 100755 index 0000000000..b72356cf6f --- /dev/null +++ b/activerecord/test/cases/counter_cache_test.rb @@ -0,0 +1,58 @@ +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 |