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

91 lines
2.9 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
require 'spec_helper'
2015-12-23 02:04:40 +05:30
describe Snippet, models: true do
2015-09-11 14:41:01 +05:30
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) }
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
describe 'associations' do
it { is_expected.to belong_to(:author).class_name('User') }
it { is_expected.to belong_to(:project) }
it { is_expected.to have_many(:notes).dependent(:destroy) }
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
describe 'validation' do
2015-04-26 12:48:37 +05:30
it { is_expected.to validate_presence_of(:author) }
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
it { is_expected.to validate_presence_of(:title) }
2015-09-11 14:41:01 +05:30
it { is_expected.to validate_length_of(:title).is_within(0..255) }
2014-09-02 18:07:02 +05:30
2015-09-11 14:41:01 +05:30
it { is_expected.to validate_length_of(:file_name).is_within(0..255) }
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
it { is_expected.to validate_presence_of(:content) }
2015-09-11 14:41:01 +05:30
it { is_expected.to validate_inclusion_of(:visibility_level).in_array(Gitlab::VisibilityLevel.values) }
end
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
2014-09-02 18:07:02 +05:30
end
2016-06-02 11:05:42 +05:30
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
2014-09-02 18:07:02 +05:30
end