diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-06 18:07:44 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-06 18:07:44 +0000 |
commit | e1867c38fc5a4b931b4b2256d4909182e94f1051 (patch) | |
tree | 3047b637f7f9a31e74c62d3fe054b24c95e3534e /spec/lib/api | |
parent | 63894d59abd34f76f399d755012cdcd32c5b1103 (diff) | |
download | gitlab-ce-e1867c38fc5a4b931b4b2256d4909182e94f1051.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/api')
-rw-r--r-- | spec/lib/api/helpers/pagination_spec.rb | 56 | ||||
-rw-r--r-- | spec/lib/api/projects_batch_counting_spec.rb | 72 |
2 files changed, 126 insertions, 2 deletions
diff --git a/spec/lib/api/helpers/pagination_spec.rb b/spec/lib/api/helpers/pagination_spec.rb index 040ff1a8ebe..2d5bec2e752 100644 --- a/spec/lib/api/helpers/pagination_spec.rb +++ b/spec/lib/api/helpers/pagination_spec.rb @@ -5,10 +5,16 @@ require 'spec_helper' describe API::Helpers::Pagination do subject { Class.new.include(described_class).new } + let(:expected_result) { double("result", to_a: double) } + let(:relation) { double("relation") } + let(:params) { {} } + + before do + allow(subject).to receive(:params).and_return(params) + end + describe '#paginate' do - let(:relation) { double("relation") } let(:offset_pagination) { double("offset pagination") } - let(:expected_result) { double("result") } it 'delegates to OffsetPagination' do expect(::Gitlab::Pagination::OffsetPagination).to receive(:new).with(subject).and_return(offset_pagination) @@ -19,4 +25,50 @@ describe API::Helpers::Pagination do expect(result).to eq(expected_result) end end + + describe '#paginate_and_retrieve!' do + context 'for offset pagination' do + before do + allow(Gitlab::Pagination::Keyset).to receive(:available?).and_return(false) + end + + it 'delegates to paginate' do + expect(subject).to receive(:paginate).with(relation).and_return(expected_result) + + result = subject.paginate_and_retrieve!(relation) + + expect(result).to eq(expected_result.to_a) + end + end + + context 'for keyset pagination' do + let(:params) { { pagination: 'keyset' } } + let(:request_context) { double('request context') } + + before do + allow(Gitlab::Pagination::Keyset::RequestContext).to receive(:new).with(subject).and_return(request_context) + end + + context 'when keyset pagination is available' do + it 'delegates to KeysetPagination' do + expect(Gitlab::Pagination::Keyset).to receive(:available?).and_return(true) + expect(Gitlab::Pagination::Keyset).to receive(:paginate).with(request_context, relation).and_return(expected_result) + + result = subject.paginate_and_retrieve!(relation) + + expect(result).to eq(expected_result.to_a) + end + end + + context 'when keyset pagination is not available' do + it 'renders a 501 error if keyset pagination isnt available yet' do + expect(Gitlab::Pagination::Keyset).to receive(:available?).with(request_context, relation).and_return(false) + expect(Gitlab::Pagination::Keyset).not_to receive(:paginate) + expect(subject).to receive(:error!).with(/not yet available/, 405) + + subject.paginate_and_retrieve!(relation) + end + end + end + end end diff --git a/spec/lib/api/projects_batch_counting_spec.rb b/spec/lib/api/projects_batch_counting_spec.rb new file mode 100644 index 00000000000..6094952bb52 --- /dev/null +++ b/spec/lib/api/projects_batch_counting_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe API::ProjectsBatchCounting do + subject do + Class.new do + include ::API::ProjectsBatchCounting + end + end + + describe '.preload_and_batch_count!' do + let(:projects) { double } + let(:preloaded_projects) { double } + + it 'preloads the relation' do + allow(subject).to receive(:execute_batch_counting).with(preloaded_projects) + + expect(subject).to receive(:preload_relation).with(projects).and_return(preloaded_projects) + + expect(subject.preload_and_batch_count!(projects)).to eq(preloaded_projects) + end + + it 'executes batch counting' do + allow(subject).to receive(:preload_relation).with(projects).and_return(preloaded_projects) + + expect(subject).to receive(:execute_batch_counting).with(preloaded_projects) + + subject.preload_and_batch_count!(projects) + end + end + + describe '.execute_batch_counting' do + let(:projects) { create_list(:project, 2) } + let(:count_service) { double } + + it 'counts forks' do + allow(::Projects::BatchForksCountService).to receive(:new).with(projects).and_return(count_service) + + expect(count_service).to receive(:refresh_cache) + + subject.execute_batch_counting(projects) + end + + it 'counts open issues' do + allow(::Projects::BatchOpenIssuesCountService).to receive(:new).with(projects).and_return(count_service) + + expect(count_service).to receive(:refresh_cache) + + subject.execute_batch_counting(projects) + end + + context 'custom fork counting' do + subject do + Class.new do + include ::API::ProjectsBatchCounting + def self.forks_counting_projects(projects) + [projects.first] + end + end + end + + it 'counts forks for other projects' do + allow(::Projects::BatchForksCountService).to receive(:new).with([projects.first]).and_return(count_service) + + expect(count_service).to receive(:refresh_cache) + + subject.execute_batch_counting(projects) + end + end + end +end |