gitlab-org--gitlab-foss/spec/services/namespaces/statistics_refresher_service_spec.rb
Mayra Cabrera dfdfa913ba Includes logic to persist namespace statistics
- Add two new ActiveRecord models:
  - RootNamespaceStoragestatistics will persist root namespace statistics
  - NamespaceAggregationSchedule will save information when a new update
to the namespace statistics needs to be scheduled
- Inject into UpdateProjectStatistics concern a new callback that will
call an async job to insert a new row onto NamespaceAggregationSchedule
table
- When a new row is inserted a new job is scheduled. This job will
update call an specific service to update the statistics and after that
it will delete thee aggregated scheduled row
- The RefresherServices makes heavy use of arel to build composable
queries to update Namespace::RootStorageStatistics attributes.
- Add an extra worker to traverse pending rows on
NAmespace::AggregationSchedule table and schedule a worker for each one
of this rows.
- Add an extra worker to traverse pending rows on
NAmespace::AggregationSchedule table and schedule a worker for each one
of this rows
2019-07-02 14:44:39 +00:00

58 lines
1.6 KiB
Ruby

# frozen_string_literal: true
require 'spec_helper'
describe Namespaces::StatisticsRefresherService, '#execute' do
let(:group) { create(:group) }
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
expect_any_instance_of(Namespace::RootStorageStatistics).to receive(:recalculate!).once
service.execute(group)
end
end
context 'with a root storage statistics relation' do
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
end
context 'when something goes wrong' do
before do
allow_any_instance_of(Namespace::RootStorageStatistics)
.to receive(:recalculate!).and_raise(ActiveRecord::ActiveRecordError)
end
it 'raises RefreshError' do
expect do
service.execute(group)
end.to raise_error(Namespaces::StatisticsRefresherService::RefresherError)
end
end
end