summaryrefslogtreecommitdiff
path: root/spec/services/projects/count_service_spec.rb
blob: 79b01e7620e9a55203b5e8212418e5de60a47dad (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
require 'spec_helper'

describe Projects::CountService do
  let(:project) { build(:project, id: 1) }
  let(:service) { described_class.new(project) }

  describe '#relation_for_count' do
    it 'raises NotImplementedError' do
      expect { service.relation_for_count }.to raise_error(NotImplementedError)
    end
  end

  describe '#count' do
    before do
      allow(service).to receive(:cache_key_name).and_return('count_service')
    end

    it 'returns the number of rows' do
      allow(service).to receive(:uncached_count).and_return(1)

      expect(service.count).to eq(1)
    end

    it 'caches the number of rows', :use_clean_rails_memory_store_caching do
      expect(service).to receive(:uncached_count).once.and_return(1)

      2.times do
        expect(service.count).to eq(1)
      end
    end
  end

  describe '#refresh_cache', :use_clean_rails_memory_store_caching do
    before do
      allow(service).to receive(:cache_key_name).and_return('count_service')
    end

    it 'refreshes the cache' do
      expect(service).to receive(:uncached_count).once.and_return(1)

      service.refresh_cache

      expect(service.count).to eq(1)
    end
  end

  describe '#delete_cache', :use_clean_rails_memory_store_caching do
    before do
      allow(service).to receive(:cache_key_name).and_return('count_service')
    end

    it 'removes the cache' do
      expect(service).to receive(:uncached_count).twice.and_return(1)

      service.count
      service.delete_cache
      service.count
    end
  end

  describe '#cache_key_name' do
    it 'raises NotImplementedError' do
      expect { service.cache_key_name }.to raise_error(NotImplementedError)
    end
  end

  describe '#cache_key' do
    it 'returns the cache key as an Array' do
      allow(service).to receive(:cache_key_name).and_return('count_service')
      expect(service.cache_key).to eq(['projects', 1, 'count_service'])
    end
  end
end