diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-11-19 11:27:35 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-11-19 11:27:35 +0300 |
commit | 7e9c479f7de77702622631cff2628a9c8dcbc627 (patch) | |
tree | c8f718a08e110ad7e1894510980d2155a6549197 /spec/models/analytics/devops_adoption/segment_selection_spec.rb | |
parent | e852b0ae16db4052c1c567d9efa4facc81146e88 (diff) |
Add latest changes from gitlab-org/gitlab@13-6-stable-eev13.6.0-rc42
Diffstat (limited to 'spec/models/analytics/devops_adoption/segment_selection_spec.rb')
-rw-r--r-- | spec/models/analytics/devops_adoption/segment_selection_spec.rb | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/spec/models/analytics/devops_adoption/segment_selection_spec.rb b/spec/models/analytics/devops_adoption/segment_selection_spec.rb new file mode 100644 index 00000000000..5866cbaa48e --- /dev/null +++ b/spec/models/analytics/devops_adoption/segment_selection_spec.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Analytics::DevopsAdoption::SegmentSelection, type: :model do + subject { build(:devops_adoption_segment_selection, :project) } + + describe 'validation' do + let_it_be(:group) { create(:group) } + let_it_be(:project) { create(:project) } + + it { is_expected.to validate_presence_of(:segment) } + + context do + subject { create(:devops_adoption_segment_selection, :project, project: project) } + + it { is_expected.to validate_uniqueness_of(:project_id).scoped_to(:segment_id) } + end + + context do + subject { create(:devops_adoption_segment_selection, :group, group: group) } + + it { is_expected.to validate_uniqueness_of(:group_id).scoped_to(:segment_id) } + end + + it 'project is required' do + selection = build(:devops_adoption_segment_selection, project: nil, group: nil) + + selection.validate + + expect(selection.errors).to have_key(:project) + end + + it 'project is not required when a group is given' do + selection = build(:devops_adoption_segment_selection, :group, group: group) + + expect(selection).to be_valid + end + + it 'does not allow group to be set when project is present' do + selection = build(:devops_adoption_segment_selection) + + selection.group = group + selection.project = project + + selection.validate + + expect(selection.errors[:group]).to eq([s_('DevopsAdoptionSegmentSelection|The selection cannot be configured for a project and for a group at the same time')]) + end + + context 'limit the number of segment selections' do + let_it_be(:segment) { create(:devops_adoption_segment) } + + subject { build(:devops_adoption_segment_selection, segment: segment, project: project) } + + before do + create(:devops_adoption_segment_selection, :project, segment: segment) + + stub_const("#{described_class}::ALLOWED_SELECTIONS_PER_SEGMENT", 1) + end + + it 'shows validation error' do + subject.validate + + expect(subject.errors[:segment]).to eq([s_('DevopsAdoptionSegment|The maximum number of selections has been reached')]) + end + end + end +end |