1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
|