summaryrefslogtreecommitdiff
path: root/spec/lib/bulk_imports/common/pipelines/entity_finisher_spec.rb
blob: f03a178b993a95b020f61621f5ddff3431f1ef48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BulkImports::Common::Pipelines::EntityFinisher do
  it 'updates the entity status to finished' do
    entity = create(:bulk_import_entity, :started)
    pipeline_tracker = create(:bulk_import_tracker, entity: entity)
    context = BulkImports::Pipeline::Context.new(pipeline_tracker)
    subject = described_class.new(context)

    expect_next_instance_of(Gitlab::Import::Logger) do |logger|
      expect(logger)
        .to receive(:info)
        .with(
          bulk_import_id: entity.bulk_import.id,
          bulk_import_entity_id: entity.id,
          bulk_import_entity_type: entity.source_type,
          pipeline_class: described_class.name,
          message: 'Entity finished'
        )
    end

    expect(context.portable).to receive(:try).with(:after_import)

    expect { subject.run }
      .to change(entity, :status_name).to(:finished)
  end

  context 'when entity is in a final finished or failed state' do
    shared_examples 'performs no state update' do |entity_state|
      it 'does nothing' do
        entity = create(:bulk_import_entity, entity_state)
        pipeline_tracker = create(:bulk_import_tracker, entity: entity)
        context = BulkImports::Pipeline::Context.new(pipeline_tracker)
        subject = described_class.new(context)

        expect { subject.run }
          .not_to change(entity, :status_name)
      end
    end

    include_examples 'performs no state update', :finished
    include_examples 'performs no state update', :failed
  end

  context 'when all entity trackers failed' do
    it 'marks entity as failed' do
      entity = create(:bulk_import_entity, :started)
      create(:bulk_import_tracker, :failed, entity: entity)
      pipeline_tracker = create(:bulk_import_tracker, entity: entity, relation: described_class)
      context = BulkImports::Pipeline::Context.new(pipeline_tracker)

      described_class.new(context).run

      expect(entity.reload.failed?).to eq(true)
    end
  end
end