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

label_finder_spec.rb « github_import « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e46595974d14bbe88db9a2571eb0cf24504c0d5f (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GithubImport::LabelFinder, :clean_gitlab_redis_cache, feature_category: :importers do
  let_it_be(:project) { create(:project) }
  let_it_be(:finder) { described_class.new(project) }
  let_it_be(:bug) { create(:label, project: project, name: 'Bug') }
  let_it_be(: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 'fetches object id from database if not in cache' do
        key = finder.cache_key_for(bug.name)

        Gitlab::Cache::Import::Caching.write(key, '')

        expect(finder.id_for(bug.name)).to eq(bug.id)
      end

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

      it 'returns nil and skips database read if cache has no record' do
        key = finder.cache_key_for(bug.name)

        Gitlab::Cache::Import::Caching.write(key, -1)

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

    context 'without a cache in place' do
      it 'caches the ID of a database row and returns the ID' do
        expect(Gitlab::Cache::Import::Caching)
        .to receive(:write)
        .with("github-import/label-finder/#{project.id}/#{feature.name}", feature.id)
        .and_call_original

        expect(finder.id_for(feature.name)).to eq(feature.id)
      end
    end

    context 'with FF import_fallback_to_db_empty_cache disabled' do
      before do
        stub_feature_flags(import_fallback_to_db_empty_cache: false)
      end

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

      it 'does not fetch object id from database if not in cache' do
        expect(finder.id_for(feature.name)).to be_nil
      end

      it 'fetches object id from cache if present' do
        finder.build_cache

        expect(finder.id_for(feature.name)).to eq(feature.id)
      end

      it 'returns -1 if cache is -1' do
        key = finder.cache_key_for(bug.name)

        Gitlab::Cache::Import::Caching.write(key, -1)

        expect(finder.id_for(bug.name)).to eq(-1)
      end
    end
  end

  describe '#build_cache' do
    it 'builds the cache of all project labels' do
      expect(Gitlab::Cache::Import::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