summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/github_import/milestone_finder_spec.rb
blob: fe8652eb5a2f789b997a7501f2c6a728d69e50ce (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GithubImport::MilestoneFinder, :clean_gitlab_redis_cache do
  let_it_be(:project) { create(:project) }
  let_it_be(:milestone) { create(:milestone, project: project) }

  let(:finder) { described_class.new(project) }

  describe '#id_for' do
    let(:issuable) { double(:issuable, milestone_number: milestone.iid) }

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

      it 'returns the milestone ID of the given issuable' do
        expect(finder.id_for(issuable)).to eq(milestone.id)
      end

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

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

        expect(finder.id_for(issuable)).to be_nil
      end

      it 'returns nil for an issuable with a non-existing milestone' do
        expect(finder.id_for(double(:issuable, milestone_number: 5))).to be_nil
      end
    end

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

  describe '#build_cache' do
    it 'builds the cache of all project milestones' do
      expect(Gitlab::Cache::Import::Caching)
        .to receive(:write_multiple)
        .with("github-import/milestone-finder/#{project.id}/1" => milestone.id)
        .and_call_original

      finder.build_cache
    end
  end

  describe '#cache_key_for' do
    it 'returns the cache key for an IID' do
      expect(finder.cache_key_for(10))
        .to eq("github-import/milestone-finder/#{project.id}/10")
    end
  end
end