debian-mirror-gitlab/spec/models/project_label_spec.rb

158 lines
4.5 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2016-11-03 12:29:30 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe ProjectLabel do
2016-11-03 12:29:30 +05:30
describe 'relationships' do
it { is_expected.to belong_to(:project) }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:project) }
context 'validates if title must not exist at group level' do
let(:group) { create(:group, name: 'gitlab-org') }
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, group: group) }
2016-11-03 12:29:30 +05:30
before do
create(:group_label, group: group, title: 'Bug')
end
it 'returns error if title already exists at group level' do
label = described_class.new(project: project, title: 'Bug')
label.valid?
expect(label.errors[:title]).to include 'already exists at group level for gitlab-org. Please choose another one.'
end
it 'does not returns error if title does not exist at group level' do
label = described_class.new(project: project, title: 'Security')
label.valid?
expect(label.errors[:title]).to be_empty
end
it 'does not returns error if project does not belong to group' do
2017-09-10 17:25:29 +05:30
another_project = create(:project)
2016-11-03 12:29:30 +05:30
label = described_class.new(project: another_project, title: 'Bug')
label.valid?
expect(label.errors[:title]).to be_empty
end
it 'does not returns error when title does not change' do
project_label = create(:label, project: project, name: 'Security')
create(:group_label, group: group, name: 'Security')
project_label.description = 'Security related stuff.'
project_label.valid?
expect(project_label.errors[:title]).to be_empty
end
end
context 'when attempting to add more than one priority to the project label' do
it 'returns error' do
subject.priorities.build
subject.priorities.build
subject.valid?
expect(subject.errors[:priorities]).to include 'Number of permitted priorities exceeded'
end
end
end
describe '#subject' do
it 'aliases project to subject' do
2017-09-10 17:25:29 +05:30
subject = described_class.new(project: build(:project))
2016-11-03 12:29:30 +05:30
expect(subject.subject).to be(subject.project)
end
end
describe '#to_reference' do
let(:label) { create(:label) }
context 'using id' do
it 'returns a String reference to the object' do
expect(label.to_reference).to eq "~#{label.id}"
end
end
context 'using name' do
it 'returns a String reference to the object' do
expect(label.to_reference(format: :name)).to eq %(~"#{label.name}")
end
it 'uses id when name contains double quote' do
label = create(:label, name: %q{"irony"})
expect(label.to_reference(format: :name)).to eq "~#{label.id}"
end
end
context 'using invalid format' do
it 'raises error' do
expect { label.to_reference(format: :invalid) }
.to raise_error StandardError, /Unknown format/
end
end
context 'cross project reference' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project) }
2016-11-03 12:29:30 +05:30
context 'using name' do
it 'returns cross reference with label name' do
expect(label.to_reference(project, format: :name))
2017-09-10 17:25:29 +05:30
.to eq %Q(#{label.project.full_path}~"#{label.name}")
2016-11-03 12:29:30 +05:30
end
end
context 'using id' do
it 'returns cross reference with label id' do
expect(label.to_reference(project, format: :id))
2017-09-10 17:25:29 +05:30
.to eq %Q(#{label.project.full_path}~#{label.id})
2016-11-03 12:29:30 +05:30
end
end
end
end
2023-06-20 00:43:36 +05:30
describe '#preloaded_parent_container' do
let_it_be(:label) { create(:label) }
before do
label.reload # ensure associations are not loaded
end
context 'when project is loaded' do
it 'does not invoke a DB query' do
label.project
count = ActiveRecord::QueryRecorder.new { label.preloaded_parent_container }.count
expect(count).to eq(0)
expect(label.preloaded_parent_container).to eq(label.project)
end
end
context 'when parent_container is loaded' do
it 'does not invoke a DB query' do
label.parent_container
count = ActiveRecord::QueryRecorder.new { label.preloaded_parent_container }.count
expect(count).to eq(0)
expect(label.preloaded_parent_container).to eq(label.parent_container)
end
end
context 'when none of them are loaded' do
it 'invokes a DB query' do
count = ActiveRecord::QueryRecorder.new { label.preloaded_parent_container }.count
expect(count).to eq(1)
end
end
end
2016-11-03 12:29:30 +05:30
end