summaryrefslogtreecommitdiff
path: root/spec/services/projects/update_statistics_service_spec.rb
blob: 1cc69e7e2fee006497371ff66d8cce180a564f83 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::UpdateStatisticsService do
  using RSpec::Parameterized::TableSyntax

  let(:service) { described_class.new(project, nil, statistics: statistics) }
  let(:statistics) { %w(repository_size) }

  describe '#execute' do
    context 'with a non-existing project' do
      let(:project) { nil }

      it 'does nothing' do
        expect_any_instance_of(ProjectStatistics).not_to receive(:refresh!)

        service.execute
      end
    end

    context 'with an existing project' do
      let_it_be(:project) { create(:project) }

      where(:statistics, :method_caches) do
        []                                                   | %i(size commit_count)
        ['repository_size']                                  | [:size]
        [:repository_size]                                   | [:size]
        [:lfs_objects_size]                                  | nil
        [:commit_count]                                      | [:commit_count] # rubocop:disable Lint/BinaryOperatorWithIdenticalOperands
        [:repository_size, :commit_count]                    | %i(size commit_count)
        [:repository_size, :commit_count, :lfs_objects_size] | %i(size commit_count)
      end

      with_them do
        it 'refreshes the project statistics' do
          expect(project.statistics).to receive(:refresh!).with(only: statistics.map(&:to_sym)).and_call_original

          service.execute
        end

        it 'invalidates the method caches after a refresh' do
          expect(project.wiki.repository).not_to receive(:expire_method_caches)

          if method_caches.present?
            expect(project.repository).to receive(:expire_method_caches).with(method_caches).and_call_original
          else
            expect(project.repository).not_to receive(:expire_method_caches)
          end

          service.execute
        end
      end
    end

    context 'with an existing project with a Wiki' do
      let(:project) { create(:project, :repository, :wiki_enabled) }
      let(:statistics) { [:wiki_size] }

      it 'invalidates and refreshes Wiki size' do
        expect(project.statistics).to receive(:refresh!).with(only: statistics).and_call_original
        expect(project.wiki.repository).to receive(:expire_method_caches).with(%i(size)).and_call_original

        service.execute
      end
    end
  end
end