diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-12-28 18:38:28 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-12-28 18:38:28 +0530 |
commit | 8f5d9eb0e2d05c5ca10c313bb47dbcab335c6fa7 (patch) | |
tree | eb15bd788eed4ae3a6be65044f131b9ae6ec0852 /activerecord/test | |
parent | aefa975fdde01b1beaacbe065fe4b2bad69295d3 (diff) | |
download | rails-8f5d9eb0e2d05c5ca10c313bb47dbcab335c6fa7.tar.gz rails-8f5d9eb0e2d05c5ca10c313bb47dbcab335c6fa7.tar.bz2 rails-8f5d9eb0e2d05c5ca10c313bb47dbcab335c6fa7.zip |
Add Relation#count
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index cf6708cc8b..ded4f2f479 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -358,4 +358,34 @@ class RelationTest < ActiveRecord::TestCase assert_raises(ArgumentError) { Post.scoped & Developer.scoped } end + def test_count + posts = Post.scoped + + assert_equal 7, posts.count + assert_equal 7, posts.count(:all) + assert_equal 7, posts.count(:id) + + assert_equal 1, posts.where('comments_count > 1').count + assert_equal 5, posts.where(:comments_count => 0).count + end + + def test_count_with_distinct + posts = Post.scoped + + assert_equal 3, posts.count(:comments_count, :distinct => true) + assert_equal 7, posts.count(:comments_count, :distinct => false) + + assert_equal 3, posts.select(:comments_count).count(:distinct => true) + assert_equal 7, posts.select(:comments_count).count(:distinct => false) + end + + def test_count_explicit_columns + Post.update_all(:comments_count => nil) + posts = Post.scoped + + assert_equal 7, posts.select('comments_count').count('id') + assert_equal 0, posts.select('comments_count').count + assert_equal 0, posts.count(:comments_count) + assert_equal 0, posts.count('comments_count') + end end |