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

issuable_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: 977fef95d6426e2f5eeff3a2db0f2a4e9f21ddaf (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GithubImport::IssuableFinder, :clean_gitlab_redis_cache, feature_category: :importers do
  let(:project) { build(:project, id: 20, import_data_attributes: import_data_attributes) }
  let(:single_endpoint_optional_stage) { false }
  let(:import_data_attributes) do
    {
      data: {
        optional_stages: {
          single_endpoint_notes_import: single_endpoint_optional_stage
        }
      }
    }
  end

  let(:merge_request) { create(:merge_request, source_project: project) }
  let(:issue) { double(:issue, issuable_type: 'MergeRequest', issuable_id: merge_request.iid) }
  let(:finder) { described_class.new(project, issue) }

  describe '#database_id' do
    it 'returns nil if object does not exist' do
      missing_issue = double(:issue, issuable_type: 'MergeRequest', issuable_id: 999)

      expect(described_class.new(project, missing_issue).database_id).to be_nil
    end

    it 'fetches object id from database if not in cache' do
      expect(finder.database_id).to eq(merge_request.id)
    end

    it 'fetches object id from cache if present' do
      finder.cache_database_id(10)

      expect(finder.database_id).to eq(10)
    end

    it 'returns nil and skips database read if cache has no record' do
      finder.cache_database_id(-1)

      expect(finder.database_id).to be_nil
    end

    it 'raises TypeError when the object is not supported' do
      finder = described_class.new(project, double(:issue))

      expect { finder.database_id }.to raise_error(TypeError)
    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 if object does not exist' do
        missing_issue = double(:issue, issuable_type: 'MergeRequest', issuable_id: 999)

        expect(described_class.new(project, missing_issue).database_id).to be_nil
      end

      it 'does not fetch object id from database if not in cache' do
        expect(finder.database_id).to eq(nil)
      end

      it 'fetches object id from cache if present' do
        finder.cache_database_id(10)

        expect(finder.database_id).to eq(10)
      end

      it 'returns -1 if cache is -1' do
        finder.cache_database_id(-1)

        expect(finder.database_id).to eq(-1)
      end
    end

    context 'when group is present' do
      context 'when settings single_endpoint_notes_import is enabled' do
        let(:single_endpoint_optional_stage) { true }

        it 'reads cache value with longer timeout' do
          expect(Gitlab::Cache::Import::Caching)
            .to receive(:read)
            .with(anything, timeout: Gitlab::Cache::Import::Caching::LONGER_TIMEOUT)

          described_class.new(project, issue).database_id
        end
      end

      context 'when settings single_endpoint_notes_import is disabled' do
        it 'reads cache value with default timeout' do
          expect(Gitlab::Cache::Import::Caching)
            .to receive(:read)
            .with(anything, timeout: Gitlab::Cache::Import::Caching::TIMEOUT)

          described_class.new(project, issue).database_id
        end
      end
    end
  end

  describe '#cache_database_id' do
    it 'caches the ID of a database row' do
      expect(Gitlab::Cache::Import::Caching)
        .to receive(:write)
        .with("github-import/issuable-finder/20/MergeRequest/#{merge_request.iid}", 10, timeout: 86400)

      finder.cache_database_id(10)
    end

    context 'when settings single_endpoint_notes_import is enabled' do
      let(:single_endpoint_optional_stage) { true }

      it 'caches value with longer timeout' do
        expect(Gitlab::Cache::Import::Caching)
          .to receive(:write)
          .with(anything, anything, timeout: Gitlab::Cache::Import::Caching::LONGER_TIMEOUT)

        described_class.new(project, issue).cache_database_id(10)
      end
    end

    context 'when settings single_endpoint_notes_import is disabled' do
      it 'caches value with default timeout' do
        expect(Gitlab::Cache::Import::Caching)
          .to receive(:write)
          .with(anything, anything, timeout: Gitlab::Cache::Import::Caching::TIMEOUT)

        described_class.new(project, issue).cache_database_id(10)
      end
    end
  end
end