summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/github_import/label_finder_spec.rb
blob: 8ba766944d68bdc176d91b4671158bd094b05880 (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
require 'spec_helper'

describe Gitlab::GithubImport::LabelFinder, :clean_gitlab_redis_cache do
  let(:project) { create(:project) }
  let(:finder) { described_class.new(project) }
  let!(:bug) { create(:label, project: project, name: 'Bug') }
  let!(:feature) { create(:label, project: project, name: 'Feature') }

  describe '#id_for' do
    context 'with a cache in place' do
      before do
        finder.build_cache
      end

      it 'returns the ID of the given label' do
        expect(finder.id_for(feature.name)).to eq(feature.id)
      end

      it 'returns nil for an empty cache key' do
        key = finder.cache_key_for(bug.name)

        Gitlab::GithubImport::Caching.write(key, '')

        expect(finder.id_for(bug.name)).to be_nil
      end

      it 'returns nil for a non existing label name' do
        expect(finder.id_for('kittens')).to be_nil
      end
    end

    context 'without a cache in place' do
      it 'returns nil for a label' do
        expect(finder.id_for(feature.name)).to be_nil
      end
    end
  end

  describe '#build_cache' do
    it 'builds the cache of all project labels' do
      expect(Gitlab::GithubImport::Caching)
        .to receive(:write_multiple)
        .with(
          {
            "github-import/label-finder/#{project.id}/Bug" => bug.id,
            "github-import/label-finder/#{project.id}/Feature" => feature.id
          }
        )
        .and_call_original

      finder.build_cache
    end
  end

  describe '#cache_key_for' do
    it 'returns the cache key for a label name' do
      expect(finder.cache_key_for('foo'))
        .to eq("github-import/label-finder/#{project.id}/foo")
    end
  end
end