diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-23 18:08:41 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-23 18:08:41 +0300 |
commit | 5eeb39104356356bec81abe19479bca591e6f299 (patch) | |
tree | 7534ccf94fb8100ce39b78278d275b2061773f95 /spec/models | |
parent | b7e512c8970dcce6feabc096885c7a1ea91e4694 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/issue_assignee_spec.rb | 35 | ||||
-rw-r--r-- | spec/models/merge_request_assignee_spec.rb | 22 | ||||
-rw-r--r-- | spec/models/snippet_spec.rb | 11 | ||||
-rw-r--r-- | spec/models/snippet_statistics_spec.rb | 81 |
4 files changed, 148 insertions, 1 deletions
diff --git a/spec/models/issue_assignee_spec.rb b/spec/models/issue_assignee_spec.rb index 2d59ba15101..df8e91cd133 100644 --- a/spec/models/issue_assignee_spec.rb +++ b/spec/models/issue_assignee_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe IssueAssignee do +RSpec.describe IssueAssignee do let(:issue) { create(:issue) } subject { issue.issue_assignees.build(assignee: create(:user)) } @@ -15,4 +15,37 @@ describe IssueAssignee do describe 'validations' do it { is_expected.to validate_uniqueness_of(:assignee).scoped_to(:issue_id) } end + + describe 'scopes' do + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:project_issue) { create(:issue, project: project, assignee_ids: [user.id]) } + + before do + issue.update!(assignee_ids: [user.id]) + end + + context 'in_projects' do + it 'returns issue assignees for given project' do + expect(IssueAssignee.count).to eq 2 + + assignees = IssueAssignee.in_projects([project]) + + expect(assignees.count).to eq 1 + expect(assignees.first.user_id).to eq project_issue.issue_assignees.first.user_id + expect(assignees.first.issue_id).to eq project_issue.issue_assignees.first.issue_id + end + end + + context 'on_issues' do + it 'returns issue assignees for given issues' do + expect(IssueAssignee.count).to eq 2 + + assignees = IssueAssignee.on_issues([project_issue]) + + expect(assignees.count).to eq 1 + expect(assignees.first.issue_id).to eq project_issue.issue_assignees.first.issue_id + end + end + end end diff --git a/spec/models/merge_request_assignee_spec.rb b/spec/models/merge_request_assignee_spec.rb index d6aab15d990..bd2df428ed0 100644 --- a/spec/models/merge_request_assignee_spec.rb +++ b/spec/models/merge_request_assignee_spec.rb @@ -15,4 +15,26 @@ describe MergeRequestAssignee do describe 'validations' do it { is_expected.to validate_uniqueness_of(:assignee).scoped_to(:merge_request_id) } end + + describe 'scopes' do + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project, :repository) } + let_it_be(:project_merge_request) { create(:merge_request, target_project: project, source_project: project, assignee_ids: [user.id]) } + + before do + merge_request.update!(assignee_ids: [user.id]) + end + + context 'in_projects' do + it 'returns issue assignees for given project' do + expect(MergeRequestAssignee.count).to eq 2 + + assignees = MergeRequestAssignee.in_projects([project]) + + expect(assignees.count).to eq 1 + expect(assignees.first.user_id).to eq project_merge_request.merge_request_assignees.first.user_id + expect(assignees.first.merge_request_id).to eq project_merge_request.merge_request_assignees.first.merge_request_id + end + end + end end diff --git a/spec/models/snippet_spec.rb b/spec/models/snippet_spec.rb index 6edaf0410f5..7ded8c380fe 100644 --- a/spec/models/snippet_spec.rb +++ b/spec/models/snippet_spec.rb @@ -92,6 +92,17 @@ describe Snippet do end end + describe 'callbacks' do + it 'creates snippet statistics when the snippet is created' do + snippet = build(:snippet) + expect(snippet.statistics).to be_nil + + snippet.save + + expect(snippet.statistics).to be_persisted + end + end + describe '#to_reference' do context 'when snippet belongs to a project' do let(:project) { build(:project, name: 'sample-project') } diff --git a/spec/models/snippet_statistics_spec.rb b/spec/models/snippet_statistics_spec.rb index d76fe5fb589..1bfb80e2c1d 100644 --- a/spec/models/snippet_statistics_spec.rb +++ b/spec/models/snippet_statistics_spec.rb @@ -3,6 +3,87 @@ require 'spec_helper' 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 end |