debian-mirror-gitlab/spec/lib/gitlab/graphql/loaders/issuable_loader_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

106 lines
3.1 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Graphql::Loaders::IssuableLoader do
subject { described_class.new(parent, finder) }
let(:params) { HashWithIndifferentAccess.new }
2020-11-24 15:15:51 +05:30
let(:finder_params) { finder.params.to_h.with_indifferent_access }
# Dumb finder class, that only implements what we need, and has
# predictable query counts.
let(:finder_class) do
Class.new(IssuesFinder) do
def execute
params[:project_id].issues.where(iid: params[:iids])
end
private
def params_class
IssuesFinder::Params
end
end
end
2020-07-28 23:09:34 +05:30
describe '#find_all' do
2020-11-24 15:15:51 +05:30
let(:finder) { issuable_finder(params: params, result: [:x, :y, :z]) }
2020-07-28 23:09:34 +05:30
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)
2020-11-24 15:15:51 +05:30
expect(finder_params).to include(param_name => parent)
2020-07-28 23:09:34 +05:30
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
2020-11-24 15:15:51 +05:30
let(:finder) { issuable_finder(params: params, result: [:foo]) }
2020-07-28 23:09:34 +05:30
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])
2020-11-24 15:15:51 +05:30
expect(finder_params[:project_id]).to eq(parent)
2020-07-28 23:09:34 +05:30
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
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
2020-11-24 15:15:51 +05:30
private
def issuable_finder(user: double(:user), params: {}, result: nil)
new_finder = finder_class.new(user, params)
allow(new_finder).to receive(:execute).and_return(result) if result
new_finder
end
2020-07-28 23:09:34 +05:30
end