2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-22 12:51:53 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::SQL::Pattern do
|
2020-10-02 11:08:13 -04:00
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
describe '.fuzzy_search' do
|
|
|
|
let_it_be(:issue1) { create(:issue, title: 'noise foo noise', description: 'noise bar noise') }
|
|
|
|
let_it_be(:issue2) { create(:issue, title: 'noise baz noise', description: 'noise foo noise') }
|
|
|
|
let_it_be(:issue3) { create(:issue, title: 'Oh', description: 'Ah') }
|
|
|
|
|
|
|
|
subject(:fuzzy_search) { Issue.fuzzy_search(query, columns) }
|
|
|
|
|
|
|
|
where(:query, :columns, :expected) do
|
|
|
|
'foo' | [Issue.arel_table[:title]] | %i[issue1]
|
|
|
|
|
|
|
|
'foo' | %i[title] | %i[issue1]
|
|
|
|
'foo' | %w[title] | %i[issue1]
|
|
|
|
'foo' | %i[description] | %i[issue2]
|
|
|
|
'foo' | %i[title description] | %i[issue1 issue2]
|
|
|
|
'bar' | %i[title description] | %i[issue1]
|
|
|
|
'baz' | %i[title description] | %i[issue2]
|
|
|
|
'qux' | %i[title description] | []
|
|
|
|
|
|
|
|
'oh' | %i[title description] | %i[issue3]
|
|
|
|
'OH' | %i[title description] | %i[issue3]
|
|
|
|
'ah' | %i[title description] | %i[issue3]
|
|
|
|
'AH' | %i[title description] | %i[issue3]
|
|
|
|
'oh' | %i[title] | %i[issue3]
|
|
|
|
'ah' | %i[description] | %i[issue3]
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
let(:expected_issues) { expected.map { |sym| send(sym) } }
|
|
|
|
|
|
|
|
it 'finds the expected issues' do
|
|
|
|
expect(fuzzy_search).to match_array(expected_issues)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-29 05:00:03 -04:00
|
|
|
describe '.to_pattern' do
|
2017-08-28 18:14:41 -04:00
|
|
|
subject(:to_pattern) { User.to_pattern(query) }
|
2017-08-22 12:51:53 -04:00
|
|
|
|
|
|
|
context 'when a query is shorter than 3 chars' do
|
|
|
|
let(:query) { '12' }
|
|
|
|
|
|
|
|
it 'returns exact matching pattern' do
|
2017-08-28 18:14:41 -04:00
|
|
|
expect(to_pattern).to eq('12')
|
2017-08-22 12:51:53 -04:00
|
|
|
end
|
2019-06-12 20:08:44 -04:00
|
|
|
|
|
|
|
context 'and ignore_minimum_char_limit is true' do
|
|
|
|
it 'returns partial matching pattern' do
|
|
|
|
expect(User.to_pattern(query, use_minimum_char_limit: false)).to eq('%12%')
|
|
|
|
end
|
|
|
|
end
|
2017-08-22 12:51:53 -04:00
|
|
|
end
|
|
|
|
|
2017-08-26 09:32:55 -04:00
|
|
|
context 'when a query with a escape character is shorter than 3 chars' do
|
|
|
|
let(:query) { '_2' }
|
|
|
|
|
|
|
|
it 'returns sanitized exact matching pattern' do
|
2017-08-28 18:14:41 -04:00
|
|
|
expect(to_pattern).to eq('\_2')
|
2017-08-26 09:32:55 -04:00
|
|
|
end
|
2019-06-12 20:08:44 -04:00
|
|
|
|
|
|
|
context 'and ignore_minimum_char_limit is true' do
|
|
|
|
it 'returns sanitized partial matching pattern' do
|
|
|
|
expect(User.to_pattern(query, use_minimum_char_limit: false)).to eq('%\_2%')
|
|
|
|
end
|
|
|
|
end
|
2017-08-26 09:32:55 -04:00
|
|
|
end
|
|
|
|
|
2017-08-22 12:51:53 -04:00
|
|
|
context 'when a query is equal to 3 chars' do
|
|
|
|
let(:query) { '123' }
|
|
|
|
|
|
|
|
it 'returns partial matching pattern' do
|
2017-08-28 18:14:41 -04:00
|
|
|
expect(to_pattern).to eq('%123%')
|
2017-08-22 12:51:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-26 09:32:55 -04:00
|
|
|
context 'when a query with a escape character is equal to 3 chars' do
|
|
|
|
let(:query) { '_23' }
|
|
|
|
|
|
|
|
it 'returns partial matching pattern' do
|
2017-08-28 18:14:41 -04:00
|
|
|
expect(to_pattern).to eq('%\_23%')
|
2017-08-26 09:32:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-22 12:51:53 -04:00
|
|
|
context 'when a query is longer than 3 chars' do
|
|
|
|
let(:query) { '1234' }
|
|
|
|
|
|
|
|
it 'returns partial matching pattern' do
|
2017-08-28 18:14:41 -04:00
|
|
|
expect(to_pattern).to eq('%1234%')
|
2017-08-22 12:51:53 -04:00
|
|
|
end
|
|
|
|
end
|
2017-08-26 09:32:55 -04:00
|
|
|
|
|
|
|
context 'when a query with a escape character is longer than 3 chars' do
|
|
|
|
let(:query) { '_234' }
|
|
|
|
|
|
|
|
it 'returns sanitized partial matching pattern' do
|
2017-08-28 18:14:41 -04:00
|
|
|
expect(to_pattern).to eq('%\_234%')
|
2017-08-26 09:32:55 -04:00
|
|
|
end
|
|
|
|
end
|
2017-08-22 12:51:53 -04:00
|
|
|
end
|
2017-08-23 06:54:14 -04:00
|
|
|
|
|
|
|
describe '.select_fuzzy_words' do
|
|
|
|
subject(:select_fuzzy_words) { Issue.select_fuzzy_words(query) }
|
|
|
|
|
|
|
|
context 'with a word equal to 3 chars' do
|
|
|
|
let(:query) { 'foo' }
|
|
|
|
|
|
|
|
it 'returns array cotaining a word' do
|
|
|
|
expect(select_fuzzy_words).to match_array(['foo'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a word shorter than 3 chars' do
|
|
|
|
let(:query) { 'fo' }
|
|
|
|
|
|
|
|
it 'returns empty array' do
|
|
|
|
expect(select_fuzzy_words).to match_array([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with two words both equal to 3 chars' do
|
|
|
|
let(:query) { 'foo baz' }
|
|
|
|
|
|
|
|
it 'returns array containing two words' do
|
|
|
|
expect(select_fuzzy_words).to match_array(%w[foo baz])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-05 09:23:02 -04:00
|
|
|
context 'with two words divided by two spaces both equal to 3 chars' do
|
2017-08-23 06:54:14 -04:00
|
|
|
let(:query) { 'foo baz' }
|
|
|
|
|
|
|
|
it 'returns array containing two words' do
|
|
|
|
expect(select_fuzzy_words).to match_array(%w[foo baz])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with two words equal to 3 chars and shorter than 3 chars' do
|
|
|
|
let(:query) { 'foo ba' }
|
|
|
|
|
|
|
|
it 'returns array containing a word' do
|
|
|
|
expect(select_fuzzy_words).to match_array(['foo'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a multi-word surrounded by double quote' do
|
|
|
|
let(:query) { '"really bar"' }
|
|
|
|
|
|
|
|
it 'returns array containing a multi-word' do
|
|
|
|
expect(select_fuzzy_words).to match_array(['really bar'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a multi-word surrounded by double quote and two words' do
|
|
|
|
let(:query) { 'foo "really bar" baz' }
|
|
|
|
|
|
|
|
it 'returns array containing a multi-word and tow words' do
|
|
|
|
expect(select_fuzzy_words).to match_array(['foo', 'really bar', 'baz'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a multi-word surrounded by double quote missing a spece before the first double quote' do
|
|
|
|
let(:query) { 'foo"really bar"' }
|
|
|
|
|
|
|
|
it 'returns array containing two words with double quote' do
|
|
|
|
expect(select_fuzzy_words).to match_array(['foo"really', 'bar"'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a multi-word surrounded by double quote missing a spece after the second double quote' do
|
|
|
|
let(:query) { '"really bar"baz' }
|
|
|
|
|
|
|
|
it 'returns array containing two words with double quote' do
|
|
|
|
expect(select_fuzzy_words).to match_array(['"really', 'bar"baz'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with two multi-word surrounded by double quote and two words' do
|
|
|
|
let(:query) { 'foo "really bar" baz "awesome feature"' }
|
|
|
|
|
|
|
|
it 'returns array containing two multi-words and tow words' do
|
|
|
|
expect(select_fuzzy_words).to match_array(['foo', 'really bar', 'baz', 'awesome feature'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-24 06:08:16 -05:00
|
|
|
describe '.fuzzy_arel_match' do
|
|
|
|
subject(:fuzzy_arel_match) { Issue.fuzzy_arel_match(:title, query) }
|
2017-08-23 06:54:14 -04:00
|
|
|
|
|
|
|
context 'with a word equal to 3 chars' do
|
|
|
|
let(:query) { 'foo' }
|
|
|
|
|
|
|
|
it 'returns a single ILIKE condition' do
|
2017-11-24 06:08:16 -05:00
|
|
|
expect(fuzzy_arel_match.to_sql).to match(/title.*I?LIKE '\%foo\%'/)
|
2017-08-23 06:54:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a word shorter than 3 chars' do
|
|
|
|
let(:query) { 'fo' }
|
|
|
|
|
2017-11-24 06:23:47 -05:00
|
|
|
it 'returns a single equality condition' do
|
|
|
|
expect(fuzzy_arel_match.to_sql).to match(/title.*I?LIKE 'fo'/)
|
2017-08-23 06:54:14 -04:00
|
|
|
end
|
2018-02-15 13:34:44 -05:00
|
|
|
|
|
|
|
it 'uses LOWER instead of ILIKE when LOWER is enabled' do
|
|
|
|
rel = Issue.fuzzy_arel_match(:title, query, lower_exact_match: true)
|
|
|
|
|
|
|
|
expect(rel.to_sql).to match(/LOWER\(.*title.*\).*=.*'fo'/)
|
|
|
|
end
|
2017-08-23 06:54:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with two words both equal to 3 chars' do
|
|
|
|
let(:query) { 'foo baz' }
|
|
|
|
|
2017-08-30 18:49:19 -04:00
|
|
|
it 'returns a joining LIKE condition using a AND' do
|
2017-11-24 06:08:16 -05:00
|
|
|
expect(fuzzy_arel_match.to_sql).to match(/title.+I?LIKE '\%foo\%' AND .*title.*I?LIKE '\%baz\%'/)
|
2017-08-23 06:54:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-24 06:23:47 -05:00
|
|
|
context 'with two words both shorter than 3 chars' do
|
|
|
|
let(:query) { 'fo ba' }
|
|
|
|
|
|
|
|
it 'returns a single ILIKE condition' do
|
|
|
|
expect(fuzzy_arel_match.to_sql).to match(/title.*I?LIKE 'fo ba'/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with two words, one shorter 3 chars' do
|
|
|
|
let(:query) { 'foo ba' }
|
|
|
|
|
|
|
|
it 'returns a single ILIKE condition using the longer word' do
|
|
|
|
expect(fuzzy_arel_match.to_sql).to match(/title.+I?LIKE '\%foo\%'/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-23 06:54:14 -04:00
|
|
|
context 'with a multi-word surrounded by double quote and two words' do
|
|
|
|
let(:query) { 'foo "really bar" baz' }
|
|
|
|
|
2017-08-30 18:49:19 -04:00
|
|
|
it 'returns a joining LIKE condition using a AND' do
|
2017-11-24 06:08:16 -05:00
|
|
|
expect(fuzzy_arel_match.to_sql).to match(/title.+I?LIKE '\%foo\%' AND .*title.*I?LIKE '\%baz\%' AND .*title.*I?LIKE '\%really bar\%'/)
|
2017-08-23 06:54:14 -04:00
|
|
|
end
|
|
|
|
end
|
2019-12-02 16:06:51 -05:00
|
|
|
|
|
|
|
context 'when passing an Arel column' do
|
|
|
|
let(:query) { 'foo' }
|
|
|
|
|
|
|
|
subject(:fuzzy_arel_match) { Project.fuzzy_arel_match(Route.arel_table[:path], query) }
|
|
|
|
|
|
|
|
it 'returns a condition with the table and column name' do
|
|
|
|
expect(fuzzy_arel_match.to_sql).to match(/"routes"."path".*ILIKE '\%foo\%'/)
|
|
|
|
end
|
|
|
|
end
|
2017-08-23 06:54:14 -04:00
|
|
|
end
|
2017-08-22 12:51:53 -04:00
|
|
|
end
|