2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
shared_examples_for 'UpdateProjectStatistics' do
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:project) { subject.project }
|
|
|
|
let(:project_statistics_name) { described_class.project_statistics_name }
|
|
|
|
let(:statistic_attribute) { described_class.statistic_attribute }
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
def reload_stat
|
2019-09-04 21:01:54 +05:30
|
|
|
project.statistics.reload.send(project_statistics_name).to_i
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def read_attribute
|
2019-09-04 21:01:54 +05:30
|
|
|
subject.read_attribute(statistic_attribute).to_i
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_new_record }
|
|
|
|
|
|
|
|
context 'when creating' do
|
|
|
|
it 'updates the project statistics' do
|
|
|
|
delta = read_attribute
|
|
|
|
|
|
|
|
expect { subject.save! }
|
|
|
|
.to change { reload_stat }
|
|
|
|
.by(delta)
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
it 'schedules a namespace statistics worker' do
|
|
|
|
expect(Namespaces::ScheduleAggregationWorker)
|
|
|
|
.to receive(:perform_async).once
|
|
|
|
|
|
|
|
subject.save!
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when updating' do
|
2019-09-30 21:07:59 +05:30
|
|
|
let(:delta) { 42 }
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
before do
|
|
|
|
subject.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates project statistics' do
|
|
|
|
expect(ProjectStatistics)
|
|
|
|
.to receive(:increment_statistic)
|
|
|
|
.and_call_original
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
subject.write_attribute(statistic_attribute, read_attribute + delta)
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
expect { subject.save! }
|
|
|
|
.to change { reload_stat }
|
|
|
|
.by(delta)
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
it 'schedules a namespace statistics worker' do
|
|
|
|
expect(Namespaces::ScheduleAggregationWorker)
|
|
|
|
.to receive(:perform_async).once
|
|
|
|
|
|
|
|
subject.write_attribute(statistic_attribute, read_attribute + delta)
|
|
|
|
subject.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'avoids N + 1 queries' do
|
|
|
|
subject.write_attribute(statistic_attribute, read_attribute + delta)
|
|
|
|
|
|
|
|
control_count = ActiveRecord::QueryRecorder.new do
|
|
|
|
subject.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
subject.write_attribute(statistic_attribute, read_attribute + delta)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
subject.save!
|
|
|
|
end.not_to exceed_query_limit(control_count)
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when destroying' do
|
|
|
|
before do
|
|
|
|
subject.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the project statistics' do
|
|
|
|
delta = -read_attribute
|
|
|
|
|
|
|
|
expect(ProjectStatistics)
|
|
|
|
.to receive(:increment_statistic)
|
|
|
|
.and_call_original
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
expect { subject.destroy! }
|
2019-07-31 22:56:46 +05:30
|
|
|
.to change { reload_stat }
|
|
|
|
.by(delta)
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
it 'schedules a namespace statistics worker' do
|
|
|
|
expect(Namespaces::ScheduleAggregationWorker)
|
|
|
|
.to receive(:perform_async).once
|
|
|
|
|
|
|
|
subject.destroy!
|
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
context 'when it is destroyed from the project level' do
|
|
|
|
it 'does not update the project statistics' do
|
|
|
|
expect(ProjectStatistics)
|
|
|
|
.not_to receive(:increment_statistic)
|
|
|
|
|
|
|
|
project.update(pending_delete: true)
|
|
|
|
project.destroy!
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
it 'does not schedule a namespace statistics worker' do
|
|
|
|
expect(Namespaces::ScheduleAggregationWorker)
|
|
|
|
.not_to receive(:perform_async)
|
|
|
|
|
|
|
|
project.update(pending_delete: true)
|
|
|
|
project.destroy!
|
|
|
|
end
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
end
|