2011-06-06 14:17:44 -04:00
|
|
|
require 'cases/helper'
|
2009-02-23 06:10:24 -05:00
|
|
|
require 'models/post'
|
2012-09-22 09:57:54 -04:00
|
|
|
require 'models/subscriber'
|
2009-02-23 06:10:24 -05:00
|
|
|
|
|
|
|
class EachTest < ActiveRecord::TestCase
|
2012-09-22 09:57:54 -04:00
|
|
|
fixtures :posts, :subscribers
|
2009-02-23 06:10:24 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
2013-03-18 14:34:39 -04:00
|
|
|
def test_each_should_execute_one_query_per_batch
|
2013-05-21 04:26:13 -04:00
|
|
|
assert_queries(@total + 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
|
|
|
|
|
2013-03-28 09:45:14 -04:00
|
|
|
def test_each_should_not_return_query_chain_and_execute_only_one_query
|
2011-07-07 05:40:11 -04:00
|
|
|
assert_queries(1) do
|
|
|
|
result = Post.find_each(:batch_size => 100000){ }
|
|
|
|
assert_nil result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-18 05:21:52 -04:00
|
|
|
def test_each_should_return_an_enumerator_if_no_block_is_present
|
|
|
|
assert_queries(1) do
|
|
|
|
Post.find_each(:batch_size => 100000).with_index do |post, index|
|
|
|
|
assert_kind_of Post, post
|
|
|
|
assert_kind_of Integer, index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-29 15:42:07 -05:00
|
|
|
if Enumerator.method_defined? :size
|
|
|
|
def test_each_should_return_a_sized_enumerator
|
|
|
|
assert_equal 11, Post.find_each(:batch_size => 1).size
|
|
|
|
assert_equal 5, Post.find_each(:batch_size => 2, :start => 7).size
|
|
|
|
assert_equal 11, Post.find_each(:batch_size => 10_000).size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-18 05:21:52 -04:00
|
|
|
def test_each_enumerator_should_execute_one_query_per_batch
|
|
|
|
assert_queries(@total + 1) do
|
|
|
|
Post.find_each(:batch_size => 1).with_index do |post, index|
|
|
|
|
assert_kind_of Post, post
|
|
|
|
assert_kind_of Integer, index
|
|
|
|
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
|
2013-03-28 08:03:02 -04:00
|
|
|
Post.select(:title).find_each(batch_size: 1) { |post|
|
|
|
|
flunk "should not call this block"
|
|
|
|
}
|
2010-05-09 05:24:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_should_execute_if_id_is_in_select
|
2010-10-18 19:27:40 -04:00
|
|
|
assert_queries(6) do
|
2012-04-26 10:51:23 -04:00
|
|
|
Post.select("id, title, type").find_each(:batch_size => 2) do |post|
|
2010-05-09 05:24:25 -04:00
|
|
|
assert_kind_of Post, post
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2013-06-28 07:07:34 -04:00
|
|
|
def test_logger_not_required
|
|
|
|
previous_logger = ActiveRecord::Base.logger
|
|
|
|
ActiveRecord::Base.logger = nil
|
|
|
|
assert_nothing_raised do
|
|
|
|
Post.limit(1).find_each { |post| post }
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.logger = previous_logger
|
|
|
|
end
|
|
|
|
|
2009-02-23 06:10:24 -05:00
|
|
|
def test_find_in_batches_should_return_batches
|
2013-05-21 04:26:13 -04:00
|
|
|
assert_queries(@total + 1) do
|
2009-02-23 06:10:24 -05:00
|
|
|
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
|
2013-05-21 04:26:13 -04:00
|
|
|
assert_queries(@total) do
|
2009-02-23 06:10:24 -05:00
|
|
|
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
|
|
|
|
2013-03-28 10:55:43 -04:00
|
|
|
def test_find_in_batches_shouldnt_execute_query_unless_needed
|
2009-03-11 11:24:30 -04:00
|
|
|
assert_queries(2) do
|
2013-05-21 04:26:13 -04:00
|
|
|
Post.find_in_batches(:batch_size => @total) {|batch| assert_kind_of Array, batch }
|
2009-03-11 11:24:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_queries(1) do
|
2013-05-21 04:26:13 -04:00
|
|
|
Post.find_in_batches(:batch_size => @total + 1) {|batch| assert_kind_of Array, batch }
|
2009-03-11 11:24:30 -04:00
|
|
|
end
|
|
|
|
end
|
2011-03-27 04:54:38 -04:00
|
|
|
|
|
|
|
def test_find_in_batches_should_quote_batch_order
|
|
|
|
c = Post.connection
|
|
|
|
assert_sql(/ORDER BY #{c.quote_table_name('posts')}.#{c.quote_column_name('id')}/) do
|
|
|
|
Post.find_in_batches(:batch_size => 1) do |batch|
|
|
|
|
assert_kind_of Array, batch
|
|
|
|
assert_kind_of Post, batch.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-05-31 17:48:40 -04:00
|
|
|
|
|
|
|
def test_find_in_batches_should_not_use_records_after_yielding_them_in_case_original_array_is_modified
|
|
|
|
not_a_post = "not a post"
|
|
|
|
not_a_post.stubs(:id).raises(StandardError, "not_a_post had #id called on it")
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
Post.find_in_batches(:batch_size => 1) do |batch|
|
|
|
|
assert_kind_of Array, batch
|
|
|
|
assert_kind_of Post, batch.first
|
|
|
|
|
|
|
|
batch.map! { not_a_post }
|
|
|
|
end
|
|
|
|
end
|
2011-09-04 14:42:57 -04:00
|
|
|
end
|
2011-05-31 17:48:40 -04:00
|
|
|
|
2011-09-04 14:42:57 -04:00
|
|
|
def test_find_in_batches_should_ignore_the_order_default_scope
|
|
|
|
# First post is with title scope
|
|
|
|
first_post = PostWithDefaultScope.first
|
|
|
|
posts = []
|
|
|
|
PostWithDefaultScope.find_in_batches do |batch|
|
|
|
|
posts.concat(batch)
|
|
|
|
end
|
|
|
|
# posts.first will be ordered using id only. Title order scope should not apply here
|
|
|
|
assert_not_equal first_post, posts.first
|
|
|
|
assert_equal posts(:welcome), posts.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_in_batches_should_not_ignore_the_default_scope_if_it_is_other_then_order
|
2012-08-03 06:51:52 -04:00
|
|
|
special_posts_ids = SpecialPostWithDefaultScope.all.map(&:id).sort
|
2011-09-04 14:42:57 -04:00
|
|
|
posts = []
|
|
|
|
SpecialPostWithDefaultScope.find_in_batches do |batch|
|
|
|
|
posts.concat(batch)
|
|
|
|
end
|
|
|
|
assert_equal special_posts_ids, posts.map(&:id)
|
2011-05-31 17:48:40 -04:00
|
|
|
end
|
|
|
|
|
2014-01-29 15:02:13 -05:00
|
|
|
def test_find_in_batches_should_not_modify_passed_options
|
|
|
|
assert_nothing_raised do
|
|
|
|
Post.find_in_batches({ batch_size: 42, start: 1 }.freeze){}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-15 04:39:11 -04:00
|
|
|
def test_find_in_batches_should_use_any_column_as_primary_key
|
2012-09-22 09:57:54 -04:00
|
|
|
nick_order_subscribers = Subscriber.order('nick asc')
|
|
|
|
start_nick = nick_order_subscribers.second.nick
|
2012-09-15 04:39:11 -04:00
|
|
|
|
2012-09-22 09:57:54 -04:00
|
|
|
subscribers = []
|
|
|
|
Subscriber.find_in_batches(:batch_size => 1, :start => start_nick) do |batch|
|
|
|
|
subscribers.concat(batch)
|
2012-09-15 04:39:11 -04:00
|
|
|
end
|
|
|
|
|
2012-09-22 09:57:54 -04:00
|
|
|
assert_equal nick_order_subscribers[1..-1].map(&:id), subscribers.map(&:id)
|
2012-09-15 04:39:11 -04:00
|
|
|
end
|
2012-10-30 10:54:16 -04:00
|
|
|
|
|
|
|
def test_find_in_batches_should_use_any_column_as_primary_key_when_start_is_not_specified
|
|
|
|
assert_queries(Subscriber.count + 1) do
|
|
|
|
Subscriber.find_each(:batch_size => 1) do |subscriber|
|
|
|
|
assert_kind_of Subscriber, subscriber
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-12-05 12:20:03 -05:00
|
|
|
|
|
|
|
def test_find_in_batches_should_return_an_enumerator
|
|
|
|
enum = nil
|
|
|
|
assert_queries(0) do
|
|
|
|
enum = Post.find_in_batches(:batch_size => 1)
|
|
|
|
end
|
|
|
|
assert_queries(4) do
|
|
|
|
enum.first(4) do |batch|
|
|
|
|
assert_kind_of Array, batch
|
|
|
|
assert_kind_of Post, batch.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-01-29 14:53:54 -05:00
|
|
|
|
|
|
|
if Enumerator.method_defined? :size
|
|
|
|
def test_find_in_batches_should_return_a_sized_enumerator
|
|
|
|
assert_equal 11, Post.find_in_batches(:batch_size => 1).size
|
|
|
|
assert_equal 6, Post.find_in_batches(:batch_size => 2).size
|
|
|
|
assert_equal 4, Post.find_in_batches(:batch_size => 2, :start => 4).size
|
|
|
|
assert_equal 4, Post.find_in_batches(:batch_size => 3).size
|
|
|
|
assert_equal 1, Post.find_in_batches(:batch_size => 10_000).size
|
|
|
|
end
|
|
|
|
end
|
2010-03-29 10:24:35 -04:00
|
|
|
end
|