summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/batching_strategies/loose_index_scan_batching_strategy_spec.rb
blob: 1a00fd7c8b31f66eca0e31c7e3bd53d8477e1c9d (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::BatchingStrategies::LooseIndexScanBatchingStrategy, '#next_batch' do
  let(:batching_strategy) { described_class.new(connection: ActiveRecord::Base.connection) }
  let(:namespaces) { table(:namespaces) }
  let(:projects) { table(:projects) }
  let(:issues) { table(:issues) }

  let!(:namespace1) { namespaces.create!(name: 'ns1', path: 'ns1') }
  let!(:namespace2) { namespaces.create!(name: 'ns2', path: 'ns2') }
  let!(:namespace3) { namespaces.create!(name: 'ns3', path: 'ns3') }
  let!(:namespace4) { namespaces.create!(name: 'ns4', path: 'ns4') }
  let!(:namespace5) { namespaces.create!(name: 'ns5', path: 'ns5') }
  let!(:project1) { projects.create!(name: 'p1', namespace_id: namespace1.id, project_namespace_id: namespace1.id) }
  let!(:project2) { projects.create!(name: 'p2', namespace_id: namespace2.id, project_namespace_id: namespace2.id) }
  let!(:project3) { projects.create!(name: 'p3', namespace_id: namespace3.id, project_namespace_id: namespace3.id) }
  let!(:project4) { projects.create!(name: 'p4', namespace_id: namespace4.id, project_namespace_id: namespace4.id) }
  let!(:project5) { projects.create!(name: 'p5', namespace_id: namespace5.id, project_namespace_id: namespace5.id) }

  let!(:issue1) { issues.create!(title: 'title', description: 'description', project_id: project2.id) }
  let!(:issue2) { issues.create!(title: 'title', description: 'description', project_id: project1.id) }
  let!(:issue3) { issues.create!(title: 'title', description: 'description', project_id: project2.id) }
  let!(:issue4) { issues.create!(title: 'title', description: 'description', project_id: project3.id) }
  let!(:issue5) { issues.create!(title: 'title', description: 'description', project_id: project2.id) }
  let!(:issue6) { issues.create!(title: 'title', description: 'description', project_id: project4.id) }
  let!(:issue7) { issues.create!(title: 'title', description: 'description', project_id: project5.id) }

  it { expect(described_class).to be < Gitlab::BackgroundMigration::BatchingStrategies::BaseStrategy }

  context 'when starting on the first batch' do
    it 'returns the bounds of the next batch' do
      batch_bounds = batching_strategy
        .next_batch(:issues, :project_id, batch_min_value: project1.id, batch_size: 2, job_arguments: [])

      expect(batch_bounds).to eq([project1.id, project2.id])
    end
  end

  context 'when additional batches remain' do
    it 'returns the bounds of the next batch' do
      batch_bounds = batching_strategy
        .next_batch(:issues, :project_id, batch_min_value: project2.id, batch_size: 3, job_arguments: [])

      expect(batch_bounds).to eq([project2.id, project4.id])
    end
  end

  context 'when on the final batch' do
    it 'returns the bounds of the next batch' do
      batch_bounds = batching_strategy
        .next_batch(:issues, :project_id, batch_min_value: project4.id, batch_size: 3, job_arguments: [])

      expect(batch_bounds).to eq([project4.id, project5.id])
    end
  end

  context 'when no additional batches remain' do
    it 'returns nil' do
      batch_bounds = batching_strategy
        .next_batch(:issues, :project_id, batch_min_value: project5.id + 1, batch_size: 1, job_arguments: [])

      expect(batch_bounds).to be_nil
    end
  end
end