File: tag_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 (149 lines) | stat: -rw-r--r-- 4,159 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::Tag, feature_category: :continuous_integration do
  let_it_be(:tags) do
    [create(:ci_tag, name: 'Awesome'), create(:ci_tag, name: 'awesome'), create(:ci_tag, name: 'epic')]
  end

  it { is_expected.to have_many(:taggings).class_name('Ci::Tagging') }

  describe 'validations' do
    it { is_expected.to validate_presence_of(:name) }
    it { is_expected.to validate_uniqueness_of(:name).ignoring_case_sensitivity }
    it { is_expected.to validate_length_of(:name).is_at_most(255) }
  end

  describe '.named' do
    it { expect(described_class.named('Awesome')).to contain_exactly(tags.first) }
  end

  describe '.named_like' do
    it { expect(described_class.named_like('awes')).to contain_exactly(*tags[0..1]) }
  end

  describe '.named_any' do
    it { expect(described_class.named_any(%w[awesome epic])).to contain_exactly(*tags[1..]) }
  end

  describe '.for_context' do
    it 'returns the tags for the specified context' do
      Ci::Tagging.create!(tag_id: tags.first.id, context: 'tags')

      expect(described_class.for_context('tags')).to contain_exactly(tags.first)
    end
  end

  describe '.find_or_create_all_with_like_by_name' do
    let(:tags) { %w[tag] }

    subject(:find_or_create) { described_class.find_or_create_all_with_like_by_name(tags) }

    it 'creates a tag' do
      expect { find_or_create }.to change { described_class.count }.by(1)
    end

    it 'returns the Tag record' do
      results = find_or_create

      expect(results.size).to eq(1)
      expect(results.first).to be_an_instance_of(described_class)
      expect(results.first.name).to eq('tag')
    end

    context 'with some tags already existing' do
      let(:tags) { %w[tag preexisting_tag tag2] }

      let_it_be(:preexisting_tag) do
        create(:ci_tag, name: 'preexisting_tag')
      end

      it 'creates only the missing tag' do
        expect(described_class).to receive(:insert_all)
          .with([{ name: 'tag' }, { name: 'tag2' }], unique_by: :name)
          .and_call_original

        expect { find_or_create }.to change { described_class.count }.by(2)
      end

      it 'returns the Tag records' do
        results = find_or_create

        expect(results.map(&:name)).to match_array(tags)
      end
    end

    context 'with all tags already existing' do
      let(:tags) { %w[preexisting_tag preexisting_tag2] }

      before_all do
        create(:ci_tag, name: 'preexisting_tag')
        create(:ci_tag, name: 'preexisting_tag2')
      end

      it 'does not create new tags' do
        expect { find_or_create }.not_to change { described_class.count }
      end

      it 'returns the Tag records' do
        results = find_or_create

        expect(results.map(&:name)).to match_array(tags)
      end
    end
  end

  describe '.find_or_create_with_like_by_name' do
    let(:tag_name) { 'tag' }

    subject(:find_or_create) { described_class.find_or_create_with_like_by_name(tag_name) }

    it 'creates a tag' do
      expect { find_or_create }.to change { described_class.count }.by(1)
    end

    it 'returns the Tag record' do
      result = find_or_create

      expect(result).to be_an_instance_of(described_class)
      expect(result.name).to eq(tag_name)
    end

    context 'when tag already exists' do
      let_it_be(:tag) { create(:ci_tag, name: 'tag') }

      it 'does not create new tag' do
        expect { find_or_create }.not_to change { described_class.count }
      end

      it 'returns the Tag record' do
        result = find_or_create

        expect(result.name).to eq(tag_name)
      end
    end
  end

  describe '#==' do
    let(:tag) { tags.first }

    let(:subclass) { Class.new(described_class) }

    it 'is equal to itself' do
      expect(tag).to eq(tag)
    end

    it 'is equal to another tag when the name matches' do
      expect(tag).to eq(described_class.new(name: tag.name))
    end

    it 'is equal the other when the class matches' do
      expect(tag).to eq(subclass.new(name: tag.name))
    end
  end

  describe '#to_s' do
    it { expect(tags.first.to_s).to eq(tags.first.name) }
  end
end