diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2009-02-23 12:10:24 +0100 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2009-02-23 12:11:02 +0100 |
commit | d13623ca46c82120c398f4634e206422fc3ad7ea (patch) | |
tree | c39ad7d17b073761f52eda2b9c0f4ff2e019beb8 /activerecord/test/cases | |
parent | 441e4e22352c8805a882f6a661ab3982dd7eda12 (diff) | |
download | rails-d13623ca46c82120c398f4634e206422fc3ad7ea.tar.gz rails-d13623ca46c82120c398f4634e206422fc3ad7ea.tar.bz2 rails-d13623ca46c82120c398f4634e206422fc3ad7ea.zip |
Added ActiveRecord::Base.each and ActiveRecord::Base.find_in_batches for batch processing [DHH/Jamis Buck]
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/batches_test.rb | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/activerecord/test/cases/batches_test.rb b/activerecord/test/cases/batches_test.rb new file mode 100644 index 0000000000..108d679108 --- /dev/null +++ b/activerecord/test/cases/batches_test.rb @@ -0,0 +1,49 @@ +require 'cases/helper' +require 'models/post' + +class EachTest < ActiveRecord::TestCase + fixtures :posts + + def setup + @posts = Post.all(:order => "id asc") + @total = Post.count + end + + def test_each_should_excecute_one_query_per_batch + assert_queries(Post.count + 1) do + Post.each(:batch_size => 1) do |post| + assert_kind_of Post, post + end + end + end + + def test_each_should_raise_if_the_order_is_set + assert_raise(RuntimeError) do + Post.each(:order => "title") { |post| post } + end + end + + def test_each_should_raise_if_the_limit_is_set + assert_raise(RuntimeError) do + Post.each(:limit => 1) { |post| post } + end + end + + def test_find_in_batches_should_return_batches + assert_queries(Post.count + 1) do + Post.find_in_batches(:batch_size => 1) do |batch| + assert_kind_of Array, batch + assert_kind_of Post, batch.first + end + end + end + + def test_find_in_batches_should_start_from_the_start_option + assert_queries(Post.count) do + Post.find_in_batches(:batch_size => 1, :start => 2) do |batch| + assert_kind_of Array, batch + assert_kind_of Post, batch.first + end + end + end +end
\ No newline at end of file |