summaryrefslogtreecommitdiff
path: root/spec/workers/namespaces/schedule_aggregation_worker_spec.rb
blob: 7432ca12f2a140bebfaecaab64a2448366323e45 (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
# frozen_string_literal: true

require 'spec_helper'

describe Namespaces::ScheduleAggregationWorker, '#perform' do
  let(:group) { create(:group) }

  subject(:worker) { described_class.new }

  context 'when group is the root ancestor' do
    context 'when aggregation schedule exists' do
      it 'does not create a new one' do
        Namespace::AggregationSchedule.safe_find_or_create_by!(namespace_id: group.id)

        expect do
          worker.perform(group.id)
        end.not_to change(Namespace::AggregationSchedule, :count)
      end
    end

    context 'when update_statistics_namespace is off' do
      it 'does not create a new one' do
        stub_feature_flags(update_statistics_namespace: false, namespace: group)

        expect do
          worker.perform(group.id)
        end.not_to change(Namespace::AggregationSchedule, :count)
      end
    end

    context 'when aggregation schedule does not exist' do
      it 'creates one' do
        allow_any_instance_of(Namespace::AggregationSchedule)
          .to receive(:schedule_root_storage_statistics).and_return(nil)

        expect do
          worker.perform(group.id)
        end.to change(Namespace::AggregationSchedule, :count).by(1)

        expect(group.aggregation_schedule).to be_present
      end
    end
  end

  context 'when group is not the root ancestor' do
    let(:parent_group) { create(:group) }
    let(:group) { create(:group, parent: parent_group) }

    it 'creates an aggregation schedule for the root' do
      allow_any_instance_of(Namespace::AggregationSchedule)
        .to receive(:schedule_root_storage_statistics).and_return(nil)

      worker.perform(group.id)

      expect(parent_group.aggregation_schedule).to be_present
    end
  end

  context 'when namespace does not exist' do
    it 'logs the error' do
      expect(Gitlab::SidekiqLogger).to receive(:error).once

      worker.perform(12345)
    end
  end
end