Empact-will_paginate 2.3.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ plugin_root = File.join(File.dirname(__FILE__), '..')
2
+ version = ENV['RAILS_VERSION']
3
+ version = nil if version and version == ""
4
+
5
+ # first look for a symlink to a copy of the framework
6
+ if !version and framework_root = ["#{plugin_root}/rails", "#{plugin_root}/../../rails"].find { |p| File.directory? p }
7
+ puts "found framework root: #{framework_root}"
8
+ # this allows for a plugin to be tested outside of an app and without Rails gems
9
+ $:.unshift "#{framework_root}/activesupport/lib", "#{framework_root}/activerecord/lib", "#{framework_root}/actionpack/lib"
10
+ else
11
+ # simply use installed gems if available
12
+ puts "using Rails#{version ? ' ' + version : nil} gems"
13
+ require 'rubygems'
14
+
15
+ if version
16
+ gem 'rails', version
17
+ else
18
+ gem 'actionpack', '< 3.0.0.a'
19
+ gem 'activerecord', '< 3.0.0.a'
20
+ end
21
+ end
@@ -0,0 +1,143 @@
1
+ require 'helper'
2
+ require 'will_paginate/array'
3
+
4
+ class ArrayPaginationTest < Test::Unit::TestCase
5
+
6
+ def setup ; end
7
+
8
+ def test_simple
9
+ collection = ('a'..'e').to_a
10
+
11
+ [{ :page => 1, :per_page => 3, :expected => %w( a b c ) },
12
+ { :page => 2, :per_page => 3, :expected => %w( d e ) },
13
+ { :page => 1, :per_page => 5, :expected => %w( a b c d e ) },
14
+ { :page => 3, :per_page => 5, :expected => [] },
15
+ ].
16
+ each do |conditions|
17
+ expected = conditions.delete :expected
18
+ assert_equal expected, collection.paginate(conditions)
19
+ end
20
+ end
21
+
22
+ def test_defaults
23
+ result = (1..50).to_a.paginate
24
+ assert_equal 1, result.current_page
25
+ assert_equal 30, result.size
26
+ end
27
+
28
+ def test_deprecated_api
29
+ assert_raise(ArgumentError) { [].paginate(2) }
30
+ assert_raise(ArgumentError) { [].paginate(2, 10) }
31
+ end
32
+
33
+ def test_total_entries_has_precedence
34
+ result = %w(a b c).paginate :total_entries => 5
35
+ assert_equal 5, result.total_entries
36
+ end
37
+
38
+ def test_argument_error_with_params_and_another_argument
39
+ assert_raise ArgumentError do
40
+ [].paginate({}, 5)
41
+ end
42
+ end
43
+
44
+ def test_paginated_collection
45
+ entries = %w(a b c)
46
+ collection = create(2, 3, 10) do |pager|
47
+ assert_equal entries, pager.replace(entries)
48
+ end
49
+
50
+ assert_equal entries, collection
51
+ assert_respond_to_all collection, %w(total_pages each offset size current_page per_page total_entries)
52
+ assert_kind_of Array, collection
53
+ assert_instance_of Array, collection.entries
54
+ assert_equal 3, collection.offset
55
+ assert_equal 4, collection.total_pages
56
+ assert !collection.out_of_bounds?
57
+ end
58
+
59
+ def test_previous_next_pages
60
+ collection = create(1, 1, 3)
61
+ assert_nil collection.previous_page
62
+ assert_equal 2, collection.next_page
63
+
64
+ collection = create(2, 1, 3)
65
+ assert_equal 1, collection.previous_page
66
+ assert_equal 3, collection.next_page
67
+
68
+ collection = create(3, 1, 3)
69
+ assert_equal 2, collection.previous_page
70
+ assert_nil collection.next_page
71
+ end
72
+
73
+ def test_out_of_bounds
74
+ entries = create(2, 3, 2){}
75
+ assert entries.out_of_bounds?
76
+
77
+ entries = create(1, 3, 2){}
78
+ assert !entries.out_of_bounds?
79
+ end
80
+
81
+ def test_guessing_total_count
82
+ entries = create do |pager|
83
+ # collection is shorter than limit
84
+ pager.replace array
85
+ end
86
+ assert_equal 8, entries.total_entries
87
+
88
+ entries = create(2, 5, 10) do |pager|
89
+ # collection is shorter than limit, but we have an explicit count
90
+ pager.replace array
91
+ end
92
+ assert_equal 10, entries.total_entries
93
+
94
+ entries = create do |pager|
95
+ # collection is the same as limit; we can't guess
96
+ pager.replace array(5)
97
+ end
98
+ assert_equal nil, entries.total_entries
99
+
100
+ entries = create do |pager|
101
+ # collection is empty; we can't guess
102
+ pager.replace array(0)
103
+ end
104
+ assert_equal nil, entries.total_entries
105
+
106
+ entries = create(1) do |pager|
107
+ # collection is empty and we're on page 1,
108
+ # so the whole thing must be empty, too
109
+ pager.replace array(0)
110
+ end
111
+ assert_equal 0, entries.total_entries
112
+ end
113
+
114
+ def test_invalid_page
115
+ bad_inputs = [0, -1, nil, '', 'Schnitzel']
116
+
117
+ bad_inputs.each do |bad|
118
+ assert_raise(WillPaginate::InvalidPage) { create bad }
119
+ end
120
+ end
121
+
122
+ def test_invalid_per_page_setting
123
+ assert_raise(ArgumentError) { create(1, -1) }
124
+ end
125
+
126
+ def test_page_count_was_removed
127
+ assert_raise(NoMethodError) { create.page_count }
128
+ # It's `total_pages` now.
129
+ end
130
+
131
+ private
132
+ def create(page = 2, limit = 5, total = nil, &block)
133
+ if block_given?
134
+ WillPaginate::Collection.create(page, limit, total, &block)
135
+ else
136
+ WillPaginate::Collection.new(page, limit, total)
137
+ end
138
+ end
139
+
140
+ def array(size = 3)
141
+ Array.new(size)
142
+ end
143
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
3
+ libs = []
4
+
5
+ libs << 'irb/completion'
6
+ libs << File.join('lib', 'load_fixtures')
7
+
8
+ exec "#{irb} -Ilib:test#{libs.map{ |l| " -r #{l}" }.join} --simple-prompt"
@@ -0,0 +1,22 @@
1
+ sqlite3:
2
+ database: ":memory:"
3
+ adapter: sqlite3
4
+ timeout: 500
5
+
6
+ sqlite2:
7
+ database: ":memory:"
8
+ adapter: sqlite2
9
+
10
+ mysql:
11
+ adapter: mysql
12
+ username: root
13
+ password:
14
+ encoding: utf8
15
+ database: will_paginate_unittest
16
+
17
+ postgres:
18
+ adapter: postgresql
19
+ username: mislav
20
+ password:
21
+ database: will_paginate_unittest
22
+ min_messages: warning
@@ -0,0 +1,512 @@
1
+ require 'helper'
2
+ require 'lib/activerecord_test_case'
3
+
4
+ require 'will_paginate'
5
+ WillPaginate.enable_activerecord
6
+ WillPaginate.enable_named_scope
7
+
8
+ class FinderTest < ActiveRecordTestCase
9
+ fixtures :topics, :replies, :users, :projects, :developers_projects
10
+
11
+ def test_new_methods_presence
12
+ assert_respond_to_all Topic, %w(per_page paginate paginate_by_sql paginate_by_definition_in_class)
13
+ end
14
+
15
+ def test_simple_paginate
16
+ assert_queries(1) do
17
+ entries = Topic.paginate :page => nil
18
+ assert_equal 1, entries.current_page
19
+ assert_equal 1, entries.total_pages
20
+ assert_equal 4, entries.size
21
+ end
22
+
23
+ assert_queries(2) do
24
+ entries = Topic.paginate :page => 2
25
+ assert_equal 1, entries.total_pages
26
+ assert entries.empty?
27
+ end
28
+ end
29
+
30
+ def test_parameter_api
31
+ # :page parameter in options is required!
32
+ assert_raise(ArgumentError){ Topic.paginate }
33
+ assert_raise(ArgumentError){ Topic.paginate({}) }
34
+
35
+ # explicit :all should not break anything
36
+ assert_equal Topic.paginate(:page => nil), Topic.paginate(:all, :page => 1)
37
+
38
+ # :count could be nil and we should still not cry
39
+ assert_nothing_raised { Topic.paginate :page => 1, :count => nil }
40
+ end
41
+
42
+ def test_counting_when_integer_has_length_method
43
+ Integer.module_eval { def length; to_s.length; end }
44
+ begin
45
+ assert_equal 2, 11.length
46
+ entries = Developer.paginate :page => 1, :per_page => 5
47
+ assert_equal 11, entries.total_entries
48
+ assert_equal 5, entries.size
49
+ assert_equal 3, entries.total_pages
50
+ ensure
51
+ begin
52
+ Integer.module_eval { remove_method :length }
53
+ rescue
54
+ end
55
+ end
56
+ end
57
+
58
+ def test_paginate_with_per_page
59
+ entries = Topic.paginate :page => 1, :per_page => 1
60
+ assert_equal 1, entries.size
61
+ assert_equal 4, entries.total_pages
62
+
63
+ # Developer class has explicit per_page at 10
64
+ entries = Developer.paginate :page => 1
65
+ assert_equal 10, entries.size
66
+ assert_equal 2, entries.total_pages
67
+
68
+ entries = Developer.paginate :page => 1, :per_page => 5
69
+ assert_equal 11, entries.total_entries
70
+ assert_equal 5, entries.size
71
+ assert_equal 3, entries.total_pages
72
+ end
73
+
74
+ def test_paginate_with_order
75
+ entries = Topic.paginate :page => 1, :order => 'created_at desc'
76
+ expected = [topics(:futurama), topics(:harvey_birdman), topics(:rails), topics(:ar)].reverse
77
+ assert_equal expected, entries.to_a
78
+ assert_equal 1, entries.total_pages
79
+ end
80
+
81
+ def test_paginate_with_conditions
82
+ entries = Topic.paginate :page => 1, :conditions => ["created_at > ?", 30.minutes.ago]
83
+ expected = [topics(:rails), topics(:ar)]
84
+ assert_equal expected, entries.to_a
85
+ assert_equal 1, entries.total_pages
86
+ end
87
+
88
+ def test_paginate_with_include_and_conditions
89
+ entries = Topic.paginate \
90
+ :page => 1,
91
+ :include => :replies,
92
+ :conditions => "replies.content LIKE 'Bird%' ",
93
+ :per_page => 10
94
+
95
+ expected = Topic.find :all,
96
+ :include => 'replies',
97
+ :conditions => "replies.content LIKE 'Bird%' ",
98
+ :limit => 10
99
+
100
+ assert_equal expected, entries.to_a
101
+ assert_equal 1, entries.total_entries
102
+ end
103
+
104
+ def test_paginate_with_include_and_order
105
+ entries = nil
106
+ assert_queries(2) do
107
+ entries = Topic.paginate \
108
+ :page => 1,
109
+ :include => :replies,
110
+ :order => 'replies.created_at asc, topics.created_at asc',
111
+ :per_page => 10
112
+ end
113
+
114
+ expected = Topic.find :all,
115
+ :include => 'replies',
116
+ :order => 'replies.created_at asc, topics.created_at asc',
117
+ :limit => 10
118
+
119
+ assert_equal expected, entries.to_a
120
+ assert_equal 4, entries.total_entries
121
+ end
122
+
123
+ def test_paginate_associations_with_include
124
+ entries, project = nil, projects(:active_record)
125
+
126
+ assert_nothing_raised "THIS IS A BUG in Rails 1.2.3 that was fixed in [7326]. " +
127
+ "Please upgrade to a newer version of Rails." do
128
+ entries = project.topics.paginate \
129
+ :page => 1,
130
+ :include => :replies,
131
+ :conditions => "replies.content LIKE 'Nice%' ",
132
+ :per_page => 10
133
+ end
134
+
135
+ expected = Topic.find :all,
136
+ :include => 'replies',
137
+ :conditions => "project_id = #{project.id} AND replies.content LIKE 'Nice%' ",
138
+ :limit => 10
139
+
140
+ assert_equal expected, entries.to_a
141
+ end
142
+
143
+ def test_paginate_associations
144
+ dhh = users :david
145
+ expected_name_ordered = [projects(:action_controller), projects(:active_record)]
146
+ expected_id_ordered = [projects(:active_record), projects(:action_controller)]
147
+
148
+ assert_queries(2) do
149
+ # with association-specified order
150
+ entries = dhh.projects.paginate(:page => 1)
151
+ assert_equal expected_name_ordered, entries
152
+ assert_equal 2, entries.total_entries
153
+ end
154
+
155
+ # with explicit order
156
+ entries = dhh.projects.paginate(:page => 1, :order => 'projects.id')
157
+ assert_equal expected_id_ordered, entries
158
+ assert_equal 2, entries.total_entries
159
+
160
+ assert_nothing_raised { dhh.projects.find(:all, :order => 'projects.id', :limit => 4) }
161
+ entries = dhh.projects.paginate(:page => 1, :order => 'projects.id', :per_page => 4)
162
+ assert_equal expected_id_ordered, entries
163
+
164
+ # has_many with implicit order
165
+ topic = Topic.find(1)
166
+ expected = [replies(:spam), replies(:witty_retort)]
167
+ assert_equal expected.map(&:id).sort, topic.replies.paginate(:page => 1).map(&:id).sort
168
+ assert_equal expected.reverse, topic.replies.paginate(:page => 1, :order => 'replies.id ASC')
169
+ end
170
+
171
+ def test_paginate_association_extension
172
+ project = Project.find(:first)
173
+
174
+ assert_queries(2) do
175
+ entries = project.replies.paginate_recent :page => 1
176
+ assert_equal [replies(:brave)], entries
177
+ end
178
+ end
179
+
180
+ def test_paginate_with_joins
181
+ entries = nil
182
+
183
+ assert_queries(1) do
184
+ entries = Developer.paginate :page => 1,
185
+ :joins => 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id',
186
+ :conditions => 'project_id = 1'
187
+ assert_equal 2, entries.size
188
+ developer_names = entries.map &:name
189
+ assert developer_names.include?('David')
190
+ assert developer_names.include?('Jamis')
191
+ end
192
+
193
+ assert_queries(1) do
194
+ expected = entries.to_a
195
+ entries = Developer.paginate :page => 1,
196
+ :joins => 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id',
197
+ :conditions => 'project_id = 1', :count => { :select => "users.id" }
198
+ assert_equal expected, entries.to_a
199
+ assert_equal 2, entries.total_entries
200
+ end
201
+ end
202
+
203
+ def test_paginate_with_group
204
+ entries = nil
205
+ assert_queries(1) do
206
+ entries = Developer.paginate :page => 1, :per_page => 10,
207
+ :group => 'salary', :select => 'salary', :order => 'salary'
208
+ end
209
+
210
+ expected = [ users(:david), users(:jamis), users(:dev_10), users(:poor_jamis) ].map(&:salary).sort
211
+ assert_equal expected, entries.map(&:salary)
212
+ end
213
+
214
+ def test_paginate_with_dynamic_finder
215
+ expected = [replies(:witty_retort), replies(:spam)]
216
+ assert_equal expected, Reply.paginate_by_topic_id(1, :page => 1, :order => :created_at)
217
+
218
+ entries = Developer.paginate :conditions => { :salary => 100000 }, :page => 1, :per_page => 5
219
+ assert_equal 8, entries.total_entries
220
+ assert_equal entries, Developer.paginate_by_salary(100000, :page => 1, :per_page => 5)
221
+
222
+ # dynamic finder + conditions
223
+ entries = Developer.paginate_by_salary(100000, :page => 1,
224
+ :conditions => ['id > ?', 6])
225
+ assert_equal 4, entries.total_entries
226
+ assert_equal (7..10).to_a, entries.map(&:id)
227
+
228
+ assert_raises NoMethodError do
229
+ Developer.paginate_by_inexistent_attribute 100000, :page => 1
230
+ end
231
+ end
232
+
233
+ def test_scoped_paginate
234
+ entries = Developer.with_poor_ones { Developer.paginate :page => 1 }
235
+
236
+ assert_equal 2, entries.size
237
+ assert_equal 2, entries.total_entries
238
+ end
239
+
240
+ ## named_scope ##
241
+
242
+ def test_paginate_in_named_scope
243
+ entries = Developer.poor.paginate :page => 1, :per_page => 1
244
+
245
+ assert_equal 1, entries.size
246
+ assert_equal 2, entries.total_entries
247
+ end
248
+
249
+ def test_paginate_in_named_scope_on_habtm_association
250
+ project = projects(:active_record)
251
+ assert_queries(2) do
252
+ entries = project.developers.poor.paginate :page => 1, :per_page => 1
253
+
254
+ assert_equal 1, entries.size, 'one developer should be found'
255
+ assert_equal 1, entries.total_entries, 'only one developer should be found'
256
+ end
257
+ end
258
+
259
+ def test_paginate_in_named_scope_on_hmt_association
260
+ project = projects(:active_record)
261
+ expected = [replies(:brave)]
262
+
263
+ assert_queries(2) do
264
+ entries = project.replies.recent.paginate :page => 1, :per_page => 1
265
+ assert_equal expected, entries
266
+ assert_equal 1, entries.total_entries, 'only one reply should be found'
267
+ end
268
+ end
269
+
270
+ def test_paginate_in_named_scope_on_has_many_association
271
+ project = projects(:active_record)
272
+ expected = [topics(:ar)]
273
+
274
+ assert_queries(2) do
275
+ entries = project.topics.mentions_activerecord.paginate :page => 1, :per_page => 1
276
+ assert_equal expected, entries
277
+ assert_equal 1, entries.total_entries, 'only one topic should be found'
278
+ end
279
+ end
280
+
281
+ def test_named_scope_with_include
282
+ project = projects(:active_record)
283
+ entries = project.topics.with_replies_starting_with('AR ').paginate(:page => 1, :per_page => 1)
284
+ assert_equal 1, entries.size
285
+ end
286
+
287
+ ## misc ##
288
+
289
+ def test_count_and_total_entries_options_are_mutually_exclusive
290
+ e = assert_raise ArgumentError do
291
+ Developer.paginate :page => 1, :count => {}, :total_entries => 1
292
+ end
293
+ assert_match /exclusive/, e.to_s
294
+ end
295
+
296
+ def test_readonly
297
+ assert_nothing_raised { Developer.paginate :readonly => true, :page => 1 }
298
+ end
299
+
300
+ # this functionality is temporarily removed
301
+ def xtest_pagination_defines_method
302
+ pager = "paginate_by_created_at"
303
+ assert !User.methods.include_method?(pager), "User methods should not include `#{pager}` method"
304
+ # paginate!
305
+ assert 0, User.send(pager, nil, :page => 1).total_entries
306
+ # the paging finder should now be defined
307
+ assert User.methods.include_method?(pager), "`#{pager}` method should be defined on User"
308
+ end
309
+
310
+ # Is this Rails 2.0? Find out by testing find_all which was removed in [6998]
311
+ unless ActiveRecord::Base.respond_to? :find_all
312
+ def test_paginate_array_of_ids
313
+ # AR finders also accept arrays of IDs
314
+ # (this was broken in Rails before [6912])
315
+ assert_queries(1) do
316
+ entries = Developer.paginate((1..8).to_a, :per_page => 3, :page => 2, :order => 'id')
317
+ assert_equal (4..6).to_a, entries.map(&:id)
318
+ assert_equal 8, entries.total_entries
319
+ end
320
+ end
321
+ end
322
+
323
+ uses_mocha 'internals' do
324
+ def test_implicit_all_with_dynamic_finders
325
+ Topic.expects(:find_all_by_foo).returns([])
326
+ Topic.expects(:count).returns(0)
327
+ Topic.paginate_by_foo :page => 2
328
+ end
329
+
330
+ def test_guessing_the_total_count
331
+ Topic.expects(:find).returns(Array.new(2))
332
+ Topic.expects(:count).never
333
+
334
+ entries = Topic.paginate :page => 2, :per_page => 4
335
+ assert_equal 6, entries.total_entries
336
+ end
337
+
338
+ def test_guessing_that_there_are_no_records
339
+ Topic.expects(:find).returns([])
340
+ Topic.expects(:count).never
341
+
342
+ entries = Topic.paginate :page => 1, :per_page => 4
343
+ assert_equal 0, entries.total_entries
344
+ end
345
+
346
+ def test_extra_parameters_stay_untouched
347
+ Topic.expects(:find).with(:all, {:foo => 'bar', :limit => 4, :offset => 0 }).returns(Array.new(5))
348
+ Topic.expects(:count).with({:foo => 'bar'}).returns(1)
349
+
350
+ Topic.paginate :foo => 'bar', :page => 1, :per_page => 4
351
+ end
352
+
353
+ def test_count_skips_select
354
+ Developer.stubs(:find).returns([])
355
+ Developer.expects(:count).with({}).returns(0)
356
+ Developer.paginate :select => 'salary', :page => 2
357
+ end
358
+
359
+ def test_count_select_when_distinct
360
+ Developer.stubs(:find).returns([])
361
+ Developer.expects(:count).with(:select => 'DISTINCT salary').returns(0)
362
+ Developer.paginate :select => 'DISTINCT salary', :page => 2
363
+ end
364
+
365
+ def test_count_with_scoped_select_when_distinct
366
+ Developer.stubs(:find).returns([])
367
+ Developer.expects(:count).with(:select => 'DISTINCT users.id').returns(0)
368
+ Developer.distinct.paginate :page => 2
369
+ end
370
+
371
+ def test_should_use_scoped_finders_if_present
372
+ # scope-out compatibility
373
+ Topic.expects(:find_best).returns(Array.new(5))
374
+ Topic.expects(:with_best).returns(1)
375
+
376
+ Topic.paginate_best :page => 1, :per_page => 4
377
+ end
378
+
379
+ def test_paginate_by_sql
380
+ sql = "SELECT * FROM users WHERE type = 'Developer' ORDER BY id"
381
+ entries = Developer.paginate_by_sql(sql, :page => 2, :per_page => 3)
382
+ assert_equal 11, entries.total_entries
383
+ assert_equal [users(:dev_4), users(:dev_5), users(:dev_6)], entries
384
+ end
385
+
386
+ def test_paginate_by_sql_respects_total_entries_setting
387
+ sql = "SELECT * FROM users"
388
+ entries = Developer.paginate_by_sql(sql, :page => 1, :total_entries => 999)
389
+ assert_equal 999, entries.total_entries
390
+ end
391
+
392
+ def test_paginate_by_sql_strips_order_by_when_counting
393
+ Developer.expects(:find_by_sql).returns([])
394
+ Developer.expects(:count_by_sql).with("SELECT COUNT(*) FROM (sql\n ) AS count_table").returns(0)
395
+
396
+ Developer.paginate_by_sql "sql\n ORDER\nby foo, bar, `baz` ASC", :page => 2
397
+ end
398
+
399
+ # TODO: counts are still wrong
400
+ def test_ability_to_use_with_custom_finders
401
+ # acts_as_taggable defines find_tagged_with(tag, options)
402
+ Topic.expects(:find_tagged_with).with('will_paginate', :offset => 5, :limit => 5).returns([])
403
+ Topic.expects(:count).with({}).returns(0)
404
+
405
+ Topic.paginate_tagged_with 'will_paginate', :page => 2, :per_page => 5
406
+ end
407
+
408
+ def test_array_argument_doesnt_eliminate_count
409
+ ids = (1..8).to_a
410
+ Developer.expects(:find_all_by_id).returns([])
411
+ Developer.expects(:count).returns(0)
412
+
413
+ Developer.paginate_by_id(ids, :per_page => 3, :page => 2, :order => 'id')
414
+ end
415
+
416
+ def test_paginating_finder_doesnt_mangle_options
417
+ Developer.expects(:find).returns([])
418
+ options = { :page => 1, :per_page => 2, :foo => 'bar' }
419
+ options_before = options.dup
420
+
421
+ Developer.paginate(options)
422
+ assert_equal options_before, options
423
+ end
424
+
425
+ def test_paginate_by_sql_doesnt_change_original_query
426
+ query = 'SQL QUERY'
427
+ original_query = query.dup
428
+ Developer.expects(:find_by_sql).returns([])
429
+
430
+ Developer.paginate_by_sql query, :page => 1
431
+ assert_equal original_query, query
432
+ end
433
+
434
+ def test_each_page
435
+ collection = stub('collection', :size => 5, :empty? => false, :per_page => 5)
436
+ collection.expects(:next_page).returns(2)
437
+ second_collection = stub('collection', :size => 5, :empty? => false, :per_page => 5)
438
+ second_collection.expects(:next_page).returns(3)
439
+ last_collection = stub('collection', :size => 4, :empty? => false, :per_page => 5)
440
+ last_collection.expects(:next_page).returns(nil)
441
+
442
+ params = { :order => 'id', :total_entries => 0 }
443
+
444
+ Developer.expects(:paginate).with(:page => 1).returns(collection)
445
+ Developer.expects(:paginate).with(:page => 2).returns(second_collection)
446
+ Developer.expects(:paginate).with(:page => 3).returns(last_collection)
447
+ assert_nil Developer.each_page { }
448
+ end
449
+
450
+ def test_paginated_each
451
+ collection = stub('collection', :size => 5, :empty? => false, :per_page => 5)
452
+ collection.expects(:each).times(2).returns(collection)
453
+ last_collection = stub('collection', :size => 4, :empty? => false, :per_page => 5)
454
+ last_collection.expects(:each).returns(last_collection)
455
+
456
+ params = { :order => 'id', :total_entries => 0 }
457
+
458
+ Developer.expects(:paginate).with(params.merge(:page => 2)).returns(collection)
459
+ Developer.expects(:paginate).with(params.merge(:page => 3)).returns(collection)
460
+ Developer.expects(:paginate).with(params.merge(:page => 4)).returns(last_collection)
461
+
462
+ assert_equal 14, Developer.paginated_each(:page => '2') { }
463
+ end
464
+
465
+ def test_paginated_each_with_named_scope
466
+ assert_equal 2, Developer.poor.paginated_each(:per_page => 1) {
467
+ assert_equal 11, Developer.count
468
+ }
469
+ end
470
+
471
+ # detect ActiveRecord 2.1
472
+ if ActiveRecord::Base.private_methods.include_method?(:references_eager_loaded_tables?)
473
+ def test_removes_irrelevant_includes_in_count
474
+ Developer.expects(:find).returns([1])
475
+ Developer.expects(:count).with({}).returns(0)
476
+
477
+ Developer.paginate :page => 1, :per_page => 1, :include => :projects
478
+ end
479
+
480
+ def test_doesnt_remove_referenced_includes_in_count
481
+ Developer.expects(:find).returns([1])
482
+ Developer.expects(:count).with({ :include => :projects, :conditions => 'projects.id > 2' }).returns(0)
483
+
484
+ Developer.paginate :page => 1, :per_page => 1,
485
+ :include => :projects, :conditions => 'projects.id > 2'
486
+ end
487
+ end
488
+
489
+ def test_paginate_from
490
+ result = Developer.paginate(:from => 'users', :page => 1, :per_page => 1)
491
+ assert_equal 1, result.size
492
+ end
493
+
494
+ def test_hmt_with_include
495
+ # ticket #220
496
+ reply = projects(:active_record).replies.find(:first, :order => 'replies.id')
497
+ assert_equal replies(:decisive), reply
498
+
499
+ # ticket #223
500
+ Project.find(1, :include => :replies)
501
+
502
+ # I cannot reproduce any of the failures from those reports :(
503
+ end
504
+
505
+ def test_hmt_with_uniq
506
+ project = Project.find(1)
507
+ result = project.unique_replies.paginate :page => 1, :per_page => 1,
508
+ :order => 'replies.id'
509
+ assert_equal replies(:decisive), result.first
510
+ end
511
+ end
512
+ end