Fix unmatched quotes and prefixes causing search to fail (#26701)
This commit is contained in:
parent
872145d1c2
commit
e754083e8a
4 changed files with 200 additions and 60 deletions
98
spec/lib/search_query_parser_spec.rb
Normal file
98
spec/lib/search_query_parser_spec.rb
Normal file
|
@ -0,0 +1,98 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
require 'parslet/rig/rspec'
|
||||
|
||||
describe SearchQueryParser do
|
||||
let(:parser) { described_class.new }
|
||||
|
||||
context 'with term' do
|
||||
it 'consumes "hello"' do
|
||||
expect(parser.term).to parse('hello')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with prefix' do
|
||||
it 'consumes "foo:"' do
|
||||
expect(parser.prefix).to parse('foo:')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with operator' do
|
||||
it 'consumes "+"' do
|
||||
expect(parser.operator).to parse('+')
|
||||
end
|
||||
|
||||
it 'consumes "-"' do
|
||||
expect(parser.operator).to parse('-')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with shortcode' do
|
||||
it 'consumes ":foo:"' do
|
||||
expect(parser.shortcode).to parse(':foo:')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with phrase' do
|
||||
it 'consumes "hello world"' do
|
||||
expect(parser.phrase).to parse('"hello world"')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with clause' do
|
||||
it 'consumes "foo"' do
|
||||
expect(parser.clause).to parse('foo')
|
||||
end
|
||||
|
||||
it 'consumes "-foo"' do
|
||||
expect(parser.clause).to parse('-foo')
|
||||
end
|
||||
|
||||
it 'consumes "foo:bar"' do
|
||||
expect(parser.clause).to parse('foo:bar')
|
||||
end
|
||||
|
||||
it 'consumes "-foo:bar"' do
|
||||
expect(parser.clause).to parse('-foo:bar')
|
||||
end
|
||||
|
||||
it 'consumes \'foo:"hello world"\'' do
|
||||
expect(parser.clause).to parse('foo:"hello world"')
|
||||
end
|
||||
|
||||
it 'consumes \'-foo:"hello world"\'' do
|
||||
expect(parser.clause).to parse('-foo:"hello world"')
|
||||
end
|
||||
|
||||
it 'consumes "foo:"' do
|
||||
expect(parser.clause).to parse('foo:')
|
||||
end
|
||||
|
||||
it 'consumes \'"\'' do
|
||||
expect(parser.clause).to parse('"')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with query' do
|
||||
it 'consumes "hello -world"' do
|
||||
expect(parser.query).to parse('hello -world')
|
||||
end
|
||||
|
||||
it 'consumes \'foo "hello world"\'' do
|
||||
expect(parser.query).to parse('foo "hello world"')
|
||||
end
|
||||
|
||||
it 'consumes "foo:bar hello"' do
|
||||
expect(parser.query).to parse('foo:bar hello')
|
||||
end
|
||||
|
||||
it 'consumes \'"hello" world "\'' do
|
||||
expect(parser.query).to parse('"hello" world "')
|
||||
end
|
||||
|
||||
it 'consumes "foo:bar bar: hello"' do
|
||||
expect(parser.query).to parse('foo:bar bar: hello')
|
||||
end
|
||||
end
|
||||
end
|
|
@ -3,16 +3,57 @@
|
|||
require 'rails_helper'
|
||||
|
||||
describe SearchQueryTransformer do
|
||||
describe 'initialization' do
|
||||
let(:parser) { SearchQueryParser.new.parse('query') }
|
||||
subject { described_class.new.apply(parser, current_account: nil) }
|
||||
|
||||
it 'sets attributes' do
|
||||
transformer = described_class.new.apply(parser)
|
||||
let(:parser) { SearchQueryParser.new.parse(query) }
|
||||
|
||||
expect(transformer.should_clauses.first).to be_nil
|
||||
expect(transformer.must_clauses.first).to be_a(SearchQueryTransformer::TermClause)
|
||||
expect(transformer.must_not_clauses.first).to be_nil
|
||||
expect(transformer.filter_clauses.first).to be_nil
|
||||
context 'with "hello world"' do
|
||||
let(:query) { 'hello world' }
|
||||
|
||||
it 'transforms clauses' do
|
||||
expect(subject.must_clauses.map(&:term)).to match_array %w(hello world)
|
||||
expect(subject.must_not_clauses).to be_empty
|
||||
expect(subject.filter_clauses).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
context 'with "hello -world"' do
|
||||
let(:query) { 'hello -world' }
|
||||
|
||||
it 'transforms clauses' do
|
||||
expect(subject.must_clauses.map(&:term)).to match_array %w(hello)
|
||||
expect(subject.must_not_clauses.map(&:term)).to match_array %w(world)
|
||||
expect(subject.filter_clauses).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
context 'with "hello is:reply"' do
|
||||
let(:query) { 'hello is:reply' }
|
||||
|
||||
it 'transforms clauses' do
|
||||
expect(subject.must_clauses.map(&:term)).to match_array %w(hello)
|
||||
expect(subject.must_not_clauses).to be_empty
|
||||
expect(subject.filter_clauses.map(&:term)).to match_array %w(reply)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with "foo: bar"' do
|
||||
let(:query) { 'foo: bar' }
|
||||
|
||||
it 'transforms clauses' do
|
||||
expect(subject.must_clauses.map(&:term)).to match_array %w(foo bar)
|
||||
expect(subject.must_not_clauses).to be_empty
|
||||
expect(subject.filter_clauses).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
context 'with "foo:bar"' do
|
||||
let(:query) { 'foo:bar' }
|
||||
|
||||
it 'transforms clauses' do
|
||||
expect(subject.must_clauses.map(&:term)).to contain_exactly('foo bar')
|
||||
expect(subject.must_not_clauses).to be_empty
|
||||
expect(subject.filter_clauses).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue