summaryrefslogtreecommitdiff
path: root/spec/services/projects/batch_open_issues_count_service_spec.rb
blob: 720d7036396c352b92f2312376d2e3b38f0a587c (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
require 'spec_helper'

describe Projects::BatchOpenIssuesCountService do
  let!(:project_1) { create(:project) }
  let!(:project_2) { create(:project) }

  let(:subject) { described_class.new([project_1, project_2]) }

  context '#refresh_cache', :use_clean_rails_memory_store_caching do
    before do
      create(:issue, project: project_1)
      create(:issue, project: project_1, confidential: true)

      create(:issue, project: project_2)
      create(:issue, project: project_2, confidential: true)
    end

    context 'when cache is clean' do
      it 'refreshes cache keys correctly' do
        subject.refresh_cache

        expect(Rails.cache.read(get_cache_key(subject, project_1))).to eq(2)
        expect(Rails.cache.read(get_cache_key(subject, project_2))).to eq(2)
        expect(Rails.cache.read(get_cache_key(subject, project_1, true))).to eq(1)
        expect(Rails.cache.read(get_cache_key(subject, project_1, true))).to eq(1)
      end
    end

    context 'when issues count is already cached' do
      before do
        create(:issue, project: project_2)
        subject.refresh_cache
      end

      it 'does update cache again' do
        expect(Rails.cache).not_to receive(:write)

        subject.refresh_cache
      end
    end
  end

  def get_cache_key(subject, project, public_key = false)
    service = subject.count_service.new(project)

    if public_key
      service.cache_key(service.class::PUBLIC_COUNT_KEY)
    else
      service.cache_key(service.class::TOTAL_COUNT_KEY)
    end
  end
end