summaryrefslogtreecommitdiff
path: root/spec/models/site_statistic_spec.rb
blob: 49b54fb6994e34f361e3a76eec88bcc800c9915c (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
require 'spec_helper'

describe SiteStatistic do
  describe '.fetch' do
    context 'existing record' do
      it 'returns existing SiteStatistic model' do
        statistics = create(:site_statistics)

        expect(described_class.fetch).to be_a(described_class)
        expect(described_class.fetch).to eq(statistics)
      end
    end

    context 'non existing record' do
      it 'creates a new SiteStatistic model' do
        expect(described_class.first).to be_nil
        expect(described_class.fetch).to be_a(described_class)
      end
    end
  end

  describe '.track' do
    context 'with allowed attributes' do
      let(:statistics) { create(:site_statistics) }

      it 'increases the attribute counter' do
        expect { described_class.track('repositories_count') }.to change { statistics.reload.repositories_count }.by(1)
        expect { described_class.track('wikis_count') }.to change { statistics.reload.wikis_count }.by(1)
      end

      it 'doesnt increase the attribute counter when an exception happens during transaction' do
        expect do
          begin
            described_class.transaction do
              described_class.track('repositories_count')

              raise StandardError
            end
          rescue StandardError
            # no-op
          end
        end.not_to change { statistics.reload.repositories_count }
      end
    end

    context 'with not allowed attributes' do
      it 'returns error' do
        expect { described_class.track('something_else') }.to raise_error(ArgumentError).with_message(/Invalid attribute: \'something_else\' to \'track\' method/)
      end
    end
  end

  describe '.untrack' do
    context 'with allowed attributes' do
      let(:statistics) { create(:site_statistics) }

      it 'decreases the attribute counter' do
        expect { described_class.untrack('repositories_count') }.to change { statistics.reload.repositories_count }.by(-1)
        expect { described_class.untrack('wikis_count') }.to change { statistics.reload.wikis_count }.by(-1)
      end

      it 'doesnt decrease the attribute counter when an exception happens during transaction' do
        expect do
          begin
            described_class.transaction do
              described_class.track('repositories_count')

              raise StandardError
            end
          rescue StandardError
            # no-op
          end
        end.not_to change { described_class.fetch.repositories_count }
      end
    end

    context 'with not allowed attributes' do
      it 'returns error' do
        expect { described_class.untrack('something_else') }.to raise_error(ArgumentError).with_message(/Invalid attribute: \'something_else\' to \'untrack\' method/)
      end
    end
  end

  describe '.recalculate_counters!' do
    it 'recalculates existing counters' do
      create(:project)
      described_class.fetch.update(repositories_count: 0, wikis_count: 0)

      described_class.recalculate_counters!

      expect(described_class.fetch.repositories_count).to eq(1)
      expect(described_class.fetch.wikis_count).to eq(1)
    end
  end
end