summaryrefslogtreecommitdiff
path: root/spec/finders/tags_finder_spec.rb
blob: 08978a32e509cc5e175ccfbe1a50d49dd4b0dd4a (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
# frozen_string_literal: true

require 'spec_helper'

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

  describe '#execute' do
    context 'sort only' do
      it 'sorts by name' do
        tags_finder = described_class.new(repository, {})

        result = tags_finder.execute

        expect(result.first.name).to eq("v1.0.0")
      end

      it 'sorts by recently_updated' do
        tags_finder = described_class.new(repository, { sort: 'updated_desc' })

        result = tags_finder.execute
        recently_updated_tag = repository.tags.max do |a, b|
          repository.commit(a.dereferenced_target).committed_date <=> repository.commit(b.dereferenced_target).committed_date
        end

        expect(result.first.name).to eq(recently_updated_tag.name)
      end

      it 'sorts by last_updated' do
        tags_finder = described_class.new(repository, { sort: 'updated_asc' })

        result = tags_finder.execute

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

    context 'filter only' do
      it 'filters tags by name' do
        tags_finder = described_class.new(repository, { search: '1.0.0' })

        result = tags_finder.execute

        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
        tags_finder = described_class.new(repository, { search: 'hey' })

        result = tags_finder.execute

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

      it 'filters tags by name that begins with' do
        params = { search: '^v1.0' }
        tags_finder = described_class.new(repository, params)

        result = tags_finder.execute

        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
        params = { search: '0.0$' }
        tags_finder = described_class.new(repository, params)

        result = tags_finder.execute

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

      it 'filters tags by nonexistent name that begins with' do
        params = { search: '^nope' }
        tags_finder = described_class.new(repository, params)

        result = tags_finder.execute

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

      it 'filters tags by nonexistent name that ends with' do
        params = { search: 'nope$' }
        tags_finder = described_class.new(repository, params)

        result = tags_finder.execute

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

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

      subject { described_class.new(repository, params).execute.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
  end
end