Read about our upcoming Code of Conduct on this issue

This instance will be upgraded to Heptapod 0.28.1 on 2022-01-26 at 16:00 UTC+1 (a few minutes of down time)

remove_orphaned_label_links_spec.rb 1.38 KB
Newer Older
Stan Hu's avatar
Stan Hu committed
1
2
3
4
5
6
7
8
9
10
11
12
# frozen_string_literal: true

require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20180906051323_remove_orphaned_label_links.rb')

describe RemoveOrphanedLabelLinks, :migration do
  let(:label_links) { table(:label_links) }
  let(:labels) { table(:labels) }

  let(:project) { create(:project) } # rubocop:disable RSpec/FactoriesInMigrationSpecs
  let(:label) { create_label }

13
14
15
16
17
18
  before do
    # This migration was created before we introduced ProjectCiCdSetting#default_git_depth
    allow_any_instance_of(ProjectCiCdSetting).to receive(:default_git_depth?).and_return(true)
    allow_any_instance_of(ProjectCiCdSetting).to receive(:default_git_depth).and_return(nil)
  end

Stan Hu's avatar
Stan Hu committed
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
  context 'add foreign key on label_id' do
    let!(:label_link_with_label) { create_label_link(label_id: label.id) }
    let!(:label_link_without_label) { create_label_link(label_id: nil) }

    it 'removes orphaned labels without corresponding label' do
      expect { migrate! }.to change { LabelLink.count }.from(2).to(1)
    end

    it 'does not remove entries with valid label_id' do
      expect { migrate! }.not_to change { label_link_with_label.reload }
    end
  end

  def create_label(**opts)
    labels.create!(
      project_id: project.id,
      **opts
    )
  end

  def create_label_link(**opts)
    label_links.create!(
      target_id: 1,
      target_type: 'Issue',
      **opts
    )
  end
end