Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / activerecord / test / cases / batches_test.rb
1 require 'cases/helper'
2 require 'models/post'
3
4 class EachTest < ActiveRecord::TestCase
5 fixtures :posts
6
7 def setup
8 @posts = Post.all(:order => "id asc")
9 @total = Post.count
10 end
11
12 def test_each_should_excecute_one_query_per_batch
13 assert_queries(Post.count + 1) do
14 Post.find_each(:batch_size => 1) do |post|
15 assert_kind_of Post, post
16 end
17 end
18 end
19
20 def test_each_should_raise_if_the_order_is_set
21 assert_raise(RuntimeError) do
22 Post.find_each(:order => "title") { |post| post }
23 end
24 end
25
26 def test_each_should_raise_if_the_limit_is_set
27 assert_raise(RuntimeError) do
28 Post.find_each(:limit => 1) { |post| post }
29 end
30 end
31
32 def test_find_in_batches_should_return_batches
33 assert_queries(Post.count + 1) do
34 Post.find_in_batches(:batch_size => 1) do |batch|
35 assert_kind_of Array, batch
36 assert_kind_of Post, batch.first
37 end
38 end
39 end
40
41 def test_find_in_batches_should_start_from_the_start_option
42 assert_queries(Post.count) do
43 Post.find_in_batches(:batch_size => 1, :start => 2) do |batch|
44 assert_kind_of Array, batch
45 assert_kind_of Post, batch.first
46 end
47 end
48 end
49
50 def test_find_in_batches_shouldnt_excute_query_unless_needed
51 post_count = Post.count
52
53 assert_queries(2) do
54 Post.find_in_batches(:batch_size => post_count) {|batch| assert_kind_of Array, batch }
55 end
56
57 assert_queries(1) do
58 Post.find_in_batches(:batch_size => post_count + 1) {|batch| assert_kind_of Array, batch }
59 end
60 end
61 end