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

tags_finder_spec.rb « finders « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 525c19ba137d2bb81c9fc7e6b69be70cc93f716d (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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe TagsFinder do
  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:repository) { project.repository }

  def load_tags(params, gitaly_pagination: false)
    described_class.new(repository, params).execute(gitaly_pagination: gitaly_pagination)
  end

  describe '#execute' do
    context 'sort only' do
      it 'sorts by name' do
        expect(load_tags({}).first.name).to eq("v1.0.0")
      end

      it 'sorts by recently_updated' do
        recently_updated_tag = repository.tags.max do |a, b|
          repository.commit(a.dereferenced_target).committed_date <=> repository.commit(b.dereferenced_target).committed_date
        end

        params = { sort: 'updated_desc' }

        expect(load_tags(params).first.name).to eq(recently_updated_tag.name)
      end

      it 'sorts by last_updated' do
        params = { sort: 'updated_asc' }

        expect(load_tags(params).first.name).to eq('v1.0.0')
      end

      context 'when sort is not a string' do
        it 'ignores sort parameter' do
          params = { sort: { 'invalid' => 'string' } }

          expect(load_tags(params).first.name).to eq('v1.0.0')
        end
      end
    end

    context 'filter only' do
      it 'filters tags by name' do
        result = load_tags({ search: '1.0.0' })

        expect(result.first.name).to eq('v1.0.0')
        expect(result.count).to eq(1)
      end

      it 'does not find any tags with that name' do
        expect(load_tags({ search: 'hey' }).count).to eq(0)
      end

      it 'filters tags by name that begins with' do
        result = load_tags({ search: '^v1.0' })

        expect(result.first.name).to eq('v1.0.0')
        expect(result.count).to eq(1)
      end

      it 'filters tags by name that ends with' do
        result = load_tags({ search: '0.0$' })

        expect(result.first.name).to eq('v1.0.0')
        expect(result.count).to eq(1)
      end

      it 'filters tags by name with wildcard' do
        result = load_tags({ search: 'v1.*.0' })

        expect(result.first.name).to eq('v1.0.0')
        expect(result.second.name).to eq('v1.1.0')
        expect(result.count).to eq(2)
      end

      it 'filters tags by nonexistent name that begins with' do
        result = load_tags({ search: '^nope' })

        expect(result.count).to eq(0)
      end

      it 'filters tags by nonexistent name that ends with' do
        result = load_tags({ search: 'nope$' })
        expect(result.count).to eq(0)
      end

      it 'filters tags by nonexistent name with wildcard' do
        result = load_tags({ search: 'n*e' })
        expect(result.count).to eq(0)
      end

      context 'when search is not a string' do
        it 'returns no matches' do
          result = load_tags({ search: { 'a' => 'b' } })
          expect(result.count).to eq(0)
        end
      end
    end

    context 'filter and sort' do
      let(:tags_to_compare) { %w[v1.0.0 v1.1.0] }

      subject { load_tags(params).select { |tag| tags_to_compare.include?(tag.name) } }

      context 'when sort by updated_desc' do
        let(:params) { { sort: 'updated_desc', search: 'v1' } }

        it 'filters tags by name' do
          expect(subject.first.name).to eq('v1.1.0')
          expect(subject.count).to eq(2)
        end
      end

      context 'when sort by updated_asc' do
        let(:params) { { sort: 'updated_asc', search: 'v1' } }

        it 'filters tags by name' do
          expect(subject.first.name).to eq('v1.0.0')
          expect(subject.count).to eq(2)
        end
      end
    end

    context 'with Gitaly pagination' do
      subject { load_tags(params, gitaly_pagination: true) }

      context 'by page_token and per_page' do
        let(:params) { { page_token: 'v1.0.0', per_page: 1 } }

        it 'filters tags' do
          result = subject

          expect(result.map(&:name)).to eq(%w[v1.1.0])
        end
      end

      context 'by next page_token and per_page' do
        let(:params) { { page_token: 'v1.1.0', per_page: 2 } }

        it 'filters branches' do
          result = subject

          expect(result.map(&:name)).to eq(%w[v1.1.1])
        end
      end

      context 'by per_page only' do
        let(:params) { { per_page: 2 } }

        it 'filters branches' do
          result = subject

          expect(result.map(&:name)).to eq(%w[v1.0.0 v1.1.0])
        end
      end

      context 'by page_token only' do
        let(:params) { { page_token: 'feature' } }

        it 'raises an error' do
          expect do
            subject
          end.to raise_error(Gitlab::Git::InvalidPageToken, 'Invalid page token: refs/tags/feature')
        end
      end

      context 'pagination and sort' do
        context 'by per_page' do
          let(:params) { { sort: 'updated_desc', per_page: 5 } }

          it 'filters branches' do
            result = subject

            expect(result.map(&:name)).to eq(%w[v1.1.1 v1.1.0 v1.0.0])
          end
        end

        context 'by page_token and per_page' do
          let(:params) { { sort: 'updated_desc', page_token: 'v1.1.1', per_page: 2 } }

          it 'filters branches' do
            result = subject

            expect(result.map(&:name)).to eq(%w[v1.1.0 v1.0.0])
          end
        end
      end

      context 'pagination and search' do
        let(:params) { { search: '1.1.1', per_page: 1 } }

        it 'ignores the pagination for search' do
          result = subject

          expect(result.map(&:name)).to eq(%w[v1.1.1])
        end
      end
    end

    context 'when Gitaly is unavailable' do
      it 'raises an exception' do
        expect(Gitlab::GitalyClient).to receive(:call).and_raise(GRPC::Unavailable)

        tags_finder = described_class.new(repository, {})

        expect { tags_finder.execute }.to raise_error(Gitlab::Git::CommandError)
      end
    end
  end
end