debian-mirror-gitlab/spec/lib/gitlab/github_import/importer/diff_note_importer_spec.rb

158 lines
4.4 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::GithubImport::Importer::DiffNoteImporter do
2018-03-17 18:26:18 +05:30
let(:project) { create(:project) }
let(:client) { double(:client) }
let(:user) { create(:user) }
let(:created_at) { Time.new(2017, 1, 1, 12, 00) }
let(:updated_at) { Time.new(2017, 1, 1, 12, 15) }
let(:hunk) do
'@@ -1 +1 @@
-Hello
+Hello world'
end
let(:note) do
Gitlab::GithubImport::Representation::DiffNote.new(
noteable_type: 'MergeRequest',
noteable_id: 1,
commit_id: '123abc',
2021-11-11 11:23:49 +05:30
original_commit_id: 'original123abc',
2018-03-17 18:26:18 +05:30
file_path: 'README.md',
diff_hunk: hunk,
author: Gitlab::GithubImport::Representation::User
.new(id: user.id, login: user.username),
note: 'Hello',
created_at: created_at,
updated_at: updated_at,
github_id: 1
)
end
let(:importer) { described_class.new(note, project, client) }
describe '#execute' do
context 'when the merge request no longer exists' do
it 'does not import anything' do
2021-10-27 15:23:28 +05:30
expect(Gitlab::Database.main).not_to receive(:bulk_insert)
2018-03-17 18:26:18 +05:30
importer.execute
end
end
context 'when the merge request exists' do
let!(:merge_request) do
create(:merge_request, source_project: project, target_project: project)
end
before do
allow(importer)
.to receive(:find_merge_request_id)
.and_return(merge_request.id)
end
it 'imports the note' do
allow(importer.user_finder)
.to receive(:author_id_for)
.and_return([user.id, true])
2021-10-27 15:23:28 +05:30
expect(Gitlab::Database.main)
2018-03-17 18:26:18 +05:30
.to receive(:bulk_insert)
.with(
LegacyDiffNote.table_name,
[
{
2021-11-11 11:23:49 +05:30
discussion_id: anything,
2018-03-17 18:26:18 +05:30
noteable_type: 'MergeRequest',
noteable_id: merge_request.id,
project_id: project.id,
author_id: user.id,
note: 'Hello',
system: false,
2021-11-11 11:23:49 +05:30
commit_id: 'original123abc',
2018-03-17 18:26:18 +05:30
line_code: note.line_code,
type: 'LegacyDiffNote',
created_at: created_at,
updated_at: updated_at,
st_diff: note.diff_hash.to_yaml
}
]
)
.and_call_original
importer.execute
end
it 'imports the note when the author could not be found' do
allow(importer.user_finder)
.to receive(:author_id_for)
.and_return([project.creator_id, false])
2021-10-27 15:23:28 +05:30
expect(Gitlab::Database.main)
2018-03-17 18:26:18 +05:30
.to receive(:bulk_insert)
.with(
LegacyDiffNote.table_name,
[
{
2021-11-11 11:23:49 +05:30
discussion_id: anything,
2018-03-17 18:26:18 +05:30
noteable_type: 'MergeRequest',
noteable_id: merge_request.id,
project_id: project.id,
author_id: project.creator_id,
note: "*Created by: #{user.username}*\n\nHello",
system: false,
2021-11-11 11:23:49 +05:30
commit_id: 'original123abc',
2018-03-17 18:26:18 +05:30
line_code: note.line_code,
type: 'LegacyDiffNote',
created_at: created_at,
updated_at: updated_at,
st_diff: note.diff_hash.to_yaml
}
]
)
.and_call_original
importer.execute
end
it 'produces a valid LegacyDiffNote' do
allow(importer.user_finder)
.to receive(:author_id_for)
.and_return([user.id, true])
importer.execute
note = project.notes.diff_notes.take
expect(note).to be_valid
expect(note.diff).to be_an_instance_of(Gitlab::Git::Diff)
end
it 'does not import the note when a foreign key error is raised' do
allow(importer.user_finder)
.to receive(:author_id_for)
.and_return([project.creator_id, false])
2021-10-27 15:23:28 +05:30
expect(Gitlab::Database.main)
2018-03-17 18:26:18 +05:30
.to receive(:bulk_insert)
.and_raise(ActiveRecord::InvalidForeignKey, 'invalid foreign key')
expect { importer.execute }.not_to raise_error
end
end
end
describe '#find_merge_request_id' do
it 'returns a merge request ID' do
2020-01-01 13:55:28 +05:30
expect_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |instance|
expect(instance).to receive(:database_id).and_return(10)
end
2018-03-17 18:26:18 +05:30
expect(importer.find_merge_request_id).to eq(10)
end
end
end