debian-mirror-gitlab/spec/lib/api/entities/merge_request_basic_spec.rb

71 lines
2.1 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::API::Entities::MergeRequestBasic do
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, :public) }
let_it_be(:merge_request) { create(:merge_request) }
let_it_be(:labels) { create_list(:label, 3) }
let_it_be(:merge_requests) { create_list(:labeled_merge_request, 10, :unique_branches, :with_diffs, labels: labels) }
# This mimics the behavior of the `Grape::Entity` serializer
def present(obj)
described_class.new(obj).presented
end
context "with :with_api_entity_associations scope" do
let(:scope) { MergeRequest.with_api_entity_associations }
it "avoids N+1 queries" do
query = scope.find(merge_request.id)
control = ActiveRecord::QueryRecorder.new do
present(query).to_json
end
# stub the `head_commit_sha` as it will trigger a
# backward compatibility query that is out-of-scope
# for this test whenever it is `nil`
allow_any_instance_of(MergeRequestDiff).to receive(:head_commit_sha).and_return(Gitlab::Git::BLANK_SHA)
query = scope.all
batch = ActiveRecord::QueryRecorder.new do
entities = query.map(&method(:present))
entities.to_json
end
# The current threshold is 3 query per entity maximum.
expect(batch.count).to be_within(3 * query.count).of(control.count)
end
end
2021-02-22 17:27:13 +05:30
context 'reviewers' do
context "when merge_request_reviewers FF is enabled" do
before do
stub_feature_flags(merge_request_reviewers: true)
merge_request.reviewers = [user]
end
it 'includes assigned reviewers' do
result = Gitlab::Json.parse(present(merge_request).to_json)
expect(result['reviewers'][0]['username']).to eq user.username
end
end
context "when merge_request_reviewers FF is disabled" do
before do
stub_feature_flags(merge_request_reviewers: false)
end
it 'does not include reviewers' do
result = Gitlab::Json.parse(present(merge_request).to_json)
expect(result.keys).not_to include('reviewers')
end
end
end
2020-07-28 23:09:34 +05:30
end