From 5306e60028ddabdffec7935a69bf0ac61b5f65be Mon Sep 17 00:00:00 2001 From: miloops Date: Thu, 11 Sep 2008 17:41:55 -0300 Subject: [PATCH] Improve test coverage when using the group option in find, has_many or has_and_belongs_to_many. --- .../has_and_belongs_to_many_associations_test.rb | 12 ++++++++++++ .../associations/has_many_associations_test.rb | 5 +++++ activerecord/test/cases/finder_test.rb | 6 ++++++ activerecord/test/cases/reflection_test.rb | 4 ++-- activerecord/test/models/category.rb | 3 +++ activerecord/test/models/company.rb | 2 ++ 6 files changed, 30 insertions(+), 2 deletions(-) diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index edca3c6..9981f4c 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -636,6 +636,18 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal 3, Developer.find(:all, :include => {:projects => :developers}, :conditions => 'developers_projects_join.joined_on IS NOT NULL', :group => group.join(",")).size end + def test_find_grouped + all_posts_from_category1 = Post.find(:all, :conditions => "category_id = 1", :joins => :categories) + grouped_posts_of_category1 = Post.find(:all, :conditions => "category_id = 1", :group => "author_id", :select => 'count(posts.id) as posts_count', :joins => :categories) + assert_equal 4, all_posts_from_category1.size + assert_equal 1, grouped_posts_of_category1.size + end + + def test_find_scoped_grouped + assert_equal 4, categories(:general).posts_gruoped_by_title.size + assert_equal 1, categories(:technology).posts_gruoped_by_title.size + end + def test_get_ids assert_equal projects(:active_record, :action_controller).map(&:id).sort, developers(:david).project_ids.sort assert_equal [projects(:active_record).id], developers(:jamis).project_ids diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index feac4b0..5bcbc5e 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -248,6 +248,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 1, grouped_clients_of_firm1.size end + def test_find_scoped_grouped + assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.length + assert_equal 2, companies(:first_firm).clients_grouped_by_name.length + end + def test_adding force_signal37_to_load_all_clients_of_firm natural = Client.new("name" => "Natural Company") diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 1cbc518..b9ed17c 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -169,6 +169,12 @@ class FinderTest < ActiveRecord::TestCase assert_equal("fixture_3", developers.first.name) end + def test_find_with_group + developers = Developer.find(:all, :group => "salary") + assert_equal 4, developers.size + assert_equal 4, developers.uniq(&:salary).size + end + def test_find_with_entire_select_statement topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'" diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index 4b86e32..e339ef4 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -160,8 +160,8 @@ class ReflectionTest < ActiveRecord::TestCase def test_reflection_of_all_associations # FIXME these assertions bust a lot - assert_equal 24, Firm.reflect_on_all_associations.size - assert_equal 18, Firm.reflect_on_all_associations(:has_many).size + assert_equal 26, Firm.reflect_on_all_associations.size + assert_equal 20, Firm.reflect_on_all_associations(:has_many).size assert_equal 6, Firm.reflect_on_all_associations(:has_one).size assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size end diff --git a/activerecord/test/models/category.rb b/activerecord/test/models/category.rb index 1660c61..a06dd78 100644 --- a/activerecord/test/models/category.rb +++ b/activerecord/test/models/category.rb @@ -13,6 +13,9 @@ class Category < ActiveRecord::Base has_and_belongs_to_many :post_with_conditions, :class_name => 'Post', :conditions => { :title => 'Yet Another Testing Title' } + + has_and_belongs_to_many :posts_gruoped_by_title, :class_name => "Post", :group => "title" + def self.what_are_you 'a category...' end diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index cd43594..1abf3f3 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -55,6 +55,8 @@ class Firm < Company has_many :readonly_clients, :class_name => 'Client', :readonly => true has_many :clients_using_primary_key, :class_name => 'Client', :primary_key => 'name', :foreign_key => 'firm_name' + has_many :clients_grouped_by_firm_id, :class_name => "Client", :group => "firm_id" + has_many :clients_grouped_by_name, :class_name => "Client", :group => "name" has_one :account, :foreign_key => "firm_id", :dependent => :destroy, :validate => true has_one :unvalidated_account, :foreign_key => "firm_id", :class_name => 'Account', :validate => false -- 1.5.5.1 From e8df0183993f8ae3065f84d6c26d59465ffc7675 Mon Sep 17 00:00:00 2001 From: miloops Date: Thu, 11 Sep 2008 19:14:06 -0300 Subject: [PATCH] Use select and change test so new tests can work on postgres. --- activerecord/test/cases/finder_test.rb | 4 ++-- activerecord/test/models/category.rb | 2 +- activerecord/test/models/company.rb | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index b9ed17c..9d0796e 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -170,9 +170,9 @@ class FinderTest < ActiveRecord::TestCase end def test_find_with_group - developers = Developer.find(:all, :group => "salary") + developers = Developer.find(:all, :group => "salary", :select => "salary") assert_equal 4, developers.size - assert_equal 4, developers.uniq(&:salary).size + assert_equal 4, developers.map(&:salary).uniq.size end def test_find_with_entire_select_statement diff --git a/activerecord/test/models/category.rb b/activerecord/test/models/category.rb index a06dd78..4e9d247 100644 --- a/activerecord/test/models/category.rb +++ b/activerecord/test/models/category.rb @@ -14,7 +14,7 @@ class Category < ActiveRecord::Base :class_name => 'Post', :conditions => { :title => 'Yet Another Testing Title' } - has_and_belongs_to_many :posts_gruoped_by_title, :class_name => "Post", :group => "title" + has_and_belongs_to_many :posts_gruoped_by_title, :class_name => "Post", :group => "title", :select => "title" def self.what_are_you 'a category...' diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index 1abf3f3..0eb8ae0 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -55,8 +55,8 @@ class Firm < Company has_many :readonly_clients, :class_name => 'Client', :readonly => true has_many :clients_using_primary_key, :class_name => 'Client', :primary_key => 'name', :foreign_key => 'firm_name' - has_many :clients_grouped_by_firm_id, :class_name => "Client", :group => "firm_id" - has_many :clients_grouped_by_name, :class_name => "Client", :group => "name" + has_many :clients_grouped_by_firm_id, :class_name => "Client", :group => "firm_id", :select => "firm_id" + has_many :clients_grouped_by_name, :class_name => "Client", :group => "name", :select => "name" has_one :account, :foreign_key => "firm_id", :dependent => :destroy, :validate => true has_one :unvalidated_account, :foreign_key => "firm_id", :class_name => 'Account', :validate => false -- 1.5.5.1