Welcome to mirror list, hosted at ThFree Co, Russian Federation.

glob_spec.rb « sql « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f0bb4294d624b4120d7c9af022a327bdffeebdab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'spec_helper'

describe Gitlab::SQL::Glob do
  describe '.to_like' do
    it 'matches * as %' do
      expect(glob('apple', '*')).to be(true)
      expect(glob('apple', 'app*')).to be(true)
      expect(glob('apple', 'apple*')).to be(true)
      expect(glob('apple', '*pple')).to be(true)
      expect(glob('apple', 'ap*le')).to be(true)

      expect(glob('apple', '*a')).to be(false)
      expect(glob('apple', 'app*a')).to be(false)
      expect(glob('apple', 'ap*l')).to be(false)
    end

    it 'matches % literally' do
      expect(glob('100%', '100%')).to be(true)

      expect(glob('100%', '%')).to be(false)
    end

    it 'matches _ literally' do
      expect(glob('^_^', '^_^')).to be(true)

      expect(glob('^A^', '^_^')).to be(false)
    end
  end

  def glob(string, pattern)
    match(string, subject.to_like(quote(pattern)))
  end

  def match(string, pattern)
    value = query("SELECT #{quote(string)} LIKE #{pattern}")
              .rows.flatten.first

    case value
    when 't', 1
      true
    else
      false
    end
  end

  def query(sql)
    ActiveRecord::Base.connection.select_all(sql)
  end

  def quote(string)
    ActiveRecord::Base.connection.quote(string)
  end
end