Welcome to mirror list, hosted at ThFree Co, Russian Federation.

index_base_spec.rb « async_indexes « database « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d6070ff215eae9941f14e153d1112f074ae2a24c (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::AsyncIndexes::IndexBase, feature_category: :database do
  describe '#perform' do
    subject { described_class.new(async_index) }

    let(:async_index) { create(:postgres_async_index) }
    let(:model) { Gitlab::Database.database_base_models[Gitlab::Database::PRIMARY_DATABASE_NAME] }
    let(:connection) { model.connection }

    around do |example|
      Gitlab::Database::SharedModel.using_connection(connection) do
        example.run
      end
    end

    describe '#preconditions_met?' do
      it 'raises errors if preconditions is not defined' do
        expect { subject.perform }.to raise_error NotImplementedError, 'must implement preconditions_met?'
      end
    end

    describe '#action_type' do
      before do
        allow(subject).to receive(:preconditions_met?).and_return(true)
      end

      it 'raises errors if action_type is not defined' do
        expect { subject.perform }.to raise_error NotImplementedError, 'must implement action_type'
      end
    end

    context 'with error handling' do
      before do
        allow(subject).to receive(:preconditions_met?).and_return(true)
        allow(subject).to receive(:action_type).and_return('test')
        allow(async_index.connection).to receive(:execute).and_call_original

        allow(async_index.connection)
          .to receive(:execute)
          .with(async_index.definition)
          .and_raise(ActiveRecord::StatementInvalid)
      end

      context 'on production' do
        before do
          allow(Gitlab::ErrorTracking).to receive(:should_raise_for_dev?).and_return(false)
        end

        it 'increases execution attempts' do
          expect { subject.perform }.to change { async_index.attempts }.by(1)

          expect(async_index.last_error).to be_present
          expect(async_index).not_to be_destroyed
        end

        it 'logs an error message including the index_name' do
          expect(Gitlab::AppLogger)
            .to receive(:error)
            .with(a_hash_including(:message, :index_name))
            .and_call_original

          subject.perform
        end
      end

      context 'on development' do
        it 'also raises errors' do
          expect { subject.perform }
            .to raise_error(ActiveRecord::StatementInvalid)
            .and change { async_index.attempts }.by(1)

          expect(async_index.last_error).to be_present
          expect(async_index).not_to be_destroyed
        end
      end
    end
  end
end