File: group_label_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (124 lines) | stat: -rw-r--r-- 3,893 bytes parent folder | download
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe GroupLabel, feature_category: :team_planning do
  describe 'relationships' do
    it { is_expected.to belong_to(:group) }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:group) }
  end

  describe '#subject' do
    it 'aliases group to subject' do
      subject = described_class.new(group: build(:group))

      expect(subject.subject).to be(subject.group)
    end
  end

  describe '#to_reference' do
    let(:label) { create(:group_label, title: 'feature') }

    context 'using id' do
      it 'returns a String reference to the object' do
        expect(label.to_reference).to eq "~#{label.id}"
      end
    end

    context 'using name' do
      it 'returns a String reference to the object' do
        expect(label.to_reference(format: :name)).to eq %(~"#{label.name}")
      end

      it 'uses id when name contains double quote' do
        label = create(:label, name: %q("irony"))
        expect(label.to_reference(format: :name)).to eq "~#{label.id}"
      end
    end

    context 'cross-project' do
      let(:namespace) { build_stubbed(:namespace) }
      let(:source_project) { build_stubbed(:project, namespace: namespace) }
      let(:target_project) { build_stubbed(:project, namespace: namespace) }

      it 'returns a String reference to the object' do
        expect(label.to_reference(source_project, target_container: target_project)).to(
          eq("#{source_project.path}~#{label.id}")
        )
      end
    end

    context 'cross groups reference' do
      let(:parent_group) { build_stubbed(:group) }
      let(:source_group) { build_stubbed(:group, parent: parent_group) }
      let(:target_group) { build_stubbed(:group, parent: parent_group) }

      it 'returns a String reference to the object' do
        expect(label.to_reference(source_group, target_container: target_group)).to(
          eq("#{source_group.full_path}~#{label.id}")
        )
      end
    end

    context 'cross group and project reference' do
      let(:parent_group) { build_stubbed(:group) }
      let(:source_group) { build_stubbed(:group, parent: parent_group) }
      let(:target_project) { build_stubbed(:project, namespace: parent_group) }

      it 'returns a full path label reference' do
        expect(label.to_reference(source_group, target_container: target_project)).to(
          eq("#{source_group.full_path}~#{label.id}")
        )

        expect(label.to_reference(target_project, target_container: source_group)).to(
          eq("#{target_project.full_path}~#{label.id}")
        )
      end
    end

    context 'using invalid format' do
      it 'raises error' do
        expect { label.to_reference(format: :invalid) }
          .to raise_error StandardError, /Unknown format/
      end
    end
  end

  describe '#preloaded_parent_container' do
    let_it_be(:label) { create(:group_label) }

    before do
      label.reload # ensure associations are not loaded
    end

    context 'when group is loaded' do
      it 'does not invoke a DB query' do
        label.group

        count = ActiveRecord::QueryRecorder.new { label.preloaded_parent_container }.count
        expect(count).to eq(0)
        expect(label.preloaded_parent_container).to eq(label.group)
      end
    end

    context 'when parent_container is loaded' do
      it 'does not invoke a DB query' do
        label.parent_container

        count = ActiveRecord::QueryRecorder.new { label.preloaded_parent_container }.count
        expect(count).to eq(0)
        expect(label.preloaded_parent_container).to eq(label.parent_container)
      end
    end

    context 'when none of them are loaded' do
      it 'invokes a DB query' do
        count = ActiveRecord::QueryRecorder.new { label.preloaded_parent_container }.count
        expect(count).to eq(1)
      end
    end
  end
end