2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-06 03:45:38 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::SQL::Glob do
|
2017-07-06 03:45:38 -04:00
|
|
|
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
|
2018-12-15 04:06:56 -05:00
|
|
|
when true, 1
|
2017-07-06 03:45:38 -04:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def query(sql)
|
2021-07-23 08:09:05 -04:00
|
|
|
ApplicationRecord.connection.select_all(sql)
|
2017-07-06 03:45:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def quote(string)
|
2021-07-23 08:09:05 -04:00
|
|
|
ApplicationRecord.connection.quote(string)
|
2017-07-06 03:45:38 -04:00
|
|
|
end
|
|
|
|
end
|