2009-02-23 06:10:24 -05:00
|
|
|
require 'cases/helper'
|
|
|
|
require 'models/post'
|
|
|
|
|
|
|
|
class EachTest < ActiveRecord::TestCase
|
|
|
|
fixtures :posts
|
|
|
|
|
|
|
|
def setup
|
2009-12-26 13:51:24 -05:00
|
|
|
@posts = Post.order("id asc")
|
2009-02-23 06:10:24 -05:00
|
|
|
@total = Post.count
|
2011-01-14 14:16:31 -05:00
|
|
|
Post.count('id') # preheat arel's table cache
|
2009-02-23 06:10:24 -05:00
|
|
|
end
|
2010-03-29 10:24:35 -04:00
|
|
|
|
2009-02-23 06:10:24 -05:00
|
|
|
def test_each_should_excecute_one_query_per_batch
|
|
|
|
assert_queries(Post.count + 1) do
|
2009-03-11 10:08:40 -04:00
|
|
|
Post.find_each(:batch_size => 1) do |post|
|
2009-02-23 06:10:24 -05:00
|
|
|
assert_kind_of Post, post
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-09 05:24:25 -04:00
|
|
|
def test_each_should_raise_if_select_is_set_without_id
|
|
|
|
assert_raise(RuntimeError) do
|
|
|
|
Post.find_each(:select => :title, :batch_size => 1) { |post| post }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_should_execute_if_id_is_in_select
|
2010-10-18 19:27:40 -04:00
|
|
|
assert_queries(6) do
|
2010-05-09 05:24:25 -04:00
|
|
|
Post.find_each(:select => "id, title, type", :batch_size => 2) do |post|
|
|
|
|
assert_kind_of Post, post
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-23 06:10:24 -05:00
|
|
|
def test_each_should_raise_if_the_order_is_set
|
|
|
|
assert_raise(RuntimeError) do
|
2009-03-11 10:08:40 -04:00
|
|
|
Post.find_each(:order => "title") { |post| post }
|
2009-02-23 06:10:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_should_raise_if_the_limit_is_set
|
|
|
|
assert_raise(RuntimeError) do
|
2009-03-11 10:08:40 -04:00
|
|
|
Post.find_each(:limit => 1) { |post| post }
|
2009-02-23 06:10:24 -05:00
|
|
|
end
|
|
|
|
end
|
2010-03-29 10:24:35 -04:00
|
|
|
|
|
|
|
def test_warn_if_limit_scope_is_set
|
|
|
|
ActiveRecord::Base.logger.expects(:warn)
|
|
|
|
Post.limit(1).find_each { |post| post }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_warn_if_order_scope_is_set
|
|
|
|
ActiveRecord::Base.logger.expects(:warn)
|
|
|
|
Post.order("title").find_each { |post| post }
|
|
|
|
end
|
|
|
|
|
2009-02-23 06:10:24 -05:00
|
|
|
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
|
2009-03-11 11:24:30 -04:00
|
|
|
|
|
|
|
def test_find_in_batches_shouldnt_excute_query_unless_needed
|
|
|
|
post_count = Post.count
|
|
|
|
|
|
|
|
assert_queries(2) do
|
|
|
|
Post.find_in_batches(:batch_size => post_count) {|batch| assert_kind_of Array, batch }
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_queries(1) do
|
|
|
|
Post.find_in_batches(:batch_size => post_count + 1) {|batch| assert_kind_of Array, batch }
|
|
|
|
end
|
|
|
|
end
|
2010-03-29 10:24:35 -04:00
|
|
|
end
|