mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
parent
8bea972d16
commit
48ba1f01d4
2 changed files with 82 additions and 1 deletions
|
@ -64,7 +64,7 @@ module Kaminari
|
|||
end
|
||||
|
||||
def out_of_range?
|
||||
@records.empty?
|
||||
to_a.empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
# frozen_string_literal: true
|
||||
require 'test_helper'
|
||||
|
||||
if defined? ActiveRecord
|
||||
class PaginableWithoutCountTest < ActiveSupport::TestCase
|
||||
def self.startup
|
||||
26.times { User.create! }
|
||||
super
|
||||
end
|
||||
|
||||
def self.shutdown
|
||||
User.delete_all
|
||||
super
|
||||
end
|
||||
|
||||
test 'it does not make count queires after calling #each' do
|
||||
@scope = User.page(1).without_count
|
||||
@scope.each
|
||||
|
||||
assert_no_queries do
|
||||
assert_not @scope.last_page?
|
||||
end
|
||||
assert_no_queries do
|
||||
assert_not @scope.out_of_range?
|
||||
end
|
||||
end
|
||||
|
||||
test 'it does not make count queires after calling #last_page? or #out_of_range?' do
|
||||
@scope = User.page(1).without_count
|
||||
|
||||
assert_not @scope.last_page?
|
||||
assert_not @scope.out_of_range?
|
||||
assert_no_queries { @scope.each }
|
||||
end
|
||||
|
||||
test 'when on the first page' do
|
||||
@users = User.page(1).without_count.load
|
||||
|
||||
assert_equal 25, @users.size
|
||||
assert_equal 25, @users.each.size
|
||||
assert_not @users.last_page?
|
||||
assert_not @users.out_of_range?
|
||||
end
|
||||
|
||||
test 'when on the first page showing 26 elements' do
|
||||
@users = User.page(1).per(26).without_count.load
|
||||
|
||||
assert_equal 26, @users.size
|
||||
assert_equal 26, @users.each.size
|
||||
assert @users.last_page?
|
||||
assert_not @users.out_of_range?
|
||||
end
|
||||
|
||||
test 'when on the last page' do
|
||||
@users = User.page(2).without_count.load
|
||||
|
||||
assert_equal 1, @users.size
|
||||
assert_equal 1, @users.each.size
|
||||
assert @users.last_page?
|
||||
assert_not @users.out_of_range?
|
||||
end
|
||||
|
||||
test 'when out of range' do
|
||||
@users = User.page(3).without_count.load
|
||||
|
||||
assert_equal 0, @users.size
|
||||
assert_equal 0, @users.each.size
|
||||
assert_not @users.last_page?
|
||||
assert @users.out_of_range?
|
||||
end
|
||||
|
||||
def assert_no_queries
|
||||
subscriber = ActiveSupport::Notifications.subscribe 'sql.active_record' do
|
||||
raise 'A SQL query is being made to the db:'
|
||||
end
|
||||
yield
|
||||
ensure
|
||||
ActiveSupport::Notifications.unsubscribe subscriber
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue