diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 15:26:25 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 15:26:25 +0300 |
commit | a09983ae35713f5a2bbb100981116d31ce99826e (patch) | |
tree | 2ee2af7bd104d57086db360a7e6d8c9d5d43667a /spec/models/snippet_statistics_spec.rb | |
parent | 18c5ab32b738c0b6ecb4d0df3994000482f34bd8 (diff) |
Add latest changes from gitlab-org/gitlab@13-2-stable-ee
Diffstat (limited to 'spec/models/snippet_statistics_spec.rb')
-rw-r--r-- | spec/models/snippet_statistics_spec.rb | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/spec/models/snippet_statistics_spec.rb b/spec/models/snippet_statistics_spec.rb new file mode 100644 index 00000000000..ad25bd7b3be --- /dev/null +++ b/spec/models/snippet_statistics_spec.rb @@ -0,0 +1,149 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe SnippetStatistics do + let_it_be(:snippet_without_repo) { create(:snippet) } + let_it_be(:snippet_with_repo) { create(:snippet, :repository) } + + let(:statistics) { snippet_with_repo.statistics } + + it { is_expected.to belong_to(:snippet) } + it { is_expected.to validate_presence_of(:snippet) } + + describe '#update_commit_count' do + subject { statistics.update_commit_count } + + it 'updates the count of commits' do + commit_count = snippet_with_repo.repository.commit_count + + subject + + expect(statistics.commit_count).to eq commit_count + end + + context 'when the snippet does not have a repository' do + let(:statistics) { snippet_without_repo.statistics } + + it 'returns 0' do + expect(subject).to eq 0 + expect(statistics.commit_count).to eq 0 + end + end + end + + describe '#update_file_count' do + subject { statistics.update_file_count } + + it 'updates the count of files' do + file_count = snippet_with_repo.repository.ls_files(nil).count + + subject + + expect(statistics.file_count).to eq file_count + end + + context 'when the snippet does not have a repository' do + let(:statistics) { snippet_without_repo.statistics } + + it 'returns 0' do + expect(subject).to eq 0 + expect(statistics.file_count).to eq 0 + end + end + end + + describe '#update_repository_size' do + subject { statistics.update_repository_size } + + it 'updates the repository_size' do + repository_size = snippet_with_repo.repository.size.megabytes.to_i + + subject + + expect(statistics.repository_size).to eq repository_size + end + + context 'when the snippet does not have a repository' do + let(:statistics) { snippet_without_repo.statistics } + + it 'returns 0' do + expect(subject).to eq 0 + expect(statistics.repository_size).to eq 0 + end + end + end + + describe '#refresh!' do + subject { statistics.refresh! } + + it 'retrieves and saves statistic data from repository' do + expect(statistics).to receive(:update_commit_count) + expect(statistics).to receive(:update_file_count) + expect(statistics).to receive(:update_repository_size) + expect(statistics).to receive(:save!) + + subject + end + end + + context 'with a PersonalSnippet' do + let!(:snippet) { create(:personal_snippet, :repository) } + + shared_examples 'personal snippet statistics updates' do + it 'schedules a namespace statistics worker' do + expect(Namespaces::ScheduleAggregationWorker) + .to receive(:perform_async).once + + statistics.save! + end + + it 'does not try to update project stats' do + expect(statistics).not_to receive(:schedule_update_project_statistic) + + statistics.save! + end + end + + context 'when creating' do + let(:statistics) { build(:snippet_statistics, snippet_id: snippet.id, with_data: true) } + + before do + snippet.statistics.delete + end + + it_behaves_like 'personal snippet statistics updates' + end + + context 'when updating' do + let(:statistics) { snippet.statistics } + + before do + snippet.statistics.repository_size = 123 + end + + it_behaves_like 'personal snippet statistics updates' + end + end + + context 'with a ProjectSnippet' do + let!(:snippet) { create(:project_snippet) } + + it_behaves_like 'UpdateProjectStatistics' do + subject { build(:snippet_statistics, snippet: snippet, id: snippet.id, with_data: true) } + + before do + # The shared examples requires the snippet statistics not to be present + snippet.statistics.delete + snippet.reload + end + end + + it 'does not call personal snippet callbacks' do + expect(snippet.statistics).not_to receive(:update_author_root_storage_statistics) + expect(snippet.statistics).to receive(:schedule_update_project_statistic) + + snippet.statistics.update!(repository_size: 123) + end + end +end |