Skip to content
This repository
Browse code

Add tests for AssociationCollection#find_each and AssociationCollecti…

…on#find_in_batches
  • Loading branch information...
commit f23adf0ed4bd596c9e7688455489a79f8b35eb3f 1 parent 0b6f514
Pratik authored March 11, 2009
2  activerecord/lib/active_record/associations/association_collection.rb
@@ -60,7 +60,7 @@ def find(*args)
60 60
           @reflection.klass.find(*args)
61 61
         end
62 62
       end
63  
-      
  63
+
64 64
       # Fetches the first one using SQL if possible.
65 65
       def first(*args)
66 66
         if fetch_first_or_last_using_find?(args)
39  activerecord/test/cases/associations/has_many_associations_test.rb
@@ -219,6 +219,45 @@ def test_find_all
219 219
     assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length
220 220
   end
221 221
 
  222
+  def test_find_each
  223
+    firm = companies(:first_firm)
  224
+
  225
+    assert ! firm.clients.loaded?
  226
+
  227
+    assert_queries(3) do
  228
+      firm.clients.find_each(:batch_size => 1) {|c| assert_equal firm.id, c.firm_id }
  229
+    end
  230
+
  231
+    assert ! firm.clients.loaded?
  232
+  end
  233
+
  234
+  def test_find_each_with_conditions
  235
+    firm = companies(:first_firm)
  236
+
  237
+    assert_queries(2) do
  238
+      firm.clients.find_each(:batch_size => 1, :conditions => {:name => "Microsoft"}) do |c|
  239
+        assert_equal firm.id, c.firm_id
  240
+        assert_equal "Microsoft", c.name
  241
+      end
  242
+    end
  243
+
  244
+    assert ! firm.clients.loaded?
  245
+  end
  246
+
  247
+  def test_find_in_batches
  248
+    firm = companies(:first_firm)
  249
+
  250
+    assert ! firm.clients.loaded?
  251
+
  252
+    assert_queries(2) do
  253
+      firm.clients.find_in_batches(:batch_size => 2) do |clients|
  254
+        clients.each {|c| assert_equal firm.id, c.firm_id }
  255
+      end
  256
+    end
  257
+
  258
+    assert ! firm.clients.loaded?
  259
+  end
  260
+
222 261
   def test_find_all_sanitized
223 262
     firm = Firm.find(:first)
224 263
     summit = firm.clients.find(:all, :conditions => "name = 'Summit'")

0 notes on commit f23adf0

Please sign in to comment.
Something went wrong with that request. Please try again.