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

95 lines
2.7 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 GroupLabel do
2016-11-03 12:29:30 +05:30
describe 'relationships' do
it { is_expected.to belong_to(:group) }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:group) }
end
describe '#subject' do
it 'aliases group to subject' do
subject = described_class.new(group: build(:group))
expect(subject.subject).to be(subject.group)
end
end
describe '#to_reference' do
2017-08-17 22:00:37 +05:30
let(:label) { create(:group_label, title: 'feature') }
2016-11-03 12:29:30 +05:30
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
2017-08-17 22:00:37 +05:30
context 'cross-project' do
let(:namespace) { build_stubbed(:namespace) }
2017-09-10 17:25:29 +05:30
let(:source_project) { build_stubbed(:project, name: 'project-1', namespace: namespace) }
let(:target_project) { build_stubbed(:project, name: 'project-2', namespace: namespace) }
2017-08-17 22:00:37 +05:30
it 'returns a String reference to the object' do
expect(label.to_reference(source_project, target_project: target_project)).to eq %(project-1~#{label.id})
end
end
2016-11-03 12:29:30 +05:30
context 'using invalid format' do
it 'raises error' do
expect { label.to_reference(format: :invalid) }
.to raise_error StandardError, /Unknown format/
end
end
end
2023-06-20 00:43:36 +05:30
describe '#preloaded_parent_container' do
let_it_be(:label) { create(:group_label) }
before do
label.reload # ensure associations are not loaded
end
context 'when group is loaded' do
it 'does not invoke a DB query' do
label.group
count = ActiveRecord::QueryRecorder.new { label.preloaded_parent_container }.count
expect(count).to eq(0)
expect(label.preloaded_parent_container).to eq(label.group)
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