PageRenderTime 48ms CodeModel.GetById 16ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/finders/branches_finder_spec.rb

https://gitlab.com/tnir/gitlab-ce
Ruby | 149 lines | 103 code | 45 blank | 1 comment | 0 complexity | 14b5b07474c7f19b9785563a7be4e068 MD5 | raw file
  1# frozen_string_literal: true
  2
  3require 'spec_helper'
  4
  5describe BranchesFinder do
  6  let(:user) { create(:user) }
  7  let(:project) { create(:project, :repository) }
  8  let(:repository) { project.repository }
  9
 10  describe '#execute' do
 11    context 'sort only' do
 12      it 'sorts by name' do
 13        branches_finder = described_class.new(repository, {})
 14
 15        result = branches_finder.execute
 16
 17        expect(result.first.name).to eq("'test'")
 18      end
 19
 20      it 'sorts by recently_updated' do
 21        branches_finder = described_class.new(repository, { sort: 'updated_desc' })
 22
 23        result = branches_finder.execute
 24
 25        recently_updated_branch = repository.branches.max do |a, b|
 26          repository.commit(a.dereferenced_target).committed_date <=> repository.commit(b.dereferenced_target).committed_date
 27        end
 28
 29        expect(result.first.name).to eq(recently_updated_branch.name)
 30      end
 31
 32      it 'sorts by last_updated' do
 33        branches_finder = described_class.new(repository, { sort: 'updated_asc' })
 34
 35        result = branches_finder.execute
 36
 37        expect(result.first.name).to eq('feature')
 38      end
 39    end
 40
 41    context 'filter only' do
 42      it 'filters branches by name' do
 43        branches_finder = described_class.new(repository, { search: 'fix' })
 44
 45        result = branches_finder.execute
 46
 47        expect(result.first.name).to eq('fix')
 48        expect(result.count).to eq(1)
 49      end
 50
 51      it 'filters branches by name ignoring letter case' do
 52        branches_finder = described_class.new(repository, { search: 'FiX' })
 53
 54        result = branches_finder.execute
 55
 56        expect(result.first.name).to eq('fix')
 57        expect(result.count).to eq(1)
 58      end
 59
 60      it 'does not find any branch with that name' do
 61        branches_finder = described_class.new(repository, { search: 'random' })
 62
 63        result = branches_finder.execute
 64
 65        expect(result.count).to eq(0)
 66      end
 67
 68      it 'filters branches by provided names' do
 69        branches_finder = described_class.new(repository, { names: %w[fix csv lfs does-not-exist] })
 70
 71        result = branches_finder.execute
 72
 73        expect(result.count).to eq(3)
 74        expect(result.map(&:name)).to eq(%w{csv fix lfs})
 75      end
 76
 77      it 'filters branches by name that begins with' do
 78        params = { search: '^feature_' }
 79        branches_finder = described_class.new(repository, params)
 80
 81        result = branches_finder.execute
 82
 83        expect(result.first.name).to eq('feature_conflict')
 84        expect(result.count).to eq(1)
 85      end
 86
 87      it 'filters branches by name that ends with' do
 88        params = { search: 'feature$' }
 89        branches_finder = described_class.new(repository, params)
 90
 91        result = branches_finder.execute
 92
 93        expect(result.first.name).to eq('feature')
 94        expect(result.count).to eq(1)
 95      end
 96
 97      it 'filters branches by nonexistent name that begins with' do
 98        params = { search: '^nope' }
 99        branches_finder = described_class.new(repository, params)
100
101        result = branches_finder.execute
102
103        expect(result.count).to eq(0)
104      end
105
106      it 'filters branches by nonexistent name that ends with' do
107        params = { search: 'nope$' }
108        branches_finder = described_class.new(repository, params)
109
110        result = branches_finder.execute
111
112        expect(result.count).to eq(0)
113      end
114    end
115
116    context 'filter and sort' do
117      it 'filters branches by name and sorts by recently_updated' do
118        params = { sort: 'updated_desc', search: 'feat' }
119        branches_finder = described_class.new(repository, params)
120
121        result = branches_finder.execute
122
123        expect(result.first.name).to eq('feature_conflict')
124        expect(result.count).to eq(2)
125      end
126
127      it 'filters branches by name and sorts by recently_updated, with exact matches first' do
128        params = { sort: 'updated_desc', search: 'feature' }
129        branches_finder = described_class.new(repository, params)
130
131        result = branches_finder.execute
132
133        expect(result.first.name).to eq('feature')
134        expect(result.second.name).to eq('feature_conflict')
135        expect(result.count).to eq(2)
136      end
137
138      it 'filters branches by name and sorts by last_updated' do
139        params = { sort: 'updated_asc', search: 'feature' }
140        branches_finder = described_class.new(repository, params)
141
142        result = branches_finder.execute
143
144        expect(result.first.name).to eq('feature')
145        expect(result.count).to eq(2)
146      end
147    end
148  end
149end