summaryrefslogtreecommitdiff
path: root/spec/services/snippets/update_statistics_service_spec.rb
blob: 27ae054676a30629ca366c247e98183e977dcc75 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Snippets::UpdateStatisticsService do
  describe '#execute' do
    subject { described_class.new(snippet).execute }

    shared_examples 'updates statistics' do
      it 'returns a successful response' do
        expect(subject).to be_success
      end

      it 'expires statistics cache' do
        expect(snippet.repository).to receive(:expire_statistics_caches)

        subject
      end

      context 'when snippet statistics does not exist' do
        it 'creates snippet statistics' do
          snippet.statistics.delete
          snippet.reload

          expect do
            subject
          end.to change(SnippetStatistics, :count).by(1)

          expect(snippet.statistics.commit_count).not_to be_zero
          expect(snippet.statistics.file_count).not_to be_zero
          expect(snippet.statistics.repository_size).not_to be_zero
        end
      end

      context 'when snippet statistics exists' do
        it 'updates snippet statistics' do
          expect(snippet.statistics.commit_count).to be_zero
          expect(snippet.statistics.file_count).to be_zero
          expect(snippet.statistics.repository_size).to be_zero

          subject

          expect(snippet.statistics.commit_count).not_to be_zero
          expect(snippet.statistics.file_count).not_to be_zero
          expect(snippet.statistics.repository_size).not_to be_zero
        end
      end

      context 'when snippet does not have a repository' do
        it 'returns an error response' do
          expect(snippet).to receive(:repository_exists?).and_return(false)

          expect(subject).to be_error
        end
      end

      it 'schedules a namespace storage statistics update' do
        expect(Namespaces::ScheduleAggregationWorker)
            .to receive(:perform_async).once

        subject
      end
    end

    context 'with PersonalSnippet' do
      let!(:snippet) { create(:personal_snippet, :repository) }

      it_behaves_like 'updates statistics'
    end

    context 'with ProjectSnippet' do
      let!(:snippet) { create(:project_snippet, :repository) }
      let(:project_statistics) { snippet.project.statistics }

      it_behaves_like 'updates statistics'

      it 'updates projects statistics "snippets_size"' do
        expect(project_statistics.snippets_size).to be_zero

        subject

        expect(snippet.reload.statistics.repository_size).to eq project_statistics.reload.snippets_size
      end
    end
  end
end