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

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

  describe '.query' do
    it 'raises NotImplementedError' do
      expect { described_class.query(project.id) }.to raise_error(NotImplementedError)
    end
  end

  describe '#relation_for_count' do
    it 'calls the class method query with the project id' do
      expect(described_class).to receive(:query).with(project.id)

      service.relation_for_count
    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('foo')
      expect(service.cache_key).to eq(['projects', 'count_service', described_class::VERSION, 1, 'foo'])
    end
  end
end