2019-09-30 21:07:59 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Namespaces::StatisticsRefresherService, '#execute' do
|
2019-09-30 21:07:59 +05:30
|
|
|
let(:group) { create(:group) }
|
2023-01-13 00:05:48 +05:30
|
|
|
let(:subgroup) { create(:group, parent: group) }
|
2019-09-30 21:07:59 +05:30
|
|
|
let(:projects) { create_list(:project, 5, namespace: group) }
|
|
|
|
let(:service) { described_class.new }
|
|
|
|
|
|
|
|
context 'without a root storage statistics relation' do
|
|
|
|
it 'creates one' do
|
|
|
|
expect do
|
|
|
|
service.execute(group)
|
|
|
|
end.to change(Namespace::RootStorageStatistics, :count).by(1)
|
|
|
|
|
|
|
|
expect(group.reload.root_storage_statistics).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'recalculate the namespace statistics' do
|
2020-03-13 15:44:24 +05:30
|
|
|
expect_next_instance_of(Namespace::RootStorageStatistics) do |instance|
|
|
|
|
expect(instance).to receive(:recalculate!).once
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
service.execute(group)
|
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
context 'when given a subgroup' do
|
|
|
|
it 'does not create statistics for the subgroup' do
|
|
|
|
service.execute(subgroup)
|
|
|
|
|
|
|
|
expect(subgroup.reload.root_storage_statistics).not_to be_present
|
|
|
|
end
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
context 'with a root storage statistics relation', :sidekiq_might_not_need_inline do
|
2019-09-30 21:07:59 +05:30
|
|
|
before do
|
|
|
|
Namespace::AggregationSchedule.safe_find_or_create_by!(namespace_id: group.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create one' do
|
|
|
|
expect do
|
|
|
|
service.execute(group)
|
|
|
|
end.not_to change(Namespace::RootStorageStatistics, :count)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'recalculate the namespace statistics' do
|
|
|
|
expect(Namespace::RootStorageStatistics)
|
|
|
|
.to receive(:safe_find_or_create_by!).with({ namespace_id: group.id })
|
|
|
|
.and_return(group.root_storage_statistics)
|
|
|
|
|
|
|
|
service.execute(group)
|
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
context 'when given a subgroup' do
|
|
|
|
it "recalculates the root namespace's statistics" do
|
|
|
|
expect(Namespace::RootStorageStatistics)
|
|
|
|
.to receive(:safe_find_or_create_by!).with({ namespace_id: group.id })
|
|
|
|
.and_return(group.root_storage_statistics)
|
|
|
|
|
|
|
|
service.execute(subgroup)
|
|
|
|
end
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when something goes wrong' do
|
|
|
|
before do
|
2020-03-13 15:44:24 +05:30
|
|
|
allow_next_instance_of(Namespace::RootStorageStatistics) do |instance|
|
|
|
|
allow(instance).to receive(:recalculate!).and_raise(ActiveRecord::ActiveRecordError)
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises RefreshError' do
|
|
|
|
expect do
|
|
|
|
service.execute(group)
|
|
|
|
end.to raise_error(Namespaces::StatisticsRefresherService::RefresherError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|