summaryrefslogtreecommitdiff
path: root/spec/services/base_count_service_spec.rb
blob: 090b2dcdd431e801e8f7857d8e4182d839a1e6d2 (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
require 'spec_helper'

describe BaseCountService, :use_clean_rails_memory_store_caching do
  let(:service) { described_class.new }

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

  describe '#count' do
    it 'returns the number of values' do
      expect(service)
        .to receive(:cache_key)
        .and_return('foo')

      expect(service)
        .to receive(:uncached_count)
        .and_return(5)

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

  describe '#uncached_count' do
    it 'returns the uncached number of values' do
      expect(service)
        .to receive(:relation_for_count)
        .and_return(double(:relation, count: 5))

      expect(service.uncached_count).to eq(5)
    end
  end

  describe '#refresh_cache' do
    it 'refreshes the cache' do
      allow(service)
        .to receive(:cache_key)
        .and_return('foo')

      allow(service)
        .to receive(:uncached_count)
        .and_return(4)

      service.refresh_cache

      expect(Rails.cache.fetch(service.cache_key, raw: service.raw?)).to eq(4)
    end
  end

  describe '#delete_cache' do
    it 'deletes the cache' do
      allow(service)
        .to receive(:cache_key)
        .and_return('foo')

      allow(service)
        .to receive(:uncached_count)
        .and_return(4)

      service.refresh_cache
      service.delete_cache

      expect(Rails.cache.fetch(service.cache_key, raw: service.raw?)).to be_nil
    end
  end

  describe '#raw?' do
    it 'returns false' do
      expect(service.raw?).to eq(false)
    end
  end

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

  describe '#cache_options' do
    it 'returns the default in options' do
      expect(service.cache_options).to eq({ raw: false })
    end
  end
end