debian-mirror-gitlab/spec/lib/gitlab/pagination/keyset/pager_spec.rb

77 lines
2.2 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Pagination::Keyset::Pager do
2020-01-01 13:55:28 +05:30
let(:relation) { Project.all.order(id: :asc) }
let(:request) { double('request', page: page, apply_headers: nil) }
let(:page) { Gitlab::Pagination::Keyset::Page.new(order_by: { id: :asc }, per_page: 3) }
let(:next_page) { double('next page') }
before_all do
create_list(:project, 7)
end
describe '#paginate' do
subject { described_class.new(request).paginate(relation) }
2020-03-13 15:44:24 +05:30
it 'does not execute a query' do
2020-01-01 13:55:28 +05:30
expect do
subject
2020-03-13 15:44:24 +05:30
end.not_to exceed_query_limit(0)
2020-01-01 13:55:28 +05:30
end
2020-03-13 15:44:24 +05:30
it 'applies a LIMIT' do
expect(subject.limit_value).to eq(page.per_page)
end
it 'returns the limited relation' do
expect(subject).to eq(relation.limit(page.per_page))
end
context 'validating the order clause' do
let(:page) { Gitlab::Pagination::Keyset::Page.new(order_by: { created_at: :asc }, per_page: 3) }
it 'raises an error if has a different order clause than the page' do
expect { subject }.to raise_error(ArgumentError, /order_by does not match/)
end
end
end
describe '#finalize' do
let(:records) { relation.limit(page.per_page).load }
subject { described_class.new(request).finalize(records) }
2020-01-01 13:55:28 +05:30
it 'passes information about next page to request' do
2020-03-13 15:44:24 +05:30
lower_bounds = records.last.slice(:id)
expect(page).to receive(:next).with(lower_bounds).and_return(next_page)
2020-01-01 13:55:28 +05:30
expect(request).to receive(:apply_headers).with(next_page)
subject
end
context 'when retrieving the last page' do
let(:relation) { Project.where('id > ?', Project.maximum(:id) - page.per_page).order(id: :asc) }
2020-03-13 15:44:24 +05:30
it 'indicates there is another (likely empty) page' do
lower_bounds = records.last.slice(:id)
expect(page).to receive(:next).with(lower_bounds).and_return(next_page)
expect(request).to receive(:apply_headers).with(next_page)
2020-01-01 13:55:28 +05:30
subject
end
end
context 'when retrieving an empty page' do
let(:relation) { Project.where('id > ?', Project.maximum(:id) + 1).order(id: :asc) }
it 'indicates this is the last page' do
2020-03-13 15:44:24 +05:30
expect(request).not_to receive(:apply_headers)
2020-01-01 13:55:28 +05:30
subject
end
end
end
end