summaryrefslogtreecommitdiff
path: root/spec/models/namespace/root_storage_statistics_spec.rb
blob: 3229a32234e57f7f51444e507977f2b7da379514 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Namespace::RootStorageStatistics, type: :model do
  it { is_expected.to belong_to :namespace }
  it { is_expected.to have_one(:route).through(:namespace) }

  it { is_expected.to delegate_method(:all_projects).to(:namespace) }

  describe '#recalculate!' do
    let(:namespace) { create(:group) }
    let(:root_storage_statistics) { create(:namespace_root_storage_statistics, namespace: namespace) }

    let(:project1) { create(:project, namespace: namespace) }
    let(:project2) { create(:project, namespace: namespace) }

    let!(:stat1) { create(:project_statistics, project: project1, with_data: true, size_multiplier: 100) }
    let!(:stat2) { create(:project_statistics, project: project2, with_data: true, size_multiplier: 200) }

    shared_examples 'data refresh' do
      it 'aggregates project statistics' do
        root_storage_statistics.recalculate!

        root_storage_statistics.reload

        total_repository_size = stat1.repository_size + stat2.repository_size
        total_wiki_size = stat1.wiki_size + stat2.wiki_size
        total_lfs_objects_size = stat1.lfs_objects_size + stat2.lfs_objects_size
        total_build_artifacts_size = stat1.build_artifacts_size + stat2.build_artifacts_size
        total_packages_size = stat1.packages_size + stat2.packages_size
        total_storage_size = stat1.storage_size + stat2.storage_size

        expect(root_storage_statistics.repository_size).to eq(total_repository_size)
        expect(root_storage_statistics.wiki_size).to eq(total_wiki_size)
        expect(root_storage_statistics.lfs_objects_size).to eq(total_lfs_objects_size)
        expect(root_storage_statistics.build_artifacts_size).to eq(total_build_artifacts_size)
        expect(root_storage_statistics.packages_size).to eq(total_packages_size)
        expect(root_storage_statistics.storage_size).to eq(total_storage_size)
      end

      it 'works when there are no projects' do
        Project.delete_all

        root_storage_statistics.recalculate!

        root_storage_statistics.reload
        expect(root_storage_statistics.repository_size).to eq(0)
        expect(root_storage_statistics.wiki_size).to eq(0)
        expect(root_storage_statistics.lfs_objects_size).to eq(0)
        expect(root_storage_statistics.build_artifacts_size).to eq(0)
        expect(root_storage_statistics.packages_size).to eq(0)
        expect(root_storage_statistics.storage_size).to eq(0)
      end
    end

    it_behaves_like 'data refresh'

    context 'with subgroups', :nested_groups do
      let(:subgroup1) { create(:group, parent: namespace)}
      let(:subgroup2) { create(:group, parent: subgroup1)}

      let(:project1) { create(:project, namespace: subgroup1) }
      let(:project2) { create(:project, namespace: subgroup2) }

      it_behaves_like 'data refresh'
    end

    context 'with a personal namespace' do
      let(:namespace) { create(:user).namespace }

      it_behaves_like 'data refresh'
    end
  end
end