2011-10-16 17:07:10 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 04:50:51 -05:00
|
|
|
describe Snippet, models: true do
|
2015-05-02 23:11:21 -04:00
|
|
|
describe 'modules' do
|
|
|
|
subject { described_class }
|
|
|
|
|
|
|
|
it { is_expected.to include_module(Gitlab::VisibilityLevel) }
|
|
|
|
it { is_expected.to include_module(Linguist::BlobHelper) }
|
|
|
|
it { is_expected.to include_module(Participable) }
|
|
|
|
it { is_expected.to include_module(Referable) }
|
|
|
|
it { is_expected.to include_module(Sortable) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'associations' do
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to belong_to(:author).class_name('User') }
|
2015-05-02 23:14:31 -04:00
|
|
|
it { is_expected.to belong_to(:project) }
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to have_many(:notes).dependent(:destroy) }
|
2011-10-16 17:07:10 -04:00
|
|
|
end
|
|
|
|
|
2015-05-02 23:14:31 -04:00
|
|
|
describe 'validation' do
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to validate_presence_of(:author) }
|
2012-08-29 11:36:02 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to validate_presence_of(:title) }
|
2015-05-18 16:40:10 -04:00
|
|
|
it { is_expected.to validate_length_of(:title).is_within(0..255) }
|
2012-08-29 11:36:02 -04:00
|
|
|
|
2015-05-18 16:40:10 -04:00
|
|
|
it { is_expected.to validate_length_of(:file_name).is_within(0..255) }
|
2012-08-29 11:36:02 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to validate_presence_of(:content) }
|
2015-05-02 23:14:31 -04:00
|
|
|
|
|
|
|
it { is_expected.to validate_inclusion_of(:visibility_level).in_array(Gitlab::VisibilityLevel.values) }
|
2011-10-16 17:07:10 -04:00
|
|
|
end
|
2015-05-02 23:11:21 -04:00
|
|
|
|
|
|
|
describe '#to_reference' do
|
|
|
|
let(:project) { create(:empty_project) }
|
|
|
|
let(:snippet) { create(:snippet, project: project) }
|
|
|
|
|
|
|
|
it 'returns a String reference to the object' do
|
|
|
|
expect(snippet.to_reference).to eq "$#{snippet.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports a cross-project reference' do
|
|
|
|
cross = double('project')
|
|
|
|
expect(snippet.to_reference(cross)).to eq "#{project.to_reference}$#{snippet.id}"
|
|
|
|
end
|
|
|
|
end
|
2016-03-01 06:51:01 -05:00
|
|
|
|
|
|
|
describe '.search' do
|
|
|
|
let(:snippet) { create(:snippet) }
|
|
|
|
|
|
|
|
it 'returns snippets with a matching title' do
|
|
|
|
expect(described_class.search(snippet.title)).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a partially matching title' do
|
|
|
|
expect(described_class.search(snippet.title[0..2])).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a matching title regardless of the casing' do
|
|
|
|
expect(described_class.search(snippet.title.upcase)).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a matching file name' do
|
|
|
|
expect(described_class.search(snippet.file_name)).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a partially matching file name' do
|
|
|
|
expect(described_class.search(snippet.file_name[0..2])).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with a matching file name regardless of the casing' do
|
|
|
|
expect(described_class.search(snippet.file_name.upcase)).to eq([snippet])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#search_code' do
|
|
|
|
let(:snippet) { create(:snippet, content: 'class Foo; end') }
|
|
|
|
|
|
|
|
it 'returns snippets with matching content' do
|
|
|
|
expect(described_class.search_code(snippet.content)).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with partially matching content' do
|
|
|
|
expect(described_class.search_code('class')).to eq([snippet])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns snippets with matching content regardless of the casing' do
|
|
|
|
expect(described_class.search_code('FOO')).to eq([snippet])
|
|
|
|
end
|
|
|
|
end
|
2011-10-16 17:07:10 -04:00
|
|
|
end
|