diff options
Diffstat (limited to 'spec/lib/gitlab/pagination')
3 files changed, 129 insertions, 8 deletions
diff --git a/spec/lib/gitlab/pagination/gitaly_keyset_pager_spec.rb b/spec/lib/gitlab/pagination/gitaly_keyset_pager_spec.rb index 132a0e9ca78..8a26e153385 100644 --- a/spec/lib/gitlab/pagination/gitaly_keyset_pager_spec.rb +++ b/spec/lib/gitlab/pagination/gitaly_keyset_pager_spec.rb @@ -108,7 +108,6 @@ RSpec.describe Gitlab::Pagination::GitalyKeysetPager do let(:expected_next_page_link) { %Q(<#{incoming_api_projects_url}?#{query.merge(page_token: branch2.name).to_query}>; rel="next") } it 'uses keyset pagination and adds link headers' do - expect(request_context).to receive(:header).with('Links', expected_next_page_link) expect(request_context).to receive(:header).with('Link', expected_next_page_link) pager.paginate(finder) @@ -119,7 +118,6 @@ RSpec.describe Gitlab::Pagination::GitalyKeysetPager do let(:branches) { [branch1] } it 'uses keyset pagination without link headers' do - expect(request_context).not_to receive(:header).with('Links', anything) expect(request_context).not_to receive(:header).with('Link', anything) pager.paginate(finder) diff --git a/spec/lib/gitlab/pagination/keyset/paginator_spec.rb b/spec/lib/gitlab/pagination/keyset/paginator_spec.rb new file mode 100644 index 00000000000..3c9a8913876 --- /dev/null +++ b/spec/lib/gitlab/pagination/keyset/paginator_spec.rb @@ -0,0 +1,120 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Pagination::Keyset::Paginator do + let_it_be(:project_1) { create(:project, created_at: 10.weeks.ago) } + let_it_be(:project_2) { create(:project, created_at: 2.weeks.ago) } + let_it_be(:project_3) { create(:project, created_at: 3.weeks.ago) } + let_it_be(:project_4) { create(:project, created_at: 5.weeks.ago) } + let_it_be(:project_5) { create(:project, created_at: 2.weeks.ago) } + + describe 'pagination' do + let(:per_page) { 10 } + let(:cursor) { nil } + let(:scope) { Project.order(created_at: :asc, id: :asc) } + let(:expected_order) { [project_1, project_4, project_3, project_2, project_5] } + + subject(:paginator) { scope.keyset_paginate(cursor: cursor, per_page: per_page) } + + context 'when per_page is greater than the record count' do + it { expect(paginator.records).to eq(expected_order) } + it { is_expected.not_to have_next_page } + it { is_expected.not_to have_previous_page } + + it 'has no next and previous cursor values' do + expect(paginator.cursor_for_next_page).to be_nil + expect(paginator.cursor_for_previous_page).to be_nil + end + end + + context 'when 0 records are returned' do + let(:scope) { Project.where(id: non_existing_record_id).order(created_at: :asc, id: :asc) } + + it { expect(paginator.records).to be_empty } + it { is_expected.not_to have_next_page } + it { is_expected.not_to have_previous_page } + end + + context 'when page size is smaller than the record count' do + let(:per_page) { 2 } + + it { expect(paginator.records).to eq(expected_order.first(2)) } + it { is_expected.to have_next_page } + it { is_expected.not_to have_previous_page } + + it 'has next page cursor' do + expect(paginator.cursor_for_next_page).not_to be_nil + end + + it 'does not have previous page cursor' do + expect(paginator.cursor_for_previous_page).to be_nil + end + + context 'when on the second page' do + let(:cursor) { scope.keyset_paginate(per_page: per_page).cursor_for_next_page } + + it { expect(paginator.records).to eq(expected_order[2...4]) } + it { is_expected.to have_next_page } + it { is_expected.to have_previous_page } + + context 'and then going back to the first page' do + let(:previous_page_cursor) { scope.keyset_paginate(cursor: cursor, per_page: per_page).cursor_for_previous_page } + + subject(:paginator) { scope.keyset_paginate(cursor: previous_page_cursor, per_page: per_page) } + + it { expect(paginator.records).to eq(expected_order.first(2)) } + it { is_expected.to have_next_page } + it { is_expected.not_to have_previous_page } + end + end + + context 'when jumping to the last page' do + let(:cursor) { scope.keyset_paginate(per_page: per_page).cursor_for_last_page } + + it { expect(paginator.records).to eq(expected_order.last(2)) } + it { is_expected.not_to have_next_page } + it { is_expected.to have_previous_page } + + context 'when paginating backwards' do + let(:previous_page_cursor) { scope.keyset_paginate(cursor: cursor, per_page: per_page).cursor_for_previous_page } + + subject(:paginator) { scope.keyset_paginate(cursor: previous_page_cursor, per_page: per_page) } + + it { expect(paginator.records).to eq(expected_order[-4...-2]) } + it { is_expected.to have_next_page } + it { is_expected.to have_previous_page } + end + + context 'when jumping to the first page' do + let(:first_page_cursor) { scope.keyset_paginate(cursor: cursor, per_page: per_page).cursor_for_first_page } + + subject(:paginator) { scope.keyset_paginate(cursor: first_page_cursor, per_page: per_page) } + + it { expect(paginator.records).to eq(expected_order.first(2)) } + it { is_expected.to have_next_page } + it { is_expected.not_to have_previous_page } + end + end + end + + describe 'default keyset direction parameter' do + let(:cursor_converter_class) { Gitlab::Pagination::Keyset::Paginator::Base64CursorConverter } + let(:per_page) { 2 } + + it 'exposes the direction parameter in the cursor' do + cursor = paginator.cursor_for_next_page + + expect(cursor_converter_class.parse(cursor)[:_kd]).to eq(described_class::FORWARD_DIRECTION) + end + end + end + + context 'when unsupported order is given' do + it 'raises error' do + scope = Project.order(path: :asc, name: :asc, id: :desc) # Cannot build 3 column order automatically + + expect { scope.keyset_paginate }.to raise_error(/does not support keyset pagination/) + end + end +end diff --git a/spec/lib/gitlab/pagination/keyset/request_context_spec.rb b/spec/lib/gitlab/pagination/keyset/request_context_spec.rb index d4255176a4e..619d8cca28c 100644 --- a/spec/lib/gitlab/pagination/keyset/request_context_spec.rb +++ b/spec/lib/gitlab/pagination/keyset/request_context_spec.rb @@ -57,14 +57,15 @@ RSpec.describe Gitlab::Pagination::Keyset::RequestContext do subject { described_class.new(request_context).apply_headers(next_page) } - it 'sets Links header with same host/path as the original request' do + it 'sets Link header with same host/path as the original request' do orig_uri = URI.parse(request_context.request.url) - expect(request_context).to receive(:header).twice do |name, header| + expect(request_context).to receive(:header).once do |name, header| first_link, _ = /<([^>]+)>; rel="next"/.match(header).captures uri = URI.parse(first_link) + expect(name).to eq('Link') expect(uri.host).to eq(orig_uri.host) expect(uri.path).to eq(orig_uri.path) end @@ -72,14 +73,15 @@ RSpec.describe Gitlab::Pagination::Keyset::RequestContext do subject end - it 'sets Links header with a link to the next page' do + it 'sets Link header with a link to the next page' do orig_uri = URI.parse(request_context.request.url) - expect(request_context).to receive(:header).twice do |name, header| + expect(request_context).to receive(:header).once do |name, header| first_link, _ = /<([^>]+)>; rel="next"/.match(header).captures query = CGI.parse(URI.parse(first_link).query) + expect(name).to eq('Link') expect(query.except('id_after')).to eq(CGI.parse(orig_uri.query).except('id_after')) expect(query['id_after']).to eq(['42']) end @@ -90,14 +92,15 @@ RSpec.describe Gitlab::Pagination::Keyset::RequestContext do context 'with descending order' do let(:next_page) { double('next page', order_by: { id: :desc }, lower_bounds: { id: 42 }) } - it 'sets Links header with a link to the next page' do + it 'sets Link header with a link to the next page' do orig_uri = URI.parse(request_context.request.url) - expect(request_context).to receive(:header).twice do |name, header| + expect(request_context).to receive(:header).once do |name, header| first_link, _ = /<([^>]+)>; rel="next"/.match(header).captures query = CGI.parse(URI.parse(first_link).query) + expect(name).to eq('Link') expect(query.except('id_before')).to eq(CGI.parse(orig_uri.query).except('id_before')) expect(query['id_before']).to eq(['42']) end |