snippet_spec.rb 3.36 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2 3 4
# == Schema Information
#
# Table name: snippets
#
Valery Sizov's avatar
Valery Sizov committed
5 6 7 8 9 10 11 12 13 14
#  id               :integer          not null, primary key
#  title            :string(255)
#  content          :text
#  author_id        :integer          not null
#  project_id       :integer
#  created_at       :datetime
#  updated_at       :datetime
#  file_name        :string(255)
#  type             :string(255)
#  visibility_level :integer          default(0), not null
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15 16
#

gitlabhq's avatar
gitlabhq committed
17 18
require 'spec_helper'

Douwe Maan's avatar
Douwe Maan committed
19
describe Snippet, models: true do
20 21 22 23 24 25 26 27 28 29 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) }
  end

  describe 'associations' do
31
    it { is_expected.to belong_to(:author).class_name('User') }
32
    it { is_expected.to belong_to(:project) }
33
    it { is_expected.to have_many(:notes).dependent(:destroy) }
gitlabhq's avatar
gitlabhq committed
34 35
  end

36
  describe 'validation' do
37
    it { is_expected.to validate_presence_of(:author) }
38

39
    it { is_expected.to validate_presence_of(:title) }
40
    it { is_expected.to validate_length_of(:title).is_within(0..255) }
41

42
    it { is_expected.to validate_length_of(:file_name).is_within(0..255) }
43

44
    it { is_expected.to validate_presence_of(:content) }
45 46

    it { is_expected.to validate_inclusion_of(:visibility_level).in_array(Gitlab::VisibilityLevel.values) }
gitlabhq's avatar
gitlabhq committed
47
  end
48 49 50 51 52 53 54 55 56 57 58 59 60 61

  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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

  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
gitlabhq's avatar
gitlabhq committed
106
end