summaryrefslogtreecommitdiff
path: root/spec/models/snippet_statistics_spec.rb
blob: ad25bd7b3be1542c661a2e014089a10a5c757920 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe SnippetStatistics do
  let_it_be(:snippet_without_repo) { create(:snippet) }
  let_it_be(:snippet_with_repo) { create(:snippet, :repository) }

  let(:statistics) { snippet_with_repo.statistics }

  it { is_expected.to belong_to(:snippet) }
  it { is_expected.to validate_presence_of(:snippet) }

  describe '#update_commit_count' do
    subject { statistics.update_commit_count }

    it 'updates the count of commits' do
      commit_count = snippet_with_repo.repository.commit_count

      subject

      expect(statistics.commit_count).to eq commit_count
    end

    context 'when the snippet does not have a repository' do
      let(:statistics) { snippet_without_repo.statistics }

      it 'returns 0' do
        expect(subject).to eq 0
        expect(statistics.commit_count).to eq 0
      end
    end
  end

  describe '#update_file_count' do
    subject { statistics.update_file_count }

    it 'updates the count of files' do
      file_count = snippet_with_repo.repository.ls_files(nil).count

      subject

      expect(statistics.file_count).to eq file_count
    end

    context 'when the snippet does not have a repository' do
      let(:statistics) { snippet_without_repo.statistics }

      it 'returns 0' do
        expect(subject).to eq 0
        expect(statistics.file_count).to eq 0
      end
    end
  end

  describe '#update_repository_size' do
    subject { statistics.update_repository_size }

    it 'updates the repository_size' do
      repository_size = snippet_with_repo.repository.size.megabytes.to_i

      subject

      expect(statistics.repository_size).to eq repository_size
    end

    context 'when the snippet does not have a repository' do
      let(:statistics) { snippet_without_repo.statistics }

      it 'returns 0' do
        expect(subject).to eq 0
        expect(statistics.repository_size).to eq 0
      end
    end
  end

  describe '#refresh!' do
    subject { statistics.refresh! }

    it 'retrieves and saves statistic data from repository' do
      expect(statistics).to receive(:update_commit_count)
      expect(statistics).to receive(:update_file_count)
      expect(statistics).to receive(:update_repository_size)
      expect(statistics).to receive(:save!)

      subject
    end
  end

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

    shared_examples 'personal snippet statistics updates' do
      it 'schedules a namespace statistics worker' do
        expect(Namespaces::ScheduleAggregationWorker)
          .to receive(:perform_async).once

        statistics.save!
      end

      it 'does not try to update project stats' do
        expect(statistics).not_to receive(:schedule_update_project_statistic)

        statistics.save!
      end
    end

    context 'when creating' do
      let(:statistics) { build(:snippet_statistics, snippet_id: snippet.id, with_data: true) }

      before do
        snippet.statistics.delete
      end

      it_behaves_like 'personal snippet statistics updates'
    end

    context 'when updating' do
      let(:statistics) { snippet.statistics }

      before do
        snippet.statistics.repository_size = 123
      end

      it_behaves_like 'personal snippet statistics updates'
    end
  end

  context 'with a ProjectSnippet' do
    let!(:snippet) { create(:project_snippet) }

    it_behaves_like 'UpdateProjectStatistics' do
      subject { build(:snippet_statistics, snippet: snippet, id: snippet.id, with_data: true) }

      before do
        # The shared examples requires the snippet statistics not to be present
        snippet.statistics.delete
        snippet.reload
      end
    end

    it 'does not call personal snippet callbacks' do
      expect(snippet.statistics).not_to receive(:update_author_root_storage_statistics)
      expect(snippet.statistics).to receive(:schedule_update_project_statistic)

      snippet.statistics.update!(repository_size: 123)
    end
  end
end