public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Improve test coverage when using the group option in find, has_many or 
has_and_belongs_to_many.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
miloops (author)
Thu Sep 11 13:41:55 -0700 2008
NZKoz (committer)
Thu Sep 11 13:51:57 -0700 2008
commit  a37c5ae961366e3d693991b51d0830d40ae37e08
tree    0fa9d5de486f5d2fc317676df182d34040a8da1a
parent  095ad690f3fb6df4e89446dd40e03cf2c204e42a
...
636
637
638
 
 
 
 
 
 
 
 
 
 
 
 
639
640
641
...
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
0
@@ -636,6 +636,18 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
0
     assert_equal 3, Developer.find(:all, :include => {:projects => :developers}, :conditions => 'developers_projects_join.joined_on IS NOT NULL', :group => group.join(",")).size
0
   end
0
 
0
+  def test_find_grouped
0
+    all_posts_from_category1 = Post.find(:all, :conditions => "category_id = 1", :joins => :categories)
0
+    grouped_posts_of_category1 = Post.find(:all, :conditions => "category_id = 1", :group => "author_id", :select => 'count(posts.id) as posts_count', :joins => :categories)
0
+    assert_equal 4, all_posts_from_category1.size
0
+    assert_equal 1, grouped_posts_of_category1.size
0
+  end
0
+
0
+  def test_find_scoped_grouped
0
+    assert_equal 4, categories(:general).posts_gruoped_by_title.size
0
+    assert_equal 1, categories(:technology).posts_gruoped_by_title.size
0
+  end
0
+
0
   def test_get_ids
0
     assert_equal projects(:active_record, :action_controller).map(&:id).sort, developers(:david).project_ids.sort
0
     assert_equal [projects(:active_record).id], developers(:jamis).project_ids
...
248
249
250
 
 
 
 
 
251
252
253
...
248
249
250
251
252
253
254
255
256
257
258
0
@@ -248,6 +248,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
0
     assert_equal 1, grouped_clients_of_firm1.size
0
   end
0
 
0
+  def test_find_scoped_grouped
0
+    assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.length
0
+    assert_equal 2, companies(:first_firm).clients_grouped_by_name.length
0
+  end
0
+
0
   def test_adding
0
     force_signal37_to_load_all_clients_of_firm
0
     natural = Client.new("name" => "Natural Company")
...
169
170
171
 
 
 
 
 
 
172
173
174
...
169
170
171
172
173
174
175
176
177
178
179
180
0
@@ -169,6 +169,12 @@ class FinderTest < ActiveRecord::TestCase
0
     assert_equal("fixture_3", developers.first.name)
0
   end
0
 
0
+  def test_find_with_group
0
+    developers =  Developer.find(:all, :group => "salary")
0
+    assert_equal 4, developers.size
0
+    assert_equal 4, developers.uniq(&:salary).size
0
+  end
0
+
0
   def test_find_with_entire_select_statement
0
     topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'"
0
 
...
160
161
162
163
164
 
 
165
166
167
...
160
161
162
 
 
163
164
165
166
167
0
@@ -160,8 +160,8 @@ class ReflectionTest < ActiveRecord::TestCase
0
 
0
   def test_reflection_of_all_associations
0
     # FIXME these assertions bust a lot
0
-    assert_equal 24, Firm.reflect_on_all_associations.size
0
-    assert_equal 18, Firm.reflect_on_all_associations(:has_many).size
0
+    assert_equal 26, Firm.reflect_on_all_associations.size
0
+    assert_equal 20, Firm.reflect_on_all_associations(:has_many).size
0
     assert_equal 6, Firm.reflect_on_all_associations(:has_one).size
0
     assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
0
   end
...
13
14
15
 
 
 
16
17
18
...
13
14
15
16
17
18
19
20
21
0
@@ -13,6 +13,9 @@ class Category < ActiveRecord::Base
0
   has_and_belongs_to_many :post_with_conditions,
0
                           :class_name => 'Post',
0
                           :conditions => { :title => 'Yet Another Testing Title' }
0
+
0
+  has_and_belongs_to_many :posts_gruoped_by_title, :class_name => "Post", :group => "title"
0
+
0
   def self.what_are_you
0
     'a category...'
0
   end
...
55
56
57
 
 
58
59
60
...
55
56
57
58
59
60
61
62
0
@@ -55,6 +55,8 @@ class Firm < Company
0
   has_many :readonly_clients, :class_name => 'Client', :readonly => true
0
   has_many :clients_using_primary_key, :class_name => 'Client',
0
            :primary_key => 'name', :foreign_key => 'firm_name'
0
+  has_many :clients_grouped_by_firm_id, :class_name => "Client", :group => "firm_id"
0
+  has_many :clients_grouped_by_name, :class_name => "Client", :group => "name"
0
 
0
   has_one :account, :foreign_key => "firm_id", :dependent => :destroy, :validate => true
0
   has_one :unvalidated_account, :foreign_key => "firm_id", :class_name => 'Account', :validate => false

Comments