diff options
Diffstat (limited to 'spec/lib/gitlab/graphql/loaders')
5 files changed, 100 insertions, 4 deletions
diff --git a/spec/lib/gitlab/graphql/loaders/batch_lfs_oid_loader_spec.rb b/spec/lib/gitlab/graphql/loaders/batch_lfs_oid_loader_spec.rb index b3d57c899d5..ae5d9686c54 100644 --- a/spec/lib/gitlab/graphql/loaders/batch_lfs_oid_loader_spec.rb +++ b/spec/lib/gitlab/graphql/loaders/batch_lfs_oid_loader_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::Graphql::Loaders::BatchLfsOidLoader do +RSpec.describe Gitlab::Graphql::Loaders::BatchLfsOidLoader do include GraphqlHelpers let_it_be(:project) { create(:project, :repository) } diff --git a/spec/lib/gitlab/graphql/loaders/batch_model_loader_spec.rb b/spec/lib/gitlab/graphql/loaders/batch_model_loader_spec.rb index 79f9ecb39cf..cf1f00bc176 100644 --- a/spec/lib/gitlab/graphql/loaders/batch_model_loader_spec.rb +++ b/spec/lib/gitlab/graphql/loaders/batch_model_loader_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::Graphql::Loaders::BatchModelLoader do +RSpec.describe Gitlab::Graphql::Loaders::BatchModelLoader do describe '#find' do let(:issue) { create(:issue) } let(:user) { create(:user) } diff --git a/spec/lib/gitlab/graphql/loaders/batch_project_statistics_loader_spec.rb b/spec/lib/gitlab/graphql/loaders/batch_project_statistics_loader_spec.rb index ec2fcad31e5..7ae28fdcf0f 100644 --- a/spec/lib/gitlab/graphql/loaders/batch_project_statistics_loader_spec.rb +++ b/spec/lib/gitlab/graphql/loaders/batch_project_statistics_loader_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::Graphql::Loaders::BatchProjectStatisticsLoader do +RSpec.describe Gitlab::Graphql::Loaders::BatchProjectStatisticsLoader do describe '#find' do it 'only queries once for project statistics' do stats = create_list(:project_statistics, 2) diff --git a/spec/lib/gitlab/graphql/loaders/batch_root_storage_statistics_loader_spec.rb b/spec/lib/gitlab/graphql/loaders/batch_root_storage_statistics_loader_spec.rb index 38931f7ab5e..f73eace274d 100644 --- a/spec/lib/gitlab/graphql/loaders/batch_root_storage_statistics_loader_spec.rb +++ b/spec/lib/gitlab/graphql/loaders/batch_root_storage_statistics_loader_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::Graphql::Loaders::BatchRootStorageStatisticsLoader do +RSpec.describe Gitlab::Graphql::Loaders::BatchRootStorageStatisticsLoader do describe '#find' do it 'only queries once for project statistics' do stats = create_list(:namespace_root_storage_statistics, 2) diff --git a/spec/lib/gitlab/graphql/loaders/issuable_loader_spec.rb b/spec/lib/gitlab/graphql/loaders/issuable_loader_spec.rb new file mode 100644 index 00000000000..180966de895 --- /dev/null +++ b/spec/lib/gitlab/graphql/loaders/issuable_loader_spec.rb @@ -0,0 +1,96 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Graphql::Loaders::IssuableLoader do + subject { described_class.new(parent, finder) } + + let(:params) { HashWithIndifferentAccess.new } + + describe '#find_all' do + let(:finder) { double(:finder, params: params, execute: %i[x y z]) } + + where(:factory, :param_name) do + %i[project group].map { |thing| [thing, :"#{thing}_id"] } + end + + with_them do + let(:parent) { build_stubbed(factory) } + + it 'assignes the parent parameter, and batching_find_alls the finder' do + expect(subject.find_all).to contain_exactly(:x, :y, :z) + expect(params).to include(param_name => parent) + end + end + + context 'the parent is of an unexpected type' do + let(:parent) { build(:merge_request) } + + it 'raises an error if we pass an unexpected parent' do + expect { subject.find_all }.to raise_error(/Unexpected parent/) + end + end + end + + describe '#batching_find_all' do + context 'the finder params are anything other than [iids]' do + let(:finder) { double(:finder, params: params, execute: [:foo]) } + let(:parent) { build_stubbed(:project) } + + it 'batching_find_alls the finder, setting the correct parent parameter' do + expect(subject.batching_find_all).to eq([:foo]) + expect(params[:project_id]).to eq(parent) + end + + it 'allows a post-process block' do + expect(subject.batching_find_all(&:first)).to eq(:foo) + end + end + + context 'the finder params are exactly [iids]' do + # Dumb finder class, that only implements what we need, and has + # predictable query counts. + let(:finder_class) do + Class.new do + attr_reader :current_user, :params + + def initialize(user, args) + @current_user = user + @params = HashWithIndifferentAccess.new(args.to_h) + end + + def execute + params[:project_id].issues.where(iid: params[:iids]) + end + end + end + + it 'batches requests' do + issue_a = create(:issue) + issue_b = create(:issue) + issue_c = create(:issue, project: issue_a.project) + proj_1 = issue_a.project + proj_2 = issue_b.project + user = create(:user, developer_projects: [proj_1, proj_2]) + + finder_a = finder_class.new(user, iids: [issue_a.iid]) + finder_b = finder_class.new(user, iids: [issue_b.iid]) + finder_c = finder_class.new(user, iids: [issue_c.iid]) + + results = [] + + expect do + results.concat(described_class.new(proj_1, finder_a).batching_find_all) + results.concat(described_class.new(proj_2, finder_b).batching_find_all) + results.concat(described_class.new(proj_1, finder_c).batching_find_all) + end.not_to exceed_query_limit(0) + + expect do + results = results.map(&:sync) + end.not_to exceed_query_limit(2) + + expect(results).to contain_exactly(issue_a, issue_b, issue_c) + end + end + end +end |